<b>libinum</b> is a C library that implements several algorithms, intended for applications in numerical and symbolic computations.
</p>
<p>
<b>libinum</b> defines functions to perform the following tasks:
<ul>
<li>Compute roots of smooth real functions numerically using the Newton-Raphson algorithm.</li>
<li>Compute definite integrals of smooth real functions numerically using Gauss-Legendre quadratures.</li>
</ul>
In addition, <b>libinum</b> defines the following structures:
<ul>
<li>Variable-length arrays.</li>
<li>Single variable and multivariate polynomials.</li>
</ul>
</p>
<p>
As a general rule, the functions and structures can be used with several different data types. For instance, the root finding and integration functions can manipulate double precision, extended precision, or multi-precision floating point numbers. See <ahref="#sec_datatypes">Data types</a> for details.
One of the guiding principles of <b>libinum</b> is that it should should not push a specific data type on users. If, hypothetically, the computation being carried out requires the precision of the floating point numbers to be large, then multi-precision floating point numbers might be preferable to double precision numbers. If, instead, computation time is more important than precision, then extended precision floats may be preferred. The structures and functions defined in <b>libinum</b>, therefore, support various data types.
</p>
<p>
We will now briefly discuss the data types that may be used to represent numbers.
The standard integer types of the C language are <code>char</code>, <code>short int</code>, <code>int</code>, <code>long int</code> and <code>long long int</code>, as well as their <code>unsigned</code> counterparts. The number of bits, and, in consequence, the available range of integers, of each of these types, is platform-dependent. In cases where this could be problematic, fixed-precision integers can be used: with 8 bits: <code>int8_t</code>, 16: <code>int16_t</code>, 32: <code>int32_t</code> and 64: <code>int64_t</code>.
</p>
<p>
<b>libinum</b> provides a function, <code>print_datatype_info()</code>, that prints the equivalence table between <code>char</code>, <code>short int</code>, <code>int</code>, <code>long int</code> and <code>long long int</code> and <code>int8_t</code>, <code>int16_t</code>, <code>int32_t</code> and <code>int64_t</code> for the implementation of the C library used to compile <b>libinum</b>.
</p>
<h4>Multi-precision integers</h4>
<p>
In cases where the required number of bits of integers exceeds 64, one can use <i>multi-precision integers</i>, which can be as large as will fit in memory. However, computation times can greatly increase when using multi-precision integers. The implementation used in <b>libinum</b> is provided by the <ahref="http://gmplib.org">GNU GMP</a> library.
</p>
<h3class="subsection"id="subsec_float">Floating point numbers</h3>
<p>
Let us first start with a description of floating point numbers. A real number \(x\) is approximated, with a <i>precision</i> of \(m\) bits, as a collection of three numbers: the <i>sign</i> \(s\in\{-1,1\}\); <i>mantissa</i>, whose binary expansion is denoted by \(a_1\cdots a_m\); and <i>exponent</i> \(e\in\mathbb Z\). The approximate value of the number \(x\) is obtained from its sign, mantissa and exponent by
$$
x=s\times a_1.a_2\cdots a_m\times2^e.
$$
The <i>precision</i> of a floating point number is the number of bits allocated to its sign and mantissa (since \(a_1\) is necessarily equal to \(1\), it is not stored, so the precision of \(x\) is \(m\) instead of \(m+1\)).
</p>
<p> In <b>libinum</b>, floating point number may either be double, extended or multi-precision numbers.
<h4>Double precision floating point numbers</h4>
<p>
Double precision floats are represented using the <code>double</code> type.
</p>
<p>
The precision and maximal and minimal values of the exponent of <code>double</code> floats depends on the compiler. Their values can be printed using the <b>libinum</b> function <code>print_datatype_info()</code>.
</p>
<p>
For example, using version 5.3.0 of the <ahref="https://gcc.gnu.org/">GNU GCC</a> compiler on the x86-64 architecture, the precision is of 53 bits (i.e. 15 decimal digits), and the maximal and minimal values of the exponent are 1024 and -1021.
</p>
<h4>Extended precision floating point numbers</h4>
<p>
Extended precision floats are represented using the <code>long double</code> type. They require more memory than double precision floats, and, as a consequence, slightly more computation time.
</p>
<p>
The precision and maximal and minimal values of the exponent of <code>long double</code> floats depends on the compiler. Their values can be printed using the <b>libinum</b> function <code>print_datatype_info()</code>.
</p>
<p>
For example, using version 5.3.0 of the <ahref="https://gcc.gnu.org/">GNU GCC</a> compiler on the x86-64 architecture, the precision is of 64 bits (i.e. 18 decimal digits), and the maximal and minimal values of the exponent are 16384 and -16381.
</p>
<h4>Multi-precision floating point numbers</h4>
<p>
A multi-precision floating point number is a floating point number whose precision can be set to an arbitrary value (until the number fills up the entire memory of the computer). In <b>libinum</b>, multi-precision floats are implemented using the <ahref="http://mpfr.org">GNU MPFR</a> library, and will be called <i> MPFR floats</i>.
</p>
<p>
The precision of MPFR floats can be set using the function <code>mpfr_set_default_prec</code> (see the MPFR library <ahref="http://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr_005fset_005fdefault_005fprec">documentation</a> for details). The default value of the precision is 53 bits (as of version 3.1.4 of the MPFR library).
</p>
<p>
In addition, the maximal value (in absolute value) of the exponent can be controlled by setting <i>emax</i> using the function <code>mpfr_set_emax</code> (see the MPFR library <ahref="http://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr_005fset_005femax">documentation</a> for details).
</p>
<p>
Depending on the MPFR implementation, the precision and emax can either be an <code>int</code> or a <code>long int</code>, so its maximal and minimal values are platform dependent. The <b>libinum</b> function <code>print_datatype_info()</code> prints which it is.
<b>libinum</b> can compute the root of smooth real functions using the Newton-Raphson algorithm. The algorithm converges quickly, provided an adequate approximation of the root in provided, and that the root is not located at a minimum of the function.
</p>
<h4>Description</h4>
<p>
Given a function \(f\) and a real number \(x_0\), the algorithm produces a sequence \((x_n)\) which, provided the algorithm converges, tends to a root of \(f\). The sequence is defined as
$$
x_{n+1}=x_n-\frac{f(x_{n})}{f'(x_{n})}
$$
where \(f'\) denotes the derivative of \(f\). The following estimate holds:
<b>Description</b>: Compute a numerical approximation for the root.<br>
<br>
<b>Arguments</b>:
<ul>
<li>
<code>out</code>: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an <code>mpfr_t</code>), then it must be initialized.
</li>
<li>
<code>func</code>: pointer to the function that computes \(f(x)\). It must be in the format specified in <ahref="#subsec_functions">functions</a>.
</li>
<li>
<code>deriv_func</code>: pointer to the function that computes the derivative of \(f\). It must be in the format specified in <ahref="#subsec_functions">functions</a>.
</li>
<li>
<code>init</code>: the value of \(x_0\).
</li>
<li>
<code>tolerance</code>: the algorithm halts when \(|x_{n+1}-x_n|\leqslant\)<code>tolerance</code>. <code>tolerance</code> thus provides control over the error of the algorithm.
</li>
<li>
<code>maxiter</code>: maximum number of iterations. The algorithm gives up if this number is reached and throws a <code>LIBINUM_ERROR_MAXITER</code> exception.
</li>
<li>
<code>extra_args</code>: pointer to the extra arguments to be passed to the functions <code>*func</code> and <code>*deriv_func</code> when they are evaluated.
</li>
</ul>
<br>
<b>Return value</b>: returns <code>0</code> on success, and
<ul>
<li><code>LIBINUM_ERROR_MAXITER</code> if the maximal number of iterations was reached.</li>
<li><code>LIBINUM_ERROR_NAN</code> if any of the evaluations of <code>*func</code> or <code>*deriv_func</code> returned <code>nan</code> or <code>infinity</code>, or if any of the evaluations of <code>*deriv_func</code> returned <code>0</code>.</li>
<b>Description</b>: Similar to <ahref="#func_root_newton"><code>root_newton_*</code></a> except that the value of \(x_0\) is passed to the function in the argument <code>out</code>.
<b>libinum</b> can compute definite integrals of smooth real functions using Gauss-Legendre quadratures.
</p>
<h4>Description</h4>
<p>
The main idea of Gauss-Legendre quadratures is to find a set of <i>abcissa</i> \(\{t_1,\cdots,t_N\}\in[-1,1]^N\) and <i>weights</i> \(\{w_1,\cdots,w_N\}\in\mathbb R^N\) such that, if \(f\) were a polynomial of degree \(\leqslant2N-1\), then the integral would be equal to a discrete sum:
<b>Description</b>: Compute a numerical approximation for the integral of a real function using Gauss quadratures.<br>
<br>
<b>Arguments</b>:
<ul>
<li>
<code>out</code>: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an <code>mpfr_t</code>), then it must be initialized.
</li>
<li>
<code>func</code>: pointer to the function that computes the integrand \(f(x)\). It must be in the format specified in <ahref="#subsec_functions">functions</a>.
</li>
<li>
<code>lower</code>: lower bound of the integration.
</li>
<li>
<code>upper</code>: upper bound of the integration.
</li>
<li>
<code>abcissa</code>: the abcissa used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function <ahref="#func_gauss_legendre_weights"><code>gauss_legendre_weights</code></a>.
</li>
<li>
<code>weights</code>: the weights used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function <ahref="#func_gauss_legendre_weights"><code>gauss_legendre_weights</code></a>.
</li>
<li>
<code>extra_args</code>: pointer to the extra arguments to be passed to the function <code>*func</code> when it is evaluated (see <ahref="#subsec_functions">functions</a>).
</li>
</ul>
<br>
<b>Return value</b>: returns <code>0</code> on success, and
<ul>
<li><code>LIBINUM_ERROR_NAN</code> if any of the evaluations of <code>*func</code> returned <code>nan</code> or <code>infinity</code>.</li>
<li><code>LIBINUM_ERROR_SIZE_MISMATCH</code> if the lengths of the vectors <code>abcissa</code> and <code>weights</code> are different.</li>
<b>Description</b>: Multithreaded version of <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>, in which function calls are performed in parallel.<br>
<br>
<b>Arguments</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>, except for
<ul>
<li>
<code>threads</code>: number of threads to use for the computation.
</li>
<li>
<code>thread_ids</code>: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
</li>
</ul>
<br>
<b>Return value</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>.
<b>Description</b>: Similar to <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>, except that, if temporary floats are needed during the computation, they are saved to an array so that they can be re-used later on. This is useful to perform repeated integrations, in which it would be costly to re-allocate memory for temporary floats. When using this function, temporary floats are allocated as needed, but they are not discarded.<br>
<br>
<b>Arguments</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>, except for
<ul>
<li>
<code>tmps</code>: pointer to an array that is used to store temporary floats. The array must be initialized beforehand. When temporary floats are needed, the array is checked for available floats. If enough of them are already present in the array, then the routine uses them, if not, it enlarges the array and allocates as many extra floats as is needed. The floats in the array can then be re-used for other purposes.
</li>
</ul>
<br>
<b>Return value</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>.
<b>Description</b>: Multithreaded version of <ahref="#func_integrate_gauss_smarttmp"><code>integrate_gauss_smarttmp_*</code></a>, in which function calls are performed in parallel.<br>
<br>
<b>Arguments</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_smarttmp_*</code></a>, except for
<ul>
<li>
<code>threads</code>: number of threads to use for the computation.
</li>
<li>
<code>thread_ids</code>: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
</li>
</ul>
<br>
<b>Return value</b>: Same as <ahref="#func_integrate_gauss"><code>integrate_gauss_*</code></a>.
<b>Description</b>: Compute the Gauss-Legendre abcissa and weights.<br>
<br>
<b>Arguments</b>:
<ul>
<li>
<code>order</code>: the order \(N\) of the integration.
</li>
<li>
<code>tolerance</code>: tolerance for the Newton algorithm used to compute the roots of the Legendre polynomial (see <ahref="#func_root_newton"><code>root_newton</code></a>).
</li>
<li>
<code>maxiter</code>: the maximal number of steps to compute before giving up.
</li>
<li>
<code>abcissa</code>: pointer to the <ahref="#subsec_array">array</a> in which to write the abcissa. It must not be initialized.
</li>
<li>
<code>weights</code>: pointer to the <ahref="#subsec_array">array</a> in which to write the weights. It must not be initialized.
</li>
</ul>
<br>
<b>Return value</b>: returns <code>0</code> on success, and
<ul>
<li><code>LIBINUM_ERROR_MAXITER</code> if, when computing the roots of the Legendre polynomial using a Newton iteration, the maximal number of iterations was reached.</li>
</ul>
</li>
</ul>
<h2class="section"id="sec_types">Types</h2>
<p>
What follows is a detailed description of the C types defined in <b>libinum</b>.
Arrays of several types of objects are defined. They are structures, with the following keys:
<ul>
<liid="struct_array_int">
<code>array_int</code>:
<code>{ int* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_uint">
<code>array_uint</code>:
<code>{ unsigned int* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_double">
<code>array_double</code>:
<code>{ double* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_ldouble">
<code>array_ldouble</code>:
<code>{ long double* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_mpfr">
<code>array_mpfr</code>:
<code>{ mpfr_t* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_2_mpfr">
<code>array_2_mpfr</code>:
<code>{ array_mpfr* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_char">
<code>array_char</code>:
<code>{ char* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_str">
<code>array_str</code>:
<code>{ array_char* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_polynomial_double">
<code>array_polynomial_double</code>:
<code>{ polynomial_double* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_polynomial_ldouble">
<code>array_polynomial_ldouble</code>:
<code>{ polynomial_ldouble* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_polynomial_mpfr">
<code>array_polynomial_mpfr</code>:
<code>{ polynomial_mpfr* values, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_array_pthread_t">
<code>array_pthread_t</code>:
<code>{ pthread_t* values, unsigned int length, unsigned int memory }</code>
</li>
</ul>
where the keys correspond to
<ul>
<li><code>values</code>: the array in which the objects are stored</li>
<li><code>length</code>: the number of elements in the array</li>
<li><code>memory</code>: the number of elements allocated to the array</li>
</ul>
<code>array_*</code>'s must be initialized before they are used, and freed when they are no longer needed. khen the array is freed, the objects it contains are freed as well.
<h4>Functions</h4>
<ul>
<liid="func_array_init">
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_int">int array_int_init(array_int* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_uint">int array_uint_init(array_uint* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_double">int array_double_init(array_double* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_ldouble">int array_ldouble_init(array_ldouble* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_mpfr">int array_mpfr_init(array_mpfr* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_2_mpfr">int array_2_mpfr_init(array_2_mpfr* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_char">int array_char_init(array_char* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_str">int array_str_init(array_str* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_polynomial_double">int array_polynomial_double_init(array_polynomial_double* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_polynomial_ldouble">int array_polynomial_ldouble_init(array_polynomial_ldouble* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_polynomial_mpfr">int array_polynomial_mpfr_init(array_polynomial_mpfr* array, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_init_pthread_t">int array_pthread_t_init(array_pthread_t* array, unsigned int memory)</code>
</div>
<br>
initialize the array pointed to by <code>array</code>, and allocate <code>memory</code> elements.
append <code>value</code> to the end of the array pointed to by <code>array</code>. Do not initialize the new value, instead, copy <code>value</code> to the end of <code>*array</code>. <code>value</code> must not be freed, since it will be freed when <code>*array</code> is. Resize the array if needed.
copy <code>input</code> to the array pointed to by <code>output</code>. Does not initialize <code>*output</code>, so <code>*output</code> must be initialized ahead of time, and its memory must be larger or equal to the length of <code>input</code>. Returns <code>LIBINUM_ERROR_SIZE_MISMATCH</code> if the memory of <code>*output</code> is smaller than the length of <code>input</code>.
<codeid="func_array_subarray_int">int array_int_subarray(array_int array, unsigned int begin, unsigned int end, array_int* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_uint">int array_uint_subarray(array_uint array, unsigned int begin, unsigned int end, array_uint* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_double">int array_double_subarray(array_double array, unsigned int begin, unsigned int end, array_double* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_ldouble">int array_ldouble_subarray(array_ldouble array, unsigned int begin, unsigned int end, array_ldouble* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_mpfr">int array_mpfr_subarray(array_mpfr array, unsigned int begin, unsigned int end, array_mpfr* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_2_mpfr">int array_2_mpfr_subarray(array_2_mpfr array, unsigned int begin, unsigned int end, array_2_mpfr* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_char">int array_char_subarray(array_char array, unsigned int begin, unsigned int end, array_char* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_str">int array_str_subarray(array_str array, unsigned int begin, unsigned int end, array_str* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_polynomial_double">int array_polynomial_double_subarray(array_polynomial_double array, unsigned int begin, unsigned int end, array_polynomial_double* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_polynomial_ldouble">int array_polynomial_ldouble_subarray(array_polynomial_ldouble array, unsigned int begin, unsigned int end, array_polynomial_ldouble* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_polynomial_mpfr">int array_polynomial_mpfr_subarray(array_polynomial_mpfr array, unsigned int begin, unsigned int end, array_polynomial_mpfr* subarray)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_subarray_pthread_t">int array_pthread_t_subarray(array_pthread_t array, unsigned int begin, unsigned int end, array_pthread_t* subarray)</code>
</div>
<br>
extract the sub-array of <code>array</code> whose indices are larger or equal to <code>begin</code> and smaller or equal to <code>end</code>. Returns <code>LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS</code> if the subarray does not exist.
search for <code>val</code> in <code>array</code>. Returns the index of <code>val</code> in <code>array</code> if it is present, and <code>-1</code> if it is not.
sort the elements of <code>array</code> from smallest to largest (for numerical values, the ordering relation is the usual one, for characters and strings, the lexicographical ordering is used). The sorting is performed in place. The <i>quicksort</i> algorithm is used.
</li>
<br>
<liid="func_array_sort_sub">
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_int">int array_int_sort_sub(array_int array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_uint">int array_uint_sort_sub(array_uint array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_double">int array_double_sort_sub(array_double array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_ldouble">int array_ldouble_sort_sub(array_ldouble array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_mpfr">int array_mpfr_sort_sub(array_mpfr array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_2_mpfr">int array_2_mpfr_sort_sub(array_2_mpfr array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_char">int array_char_sort_sub(array_char array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_str">int array_str_sort_sub(array_str array, unsigned int begin, unsigned int end)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_array_sort_sub_pthread_t">int array_pthread_t_sort_sub(array_pthread_t array, unsigned int begin, unsigned int end)</code>
</div>
<br>
sort the elements of the sub-array of <code>array</code> consisting of the elements whose index is larger or equal than <code>begin</code> and smaller or equal than <code>end</code>. The sorting is performed in place. The <i>quicksort</i> algorithm is used.
compare <code>array1</code> and <code>array2</code>. Returns <code>0</code> if both arrays are equal (that is, if their elements are the same, and in the same order), <code>-1</code> if <code>array1</code> is smaller than <code>array2</code> in lexicographical order, and <code>1</code> if <code>array1</code> is smaller than <code>array2</code> in lexicographical order.
</li>
</ul>
<h4>Extra functions for <code>array_char</code></h4>
In addition, the following functions are defined for <code>array_char</code>.
convert the array pointed to by <code>input</code> to a string, and return it. Appends <code>'\0'</code> at the end of <code>*input</code>. The string thus generated should not be freed, since it is actually a pointer to <code>input->str</code>, which is freed when <code>*input</code> is.
compare the string in <code>array_char</code> and the string <code>str</code>. Returns <code>1</code> if the strings are identical, and <code>0</code> if not.
reads the format <code>fmt</code> and appends the output to the array pointed to by <code>output</code>. The format should follow the specifications of the standard C function <code>printf</code>.
<p>A polynomial with real coefficients. A <code>polynomial</code> is represented as an array of coefficients and an array of exponents. For example \(1+x+2x^2\) is represented as <code>({1.,1.,2.},{0,1,2})</code>.</p>
<h4>Structure</h4>
The coefficients of polynomials can be double, extended, or multi-precision floats, and their exponents are unsigned integers. Polynomials are represented as structures, with the following keys:
<codeid="func_polynomial_resize_double">polynomial_double_resize(polynomial_double* poly, unsigned int newsize)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_resize_ldouble">polynomial_ldouble_resize(polynomial_ldouble* poly, unsigned int newsize)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_resize_mpfr">polynomial_mpfr_resize(polynomial_mpfr* poly, unsigned int newsize)</code>
</div>
<br>
copy the array polynomial to by <code>poly</code> to another with memory <code>newsize</code>.
</li>
<br>
<liid="func_polynomial_append">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_append_double">polynomial_double_add_monomial(double val, unsigned int order, polynomial_double* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_append_ldouble">polynomial_ldouble_add_monomial(long double val, unsigned int order, polynomial_ldouble* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_append_mpfr">polynomial_mpfr_add_monomial(mpfr_t val, unsigned int order, polynomial_mpfr* output)</code>
</div>
<br>
add a term with coefficient <code>val</code> and exponent <code>order</code> to the polynomial pointed to by <code>output</code>. Resize the polynomial if needed.
</li>
<br>
<liid="func_polynomial_append_d">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_append_d_ldouble">polynomial_ldouble_add_monomial_dui(double val, unsigned int order, polynomial_ldouble* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_append_d_mpfr">polynomial_mpfr_add_monomial_dui(double val, unsigned int order, polynomial_mpfr* output)</code>
</div>
<br>
add a term with coefficient <code>val</code> (converted from a <code>double</code>) and exponent <code>order</code> (converted from an <code>unsigned int</code>) to the polynomial pointed to by <code>output</code>. Resize the polynomial if needed.
copy <code>input</code> to the polynomial pointed to by <code>output</code>. Does not initialize <code>*output</code>, so <code>*output</code> must be initialized ahead of time, and its memory must be larger or equal to the length of <code>input</code>. Returns <code>LIBINUM_ERROR_SIZE_MISMATCH</code> if the memory of <code>*output</code> is smaller than the length of <code>input</code>.
add <code>poly1</code> and <code>poly2</code> and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
multiply <code>x</code> and <code>poly</code> and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
multiply <code>x</code> and <code>*poly</code> and write the result to the polynomial pointed to by <code>poly</code>.
</li>
<br>
<liid="func_polynomial_mul_monomial">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_double">polynomial_double_mul_monomial(double x, unsigned int order, polynomial_double poly, polynomial_double* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_ldouble">polynomial_ldouble_mul_monomial(long double x, unsigned int order, polynomial_ldouble poly, polynomial_ldouble* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_mpfr">polynomial_mpfr_mul_monomial(mpfr_t x, unsigned int order, polynomial_mpfr poly, polynomial_mpfr* output)</code>
</div>
<br>
multiply <code>poly</code> and the monomial whose coefficient and exponent are <code>x</code> and <code>order</code>, and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
</li>
<br>
<liid="func_polynomial_mul_monomial_inplace">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_inplace_double">polynomial_double_mul_monomial_inplace(double x, unsigned int order, polynomial_double* poly)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_inplace_ldouble">polynomial_ldouble_mul_monomial_inplace(long double x, unsigned int order, polynomial_ldouble* poly)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_mul_monomial_inplace_mpfr">polynomial_mpfr_mul_monomial_inplace(mpfr_t x, unsigned int order, polynomial_mpfr* poly)</code>
</div>
<br>
multiply <code>*poly</code> and the monomial whose coefficient and exponent are <code>x</code> and <code>order</code>, and write the result to the polynomial pointed to by <code>poly</code>.
multiply <code>poly1</code> and <code>poly2</code> and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
<codeid="func_polynomial_legendre_double">polynomial_double_legendre(unsigned int n, polynomial_double* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_legendre_ldouble">polynomial_ldouble_legendre(unsigned int n, polynomial_ldouble* output)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomial_legendre_mpfr">polynomial_mpfr_legendre(unsigned int n, polynomial_mpfr* output)</code>
</div>
<br>
compute the <code>n</code>-th Legendre polynomial, and write the output to the polynomial pointed to by <code>poly</code>. Initializes <code>*poly</code>.
<p>Multi-variable polynomials. A <code>polynomial</code> is represented as an array of coefficients and an array of arrays of indices, each of which represents a variable. For example, \(3x_1^2+x_1x_2\) is represented as <code>({3,1},{{1,1},{1,2}})</code>.</p>
<h4>Structure</h4>
The coefficients of multi-variable polynomials can be integers or multiprecision integers, and the indices are integers. Multi-variable polynomials are represented as structures, with the following keys:
<ul>
<liid="struct_polynomialMV_int">
<code>polynomialMV_int</code>:
<code>{ int* coefficients, array_int factors, unsigned int length, unsigned int memory }</code>
</li>
<liid="struct_polynomialMV_mpz">
<code>polynomialMV_mpz</code>:
<code>{ mpz_t* coefficients, array_int factors, unsigned int length, unsigned int memory }</code>
</li>
</ul>
where the keys correspond to
<ul>
<li><code>coefficients</code>: array of coefficients</li>
<li><code>factors</code>: array of <i>factors</i>, i.e. arrays of indices of variables</li>
<li><code>length</code>: the number of terms in the polynomial</li>
<li><code>memory</code>: the number of terms allocated to the polynomial</li>
</ul>
<code>polynomialMV_*</code>'s must be initialized before they are used, and freed when they are no longer needed. When the polynomial is freed, its coefficients and factors are freed as well.
<h4>Functions</h4>
<ul>
<liid="func_polynomialMV_init">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_init_int">polynomialMV_int_init(polynomialMV_int* poly, unsigned int memory)</code>
</div>
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_init_mpz">polynomialMV_mpz_init(polynomialMV_mpz* poly, unsigned int memory)</code>
</div>
<br>
initialize the polynomial pointed to by <code>poly</code>, and allocate <code>memory</code> terms.
copy <code>input</code> to the polynomial pointed to by <code>output</code>. Does not initialize <code>*output</code>, so <code>*output</code> must be initialized ahead of time, and its memory must be larger or equal to the length of <code>input</code>. Returns <code>LIBINUM_ERROR_SIZE_MISMATCH</code> if the memory of <code>*output</code> is smaller than the length of <code>input</code>.
</li>
<br>
<liid="func_polynomialMV_append">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_append_int">polynomialMV_int_append(array_int factor, int coef, polynomialMV_int* output)</code>
add a term with coefficient <code>coef</code> and factor <code>factor</code> to the polynomial pointed to by <code>output</code>. Resize the polynomial if needed.
</li>
<br>
<liid="func_polynomialMV_append_inplace">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_append_inplace_int">polynomialMV_int_append_inplace(array_int factor, int coef, polynomialMV_int* output)</code>
same as <ahref="#func_polynomialMV_append"><code>polynomialMV_*_append</code></a> except that if the factor is already present in the polynomial pointed to by <code>output</code>, then add <code>coef</code> to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
</li>
<br>
<liid="func_polynomialMV_append_noinit">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_append_noinit_int">polynomialMV_int_append_noinit(array_int factor, int coef, polynomialMV_int* output)</code>
same as <ahref="#func_polynomialMV_append"><code>polynomialMV_*_append</code></a> except that the new factor and coefficient to be appended to the polynomial pointed to by <code>output</code> are not allocated. Instead, pointers to <code>factor</code> and <code>coef</code> are appended. <code>factor</code> and <code>coef</code> must therefore not be freed (since they will be freed when <code>output</code> is). Resize the polynomial if needed.
</li>
<br>
<liid="func_polynomialMV_append_noinitfactor">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_append_noinitfactor_int">polynomialMV_int_append_noinitfactor(array_int factor, int coef, polynomialMV_int* output)</code>
same as <ahref="#func_polynomialMV_append"><code>polynomialMV_*_append</code></a> except that the new factor to be appended to the polynomial pointed to by <code>output</code> is not allocated. Instead, a pointer to <code>factor</code> is appended. <code>factor</code> must therefore not be freed (since it will be freed when <code>output</code> is). <code>coef</code> is, however, copied and must be freed. Resize the polynomial if needed.
</li>
<br>
<liid="func_polynomialMV_append_noinit_inplace">
<divstyle="margin-bottom:5pt">
<codeid="func_polynomialMV_append_noinit_inplace_int">polynomialMV_int_append_noinit_inplace(array_int factor, int coef, polynomial* output)</code>
same as <ahref="#func_polynomialMV_append_noinit"><code>polynomialMV_*_append_noinit</code></a> except that if the factor is already present in the polynomial pointed to by <code>output</code>, then add <code>coef</code> to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
same as <ahref="#func_polynomialMV_append_noinitfactor"><code>polynomialMV_*_append_noinitfactor</code></a> except that if the factor is already present in the polynomial pointed to by <code>output</code>, then add <code>coef</code> to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
add <code>poly1</code> and <code>poly2</code> and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
multiply <code>poly1</code> and <code>poly2</code> and write the result to the polynomial pointed to by <code>output</code>. Initializes <code>*output</code>.
Functions are not, strictly speaking, a C type. Instead, in this section, we describe how functions whose pointers are to be passed to the various algorithms of <b>libinum</b>, should be formatted.
</p>
<p>
Functions must take 3 arguments, and return an integer (a return code):
<codeclass="codeblock">int f(TYPE* out, TYPE in, void* extra_args)</code>
where <code>TYPE</code> is the appropriate data type, e.g. <code>double</code> or <code>mpfr_t</code>. <code>in</code> is the argument of the function, <code>out</code> is made to point to \(f(\)<code>in</code>\()\), and <code>extra_args</code> is a pointer to void, which may contain extra arguments to be passed to the function, for example a parameter that the function depends on. For example, to implement the function \(f_\alpha(x)=x^\alpha\) using MPFR floats,
print <code>x</code> to <code>file</code>, with as many digits as allowed by the MPFR precision.
</li>
<liid="func_print_datatype_info">
<code>print_datatype_info()</code><br>
print miscellaneous information about integers and floats. Namely, the number of bits used in <code>char</code>, <code>short int</code>, <code>int</code>, <code>long int</code> and <code>long long int</code>; the precision and maximal and minimal exponents of <code>double</code> and <code>long double</code>; the type used to store the precision and emax of MPFR floats.
</li>
</ul>
<h2class="section"id="sec_examples">Examples</h2>
<p>
Examples of programs using <b>libinum</b> are provided with the source code, in the <code>doc/libinum-examples</code> directory. If <b>libinum</b> is installed on the filesystem, then the examples can also be found at <code>/usr/share/doc/libinum/libinum-examples</code>.