This is the official documentation for libinum, version 1.0.
libinum is a C library that implements several algorithms, intended for applications in numerical and symbolic computations.
libinum defines functions to perform the following tasks:
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 Data types for details.
One of the guiding principles of libinum 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 libinum, therefore, support various data types.
We will now briefly discuss the data types that may be used to represent numbers.
The standard integer types of the C language are char
, short int
, int
, long int
and long long int
, as well as their unsigned
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: int8_t
, 16: int16_t
, 32: int32_t
and 64: int64_t
.
libinum provides a function, print_datatype_info()
, that prints the equivalence table between char
, short int
, int
, long int
and long long int
and int8_t
, int16_t
, int32_t
and int64_t
for the implementation of the C library used to compile libinum.
In cases where the required number of bits of integers exceeds 64, one can use multi-precision integers, 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 libinum is provided by the GNU GMP library.
Let us first start with a description of floating point numbers. A real number \(x\) is approximated, with a precision of \(m\) bits, as a collection of three numbers: the sign \(s\in\{-1,1\}\); mantissa, whose binary expansion is denoted by \(a_1\cdots a_m\); and exponent \(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 precision 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\)).
In libinum, floating point number may either be double, extended or multi-precision numbers.
Double precision floats are represented using the double
type.
The precision and maximal and minimal values of the exponent of double
floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info()
.
For example, using version 5.3.0 of the GNU GCC 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.
Extended precision floats are represented using the long double
type. They require more memory than double precision floats, and, as a consequence, slightly more computation time.
The precision and maximal and minimal values of the exponent of long double
floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info()
.
For example, using version 5.3.0 of the GNU GCC 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.
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 libinum, multi-precision floats are implemented using the GNU MPFR library, and will be called MPFR floats.
The precision of MPFR floats can be set using the function mpfr_set_default_prec
(see the MPFR library documentation for details). The default value of the precision is 53 bits (as of version 3.1.4 of the MPFR library).
In addition, the maximal value (in absolute value) of the exponent can be controlled by setting emax using the function mpfr_set_emax
(see the MPFR library documentation for details).
Depending on the MPFR implementation, the precision and emax can either be an int
or a long int
, so its maximal and minimal values are platform dependent. The libinum function print_datatype_info()
prints which it is.
What follows is a detailed description of the functions that implement the algorithms supported by libinum.
libinum 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.
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: $$ |x_{n+1}-x_n|\leqslant \frac12|x_n-x_{n-1}|^2\sup_{\xi\in[x_{n+1},x_n]}\frac{f''(\xi)}{f'(\xi)} $$ so that, provided \(f\) is smooth and its derivative does not vanish in the intervals \([x_{n+1},x_n]\), the algorithm converges quadratically.
This algorithm has been implemented using double, extended and multi-precision floats.
int root_newton_double(
double* out,
int (*func)(double*, double, void*),
int (*deriv_func)(double*, double, void*),
double init,
double tolerance,
int maxiter,
void* extra_args)
int root_newton_ldouble(
long double* out,
int (*func)(long double*, long double, void*),
int (*deriv_func)(long double*, long double, void*),
long double init,
long double tolerance,
int maxiter,
void* extra_args)
int root_newton_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
int (*deriv_func)(mpfr_t*, mpfr_t, void*),
mpfr_t init,
mpfr_t tolerance,
int maxiter,
void* extra_args)
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
func
: pointer to the function that computes \(f(x)\). It must be in the format specified in functions.
deriv_func
: pointer to the function that computes the derivative of \(f\). It must be in the format specified in functions.
init
: the value of \(x_0\).
tolerance
: the algorithm halts when \(|x_{n+1}-x_n|\leqslant\)tolerance
. tolerance
thus provides control over the error of the algorithm.
maxiter
: maximum number of iterations. The algorithm gives up if this number is reached and throws a LIBINUM_ERROR_MAXITER
exception.
extra_args
: pointer to the extra arguments to be passed to the functions *func
and *deriv_func
when they are evaluated.
0
on success, and
LIBINUM_ERROR_MAXITER
if the maximal number of iterations was reached.LIBINUM_ERROR_NAN
if any of the evaluations of *func
or *deriv_func
returned nan
or infinity
, or if any of the evaluations of *deriv_func
returned 0
.
int root_newton_inplace_double(
double* out,
int (*func)(double*, double, void*),
int (*deriv_func)(double*, double, void*),
double tolerance,
int maxiter,
void* extra_args)
int root_newton_inplace_ldouble(
long double* out,
int (*func)(long double*, long double, void*),
int (*deriv_func)(long double*, long double, void*),
long double tolerance,
int maxiter,
void* extra_args)
int root_newton_inplace_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
int (*deriv_func)(mpfr_t*, mpfr_t, void*),
mpfr_t tolerance,
int maxiter,
void* extra_args)
root_newton_*
except that the value of \(x_0\) is passed to the function in the argument out
.
libinum can compute definite integrals of smooth real functions using Gauss-Legendre quadratures.
The main idea of Gauss-Legendre quadratures is to find a set of abcissa \(\{t_1,\cdots,t_N\}\in[-1,1]^N\) and weights \(\{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: $$ \int_{a}^bdx\ f(x)=\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right). $$ In the general case, if \(f\) is \(\mathcal C^{2N}\), then $$ \left|\int_{a}^bdx\ f(x)-\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right)\right| \leqslant \frac{N!^4}{(2N+1)(2N)!^3}\sup_{x\in[a,b]}\frac{d^{2N}f(x)}{dx^{2N}}. $$ The number \(N\) is called the order of the integration.
As it turns out, the abcissa are the roots of the \(N\)-th Legendre polynomial \(L_N\), defined by the recursive equation $$ (N+1)L_{N+1}(x)=(2N+1)xL_N(x)-NL_{N-1}(x),\quad L_0(x)=1,\quad L_1(x)=x $$ and the weights are $$ w_i=\frac2{(1-x_i^2)L_N'(t_i)}. $$
This algorithm has been implemented using double, extended and multi-precision floats
int integrate_gauss_double(
double* out,
int (*func)(double*, double, void*),
double lower,
double upper,
array_double abcissa,
array_double weights,
void* extra_args)
int integrate_gauss_ldouble(
long double* out,
int (*func)(long double*, long double, void*),
long double lower,
long double upper,
array_ldouble abcissa,
array_ldouble weights,
void* extra_args)
int integrate_gauss_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
mpfr_t lower,
mpfr_t upper,
array_mpfr abcissa,
array_mpfr weights,
void* extra_args)
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
func
: pointer to the function that computes the integrand \(f(x)\). It must be in the format specified in functions.
lower
: lower bound of the integration.
upper
: upper bound of the integration.
abcissa
: the abcissa used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
weights
: the weights used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
extra_args
: pointer to the extra arguments to be passed to the function *func
when it is evaluated (see functions).
0
on success, and
LIBINUM_ERROR_NAN
if any of the evaluations of *func
returned nan
or infinity
.LIBINUM_ERROR_SIZE_MISMATCH
if the lengths of the vectors abcissa
and weights
are different.
int integrate_gauss_multithread_double(
double* out,
int (*func)(double*, double, void*),
double lower,
double upper,
array_double abcissa,
array_double weights,
void* extra_args,
unsigned int threads,
array_pthread_t* thread_ids)
int integrate_gauss_multithread_ldouble(
long double* out,
int (*func)(long double*, long double, void*),
long double lower,
long double upper,
array_ldouble abcissa,
array_ldouble weights,
void* extra_args,
unsigned int threads,
array_pthread_t* thread_ids)
int integrate_gauss_multithread_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
mpfr_t lower,
mpfr_t upper,
array_mpfr abcissa,
array_mpfr weights,
void* extra_args,
unsigned int threads,
array_pthread_t* thread_ids)
integrate_gauss_*
, in which function calls are performed in parallel.integrate_gauss_*
, except for
threads
: number of threads to use for the computation.
thread_ids
: 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.
integrate_gauss_*
.
int integrate_gauss_smarttmp_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
mpfr_t lower,
mpfr_t upper,
array_mpfr abcissa,
array_mpfr weights,
array_mpfr* tmps,
void* extra_args)
integrate_gauss_*
, 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.integrate_gauss_*
, except for
tmps
: 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.
integrate_gauss_*
.
int integrate_gauss_smarttmp_multithread_mpfr(
mpfr_t* out,
int (*func)(mpfr_t*, mpfr_t, void*),
mpfr_t lower,
mpfr_t upper,
array_mpfr abcissa,
array_mpfr weights,
void* extra_args,
unsigned int threads,
array_pthread_t* thread_ids)
integrate_gauss_smarttmp_*
, in which function calls are performed in parallel.integrate_gauss_smarttmp_*
, except for
threads
: number of threads to use for the computation.
thread_ids
: 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.
integrate_gauss_*
.
gauss_legendre_weights_double(
unsigned int order,
double tolerance,
unsigned int maxiter,
array_double* abcissa,
array_double* weights)
gauss_legendre_weights_ldouble(
unsigned int order,
long double tolerance,
unsigned int maxiter,
array_ldouble* abcissa,
array_ldouble* weights)
gauss_legendre_weights_mpfr(
unsigned int order,
mpfr_t tolerance,
unsigned int maxiter,
array_mpfr* abcissa,
array_mpfr* weights)
order
: the order \(N\) of the integration.
tolerance
: tolerance for the Newton algorithm used to compute the roots of the Legendre polynomial (see root_newton
).
maxiter
: the maximal number of steps to compute before giving up.
abcissa
: pointer to the array in which to write the abcissa. It must not be initialized.
weights
: pointer to the array in which to write the weights. It must not be initialized.
0
on success, and
LIBINUM_ERROR_MAXITER
if, when computing the roots of the Legendre polynomial using a Newton iteration, the maximal number of iterations was reached.What follows is a detailed description of the C types defined in libinum.
array
Array that can be dynamically resized.
array_int
:
{ int* values, unsigned int length, unsigned int memory }
array_uint
:
{ unsigned int* values, unsigned int length, unsigned int memory }
array_double
:
{ double* values, unsigned int length, unsigned int memory }
array_ldouble
:
{ long double* values, unsigned int length, unsigned int memory }
array_mpfr
:
{ mpfr_t* values, unsigned int length, unsigned int memory }
array_2_mpfr
:
{ array_mpfr* values, unsigned int length, unsigned int memory }
array_char
:
{ char* values, unsigned int length, unsigned int memory }
array_str
:
{ array_char* values, unsigned int length, unsigned int memory }
array_polynomial_double
:
{ polynomial_double* values, unsigned int length, unsigned int memory }
array_polynomial_ldouble
:
{ polynomial_ldouble* values, unsigned int length, unsigned int memory }
array_polynomial_mpfr
:
{ polynomial_mpfr* values, unsigned int length, unsigned int memory }
array_pthread_t
:
{ pthread_t* values, unsigned int length, unsigned int memory }
values
: the array in which the objects are storedlength
: the number of elements in the arraymemory
: the number of elements allocated to the arrayarray_*
'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.
int array_int_init(array_int* array, unsigned int memory)
int array_uint_init(array_uint* array, unsigned int memory)
int array_double_init(array_double* array, unsigned int memory)
int array_ldouble_init(array_ldouble* array, unsigned int memory)
int array_mpfr_init(array_mpfr* array, unsigned int memory)
int array_2_mpfr_init(array_2_mpfr* array, unsigned int memory)
int array_char_init(array_char* array, unsigned int memory)
int array_str_init(array_str* array, unsigned int memory)
int array_polynomial_double_init(array_polynomial_double* array, unsigned int memory)
int array_polynomial_ldouble_init(array_polynomial_ldouble* array, unsigned int memory)
int array_polynomial_mpfr_init(array_polynomial_mpfr* array, unsigned int memory)
int array_pthread_t_init(array_pthread_t* array, unsigned int memory)
array
, and allocate memory
elements.
int array_int_free(array_int array)
int array_uint_free(array_uint array)
int array_double_free(array_double array)
int array_ldouble_free(array_ldouble array)
int array_mpfr_free(array_mpfr array)
int array_2_mpfr_free(array_2_mpfr array)
int array_char_free(array_char array)
int array_str_free(array_str array)
int array_polynomial_double_free(array_polynomial_double array)
int array_polynomial_ldouble_free(array_polynomial_ldouble array)
int array_polynomial_mpfr_free(array_polynomial_mpfr array)
int array_pthread_t_free(array_pthread_t array)
array
.
int array_mpfr_free_vects(array_mpfr array)
int array_2_mpfr_free_vects(array_2_mpfr array)
int array_str_free_vects(array_str array)
int array_polynomial_double_free_vects(array_polynomial_double array)
int array_polynomial_ldouble_free_vects(array_polynomial_ldouble array)
int array_polynomial_mpfr_free_vects(array_polynomial_mpfr array)
array.values
, but do not free the elements of the array.
int array_int_resize(array_int* array, unsigned int newsize)
int array_uint_resize(array_uint* array, unsigned int newsize)
int array_double_resize(array_double* array, unsigned int newsize)
int array_ldouble_resize(array_ldouble* array, unsigned int newsize)
int array_mpfr_resize(array_mpfr* array, unsigned int newsize)
int array_2_mpfr_resize(array_2_mpfr* array, unsigned int newsize)
int array_char_resize(array_char* array, unsigned int newsize)
int array_str_resize(array_str* array, unsigned int newsize)
int array_polynomial_double_resize(array_polynomial_double* array, unsigned int newsize)
int array_polynomial_ldouble_resize(array_polynomial_ldouble* array, unsigned int newsize)
int array_polynomial_mpfr_resize(array_polynomial_mpfr* array, unsigned int newsize)
int array_pthread_t_resize(array_pthread_t* array, unsigned int newsize)
array
to another with memory newsize
.
int array_int_append(int value, array_int* array)
int array_uint_append(unsigned int value, array_uint* array)
int array_double_append(double value, array_double* array)
int array_ldouble_append(long double value, array_ldouble* array)
int array_mpfr_append(mpfr_t value, array_mpfr* array)
int array_2_mpfr_append(array_mpfr value, array_2_mpfr* array)
int array_char_append(char value, array_char* array)
int array_str_append(array_char value, array_str* array)
int array_polynomial_double_append(polynomial_double value, array_polynomial_double* array)
int array_polynomial_ldouble_append(polynomial_ldouble value, array_polynomial_ldouble* array)
int array_polynomial_mpfr_append(polynomial_mpfr value, array_polynomial_mpfr* array)
int array_pthread_t_append(pthread_t value, array_pthread_t* array)
value
to the end of the array pointed to by array
. Resize the array if needed.
int array_mpfr_append_noinit(int value, array_mpfr* array)
int array_2_mpfr_append_noinit(unsigned int value, array_2_mpfr* array)
int array_str_append_noinit(double value, array_str* array)
int array_polynomial_double_append_noinit(long double value, array_polynomial_double* array)
int array_polynomial_ldouble_append_noinit(mpfr_t value, array_polynomial_ldouble* array)
int array_polynomial_mpfr_append_noinit(array_mpfr value, array_polynomial_mpfr* array)
value
to the end of the array pointed to by array
. Do not initialize the new value, instead, copy value
to the end of *array
. value
must not be freed, since it will be freed when *array
is. Resize the array if needed.
int array_int_concat(array_int input, array_int* output)
int array_uint_concat(array_uint input, array_uint* output)
int array_double_concat(array_double input, array_double* output)
int array_ldouble_concat(array_ldouble input, array_ldouble* output)
int array_mpfr_concat(array_mpfr input, array_mpfr* output)
int array_2_mpfr_concat(array_2_mpfr input, array_2_mpfr* output)
int array_char_concat(array_char input, array_char* output)
int array_str_concat(array_str input, array_str* output)
int array_polynomial_double_concat(array_polynomial_double input, array_polynomial_double* output)
int array_polynomial_ldouble_concat(array_polynomial_ldouble input, array_polynomial_ldouble* output)
int array_polynomial_mpfr_concat(array_polynomial_mpfr input, array_polynomial_mpfr* output)
int array_pthread_t_concat(array_pthread_t input, array_pthread_t* output)
input
to the end of the array pointed to by output
. Resize the array if needed.
int array_int_cpy(array_int input, array_int* output)
int array_uint_cpy(array_uint input, array_uint* output)
int array_double_cpy(array_double input, array_double* output)
int array_ldouble_cpy(array_ldouble input, array_ldouble* output)
int array_mpfr_cpy(array_mpfr input, array_mpfr* output)
int array_2_mpfr_cpy(array_2_mpfr input, array_2_mpfr* output)
int array_char_cpy(array_char input, array_char* output)
int array_str_cpy(array_str input, array_str* output)
int array_polynomial_double_cpy(array_polynomial_double input, array_polynomial_double* output)
int array_polynomial_ldouble_cpy(array_polynomial_ldouble input, array_polynomial_ldouble* output)
int array_polynomial_mpfr_cpy(array_polynomial_mpfr input, array_polynomial_mpfr* output)
int array_pthread_t_cpy(array_pthread_t input, array_pthread_t* output)
input
to the array pointed to by output
. Initializes *output
.
int array_int_cpy_noinit(array_int input, array_int* output)
int array_uint_cpy_noinit(array_uint input, array_uint* output)
int array_double_cpy_noinit(array_double input, array_double* output)
int array_ldouble_cpy_noinit(array_ldouble input, array_ldouble* output)
int array_mpfr_cpy_noinit(array_mpfr input, array_mpfr* output)
int array_2_mpfr_cpy_noinit(array_2_mpfr input, array_2_mpfr* output)
int array_char_cpy_noinit(array_char input, array_char* output)
int array_str_cpy_noinit(array_str input, array_str* output)
int array_polynomial_double_cpy_noinit(array_polynomial_double input, array_polynomial_double* output)
int array_polynomial_ldouble_cpy_noinit(array_polynomial_ldouble input, array_polynomial_ldouble* output)
int array_polynomial_mpfr_cpy_noinit(array_polynomial_mpfr input, array_polynomial_mpfr* output)
int array_pthread_t_cpy_noinit(array_pthread_t input, array_pthread_t* output)
input
to the array pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
int array_int_subarray(array_int array, unsigned int begin, unsigned int end, array_int* subarray)
int array_uint_subarray(array_uint array, unsigned int begin, unsigned int end, array_uint* subarray)
int array_double_subarray(array_double array, unsigned int begin, unsigned int end, array_double* subarray)
int array_ldouble_subarray(array_ldouble array, unsigned int begin, unsigned int end, array_ldouble* subarray)
int array_mpfr_subarray(array_mpfr array, unsigned int begin, unsigned int end, array_mpfr* subarray)
int array_2_mpfr_subarray(array_2_mpfr array, unsigned int begin, unsigned int end, array_2_mpfr* subarray)
int array_char_subarray(array_char array, unsigned int begin, unsigned int end, array_char* subarray)
int array_str_subarray(array_str array, unsigned int begin, unsigned int end, array_str* subarray)
int array_polynomial_double_subarray(array_polynomial_double array, unsigned int begin, unsigned int end, array_polynomial_double* subarray)
int array_polynomial_ldouble_subarray(array_polynomial_ldouble array, unsigned int begin, unsigned int end, array_polynomial_ldouble* subarray)
int array_polynomial_mpfr_subarray(array_polynomial_mpfr array, unsigned int begin, unsigned int end, array_polynomial_mpfr* subarray)
int array_pthread_t_subarray(array_pthread_t array, unsigned int begin, unsigned int end, array_pthread_t* subarray)
array
whose indices are larger or equal to begin
and smaller or equal to end
. Returns LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS
if the subarray does not exist.
int array_int_print(array_int array)
int array_uint_print(array_uint array)
int array_double_print(array_double array)
int array_ldouble_print(array_ldouble array)
int array_mpfr_print(array_mpfr array)
int array_2_mpfr_print(array_2_mpfr array)
int array_char_print(array_char array)
int array_str_print(array_str array)
int array_polynomial_double_print(array_polynomial_double array)
int array_polynomial_ldouble_print(array_polynomial_ldouble array)
int array_polynomial_mpfr_print(array_polynomial_mpfr array)
array
.
int, unsigned int, double, long double, mpfr, char
array_char
polynomial_*
.int array_int_append_unique(int value, array_int* array)
int array_uint_append_unique(unsigned int value, array_uint* array)
int array_double_append_unique(double value, array_double* array)
int array_ldouble_append_unique(long double value, array_ldouble* array)
int array_mpfr_append_unique(mpfr_t value, array_mpfr* array)
int array_2_mpfr_append_unique(array_mpfr value, array_2_mpfr* array)
int array_char_append_unique(char value, array_char* array)
int array_str_append_unique(array_char value, array_str* array)
int array_pthread_t_append_unique(pthread_t value, array_pthread_t* array)
value
is not already present in the array pointed to by array
, then append it. Resize the array if needed.
int array_int_concat_unique(array_int input, array_int* output)
int array_uint_concat_unique(array_uint input, array_uint* output)
int array_double_concat_unique(array_double input, array_double* output)
int array_ldouble_concat_unique(array_ldouble input, array_ldouble* output)
int array_mpfr_concat_unique(array_mpfr input, array_mpfr* output)
int array_2_mpfr_concat_unique(array_2_mpfr input, array_2_mpfr* output)
int array_char_concat_unique(array_char input, array_char* output)
int array_str_concat_unique(array_str input, array_str* output)
int array_pthread_t_concat_unique(array_pthread_t input, array_pthread_t* output)
input
that are not already present in the array pointed to by output
. Resize the array if needed.
int array_int_find(int val, array_int array)
int array_uint_find(unsigned int val, array_uint array)
int array_double_find(double val, array_double array)
int array_ldouble_find(long double val, array_ldouble array)
int array_mpfr_find(mpfr_t val, array_mpfr array)
int array_2_mpfr_find(array_mpfr val, array_2_mpfr array)
int array_char_find(char val, array_char array)
int array_str_find(array_char val, array_str array)
int array_pthread_t_find(pthread_t val, array_pthread_t array)
val
in array
. Returns the index of val
in array
if it is present, and -1
if it is not.
int array_int_sort(array_int array)
int array_uint_sort(array_uint array)
int array_double_sort(array_double array)
int array_ldouble_sort(array_ldouble array)
int array_mpfr_sort(array_mpfr array)
int array_2_mpfr_sort(array_2_mpfr array)
int array_char_sort(array_char array)
int array_str_sort(array_str array)
int array_pthread_t_sort(array_pthread_t array)
array
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 quicksort algorithm is used.
int array_int_sort_sub(array_int array, unsigned int begin, unsigned int end)
int array_uint_sort_sub(array_uint array, unsigned int begin, unsigned int end)
int array_double_sort_sub(array_double array, unsigned int begin, unsigned int end)
int array_ldouble_sort_sub(array_ldouble array, unsigned int begin, unsigned int end)
int array_mpfr_sort_sub(array_mpfr array, unsigned int begin, unsigned int end)
int array_2_mpfr_sort_sub(array_2_mpfr array, unsigned int begin, unsigned int end)
int array_char_sort_sub(array_char array, unsigned int begin, unsigned int end)
int array_str_sort_sub(array_str array, unsigned int begin, unsigned int end)
int array_pthread_t_sort_sub(array_pthread_t array, unsigned int begin, unsigned int end)
array
consisting of the elements whose index is larger or equal than begin
and smaller or equal than end
. The sorting is performed in place. The quicksort algorithm is used.
int array_int_cmp(array_int array1, array_int array2)
int array_uint_cmp(array_uint array1, array_uint array2)
int array_double_cmp(array_double array1, array_double array2)
int array_ldouble_cmp(array_ldouble array1, array_ldouble array2)
int array_mpfr_cmp(array_mpfr array1, array_mpfr array2)
int array_2_mpfr_cmp(array_2_mpfr array1, array_2_mpfr array2)
int array_char_cmp(array_char array1, array_char array2)
int array_str_cmp(array_str array1, array_str array2)
int array_pthread_t_cmp(array_pthread_t array1, array_pthread_t array2)
array1
and array2
. Returns 0
if both arrays are equal (that is, if their elements are the same, and in the same order), -1
if array1
is smaller than array2
in lexicographical order, and 1
if array1
is smaller than array2
in lexicographical order.
array_char
array_char
.
int array_char_append_str(char* str, array_char* output)
str
to the end of the array pointed to by output
. Resize the array if needed.
int array_char_to_str(array_char input, char** output)
input
to the string pointed to by output
. Initializes *output
.
char* array_char_to_str(array_char* input)
input
to a string, and return it. Appends '\0'
at the end of *input
. The string thus generated should not be freed, since it is actually a pointer to input->str
, which is freed when *input
is.
int str_to_array_char(char* str, array_char* output)
input
to the array pointed to by output
. Initializes *output
.
int array_char_cmp_str(array_char array_char, char* str)
array_char
and the string str
. Returns 1
if the strings are identical, and 0
if not.
int array_char_snprintf(array_char* output, char* fmt)
fmt
and appends the output to the array pointed to by output
. The format should follow the specifications of the standard C function printf
.
polynomial
A polynomial with real coefficients. A polynomial
is represented as an array of coefficients and an array of exponents. For example \(1+x+2x^2\) is represented as ({1.,1.,2.},{0,1,2})
.
polynomial_double
:
{ array_double coefficients, array_uint orders }
polynomial_ldouble
:
{ array_ldouble coefficients, array_uint orders }
polynomial_mpfr
:
{ array_mpfr coefficients, array_uint orders }
coefficients
: array of coefficientsorders
: array of exponents.polynomial_*
's must be initialized before they are used, and freed when they are no longer needed.
polynomial_double_init(polynomial_double* poly, unsigned int memory)
polynomial_ldouble_init(polynomial_ldouble* poly, unsigned int memory)
polynomial_mpfr_init(polynomial_mpfr* poly, unsigned int memory)
poly
, and allocate memory
terms.
polynomial_double_free(polynomial_double poly)
polynomial_ldouble_free(polynomial_ldouble poly)
polynomial_mpfr_free(polynomial_mpfr poly)
poly
.
polynomial_double_resize(polynomial_double* poly, unsigned int newsize)
polynomial_ldouble_resize(polynomial_ldouble* poly, unsigned int newsize)
polynomial_mpfr_resize(polynomial_mpfr* poly, unsigned int newsize)
poly
to another with memory newsize
.
polynomial_double_add_monomial(double val, unsigned int order, polynomial_double* output)
polynomial_ldouble_add_monomial(long double val, unsigned int order, polynomial_ldouble* output)
polynomial_mpfr_add_monomial(mpfr_t val, unsigned int order, polynomial_mpfr* output)
val
and exponent order
to the polynomial pointed to by output
. Resize the polynomial if needed.
polynomial_ldouble_add_monomial_dui(double val, unsigned int order, polynomial_ldouble* output)
polynomial_mpfr_add_monomial_dui(double val, unsigned int order, polynomial_mpfr* output)
val
(converted from a double
) and exponent order
(converted from an unsigned int
) to the polynomial pointed to by output
. Resize the polynomial if needed.
polynomial_double_cpy(polynomial_double input, polynomial_double* output)
polynomial_ldouble_cpy(polynomial_ldouble input, polynomial_ldouble* output)
polynomial_mpfr_cpy(polynomial_mpfr input, polynomial_mpfr* output)
input
to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_cpy_noinit(polynomial_double input, polynomial_double* output)
polynomial_ldouble_cpy_noinit(polynomial_ldouble input, polynomial_ldouble* output)
polynomial_mpfr_cpy_noinit(polynomial_mpfr input, polynomial_mpfr* output)
input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
polynomial_double_add(polynomial poly1, polynomial_double poly2, polynomial_double* output)
polynomial_ldouble_add(polynomial poly1, polynomial_ldouble poly2, polynomial_ldouble* output)
polynomial_mpfr_add(polynomial poly1, polynomial_mpfr poly2, polynomial_mpfr* output)
poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_add_inplace(polynomial_double poly1, polynomial_double* poly2)
polynomial_ldouble_add_inplace(polynomial_ldouble poly1, polynomial_ldouble* poly2)
polynomial_mpfr_add_inplace(polynomial_mpfr poly1, polynomial_mpfr* poly2)
poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
polynomial_double_mul_scalar(double x, polynomial_double poly, polynomial_double* output)
polynomial_ldouble_mul_scalar(long double x, polynomial_ldouble poly, polynomial_ldouble* output)
polynomial_mpfr_mul_scalar(mpfr_t x, polynomial_mpfr poly, polynomial_mpfr* output)
x
and poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_mul_scalar_inplace(double x, polynomial_double* poly)
polynomial_ldouble_mul_scalar_inplace(long double x, polynomial_ldouble* poly)
polynomial_mpfr_mul_scalar_inplace(mpfr_t x, polynomial_mpfr* poly)
x
and *poly
and write the result to the polynomial pointed to by poly
.
polynomial_double_mul_monomial(double x, unsigned int order, polynomial_double poly, polynomial_double* output)
polynomial_ldouble_mul_monomial(long double x, unsigned int order, polynomial_ldouble poly, polynomial_ldouble* output)
polynomial_mpfr_mul_monomial(mpfr_t x, unsigned int order, polynomial_mpfr poly, polynomial_mpfr* output)
poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_mul_monomial_inplace(double x, unsigned int order, polynomial_double* poly)
polynomial_ldouble_mul_monomial_inplace(long double x, unsigned int order, polynomial_ldouble* poly)
polynomial_mpfr_mul_monomial_inplace(mpfr_t x, unsigned int order, polynomial_mpfr* poly)
*poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by poly
.
polynomial_double_mul(polynomial_double poly1, polynomial_double poly2, polynomial_double* output)
polynomial_ldouble_mul(polynomial_ldouble poly1, polynomial_ldouble poly2, polynomial_ldouble* output)
polynomial_mpfr_mul(polynomial_mpfr poly1, polynomial_mpfr poly2, polynomial_mpfr* output)
poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_mul_inplace(polynomial_double poly1, polynomial_double* poly2)
polynomial_ldouble_mul_inplace(polynomial_ldouble poly1, polynomial_ldouble* poly2)
polynomial_mpfr_mul_inplace(polynomial_mpfr poly1, polynomial_mpfr* poly2)
poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
polynomial_double_derive(polynomial_double poly, polynomial_double* output)
polynomial_ldouble_derive(polynomial_ldouble poly, polynomial_ldouble* output)
polynomial_mpfr_derive(polynomial_mpfr poly, polynomial_mpfr* output)
poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomial_double_derive_inplace(polynomial_double* poly)
polynomial_ldouble_derive_inplace(polynomial_ldouble* poly)
polynomial_mpfr_derive_inplace(polynomial_mpfr* poly)
*poly
and write the result to the polynomial pointed to by poly
.
polynomial_double_evaluate(double* out, double x, polynomial_double poly)
polynomial_ldouble_evaluate(long double* out, long double x, polynomial_ldouble poly)
polynomial_mpfr_evaluate(mpfr_t* out, mpfr_t x, polynomial_mpfr poly)
poly
at x
, and write the result to out
. out
must be initialized if its type requires it.
polynomial_double_print(polynomial_double poly)
polynomial_ldouble_print(polynomial_ldouble poly)
polynomial_mpfr_print(polynomial_mpfr poly)
poly
.
polynomial_double_legendre(unsigned int n, polynomial_double* output)
polynomial_ldouble_legendre(unsigned int n, polynomial_ldouble* output)
polynomial_mpfr_legendre(unsigned int n, polynomial_mpfr* output)
n
-th Legendre polynomial, and write the output to the polynomial pointed to by poly
. Initializes *poly
.
polynomialMV
Multi-variable polynomials. A polynomial
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 ({3,1},{{1,1},{1,2}})
.
polynomialMV_int
:
{ int* coefficients, array_int factors, unsigned int length, unsigned int memory }
polynomialMV_mpz
:
{ mpz_t* coefficients, array_int factors, unsigned int length, unsigned int memory }
coefficients
: array of coefficientsfactors
: array of factors, i.e. arrays of indices of variableslength
: the number of terms in the polynomialmemory
: the number of terms allocated to the polynomialpolynomialMV_*
'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.
polynomialMV_int_init(polynomialMV_int* poly, unsigned int memory)
polynomialMV_mpz_init(polynomialMV_mpz* poly, unsigned int memory)
poly
, and allocate memory
terms.
polynomialMV_int_free(polynomialMV_int poly)
polynomialMV_mpz_free(polynomialMV_mpz poly)
poly
.
polynomialMV_int_resize(polynomialMV_int* poly, unsigned int newsize)
polynomialMV_mpz_resize(polynomialMV_mpz* poly, unsigned int newsize)
poly
to another with memory newsize
.
polynomialMV_int_cpy(polynomialMV_int input, polynomialMV_int* output)
polynomialMV_mpz_cpy(polynomialMV_mpz input, polynomialMV_mpz* output)
input
to the polynomial pointed to by output
. Initializes *output
.
polynomialMV_int_cpy_noinit(polynomialMV_int input, polynomialMV_int* output)
polynomialMV_mpz_cpy_noinit(polynomialMV_mpz input, polynomialMV_mpz* output)
input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
polynomialMV_int_append(array_int factor, int coef, polynomialMV_int* output)
polynomialMV_mpz_append(array_int factor, mpz_t coef, polynomialMV_mpz* output)
coef
and factor factor
to the polynomial pointed to by output
. Resize the polynomial if needed.
polynomialMV_int_append_inplace(array_int factor, int coef, polynomialMV_int* output)
polynomialMV_mpz_append_inplace(array_int factor, mpz_t coef, polynomialMV_mpz* output)
polynomialMV_*_append
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
polynomialMV_int_append_noinit(array_int factor, int coef, polynomialMV_int* output)
polynomialMV_mpz_append_noinit(array_int factor, mpz_t coef, polynomialMV_mpz* output)
polynomialMV_*_append
except that the new factor and coefficient to be appended to the polynomial pointed to by output
are not allocated. Instead, pointers to factor
and coef
are appended. factor
and coef
must therefore not be freed (since they will be freed when output
is). Resize the polynomial if needed.
polynomialMV_int_append_noinitfactor(array_int factor, int coef, polynomialMV_int* output)
polynomialMV_mpz_append_noinitfactor(array_int factor, mpz_t coef, polynomialMV_mpz* output)
polynomialMV_*_append
except that the new factor to be appended to the polynomial pointed to by output
is not allocated. Instead, a pointer to factor
is appended. factor
must therefore not be freed (since it will be freed when output
is). coef
is, however, copied and must be freed. Resize the polynomial if needed.
polynomialMV_int_append_noinit_inplace(array_int factor, int coef, polynomial* output)
polynomialMV_mpz_append_noinit_inplace(array_int factor, mpz_t coef, polynomial* output)
polynomialMV_*_append_noinit
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
polynomialMV_int_append_noinit_inplace(array_int factor, int coef, polynomial* output)
polynomialMV_mpz_append_noinit_inplace(array_int factor, mpz_t coef, polynomial* output)
polynomialMV_*_append_noinitfactor
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
polynomialMV_int_add(polynomialMV_int poly1, polynomialMV_int poly2, polynomialMV_int* output)
polynomialMV_mpz_add(polynomialMV_mpz poly1, polynomialMV_mpz poly2, polynomialMV_mpz* output)
poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomialMV_int_add_inplace(polynomialMV_int poly1, polynomialMV_int* poly2)
polynomialMV_mpz_add_inplace(polynomialMV_mpz poly1, polynomialMV_mpz* poly2)
poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
polynomialMV_int_multiply_scalar(polynomial poly, int num)
polynomialMV_mpz_multiply_scalar(polynomial poly, mpz_t num)
num
and poly
and write the result to poly
.
polynomialMV_int_prod(polynomialMV_int poly1, polynomialMV_int poly2, polynomialMV_int* output)
polynomialMV_mpz_prod(polynomialMV_mpz poly1, polynomialMV_mpz poly2, polynomialMV_mpz* output)
poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
polynomialMV_int_order(polynomialMV_int poly)
polynomialMV_mpz_order(polynomialMV_mpz poly)
poly
, using array_*_sort.
polynomialMV_int_print(polynomialMV_int poly)
polynomialMV_mpz_print(polynomialMV_mpz poly)
poly
.
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 libinum, should be formatted.
Functions must take 3 arguments, and return an integer (a return code):
int f(TYPE* out, TYPE in, void* extra_args)
where TYPE
is the appropriate data type, e.g. double
or mpfr_t
. in
is the argument of the function, out
is made to point to \(f(\)in
\()\), and extra_args
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,
int f(mpfr_t out, mpfr_t in, void* extra_args){
int alpha=*((*int)extra_args);
mpfr_pow_ui(out, in, alpha, MPFR_RNDN);
return(0);
}
In addition, the following functions are defined
fprint_double(FILE* file, double x)
x
to file
, with as many digits as possible.
fprint_ldouble(FILE* file, long double x)
x
to file
, with as many digits as possible.
fprint_mpfr(FILE* file, mpfr_t x)
x
to file
, with as many digits as allowed by the MPFR precision.
print_datatype_info()
char
, short int
, int
, long int
and long long int
; the precision and maximal and minimal exponents of double
and long double
; the type used to store the precision and emax of MPFR floats.
Examples of programs using libinum are provided with the source code, in the doc/libinum-examples
directory. If libinum is installed on the filesystem, then the examples can also be found at /usr/share/doc/libinum/libinum-examples
.
libinum was written by Ian Jauslin.