mathfunc.h 4.65 KB
Newer Older
1 2 3
#ifndef GNUMERIC_MATHFUNC_H
#define GNUMERIC_MATHFUNC_H

4
#include "numbers.h"
5
#include <math.h>
6
#include <glib.h>
7

8
#ifndef FINITE
9
#  if defined(HAVE_FINITE)
10 11 12
#    ifdef HAVE_IEEEFP_H
#      include <ieeefp.h>
#    endif
13
#    define FINITE finite
14
#  elif defined(HAVE_ISFINITE)
15
#    define FINITE isfinite
16 17
#  else
#    error FINITE undefined
18 19 20
#  endif
#endif

21 22 23 24 25 26 27
#ifdef qgamma
/* It was reported that mips-sgi-irix6.5 has a weird and conflicting define
   for qgamma.  See bug 1689.  */
#warning "Your <math.h> is somewhat broken; we'll work around that."
#undef qgamma
#endif

28 29 30 31 32 33 34
/* Make up for a few deficient headers.  */
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
#ifndef M_LN10
#define M_LN10 2.30258509299404568402
#endif
35 36 37

/* ------------------------------------------------------------------------- */

38
double gnumeric_add_epsilon (double x);
39
double gnumeric_sub_epsilon (double x);
40 41 42 43 44
double gnumeric_fake_floor (double x);
double gnumeric_fake_ceil (double x);
double gnumeric_fake_round (double x);
double gnumeric_fake_trunc (double x);

45 46
/* ------------------------------------------------------------------------- */

47 48 49
double bessel_i (double x, double alpha, double expo);
double bessel_k (double x, double alpha, double expo);

50 51 52 53 54 55 56 57 58 59 60 61 62 63
/* "d": density.  */
/* "p": distribution function.  */
/* "q": inverse distribution function.  */

/* The normal distribution.  */
double dnorm (double x, double mu, double sigma);
double pnorm (double x, double mu, double sigma);
double qnorm (double p, double mu, double sigma);

/* The log-normal distribution.  */
double plnorm (double x, double logmean, double logsd);
double qlnorm (double x, double logmean, double logsd);

/* The gamma distribution.  */
64
double dgamma (double x, double shape, double scale);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
double pgamma (double x, double p, double scale);
double qgamma (double p, double alpha, double scale);

/* The beta distribution.  */
double pbeta (double x, double pin, double qin);
double qbeta (double alpha, double p, double q);

/* The t distribution.  */
double pt (double x, double n);
double qt (double p, double ndf);

/* The F distribution.  */
double pf (double x, double n1, double n2);
double qf (double x, double n1, double n2);

/* The chi-squared distribution.  */
double pchisq (double x, double df);
double qchisq (double p, double df);

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/* The Weibull distribution.  */
double dweibull (double x, double shape, double scale);
double pweibull (double x, double shape, double scale);

/* The Poisson distribution.  */
double dpois (double x, double lambda);
double ppois (double x, double lambda);

/* The exponential distribution.  */
double dexp (double x, double scale);
double pexp (double x, double scale);

/* Binomial distribution.  */
double dbinom (double x, double n, double p);
double pbinom (double x, double n, double p);
double qbinom (double x, double n, double p);

101 102 103 104 105 106 107 108 109
/* Random number generation. */
double random_01          (void);
double random_poisson     (double lambda);
double random_binomial    (double p, int trials);
double random_negbinom    (double p, int f);
double random_exponential (double b);
double random_bernoulli   (double p);
double random_normal      (void);

110 111
/* ------------------------------------------------------------------------- */

112
/* Matrix functions. */
113
gnum_float mdeterm (gnum_float *A, int dim);
114 115 116
int     minverse (gnum_float *A, int dim, gnum_float *res);
void    mmult (gnum_float *A, gnum_float *B, int cols_a, int rows_a, int cols_b,
	       gnum_float *product);
117 118 119

/* ------------------------------------------------------------------------- */

120
/* Misc. */
121
gnum_float     gpow10 (int n);
122
int            gcd    (int a, int b);
123 124
gnum_float     combin (int n, int k);
gnum_float     fact   (int n);
125 126 127 128 129 130 131 132

/* ------------------------------------------------------------------------- */

/* Optimization methods for the Solver tool. */


/* Affine scaling */

133 134
typedef void (*affscale_callback_fun_t) (int iter, gnum_float *x,
					 gnum_float bv, gnum_float cx,
135 136
					 int n_variables, void *data);

137 138 139
gboolean affine_init (gnum_float *A, gnum_float *b, gnum_float *c, int n_constraints,
		      int n_variables, gnum_float *x);
gboolean affine_scale (gnum_float *A, gnum_float *b, gnum_float *c, gnum_float *x,
140
		       int n_constraints, int n_variables, gboolean max_flag,
141
		       gnum_float e, int max_iter,
142 143
		       affscale_callback_fun_t fun, void *data);

144
gboolean branch_and_bound (gnum_float *A, gnum_float *b, gnum_float *c, gnum_float *xx,
145
			   int n_constraints, int n_variables, int n_original,
146
			   gboolean max_flag, gnum_float e, int max_iter,
147 148
			   gboolean *int_r,
			   affscale_callback_fun_t fun, void *data,
149
			   gnum_float *best);
150

151 152
void stern_brocot (float val, int max_denom, int *res_num, int *res_denom);

153
#endif