/* math_utilities.c generated by valac 0.56.19, the Vala compiler
 * generated from math_utilities.vala, do not modify */

/* SPDX-License-Identifier: GPL-3.0-or-later*/

#include <glib.h>
#include <float.h>
#include <math.h>
#include "graphs.h"
#include <stdlib.h>
#include <string.h>
#include <gee.h>
#include <glib-object.h>

#define GRAPHS_MATH_TOOLS_REL_TOL 1e-9
#define GRAPHS_MATH_TOOLS_ABS_TOL 1e-4
#define GRAPHS_MATH_TOOLS_PI_THRESH 0.00010000314159265359
#define GRAPHS_MATH_TOOLS_E_THRESH 0.00010000271828182846
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

#define GRAPHS_MATH_PARSER_TYPE_LEXER (graphs_math_parser_lexer_get_type ())
#define GRAPHS_MATH_PARSER_LEXER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GRAPHS_MATH_PARSER_TYPE_LEXER, GraphsMathParserLexer))
#define GRAPHS_MATH_PARSER_LEXER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GRAPHS_MATH_PARSER_TYPE_LEXER, GraphsMathParserLexerClass))
#define GRAPHS_MATH_PARSER_IS_LEXER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GRAPHS_MATH_PARSER_TYPE_LEXER))
#define GRAPHS_MATH_PARSER_IS_LEXER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GRAPHS_MATH_PARSER_TYPE_LEXER))
#define GRAPHS_MATH_PARSER_LEXER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GRAPHS_MATH_PARSER_TYPE_LEXER, GraphsMathParserLexerClass))

typedef struct _GraphsMathParserLexer GraphsMathParserLexer;
typedef struct _GraphsMathParserLexerClass GraphsMathParserLexerClass;
#define _graphs_math_parser_lexer_unref0(var) ((var == NULL) ? NULL : (var = (graphs_math_parser_lexer_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _GraphsMathParserLexerPrivate GraphsMathParserLexerPrivate;
typedef enum  {
	GRAPHS_MATH_PARSER_TOKEN_TYPE_NUMBER,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_IDENT,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_PLUS,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_MINUS,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_STAR,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_SLASH,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_CARET,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_FACT,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_SUPERSCRIPT,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_LPAREN,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_RPAREN,
	GRAPHS_MATH_PARSER_TOKEN_TYPE_END
} GraphsMathParserTokenType;

#define GRAPHS_MATH_PARSER_TYPE_TOKEN_TYPE (graphs_math_parser_token_type_get_type ())
typedef enum  {
	GRAPHS_MATH_PARSER_IDENT_PI,
	GRAPHS_MATH_PARSER_IDENT_E,
	GRAPHS_MATH_PARSER_IDENT_INF,
	GRAPHS_MATH_PARSER_IDENT_SIN,
	GRAPHS_MATH_PARSER_IDENT_COS,
	GRAPHS_MATH_PARSER_IDENT_TAN,
	GRAPHS_MATH_PARSER_IDENT_COT,
	GRAPHS_MATH_PARSER_IDENT_SEC,
	GRAPHS_MATH_PARSER_IDENT_CSC,
	GRAPHS_MATH_PARSER_IDENT_SIND,
	GRAPHS_MATH_PARSER_IDENT_COSD,
	GRAPHS_MATH_PARSER_IDENT_TAND,
	GRAPHS_MATH_PARSER_IDENT_COTD,
	GRAPHS_MATH_PARSER_IDENT_SECD,
	GRAPHS_MATH_PARSER_IDENT_CSCD,
	GRAPHS_MATH_PARSER_IDENT_ASIN,
	GRAPHS_MATH_PARSER_IDENT_ACOS,
	GRAPHS_MATH_PARSER_IDENT_ATAN,
	GRAPHS_MATH_PARSER_IDENT_ACOT,
	GRAPHS_MATH_PARSER_IDENT_ASEC,
	GRAPHS_MATH_PARSER_IDENT_ACSC,
	GRAPHS_MATH_PARSER_IDENT_ASIND,
	GRAPHS_MATH_PARSER_IDENT_ACOSD,
	GRAPHS_MATH_PARSER_IDENT_ATAND,
	GRAPHS_MATH_PARSER_IDENT_ACOTD,
	GRAPHS_MATH_PARSER_IDENT_ASECD,
	GRAPHS_MATH_PARSER_IDENT_ACSCD,
	GRAPHS_MATH_PARSER_IDENT_LOG,
	GRAPHS_MATH_PARSER_IDENT_LOG2,
	GRAPHS_MATH_PARSER_IDENT_LOG10,
	GRAPHS_MATH_PARSER_IDENT_SQRT,
	GRAPHS_MATH_PARSER_IDENT_EXP,
	GRAPHS_MATH_PARSER_IDENT_ABS,
	GRAPHS_MATH_PARSER_IDENT_CUSTOM
} GraphsMathParserIdent;

#define GRAPHS_MATH_PARSER_TYPE_IDENT (graphs_math_parser_ident_get_type ())
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _GraphsMathParserLexer {
	GTypeInstance parent_instance;
	volatile int ref_count;
	GraphsMathParserLexerPrivate * priv;
	GraphsMathParserTokenType current_type;
	GraphsMathParserIdent current_ident;
	gdouble current_val;
};

struct _GraphsMathParserLexerClass {
	GTypeClass parent_class;
	void (*finalize) (GraphsMathParserLexer *self);
};

VALA_EXTERN gpointer graphs_math_parser_lexer_ref (gpointer instance);
VALA_EXTERN void graphs_math_parser_lexer_unref (gpointer instance);
VALA_EXTERN GParamSpec* graphs_math_parser_param_spec_lexer (const gchar* name,
                                                 const gchar* nick,
                                                 const gchar* blurb,
                                                 GType object_type,
                                                 GParamFlags flags);
VALA_EXTERN void graphs_math_parser_value_set_lexer (GValue* value,
                                         gpointer v_object);
VALA_EXTERN void graphs_math_parser_value_take_lexer (GValue* value,
                                          gpointer v_object);
VALA_EXTERN gpointer graphs_math_parser_value_get_lexer (const GValue* value);
VALA_EXTERN GType graphs_math_parser_lexer_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GraphsMathParserLexer* graphs_math_parser_lexer_new (gboolean allow_custom_ident);
VALA_EXTERN GraphsMathParserLexer* graphs_math_parser_lexer_construct (GType object_type,
                                                           gboolean allow_custom_ident);
VALA_EXTERN void graphs_math_parser_lexer_start_lexing (GraphsMathParserLexer* self,
                                            const gchar* src,
                                            gunichar decimal_separator,
                                            GError** error);
VALA_EXTERN GType graphs_math_parser_token_type_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType graphs_math_parser_ident_get_type (void) G_GNUC_CONST ;
VALA_EXTERN gchar* graphs_math_parser_lexer_get_current_token_as_string (GraphsMathParserLexer* self);
VALA_EXTERN void graphs_math_parser_lexer_next (GraphsMathParserLexer* self,
                                    GError** error);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

/**
     * Whether or not two values are close to each other.
     */
gboolean
graphs_math_tools_is_close (gdouble a,
                            gdouble b)
{
	gboolean result;
	result = fabs (a - b) <= fmax (GRAPHS_MATH_TOOLS_REL_TOL * fmax (fabs (a), fabs (b)), GRAPHS_MATH_TOOLS_ABS_TOL);
	return result;
}

/**
     * Wether or not two values contain values close to each other.
     */
gboolean
graphs_math_tools_all_close (gdouble* a,
                             gint a_length1,
                             gdouble* b,
                             gint b_length1)
{
	gboolean result;
	_vala_return_val_if_fail (a_length1 == b_length1, "a.length == b.length", FALSE);
	{
		guint i = 0U;
		i = (guint) 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				gdouble _tmp2_;
				gdouble _tmp3_;
				if (!_tmp0_) {
					guint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				if (!(i < ((guint) a_length1))) {
					break;
				}
				_tmp2_ = a[i];
				_tmp3_ = b[i];
				if (!graphs_math_tools_is_close (_tmp2_, _tmp3_)) {
					result = FALSE;
					return result;
				}
			}
		}
	}
	result = TRUE;
	return result;
}

/**
     * String representation of a double, prettifies for typical constants
     * such as integer values of pi and e.
     */
gchar*
graphs_math_tools_prettyprint_double (gdouble val)
{
	GString* builder = NULL;
	GString* _tmp1_;
	gdouble remainder = 0.0;
	gboolean _tmp3_ = FALSE;
	gboolean _tmp10_ = FALSE;
	GString* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	GString* _tmp20_;
	gchar* _tmp21_;
	gchar* result;
	if (val == ((gdouble) 0)) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup ("0");
		result = _tmp0_;
		return result;
	}
	_tmp1_ = g_string_new ("");
	builder = _tmp1_;
	if (val < ((gdouble) 0)) {
		GString* _tmp2_;
		val = val * -1;
		_tmp2_ = builder;
		g_string_append_c (_tmp2_, '-');
	}
	remainder = fmod (val, G_PI);
	if (remainder <= GRAPHS_MATH_TOOLS_PI_THRESH) {
		_tmp3_ = TRUE;
	} else {
		_tmp3_ = remainder >= (G_PI - GRAPHS_MATH_TOOLS_PI_THRESH);
	}
	if (_tmp3_) {
		gdouble factor = 0.0;
		factor = floor ((val / G_PI) + 0.5);
		if (factor != ((gdouble) 0)) {
			GString* _tmp7_;
			GString* _tmp8_;
			gchar* _tmp9_;
			if (factor != ((gdouble) 1)) {
				GString* _tmp4_;
				gchar* _tmp5_;
				gchar* _tmp6_;
				_tmp4_ = builder;
				_tmp5_ = g_strdup_printf ("%.15g", factor);
				_tmp6_ = _tmp5_;
				g_string_append (_tmp4_, _tmp6_);
				_g_free0 (_tmp6_);
			}
			_tmp7_ = builder;
			g_string_append (_tmp7_, "pi");
			_tmp8_ = builder;
			builder = NULL;
			_tmp9_ = g_string_free_and_steal (_tmp8_);
			result = _tmp9_;
			_g_string_free0 (builder);
			return result;
		}
	}
	remainder = fmod (val, G_E);
	if (remainder <= GRAPHS_MATH_TOOLS_E_THRESH) {
		_tmp10_ = TRUE;
	} else {
		_tmp10_ = remainder >= (G_E - GRAPHS_MATH_TOOLS_E_THRESH);
	}
	if (_tmp10_) {
		gdouble factor = 0.0;
		factor = floor ((val / G_E) + 0.5);
		if (factor != ((gdouble) 0)) {
			GString* _tmp14_;
			GString* _tmp15_;
			gchar* _tmp16_;
			if (factor != ((gdouble) 1)) {
				GString* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_;
				_tmp11_ = builder;
				_tmp12_ = g_strdup_printf ("%.15g", factor);
				_tmp13_ = _tmp12_;
				g_string_append (_tmp11_, _tmp13_);
				_g_free0 (_tmp13_);
			}
			_tmp14_ = builder;
			g_string_append_c (_tmp14_, 'e');
			_tmp15_ = builder;
			builder = NULL;
			_tmp16_ = g_string_free_and_steal (_tmp15_);
			result = _tmp16_;
			_g_string_free0 (builder);
			return result;
		}
	}
	_tmp17_ = builder;
	_tmp18_ = g_strdup_printf ("%.15g", val);
	_tmp19_ = _tmp18_;
	g_string_append (_tmp17_, _tmp19_);
	_g_free0 (_tmp19_);
	_tmp20_ = builder;
	builder = NULL;
	_tmp21_ = g_string_free_and_steal (_tmp20_);
	result = _tmp21_;
	_g_string_free0 (builder);
	return result;
}

/**
     * Round a number to specified digits.
     */
gdouble
graphs_math_tools_sig_fig_round (gdouble number,
                                 gint digits)
{
	gdouble abs_number = 0.0;
	gint power = 0;
	gint scale_power = 0;
	gdouble factor = 0.0;
	gdouble result;
	if (number == ((gdouble) 0)) {
		result = 0.0;
		return result;
	}
	abs_number = fabs (number);
	power = (gint) floor (log10 (abs_number));
	scale_power = (digits - power) - 1;
	factor = pow (10.0, (gdouble) scale_power);
	result = round (number * factor) / factor;
	return result;
}

/**
     * Get all free variables (without x) in an equation.
     */
gchar**
graphs_math_tools_get_free_variables (const gchar* equation,
                                      gint* result_length1,
                                      GError** error)
{
	GeeHashSet* strings = NULL;
	GeeHashSet* _tmp0_;
	GraphsMathParserLexer* lexer = NULL;
	GraphsMathParserLexer* _tmp1_;
	GraphsMathParserLexer* _tmp2_;
	GeeHashSet* _tmp16_;
	gint _tmp17_ = 0;
	gpointer* _tmp18_;
	gchar** _tmp19_;
	gint _tmp19__length1;
	GError* _inner_error0_ = NULL;
	gchar** result;
	g_return_val_if_fail (equation != NULL, NULL);
	_tmp0_ = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL);
	strings = _tmp0_;
	_tmp1_ = graphs_math_parser_lexer_new (TRUE);
	lexer = _tmp1_;
	_tmp2_ = lexer;
	graphs_math_parser_lexer_start_lexing (_tmp2_, equation, (gunichar) '.', &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == GRAPHS_MATH_ERROR) {
			g_propagate_error (error, _inner_error0_);
			_graphs_math_parser_lexer_unref0 (lexer);
			_g_object_unref0 (strings);
			return NULL;
		} else {
			_graphs_math_parser_lexer_unref0 (lexer);
			_g_object_unref0 (strings);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
	}
	while (TRUE) {
		GraphsMathParserLexer* _tmp3_;
		gboolean _tmp4_ = FALSE;
		GraphsMathParserLexer* _tmp5_;
		GraphsMathParserLexer* _tmp15_;
		_tmp3_ = lexer;
		if (!(_tmp3_->current_type != GRAPHS_MATH_PARSER_TOKEN_TYPE_END)) {
			break;
		}
		_tmp5_ = lexer;
		if (_tmp5_->current_type == GRAPHS_MATH_PARSER_TOKEN_TYPE_IDENT) {
			GraphsMathParserLexer* _tmp6_;
			_tmp6_ = lexer;
			_tmp4_ = _tmp6_->current_ident == GRAPHS_MATH_PARSER_IDENT_CUSTOM;
		} else {
			_tmp4_ = FALSE;
		}
		if (_tmp4_) {
			gchar* token = NULL;
			GraphsMathParserLexer* _tmp7_;
			gchar* _tmp8_;
			const gchar* _tmp9_;
			gchar* _tmp10_;
			gchar* _tmp11_;
			gboolean _tmp12_;
			_tmp7_ = lexer;
			_tmp8_ = graphs_math_parser_lexer_get_current_token_as_string (_tmp7_);
			token = _tmp8_;
			_tmp9_ = token;
			_tmp10_ = g_utf8_strdown (_tmp9_, (gssize) -1);
			_tmp11_ = _tmp10_;
			_tmp12_ = g_strcmp0 (_tmp11_, "x") != 0;
			_g_free0 (_tmp11_);
			if (_tmp12_) {
				GeeHashSet* _tmp13_;
				const gchar* _tmp14_;
				_tmp13_ = strings;
				_tmp14_ = token;
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp13_, _tmp14_);
			}
			_g_free0 (token);
		}
		_tmp15_ = lexer;
		graphs_math_parser_lexer_next (_tmp15_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == GRAPHS_MATH_ERROR) {
				g_propagate_error (error, _inner_error0_);
				_graphs_math_parser_lexer_unref0 (lexer);
				_g_object_unref0 (strings);
				return NULL;
			} else {
				_graphs_math_parser_lexer_unref0 (lexer);
				_g_object_unref0 (strings);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return NULL;
			}
		}
	}
	_tmp16_ = strings;
	_tmp18_ = gee_collection_to_array ((GeeCollection*) _tmp16_, &_tmp17_);
	_tmp19_ = _tmp18_;
	_tmp19__length1 = _tmp17_;
	if (result_length1) {
		*result_length1 = _tmp19__length1;
	}
	result = _tmp19_;
	_graphs_math_parser_lexer_unref0 (lexer);
	_g_object_unref0 (strings);
	return result;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

