1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 #ifndef _GNM_MATHFUNC_H_
3 # define _GNM_MATHFUNC_H_
13 /* It was reported that mips-sgi-irix6.5 has a weird and conflicting define
14 for qgamma. See bug 1689. */
15 #warning "Your <math.h> is somewhat broken; we'll work around that."
19 #define M_PIgnum GNM_const(3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117)
20 #define M_PI_2gnum (M_PIgnum / 2)
21 /* The following are very good given a good compiler. */
22 #define M_LN2gnum GNM_const(0.693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996419)
23 #define M_LN10gnum GNM_const(2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598)
24 #define M_LN10INVgnum GNM_const(0.434294481903251827651128918916605082294397005803666566114)
25 #define M_SQRT2gnum GNM_const(1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327642)
26 #define M_Egnum GNM_const(2.718281828459045235360287471352662497757247)
27 #define M_LN_SQRT_2PI GNM_const(0.918938533204672741780329736406) /* log(sqrt(2*pi)) */
29 /* ------------------------------------------------------------------------- */
31 gnm_float
log1pmx (gnm_float x
);
32 gnm_float
swap_log_tail (gnm_float lp
);
33 gnm_float
pow1p (gnm_float x
, gnm_float y
);
34 gnm_float
pow1pm1 (gnm_float x
, gnm_float y
);
35 gnm_float
gnm_trunc (gnm_float x
);
36 gnm_float
logspace_add (gnm_float logx
, gnm_float logy
);
37 gnm_float
logspace_sub (gnm_float logx
, gnm_float logy
);
38 gnm_float
gnm_owent (gnm_float h
, gnm_float a
);
39 gnm_float
gnm_logcf (gnm_float x
, gnm_float i
, gnm_float d
);
40 gnm_float
expmx2h (gnm_float x
);
41 gnm_float
agm(gnm_float a
, gnm_float b
);
44 /* "p": distribution function. */
45 /* "q": inverse distribution function. */
47 /* The normal distribution. */
48 gnm_float
pnorm (gnm_float x
, gnm_float mu
, gnm_float sigma
, gboolean lower_tail
, gboolean log_p
);
49 gnm_float
qnorm (gnm_float p
, gnm_float mu
, gnm_float sigma
, gboolean lower_tail
, gboolean log_p
);
51 /* The gamma distribution. */
52 gnm_float
dgamma (gnm_float x
, gnm_float shape
, gnm_float scale
, gboolean give_log
);
53 gnm_float
pgamma (gnm_float x
, gnm_float shape
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
54 gnm_float
qgamma (gnm_float p
, gnm_float shape
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
56 /* The beta distribution. */
57 gnm_float
dbeta (gnm_float x
, gnm_float a
, gnm_float b
, gboolean give_log
);
58 gnm_float
pbeta (gnm_float x
, gnm_float a
, gnm_float b
, gboolean lower_tail
, gboolean log_p
);
59 gnm_float
qbeta (gnm_float p
, gnm_float a
, gnm_float b
, gboolean lower_tail
, gboolean log_p
);
61 /* The t distribution. */
62 gnm_float
dt (gnm_float x
, gnm_float n
, gboolean give_log
);
63 gnm_float
pt (gnm_float x
, gnm_float n
, gboolean lower_tail
, gboolean log_p
);
64 gnm_float
qt (gnm_float p
, gnm_float n
, gboolean lower_tail
, gboolean log_p
);
66 /* The F distribution. */
67 gnm_float
df (gnm_float x
, gnm_float n1
, gnm_float n2
, gboolean give_log
);
68 gnm_float
pf (gnm_float x
, gnm_float n1
, gnm_float n2
, gboolean lower_tail
, gboolean log_p
);
69 gnm_float
qf (gnm_float p
, gnm_float n1
, gnm_float n2
, gboolean lower_tail
, gboolean log_p
);
71 /* The chi-squared distribution. */
72 gnm_float
dchisq (gnm_float x
, gnm_float df
, gboolean give_log
);
73 gnm_float
pchisq (gnm_float x
, gnm_float df
, gboolean lower_tail
, gboolean log_p
);
74 gnm_float
qchisq (gnm_float p
, gnm_float df
, gboolean lower_tail
, gboolean log_p
);
76 /* The Weibull distribution. */
77 gnm_float
dweibull (gnm_float x
, gnm_float shape
, gnm_float scale
, gboolean give_log
);
78 gnm_float
pweibull (gnm_float x
, gnm_float shape
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
79 gnm_float
qweibull (gnm_float p
, gnm_float shape
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
81 /* The Poisson distribution. */
82 gnm_float
dpois (gnm_float x
, gnm_float lambda
, gboolean give_log
);
83 gnm_float
ppois (gnm_float x
, gnm_float lambda
, gboolean lower_tail
, gboolean log_p
);
84 gnm_float
qpois (gnm_float p
, gnm_float lambda
, gboolean lower_tail
, gboolean log_p
);
86 /* The exponential distribution. */
87 gnm_float
dexp (gnm_float x
, gnm_float scale
, gboolean give_log
);
88 gnm_float
pexp (gnm_float x
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
89 gnm_float
qexp (gnm_float p
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
91 /* Binomial distribution. */
92 gnm_float
dbinom (gnm_float x
, gnm_float n
, gnm_float psuc
, gboolean give_log
);
93 gnm_float
pbinom (gnm_float x
, gnm_float n
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
94 gnm_float
pbinom2 (gnm_float x0
, gnm_float x1
, gnm_float n
, gnm_float p
);
95 gnm_float
qbinom (gnm_float p
, gnm_float n
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
97 /* Negative binomial distribution. */
98 gnm_float
dnbinom (gnm_float x
, gnm_float n
, gnm_float psuc
, gboolean give_log
);
99 gnm_float
pnbinom (gnm_float x
, gnm_float n
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
100 gnm_float
qnbinom (gnm_float p
, gnm_float n
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
102 /* Hyper-geometrical distribution. */
103 gnm_float
dhyper (gnm_float x
, gnm_float r
, gnm_float b
, gnm_float n
, gboolean give_log
);
104 gnm_float
phyper (gnm_float x
, gnm_float r
, gnm_float b
, gnm_float n
, gboolean lower_tail
, gboolean log_p
);
106 /* Geometric distribution. */
107 gnm_float
dgeom (gnm_float x
, gnm_float psuc
, gboolean give_log
);
108 gnm_float
pgeom (gnm_float x
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
109 gnm_float
qgeom (gnm_float p
, gnm_float psuc
, gboolean lower_tail
, gboolean log_p
);
111 /* Cauchy distribution. */
112 gnm_float
dcauchy (gnm_float x
, gnm_float location
, gnm_float scale
, gboolean give_log
);
113 gnm_float
pcauchy (gnm_float x
, gnm_float location
, gnm_float scale
, gboolean lower_tail
, gboolean log_p
);
115 /* The probability density functions. */
116 gnm_float
random_exppow_pdf (gnm_float x
, gnm_float a
, gnm_float b
);
117 gnm_float
random_laplace_pdf (gnm_float x
, gnm_float a
);
119 /* Studentized range distribution */
120 /* Note: argument order differs from R. */
121 gnm_float
ptukey(gnm_float x
, gnm_float nmeans
, gnm_float df
, gnm_float nranges
, gboolean lower_tail
, gboolean log_p
);
122 gnm_float
qtukey(gnm_float p
, gnm_float nmeans
, gnm_float df
, gnm_float nranges
, gboolean lower_tail
, gboolean log_p
);
124 /* ------------------------------------------------------------------------- */
126 /* Matrix functions. */
129 gnm_float
**data
; /* [y][x] */
133 GnmMatrix
*gnm_matrix_new (int rows
, int cols
); /* Note the order: y then x. */
134 void gnm_matrix_free (GnmMatrix
*m
);
135 GnmMatrix
*gnm_matrix_from_value (GnmValue
const *v
, GnmValue
**perr
, GnmEvalPos
const *ep
);
136 GnmValue
*gnm_matrix_to_value (GnmMatrix
const *m
);
137 gboolean
gnm_matrix_is_empty (GnmMatrix
const *m
);
139 void gnm_matrix_multiply (GnmMatrix
*C
, const GnmMatrix
*A
, const GnmMatrix
*B
);
141 gboolean
gnm_matrix_eigen (GnmMatrix
const *m
, GnmMatrix
*EIG
, gnm_float
*eigenvalues
);
143 gboolean
gnm_matrix_modified_cholesky (GnmMatrix
const *A
,
149 GORegressionResult
gnm_linear_solve_posdef (GnmMatrix
const *A
, const gnm_float
*b
,
152 GORegressionResult
gnm_linear_solve (GnmMatrix
const *A
, const gnm_float
*b
,
155 GORegressionResult
gnm_linear_solve_multiple (GnmMatrix
const *A
, GnmMatrix
*B
);
157 /* ------------------------------------------------------------------------- */
159 void mathfunc_init (void);
161 /* ------------------------------------------------------------------------- */
165 #endif /* _GNM_MATHFUNC_H_ */