1 | /*************************************************************************
|
---|
2 | AP library
|
---|
3 | Copyright (c) 2003-2009 Sergey Bochkanov (ALGLIB project).
|
---|
4 |
|
---|
5 | >>> LICENSE >>>
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
9 | License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | A copy of the GNU General Public License is available at
|
---|
17 | http://www.fsf.org/licensing/licenses
|
---|
18 |
|
---|
19 | >>> END OF LICENSE >>>
|
---|
20 | *************************************************************************/
|
---|
21 | using System;
|
---|
22 | public partial class alglib
|
---|
23 | {
|
---|
24 | /********************************************************************
|
---|
25 | Callback definitions for optimizers/fitters/solvers.
|
---|
26 |
|
---|
27 | Callbacks for unparameterized (general) functions:
|
---|
28 | * ndimensional_func calculates f(arg), stores result to func
|
---|
29 | * ndimensional_grad calculates func = f(arg),
|
---|
30 | grad[i] = df(arg)/d(arg[i])
|
---|
31 | * ndimensional_hess calculates func = f(arg),
|
---|
32 | grad[i] = df(arg)/d(arg[i]),
|
---|
33 | hess[i,j] = d2f(arg)/(d(arg[i])*d(arg[j]))
|
---|
34 |
|
---|
35 | Callbacks for systems of functions:
|
---|
36 | * ndimensional_fvec calculates vector function f(arg),
|
---|
37 | stores result to fi
|
---|
38 | * ndimensional_jac calculates f[i] = fi(arg)
|
---|
39 | jac[i,j] = df[i](arg)/d(arg[j])
|
---|
40 |
|
---|
41 | Callbacks for parameterized functions, i.e. for functions which
|
---|
42 | depend on two vectors: P and Q. Gradient and Hessian are calculated
|
---|
43 | with respect to P only.
|
---|
44 | * ndimensional_pfunc calculates f(p,q),
|
---|
45 | stores result to func
|
---|
46 | * ndimensional_pgrad calculates func = f(p,q),
|
---|
47 | grad[i] = df(p,q)/d(p[i])
|
---|
48 | * ndimensional_phess calculates func = f(p,q),
|
---|
49 | grad[i] = df(p,q)/d(p[i]),
|
---|
50 | hess[i,j] = d2f(p,q)/(d(p[i])*d(p[j]))
|
---|
51 |
|
---|
52 | Callbacks for progress reports:
|
---|
53 | * ndimensional_rep reports current position of optimization algo
|
---|
54 |
|
---|
55 | Callbacks for ODE solvers:
|
---|
56 | * ndimensional_ode_rp calculates dy/dx for given y[] and x
|
---|
57 |
|
---|
58 | Callbacks for integrators:
|
---|
59 | * integrator1_func calculates f(x) for given x
|
---|
60 | (additional parameters xminusa and bminusx
|
---|
61 | contain x-a and b-x)
|
---|
62 | ********************************************************************/
|
---|
63 | public delegate void ndimensional_func (double[] arg, ref double func, object obj);
|
---|
64 | public delegate void ndimensional_grad (double[] arg, ref double func, double[] grad, object obj);
|
---|
65 | public delegate void ndimensional_hess (double[] arg, ref double func, double[] grad, double[,] hess, object obj);
|
---|
66 |
|
---|
67 | public delegate void ndimensional_fvec (double[] arg, double[] fi, object obj);
|
---|
68 | public delegate void ndimensional_jac (double[] arg, double[] fi, double[,] jac, object obj);
|
---|
69 |
|
---|
70 | public delegate void ndimensional_pfunc(double[] p, double[] q, ref double func, object obj);
|
---|
71 | public delegate void ndimensional_pgrad(double[] p, double[] q, ref double func, double[] grad, object obj);
|
---|
72 | public delegate void ndimensional_phess(double[] p, double[] q, ref double func, double[] grad, double[,] hess, object obj);
|
---|
73 |
|
---|
74 | public delegate void ndimensional_rep(double[] arg, double func, object obj);
|
---|
75 |
|
---|
76 | public delegate void ndimensional_ode_rp (double[] y, double x, double[] dy, object obj);
|
---|
77 |
|
---|
78 | public delegate void integrator1_func (double x, double xminusa, double bminusx, ref double f, object obj);
|
---|
79 |
|
---|
80 | /********************************************************************
|
---|
81 | Class defining a complex number with double precision.
|
---|
82 | ********************************************************************/
|
---|
83 | public struct complex
|
---|
84 | {
|
---|
85 | public double x;
|
---|
86 | public double y;
|
---|
87 |
|
---|
88 | public complex(double _x)
|
---|
89 | {
|
---|
90 | x = _x;
|
---|
91 | y = 0;
|
---|
92 | }
|
---|
93 | public complex(double _x, double _y)
|
---|
94 | {
|
---|
95 | x = _x;
|
---|
96 | y = _y;
|
---|
97 | }
|
---|
98 | public static implicit operator complex(double _x)
|
---|
99 | {
|
---|
100 | return new complex(_x);
|
---|
101 | }
|
---|
102 | public static bool operator==(complex lhs, complex rhs)
|
---|
103 | {
|
---|
104 | return ((double)lhs.x==(double)rhs.x) & ((double)lhs.y==(double)rhs.y);
|
---|
105 | }
|
---|
106 | public static bool operator!=(complex lhs, complex rhs)
|
---|
107 | {
|
---|
108 | return ((double)lhs.x!=(double)rhs.x) | ((double)lhs.y!=(double)rhs.y);
|
---|
109 | }
|
---|
110 | public static complex operator+(complex lhs)
|
---|
111 | {
|
---|
112 | return lhs;
|
---|
113 | }
|
---|
114 | public static complex operator-(complex lhs)
|
---|
115 | {
|
---|
116 | return new complex(-lhs.x,-lhs.y);
|
---|
117 | }
|
---|
118 | public static complex operator+(complex lhs, complex rhs)
|
---|
119 | {
|
---|
120 | return new complex(lhs.x+rhs.x,lhs.y+rhs.y);
|
---|
121 | }
|
---|
122 | public static complex operator-(complex lhs, complex rhs)
|
---|
123 | {
|
---|
124 | return new complex(lhs.x-rhs.x,lhs.y-rhs.y);
|
---|
125 | }
|
---|
126 | public static complex operator*(complex lhs, complex rhs)
|
---|
127 | {
|
---|
128 | return new complex(lhs.x*rhs.x-lhs.y*rhs.y, lhs.x*rhs.y+lhs.y*rhs.x);
|
---|
129 | }
|
---|
130 | public static complex operator/(complex lhs, complex rhs)
|
---|
131 | {
|
---|
132 | complex result;
|
---|
133 | double e;
|
---|
134 | double f;
|
---|
135 | if( System.Math.Abs(rhs.y)<System.Math.Abs(rhs.x) )
|
---|
136 | {
|
---|
137 | e = rhs.y/rhs.x;
|
---|
138 | f = rhs.x+rhs.y*e;
|
---|
139 | result.x = (lhs.x+lhs.y*e)/f;
|
---|
140 | result.y = (lhs.y-lhs.x*e)/f;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | e = rhs.x/rhs.y;
|
---|
145 | f = rhs.y+rhs.x*e;
|
---|
146 | result.x = (lhs.y+lhs.x*e)/f;
|
---|
147 | result.y = (-lhs.x+lhs.y*e)/f;
|
---|
148 | }
|
---|
149 | return result;
|
---|
150 | }
|
---|
151 | public override int GetHashCode()
|
---|
152 | {
|
---|
153 | return x.GetHashCode() ^ y.GetHashCode();
|
---|
154 | }
|
---|
155 | public override bool Equals(object obj)
|
---|
156 | {
|
---|
157 | if( obj is byte)
|
---|
158 | return Equals(new complex((byte)obj));
|
---|
159 | if( obj is sbyte)
|
---|
160 | return Equals(new complex((sbyte)obj));
|
---|
161 | if( obj is short)
|
---|
162 | return Equals(new complex((short)obj));
|
---|
163 | if( obj is ushort)
|
---|
164 | return Equals(new complex((ushort)obj));
|
---|
165 | if( obj is int)
|
---|
166 | return Equals(new complex((int)obj));
|
---|
167 | if( obj is uint)
|
---|
168 | return Equals(new complex((uint)obj));
|
---|
169 | if( obj is long)
|
---|
170 | return Equals(new complex((long)obj));
|
---|
171 | if( obj is ulong)
|
---|
172 | return Equals(new complex((ulong)obj));
|
---|
173 | if( obj is float)
|
---|
174 | return Equals(new complex((float)obj));
|
---|
175 | if( obj is double)
|
---|
176 | return Equals(new complex((double)obj));
|
---|
177 | if( obj is decimal)
|
---|
178 | return Equals(new complex((double)(decimal)obj));
|
---|
179 | return base.Equals(obj);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /********************************************************************
|
---|
184 | Class defining an ALGLIB exception
|
---|
185 | ********************************************************************/
|
---|
186 | public class alglibexception : System.Exception
|
---|
187 | {
|
---|
188 | public string msg;
|
---|
189 | public alglibexception(string s)
|
---|
190 | {
|
---|
191 | msg = s;
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|
195 |
|
---|
196 | /********************************************************************
|
---|
197 | reverse communication structure
|
---|
198 | ********************************************************************/
|
---|
199 | public class rcommstate
|
---|
200 | {
|
---|
201 | public rcommstate()
|
---|
202 | {
|
---|
203 | stage = -1;
|
---|
204 | ia = new int[0];
|
---|
205 | ba = new bool[0];
|
---|
206 | ra = new double[0];
|
---|
207 | ca = new alglib.complex[0];
|
---|
208 | }
|
---|
209 | public int stage;
|
---|
210 | public int[] ia;
|
---|
211 | public bool[] ba;
|
---|
212 | public double[] ra;
|
---|
213 | public alglib.complex[] ca;
|
---|
214 | };
|
---|
215 |
|
---|
216 | /********************************************************************
|
---|
217 | internal functions
|
---|
218 | ********************************************************************/
|
---|
219 | public class ap
|
---|
220 | {
|
---|
221 | public static int len<T>(T[] a)
|
---|
222 | { return a.Length; }
|
---|
223 | public static int rows<T>(T[,] a)
|
---|
224 | { return a.GetLength(0); }
|
---|
225 | public static int cols<T>(T[,] a)
|
---|
226 | { return a.GetLength(1); }
|
---|
227 | public static void swap<T>(ref T a, ref T b)
|
---|
228 | {
|
---|
229 | T t = a;
|
---|
230 | a = b;
|
---|
231 | b = t;
|
---|
232 | }
|
---|
233 |
|
---|
234 | public static void assert(bool cond, string s)
|
---|
235 | {
|
---|
236 | if( !cond )
|
---|
237 | throw new alglibexception(s);
|
---|
238 | }
|
---|
239 |
|
---|
240 | public static void assert(bool cond)
|
---|
241 | {
|
---|
242 | assert(cond, "ALGLIB: assertion failed");
|
---|
243 | }
|
---|
244 |
|
---|
245 | /****************************************************************
|
---|
246 | returns dps (digits-of-precision) value corresponding to threshold.
|
---|
247 | dps(0.9) = dps(0.5) = dps(0.1) = 0
|
---|
248 | dps(0.09) = dps(0.05) = dps(0.01) = 1
|
---|
249 | and so on
|
---|
250 | ****************************************************************/
|
---|
251 | public static int threshold2dps(double threshold)
|
---|
252 | {
|
---|
253 | int result = 0;
|
---|
254 | double t;
|
---|
255 | for (result = 0, t = 1; t / 10 > threshold*(1+1E-10); result++, t /= 10) ;
|
---|
256 | return result;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /****************************************************************
|
---|
260 | prints formatted array
|
---|
261 | ****************************************************************/
|
---|
262 | public static string format(bool[] a)
|
---|
263 | {
|
---|
264 | string[] result = new string[len(a)];
|
---|
265 | int i;
|
---|
266 | for(i=0; i<len(a); i++)
|
---|
267 | if( a[i] )
|
---|
268 | result[i] = "true";
|
---|
269 | else
|
---|
270 | result[i] = "false";
|
---|
271 | return "{"+String.Join(",",result)+"}";
|
---|
272 | }
|
---|
273 |
|
---|
274 | /****************************************************************
|
---|
275 | prints formatted array
|
---|
276 | ****************************************************************/
|
---|
277 | public static string format(int[] a)
|
---|
278 | {
|
---|
279 | string[] result = new string[len(a)];
|
---|
280 | int i;
|
---|
281 | for (i = 0; i < len(a); i++)
|
---|
282 | result[i] = a[i].ToString();
|
---|
283 | return "{" + String.Join(",", result) + "}";
|
---|
284 | }
|
---|
285 |
|
---|
286 | /****************************************************************
|
---|
287 | prints formatted array
|
---|
288 | ****************************************************************/
|
---|
289 | public static string format(double[] a, int dps)
|
---|
290 | {
|
---|
291 | string fmt = String.Format("{{0:F{0}}}", dps);
|
---|
292 | string[] result = new string[len(a)];
|
---|
293 | int i;
|
---|
294 | for (i = 0; i < len(a); i++)
|
---|
295 | {
|
---|
296 | result[i] = String.Format(fmt, a[i]);
|
---|
297 | result[i] = result[i].Replace(',', '.');
|
---|
298 | }
|
---|
299 | return "{" + String.Join(",", result) + "}";
|
---|
300 | }
|
---|
301 |
|
---|
302 | /****************************************************************
|
---|
303 | prints formatted array
|
---|
304 | ****************************************************************/
|
---|
305 | public static string format(complex[] a, int dps)
|
---|
306 | {
|
---|
307 | string fmtx = String.Format("{{0:F{0}}}", dps);
|
---|
308 | string fmty = String.Format("{{0:F{0}}}", dps);
|
---|
309 | string[] result = new string[len(a)];
|
---|
310 | int i;
|
---|
311 | for (i = 0; i < len(a); i++)
|
---|
312 | {
|
---|
313 | result[i] = String.Format(fmtx, a[i].x) + (a[i].y >= 0 ? "+" : "-") + String.Format(fmty, Math.Abs(a[i].y)) + "i";
|
---|
314 | result[i] = result[i].Replace(',', '.');
|
---|
315 | }
|
---|
316 | return "{" + String.Join(",", result) + "}";
|
---|
317 | }
|
---|
318 |
|
---|
319 | /****************************************************************
|
---|
320 | prints formatted matrix
|
---|
321 | ****************************************************************/
|
---|
322 | public static string format(bool[,] a)
|
---|
323 | {
|
---|
324 | int i, j, m, n;
|
---|
325 | n = cols(a);
|
---|
326 | m = rows(a);
|
---|
327 | bool[] line = new bool[n];
|
---|
328 | string[] result = new string[m];
|
---|
329 | for (i = 0; i < m; i++)
|
---|
330 | {
|
---|
331 | for (j = 0; j < n; j++)
|
---|
332 | line[j] = a[i, j];
|
---|
333 | result[i] = format(line);
|
---|
334 | }
|
---|
335 | return "{" + String.Join(",", result) + "}";
|
---|
336 | }
|
---|
337 |
|
---|
338 | /****************************************************************
|
---|
339 | prints formatted matrix
|
---|
340 | ****************************************************************/
|
---|
341 | public static string format(int[,] a)
|
---|
342 | {
|
---|
343 | int i, j, m, n;
|
---|
344 | n = cols(a);
|
---|
345 | m = rows(a);
|
---|
346 | int[] line = new int[n];
|
---|
347 | string[] result = new string[m];
|
---|
348 | for (i = 0; i < m; i++)
|
---|
349 | {
|
---|
350 | for (j = 0; j < n; j++)
|
---|
351 | line[j] = a[i, j];
|
---|
352 | result[i] = format(line);
|
---|
353 | }
|
---|
354 | return "{" + String.Join(",", result) + "}";
|
---|
355 | }
|
---|
356 |
|
---|
357 | /****************************************************************
|
---|
358 | prints formatted matrix
|
---|
359 | ****************************************************************/
|
---|
360 | public static string format(double[,] a, int dps)
|
---|
361 | {
|
---|
362 | int i, j, m, n;
|
---|
363 | n = cols(a);
|
---|
364 | m = rows(a);
|
---|
365 | double[] line = new double[n];
|
---|
366 | string[] result = new string[m];
|
---|
367 | for (i = 0; i < m; i++)
|
---|
368 | {
|
---|
369 | for (j = 0; j < n; j++)
|
---|
370 | line[j] = a[i, j];
|
---|
371 | result[i] = format(line, dps);
|
---|
372 | }
|
---|
373 | return "{" + String.Join(",", result) + "}";
|
---|
374 | }
|
---|
375 |
|
---|
376 | /****************************************************************
|
---|
377 | prints formatted matrix
|
---|
378 | ****************************************************************/
|
---|
379 | public static string format(complex[,] a, int dps)
|
---|
380 | {
|
---|
381 | int i, j, m, n;
|
---|
382 | n = cols(a);
|
---|
383 | m = rows(a);
|
---|
384 | complex[] line = new complex[n];
|
---|
385 | string[] result = new string[m];
|
---|
386 | for (i = 0; i < m; i++)
|
---|
387 | {
|
---|
388 | for (j = 0; j < n; j++)
|
---|
389 | line[j] = a[i, j];
|
---|
390 | result[i] = format(line, dps);
|
---|
391 | }
|
---|
392 | return "{" + String.Join(",", result) + "}";
|
---|
393 | }
|
---|
394 |
|
---|
395 | /****************************************************************
|
---|
396 | checks that matrix is symmetric.
|
---|
397 | max|A-A^T| is calculated; if it is within 1.0E-14 of max|A|,
|
---|
398 | matrix is considered symmetric
|
---|
399 | ****************************************************************/
|
---|
400 | public static bool issymmetric(double[,] a)
|
---|
401 | {
|
---|
402 | int i, j, n;
|
---|
403 | double err, mx, v1, v2;
|
---|
404 | if( rows(a)!=cols(a) )
|
---|
405 | return false;
|
---|
406 | n = rows(a);
|
---|
407 | if( n==0 )
|
---|
408 | return true;
|
---|
409 | mx = 0;
|
---|
410 | err = 0;
|
---|
411 | for( i=0; i<n; i++)
|
---|
412 | {
|
---|
413 | for(j=i+1; j<n; j++)
|
---|
414 | {
|
---|
415 | v1 = a[i,j];
|
---|
416 | v2 = a[j,i];
|
---|
417 | if( !math.isfinite(v1) )
|
---|
418 | return false;
|
---|
419 | if( !math.isfinite(v2) )
|
---|
420 | return false;
|
---|
421 | err = Math.Max(err, Math.Abs(v1-v2));
|
---|
422 | mx = Math.Max(mx, Math.Abs(v1));
|
---|
423 | mx = Math.Max(mx, Math.Abs(v2));
|
---|
424 | }
|
---|
425 | v1 = a[i,i];
|
---|
426 | if( !math.isfinite(v1) )
|
---|
427 | return false;
|
---|
428 | mx = Math.Max(mx, Math.Abs(v1));
|
---|
429 | }
|
---|
430 | if( mx==0 )
|
---|
431 | return true;
|
---|
432 | return err/mx<=1.0E-14;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /****************************************************************
|
---|
436 | checks that matrix is Hermitian.
|
---|
437 | max|A-A^H| is calculated; if it is within 1.0E-14 of max|A|,
|
---|
438 | matrix is considered Hermitian
|
---|
439 | ****************************************************************/
|
---|
440 | public static bool ishermitian(complex[,] a)
|
---|
441 | {
|
---|
442 | int i, j, n;
|
---|
443 | double err, mx;
|
---|
444 | complex v1, v2, vt;
|
---|
445 | if( rows(a)!=cols(a) )
|
---|
446 | return false;
|
---|
447 | n = rows(a);
|
---|
448 | if( n==0 )
|
---|
449 | return true;
|
---|
450 | mx = 0;
|
---|
451 | err = 0;
|
---|
452 | for( i=0; i<n; i++)
|
---|
453 | {
|
---|
454 | for(j=i+1; j<n; j++)
|
---|
455 | {
|
---|
456 | v1 = a[i,j];
|
---|
457 | v2 = a[j,i];
|
---|
458 | if( !math.isfinite(v1.x) )
|
---|
459 | return false;
|
---|
460 | if( !math.isfinite(v1.y) )
|
---|
461 | return false;
|
---|
462 | if( !math.isfinite(v2.x) )
|
---|
463 | return false;
|
---|
464 | if( !math.isfinite(v2.y) )
|
---|
465 | return false;
|
---|
466 | vt.x = v1.x-v2.x;
|
---|
467 | vt.y = v1.y+v2.y;
|
---|
468 | err = Math.Max(err, math.abscomplex(vt));
|
---|
469 | mx = Math.Max(mx, math.abscomplex(v1));
|
---|
470 | mx = Math.Max(mx, math.abscomplex(v2));
|
---|
471 | }
|
---|
472 | v1 = a[i,i];
|
---|
473 | if( !math.isfinite(v1.x) )
|
---|
474 | return false;
|
---|
475 | if( !math.isfinite(v1.y) )
|
---|
476 | return false;
|
---|
477 | err = Math.Max(err, Math.Abs(v1.y));
|
---|
478 | mx = Math.Max(mx, math.abscomplex(v1));
|
---|
479 | }
|
---|
480 | if( mx==0 )
|
---|
481 | return true;
|
---|
482 | return err/mx<=1.0E-14;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | /****************************************************************
|
---|
487 | Forces symmetricity by copying upper half of A to the lower one
|
---|
488 | ****************************************************************/
|
---|
489 | public static bool forcesymmetric(double[,] a)
|
---|
490 | {
|
---|
491 | int i, j, n;
|
---|
492 | if( rows(a)!=cols(a) )
|
---|
493 | return false;
|
---|
494 | n = rows(a);
|
---|
495 | if( n==0 )
|
---|
496 | return true;
|
---|
497 | for( i=0; i<n; i++)
|
---|
498 | for(j=i+1; j<n; j++)
|
---|
499 | a[i,j] = a[j,i];
|
---|
500 | return true;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /****************************************************************
|
---|
504 | Forces Hermiticity by copying upper half of A to the lower one
|
---|
505 | ****************************************************************/
|
---|
506 | public static bool forcehermitian(complex[,] a)
|
---|
507 | {
|
---|
508 | int i, j, n;
|
---|
509 | complex v;
|
---|
510 | if( rows(a)!=cols(a) )
|
---|
511 | return false;
|
---|
512 | n = rows(a);
|
---|
513 | if( n==0 )
|
---|
514 | return true;
|
---|
515 | for( i=0; i<n; i++)
|
---|
516 | for(j=i+1; j<n; j++)
|
---|
517 | {
|
---|
518 | v = a[j,i];
|
---|
519 | a[i,j].x = v.x;
|
---|
520 | a[i,j].y = -v.y;
|
---|
521 | }
|
---|
522 | return true;
|
---|
523 | }
|
---|
524 | };
|
---|
525 |
|
---|
526 | /********************************************************************
|
---|
527 | math functions
|
---|
528 | ********************************************************************/
|
---|
529 | public class math
|
---|
530 | {
|
---|
531 | //public static System.Random RndObject = new System.Random(System.DateTime.Now.Millisecond);
|
---|
532 | public static System.Random rndobject = new System.Random(System.DateTime.Now.Millisecond + 1000*System.DateTime.Now.Second + 60*1000*System.DateTime.Now.Minute);
|
---|
533 |
|
---|
534 | public const double machineepsilon = 5E-16;
|
---|
535 | public const double maxrealnumber = 1E300;
|
---|
536 | public const double minrealnumber = 1E-300;
|
---|
537 |
|
---|
538 | public static bool isfinite(double d)
|
---|
539 | {
|
---|
540 | return !System.Double.IsNaN(d) && !System.Double.IsInfinity(d);
|
---|
541 | }
|
---|
542 |
|
---|
543 | public static double randomreal()
|
---|
544 | {
|
---|
545 | double r = 0;
|
---|
546 | lock(rndobject){ r = rndobject.NextDouble(); }
|
---|
547 | return r;
|
---|
548 | }
|
---|
549 | public static int randominteger(int N)
|
---|
550 | {
|
---|
551 | int r = 0;
|
---|
552 | lock(rndobject){ r = rndobject.Next(N); }
|
---|
553 | return r;
|
---|
554 | }
|
---|
555 | public static double sqr(double X)
|
---|
556 | {
|
---|
557 | return X*X;
|
---|
558 | }
|
---|
559 | public static double abscomplex(complex z)
|
---|
560 | {
|
---|
561 | double w;
|
---|
562 | double xabs;
|
---|
563 | double yabs;
|
---|
564 | double v;
|
---|
565 |
|
---|
566 | xabs = System.Math.Abs(z.x);
|
---|
567 | yabs = System.Math.Abs(z.y);
|
---|
568 | w = xabs>yabs ? xabs : yabs;
|
---|
569 | v = xabs<yabs ? xabs : yabs;
|
---|
570 | if( v==0 )
|
---|
571 | return w;
|
---|
572 | else
|
---|
573 | {
|
---|
574 | double t = v/w;
|
---|
575 | return w*System.Math.Sqrt(1+t*t);
|
---|
576 | }
|
---|
577 | }
|
---|
578 | public static complex conj(complex z)
|
---|
579 | {
|
---|
580 | return new complex(z.x, -z.y);
|
---|
581 | }
|
---|
582 | public static complex csqr(complex z)
|
---|
583 | {
|
---|
584 | return new complex(z.x*z.x-z.y*z.y, 2*z.x*z.y);
|
---|
585 | }
|
---|
586 |
|
---|
587 | }
|
---|
588 | }
|
---|