forked from nmaxwell/mathlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon-lib_things.h
executable file
·302 lines (226 loc) · 6.16 KB
/
non-lib_things.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Some usefull things which mathlib does not depend on. Include after link.cpp if you use it at all.
*
*/
#ifndef iu
#define iu (complex<double>(0.0,1.0))
#endif
#ifndef pi
#define pi 3.141592653589793238463
#endif
#ifndef _2pi
#define _2pi 6.2831853071795865
#endif
#ifndef _4pi
#define _4pi 6.2831853071795865*2.0
#endif
#ifndef _4pi2
#define _4pi2 3.9478417604357434e+1
#endif
#ifndef pi2
#define pi2 9.8696044010893586
#endif
#ifndef sq2pi
#define sq2pi 2.5066282746310005
#endif
#ifndef sqrt2pi
#define sqrt2pi 2.5066282746310005
#endif
#ifndef sqrt2
#define sqrt2 1.414213562373095
#endif
#ifndef sqrtpi
#define sqrtpi 1.772453850905516
#endif
#ifndef _log2
#define _log2 6.931471805599453e-1
#endif
#ifndef logsqrtpi
#define logsqrtpi 0.572364942924700081938738
#endif
#ifndef logpi
#define logpi 1.144729885849400174143427
#endif
template< class T >
void output( T * data, int n, const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
char str[40];
for ( int k=0; k<n; k++ )
{
sprintf(str, "%1.18e", data[k] );
out << str << delim1;
}
out.close();
}
template< class T >
void output( vector<T > data, const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
int n = data.size();
char str[40];
for ( int k=0; k<n; k++ )
{
// sprintf(str, "%1.18e", data[k][j] );
out << data[k] << delim2;
}
out.close();
}
template< class T, int n_lines, int n_cols >
void output( T data[n_lines][n_cols], const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
// not working yet, figure out...
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
for ( int k=0; k<n_lines; k++ )
{
for ( int j=0; j<n_cols-1; j++ )
out << data[k][j] << delim1;
out << data[k][n_cols-1] << delim2;
}
out.close();
}
template< class T >
void output( T **data, int n_lines, int n_cols, const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
char str[40];
for ( int k=0; k<n_lines; k++ )
{
for ( int j=0; j<n_cols-1; j++ )
{
sprintf(str, "%1.18e", data[k][j] );
out << str << delim1;
}
sprintf(str, "%1.18e", data[k][n_cols-1] );
out << data[k][n_cols-1] << delim2;
}
out.close();
}
template< class T1, class T2 >
void output( T1 * data1, T2 * data2, int n, const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
for ( int k=0; k<n; k++ )
out << data1[k] << delim1 << data2[k] << delim2;
out.close();
}
template< class T1, class T2, class T3 >
void output( T1 * data1, T2 * data2, T3 * data3, int n, const char * fname, const char * delim1="\t", const char * delim2="\n" )
{
ofstream out;
out.open(fname, fstream::out);
assert(out.good() && out.is_open());
for ( int k=0; k<n; k++ )
out << data1[k] << delim1 << data2[k] << delim1 << data3[k] << delim2;
out.close();
}
#define reg_discr regular_discretization
double * regular_discretization(double a, double b, int n)
{
double * p = ml_alloc<double > ( n );
for (int k=0; k<n; k++ )
p[k] = ((b-a)/n)*k + a;
return p;
}
double ml_mean ( double * data, int n )
{
double mean =0;
for ( int k=0; k<n; k++ )
mean += data[k];
return mean/n;
}
double std_dev ( double * data, int n )
{
double mu = 0;
double sig = 0;
for ( int k=0; k<n; k++ )
mu += data[k];
for ( int k=0; k<n; k++ )
sig += data[k]*data[k];
return sqrt((sig-mu*mu/n)/n);
}
# define mean_stdev mean_std_dev
void mean_std_dev ( double * data, int n, double & mu, double & sig )
{
mu = 0;
sig = 0;
for ( int k=0; k<n; k++ )
mu += data[k];
mu /= n;
for ( int k=0; k<n; k++ )
sig += data[k]*data[k];
sig = sqrt(sig/n-mu*mu);
}
template<class T1, class T2 >
void ml_copy( T1 * x, T2 * y, int n)
{
// x = y
for (int k=0; k<n; k++)
x[k] = y[k];
}
double ml_slope( double * x, double * y, int n, double & b, double & a )
{
double mx = ml_mean( x, n );
double my = ml_mean( y, n );
double Sxy = 0.0;
for ( int k=0; k<n; k++ )
Sxy += (x[k] - mx )*(y[k] - my );
double Sx = 0.0;
for ( int k=0; k<n; k++ )
Sx += (x[k] - mx )*(x[k] - mx );
double Sy = 0.0;
for ( int k=0; k<n; k++ )
Sy += (y[k] - my )*(y[k] - my );
b = Sxy/Sx;
a = my - b*mx;
}
#ifdef include_integration
#include <gsl/gsl_integration.h>
double integrate_R (double (*func)(double,void *), void * arg_ptr=0, double epsabs=1E-6, double epsrel=1E-10)
{
double result,real_abserr,imag_result,imag_abserr;
static gsl_integration_workspace * workspace = gsl_integration_workspace_alloc (1000000);
gsl_function F;
F.function = func;
F.params = arg_ptr;
gsl_integration_qagi (
&F,
epsabs,
epsrel,
200000,
workspace,
&result,
&real_abserr );
return result;
}
double integrate(double (*func)(double,void *), double a, double b, void * arg_ptr=0, double epsabs=1E-6, double epsrel=1E-10)
{
double result,real_abserr,imag_result,imag_abserr;
static gsl_integration_workspace * workspace = gsl_integration_workspace_alloc (1000000);
gsl_function F;
F.function = func;
F.params = arg_ptr;
gsl_integration_qag (
&F,
a,
b,
epsabs,
epsrel,
200000,
2,
workspace,
&result,
&real_abserr );
return result;
}
#endif