forked from nmaxwell/mathlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_math.h
executable file
·164 lines (110 loc) · 2.42 KB
/
std_math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef STD_MATH_H
#define STD_MATH_H
#include <complex>
#include <math.h>
#include <arprec/mp_real.h>
#include "../tools/std_tools.h"
#include "std_math_constants.h"
#include "norms.h"
//#include "LC.h"
/*
* some commonly used functions, etc.
*/
bool is_number( double x )
{
if ( isnan(x) ) return false;
if ( isinf(x) ) return false;
if ( !(x == x) ) return false;
if ( 1.0*x != x ) return false;
return true;
}
template<class T >
inline T norm_sinc(T const & x)
{
if (x != 0.0) return sin(x*ml_pi)/(x*ml_pi);
else return 1.0;
}
inline double log_factorial(double const & x)
{
return gamma(x+1); // this is log(gamma(x+1))
}
inline double factorial(double const & x)
{
return exp(gamma(x+1)); // this is exp(log(gamma(x+1)))
}
inline double binom(double const & n, double const & k)
{
return exp( gamma(n+1.0)- gamma(k+1.0)- gamma(n-k+1.0));
}
template<class T >
T max(T * x, int n)
{
T M = x[0];
for (int i=0; i<n; i++)
if (x[i] > M) M = x[i];
return M;
}
template<class T >
T min(T * x, int n)
{
T M = x[0];
for (int i=0; i<n; i++)
if (x[i] < M) M = x[i];
return M;
}
template<class T >
T max_norm(T * x, int n)
{
T M = norm(x[0]);
for (int i=0; i<n; i++)
if (norm(x[i]) > M) M = norm(x[i]);
return M;
}
inline double sign(double const & x)
{
if ( x >= 0.0 ) return 1.0;
else return -1.0;
}
template<class X = double, class Y = X >
class functor
{
public:
virtual Y operator() (X const & x) const =0;
};
template<class X = double, class Y = double >
class functor2
{
public:
virtual Y operator() (X const & x, X const & y) const =0;
};
template<class X = double, class Y = double >
class functor3
{
public:
virtual Y operator() (X const & x, X const & y, X const & z)const =0;
};
template<class X = double, class Y = double >
class functor4
{
public:
virtual Y operator() (X const & x, X const & y, X const & z, X const & t) const =0;
};
template<class X = double, class Y = X >
class composition : public functor <X,Y >
{
// f composed with g, so f(g(x))
public:
functor<X,Y > const * f;
functor<X,Y > const * g;
composition(functor<X,Y > const & F, functor<X,Y > const & G )
:f(&F),g(&G) {}
~composition () {f=0; g=0; }
composition ()
:f(0),g(0) {}
public:
Y operator() (X const & x) const
{
return (*f)((*g)(x));
}
};
#endif