-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap.cs
579 lines (536 loc) · 20.6 KB
/
ap.cs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*************************************************************************
AP library
Copyright (c) 2003-2009 Sergey Bochkanov (ALGLIB project).
>>> LICENSE >>>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (www.fsf.org); either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available at
http://www.fsf.org/licensing/licenses
>>> END OF LICENSE >>>
*************************************************************************/
using System;
public partial class alglib
{
/********************************************************************
Callback definitions for optimizers/fitters/solvers.
Callbacks for unparameterized (general) functions:
* ndimensional_func calculates f(arg), stores result to func
* ndimensional_grad calculates func = f(arg),
grad[i] = df(arg)/d(arg[i])
* ndimensional_hess calculates func = f(arg),
grad[i] = df(arg)/d(arg[i]),
hess[i,j] = d2f(arg)/(d(arg[i])*d(arg[j]))
Callbacks for systems of functions:
* ndimensional_jac calculates f[i] = fi(arg)
jac[i,j] = df[i](arg)/d(arg[j])
Callbacks for parameterized functions, i.e. for functions which
depend on two vectors: P and Q. Gradient and Hessian are calculated
with respect to P only.
* ndimensional_pfunc calculates f(p,q),
stores result to func
* ndimensional_pgrad calculates func = f(p,q),
grad[i] = df(p,q)/d(p[i])
* ndimensional_phess calculates func = f(p,q),
grad[i] = df(p,q)/d(p[i]),
hess[i,j] = d2f(p,q)/(d(p[i])*d(p[j]))
Callbacks for progress reports:
* ndimensional_rep reports current position of optimization algo
Callbacks for ODE solvers:
* ndimensional_ode_rp calculates dy/dx for given y[] and x
Callbacks for integrators:
* integrator1_func calculates f(x) for given x
(additional parameters xminusa and bminusx
contain x-a and b-x)
********************************************************************/
public delegate void ndimensional_func (double[] arg, ref double func, object obj);
public delegate void ndimensional_grad (double[] arg, ref double func, double[] grad, object obj);
public delegate void ndimensional_hess (double[] arg, ref double func, double[] grad, double[,] hess, object obj);
public delegate void ndimensional_jac (double[] arg, double[] fi, double[,] jac, object obj);
public delegate void ndimensional_pfunc(double[] p, double[] q, ref double func, object obj);
public delegate void ndimensional_pgrad(double[] p, double[] q, ref double func, double[] grad, object obj);
public delegate void ndimensional_phess(double[] p, double[] q, ref double func, double[] grad, double[,] hess, object obj);
public delegate void ndimensional_rep(double[] arg, double func, object obj);
public delegate void ndimensional_ode_rp (double[] y, double x, double[] dy, object obj);
public delegate void integrator1_func (double x, double xminusa, double bminusx, ref double f, object obj);
/********************************************************************
Class defining a complex number with double precision.
********************************************************************/
public struct complex
{
public double x;
public double y;
public complex(double _x)
{
x = _x;
y = 0;
}
public complex(double _x, double _y)
{
x = _x;
y = _y;
}
public static implicit operator complex(double _x)
{
return new complex(_x);
}
public static bool operator==(complex lhs, complex rhs)
{
return ((double)lhs.x==(double)rhs.x) & ((double)lhs.y==(double)rhs.y);
}
public static bool operator!=(complex lhs, complex rhs)
{
return ((double)lhs.x!=(double)rhs.x) | ((double)lhs.y!=(double)rhs.y);
}
public static complex operator+(complex lhs)
{
return lhs;
}
public static complex operator-(complex lhs)
{
return new complex(-lhs.x,-lhs.y);
}
public static complex operator+(complex lhs, complex rhs)
{
return new complex(lhs.x+rhs.x,lhs.y+rhs.y);
}
public static complex operator-(complex lhs, complex rhs)
{
return new complex(lhs.x-rhs.x,lhs.y-rhs.y);
}
public static complex operator*(complex lhs, complex rhs)
{
return new complex(lhs.x*rhs.x-lhs.y*rhs.y, lhs.x*rhs.y+lhs.y*rhs.x);
}
public static complex operator/(complex lhs, complex rhs)
{
complex result;
double e;
double f;
if( System.Math.Abs(rhs.y)<System.Math.Abs(rhs.x) )
{
e = rhs.y/rhs.x;
f = rhs.x+rhs.y*e;
result.x = (lhs.x+lhs.y*e)/f;
result.y = (lhs.y-lhs.x*e)/f;
}
else
{
e = rhs.x/rhs.y;
f = rhs.y+rhs.x*e;
result.x = (lhs.y+lhs.x*e)/f;
result.y = (-lhs.x+lhs.y*e)/f;
}
return result;
}
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode();
}
public override bool Equals(object obj)
{
if( obj is byte)
return Equals(new complex((byte)obj));
if( obj is sbyte)
return Equals(new complex((sbyte)obj));
if( obj is short)
return Equals(new complex((short)obj));
if( obj is ushort)
return Equals(new complex((ushort)obj));
if( obj is int)
return Equals(new complex((int)obj));
if( obj is uint)
return Equals(new complex((uint)obj));
if( obj is long)
return Equals(new complex((long)obj));
if( obj is ulong)
return Equals(new complex((ulong)obj));
if( obj is float)
return Equals(new complex((float)obj));
if( obj is double)
return Equals(new complex((double)obj));
if( obj is decimal)
return Equals(new complex((double)(decimal)obj));
return base.Equals(obj);
}
}
/********************************************************************
Class defining an ALGLIB exception
********************************************************************/
public class alglibexception : System.Exception
{
public string msg;
public alglibexception(string s)
{
msg = s;
}
}
/********************************************************************
reverse communication structure
********************************************************************/
public class rcommstate
{
public rcommstate()
{
stage = -1;
ia = new int[0];
ba = new bool[0];
ra = new double[0];
ca = new alglib.complex[0];
}
public int stage;
public int[] ia;
public bool[] ba;
public double[] ra;
public alglib.complex[] ca;
};
/********************************************************************
internal functions
********************************************************************/
public class ap
{
public static int len<T>(T[] a)
{ return a.Length; }
public static int rows<T>(T[,] a)
{ return a.GetLength(0); }
public static int cols<T>(T[,] a)
{ return a.GetLength(1); }
public static void assert(bool cond, string s)
{
if( !cond )
throw new alglibexception(s);
}
public static void assert(bool cond)
{
assert(cond, "ALGLIB: assertion failed");
}
/****************************************************************
returns dps (digits-of-precision) value corresponding to threshold.
dps(0.9) = dps(0.5) = dps(0.1) = 0
dps(0.09) = dps(0.05) = dps(0.01) = 1
and so on
****************************************************************/
public static int threshold2dps(double threshold)
{
int result = 0;
double t;
for (result = 0, t = 1; t / 10 > threshold*(1+1E-10); result++, t /= 10) ;
return result;
}
/****************************************************************
prints formatted array
****************************************************************/
public static string format(bool[] a)
{
string[] result = new string[len(a)];
int i;
for(i=0; i<len(a); i++)
if( a[i] )
result[i] = "true";
else
result[i] = "false";
return "{"+String.Join(",",result)+"}";
}
/****************************************************************
prints formatted array
****************************************************************/
public static string format(int[] a)
{
string[] result = new string[len(a)];
int i;
for (i = 0; i < len(a); i++)
result[i] = a[i].ToString();
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted array
****************************************************************/
public static string format(double[] a, int dps)
{
string fmt = String.Format("{{0:F{0}}}", dps);
string[] result = new string[len(a)];
int i;
for (i = 0; i < len(a); i++)
{
result[i] = String.Format(fmt, a[i]);
result[i] = result[i].Replace(',', '.');
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted array
****************************************************************/
public static string format(complex[] a, int dps)
{
string fmtx = String.Format("{{0:F{0}}}", dps);
string fmty = String.Format("{{0:F{0}}}", dps);
string[] result = new string[len(a)];
int i;
for (i = 0; i < len(a); i++)
{
result[i] = String.Format(fmtx, a[i].x) + (a[i].y >= 0 ? "+" : "-") + String.Format(fmty, Math.Abs(a[i].y)) + "i";
result[i] = result[i].Replace(',', '.');
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted matrix
****************************************************************/
public static string format(bool[,] a)
{
int i, j, m, n;
n = cols(a);
m = rows(a);
bool[] line = new bool[n];
string[] result = new string[m];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
line[j] = a[i, j];
result[i] = format(line);
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted matrix
****************************************************************/
public static string format(int[,] a)
{
int i, j, m, n;
n = cols(a);
m = rows(a);
int[] line = new int[n];
string[] result = new string[m];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
line[j] = a[i, j];
result[i] = format(line);
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted matrix
****************************************************************/
public static string format(double[,] a, int dps)
{
int i, j, m, n;
n = cols(a);
m = rows(a);
double[] line = new double[n];
string[] result = new string[m];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
line[j] = a[i, j];
result[i] = format(line, dps);
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
prints formatted matrix
****************************************************************/
public static string format(complex[,] a, int dps)
{
int i, j, m, n;
n = cols(a);
m = rows(a);
complex[] line = new complex[n];
string[] result = new string[m];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
line[j] = a[i, j];
result[i] = format(line, dps);
}
return "{" + String.Join(",", result) + "}";
}
/****************************************************************
checks that matrix is symmetric.
max|A-A^T| is calculated; if it is within 1.0E-14 of max|A|,
matrix is considered symmetric
****************************************************************/
public static bool issymmetric(double[,] a)
{
int i, j, n;
double err, mx, v1, v2;
if( rows(a)!=cols(a) )
return false;
n = rows(a);
if( n==0 )
return true;
mx = 0;
err = 0;
for( i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
v1 = a[i,j];
v2 = a[j,i];
if( !math.isfinite(v1) )
return false;
if( !math.isfinite(v2) )
return false;
err = Math.Max(err, Math.Abs(v1-v2));
mx = Math.Max(mx, Math.Abs(v1));
mx = Math.Max(mx, Math.Abs(v2));
}
v1 = a[i,i];
if( !math.isfinite(v1) )
return false;
mx = Math.Max(mx, Math.Abs(v1));
}
if( mx==0 )
return true;
return err/mx<=1.0E-14;
}
/****************************************************************
checks that matrix is Hermitian.
max|A-A^H| is calculated; if it is within 1.0E-14 of max|A|,
matrix is considered Hermitian
****************************************************************/
public static bool ishermitian(complex[,] a)
{
int i, j, n;
double err, mx;
complex v1, v2, vt;
if( rows(a)!=cols(a) )
return false;
n = rows(a);
if( n==0 )
return true;
mx = 0;
err = 0;
for( i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
v1 = a[i,j];
v2 = a[j,i];
if( !math.isfinite(v1.x) )
return false;
if( !math.isfinite(v1.y) )
return false;
if( !math.isfinite(v2.x) )
return false;
if( !math.isfinite(v2.y) )
return false;
vt.x = v1.x-v2.x;
vt.y = v1.y+v2.y;
err = Math.Max(err, math.abscomplex(vt));
mx = Math.Max(mx, math.abscomplex(v1));
mx = Math.Max(mx, math.abscomplex(v2));
}
v1 = a[i,i];
if( !math.isfinite(v1.x) )
return false;
if( !math.isfinite(v1.y) )
return false;
err = Math.Max(err, Math.Abs(v1.y));
mx = Math.Max(mx, math.abscomplex(v1));
}
if( mx==0 )
return true;
return err/mx<=1.0E-14;
}
/****************************************************************
Forces symmetricity by copying upper half of A to the lower one
****************************************************************/
public static bool forcesymmetric(double[,] a)
{
int i, j, n;
if( rows(a)!=cols(a) )
return false;
n = rows(a);
if( n==0 )
return true;
for( i=0; i<n; i++)
for(j=i+1; j<n; j++)
a[i,j] = a[j,i];
return true;
}
/****************************************************************
Forces Hermiticity by copying upper half of A to the lower one
****************************************************************/
public static bool forcehermitian(complex[,] a)
{
int i, j, n;
complex v;
if( rows(a)!=cols(a) )
return false;
n = rows(a);
if( n==0 )
return true;
for( i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
v = a[j,i];
a[i,j].x = v.x;
a[i,j].y = -v.y;
}
return true;
}
};
/********************************************************************
math functions
********************************************************************/
public class math
{
//public static System.Random RndObject = new System.Random(System.DateTime.Now.Millisecond);
public static System.Random rndobject = new System.Random(System.DateTime.Now.Millisecond + 1000*System.DateTime.Now.Second + 60*1000*System.DateTime.Now.Minute);
public const double machineepsilon = 5E-16;
public const double maxrealnumber = 1E300;
public const double minrealnumber = 1E-300;
public static bool isfinite(double d)
{
return !System.Double.IsNaN(d) && !System.Double.IsInfinity(d);
}
public static double randomreal()
{
double r = 0;
lock(rndobject){ r = rndobject.NextDouble(); }
return r;
}
public static int randominteger(int N)
{
int r = 0;
lock(rndobject){ r = rndobject.Next(N); }
return r;
}
public static double sqr(double X)
{
return X*X;
}
public static double abscomplex(complex z)
{
double w;
double xabs;
double yabs;
double v;
xabs = System.Math.Abs(z.x);
yabs = System.Math.Abs(z.y);
w = xabs>yabs ? xabs : yabs;
v = xabs<yabs ? xabs : yabs;
if( v==0 )
return w;
else
{
double t = v/w;
return w*System.Math.Sqrt(1+t*t);
}
}
public static complex conj(complex z)
{
return new complex(z.x, -z.y);
}
public static complex csqr(complex z)
{
return new complex(z.x*z.x-z.y*z.y, 2*z.x*z.y);
}
}
}