[2430] | 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 | *************************************************************************/
|
---|
[2154] | 21 | namespace AP
|
---|
| 22 | {
|
---|
| 23 | /********************************************************************
|
---|
| 24 | Class defining a complex number with double precision.
|
---|
| 25 | ********************************************************************/
|
---|
| 26 | public struct Complex
|
---|
| 27 | {
|
---|
| 28 | public double x;
|
---|
| 29 | public double y;
|
---|
| 30 |
|
---|
| 31 | public Complex(double _x)
|
---|
| 32 | {
|
---|
| 33 | x = _x;
|
---|
| 34 | y = 0;
|
---|
| 35 | }
|
---|
| 36 | public Complex(double _x, double _y)
|
---|
| 37 | {
|
---|
| 38 | x = _x;
|
---|
| 39 | y = _y;
|
---|
| 40 | }
|
---|
| 41 | public static implicit operator Complex(double _x)
|
---|
| 42 | {
|
---|
| 43 | return new Complex(_x);
|
---|
| 44 | }
|
---|
| 45 | public static bool operator==(Complex lhs, Complex rhs)
|
---|
| 46 | {
|
---|
| 47 | return (lhs.x==rhs.x) & (lhs.y==rhs.y);
|
---|
| 48 | }
|
---|
| 49 | public static bool operator!=(Complex lhs, Complex rhs)
|
---|
| 50 | {
|
---|
| 51 | return (lhs.x!=rhs.x) | (lhs.y!=rhs.y);
|
---|
| 52 | }
|
---|
| 53 | public static Complex operator+(Complex lhs)
|
---|
| 54 | {
|
---|
| 55 | return lhs;
|
---|
| 56 | }
|
---|
| 57 | public static Complex operator-(Complex lhs)
|
---|
| 58 | {
|
---|
| 59 | return new Complex(-lhs.x,-lhs.y);
|
---|
| 60 | }
|
---|
| 61 | public static Complex operator+(Complex lhs, Complex rhs)
|
---|
| 62 | {
|
---|
| 63 | return new Complex(lhs.x+rhs.x,lhs.y+rhs.y);
|
---|
| 64 | }
|
---|
| 65 | public static Complex operator-(Complex lhs, Complex rhs)
|
---|
| 66 | {
|
---|
| 67 | return new Complex(lhs.x-rhs.x,lhs.y-rhs.y);
|
---|
| 68 | }
|
---|
| 69 | public static Complex operator*(Complex lhs, Complex rhs)
|
---|
| 70 | {
|
---|
| 71 | return new Complex(lhs.x*rhs.x-lhs.y*rhs.y, lhs.x*rhs.y+lhs.y*rhs.x);
|
---|
| 72 | }
|
---|
| 73 | public static Complex operator/(Complex lhs, Complex rhs)
|
---|
| 74 | {
|
---|
| 75 | Complex result;
|
---|
| 76 | double e;
|
---|
| 77 | double f;
|
---|
| 78 | if( System.Math.Abs(rhs.y)<System.Math.Abs(rhs.x) )
|
---|
| 79 | {
|
---|
| 80 | e = rhs.y/rhs.x;
|
---|
| 81 | f = rhs.x+rhs.y*e;
|
---|
| 82 | result.x = (lhs.x+lhs.y*e)/f;
|
---|
| 83 | result.y = (lhs.y-lhs.x*e)/f;
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
| 87 | e = rhs.x/rhs.y;
|
---|
| 88 | f = rhs.y+rhs.x*e;
|
---|
| 89 | result.x = (lhs.y+lhs.x*e)/f;
|
---|
| 90 | result.y = (-lhs.x+lhs.y*e)/f;
|
---|
| 91 | }
|
---|
| 92 | return result;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /********************************************************************
|
---|
| 97 | AP math namespace
|
---|
| 98 | ********************************************************************/
|
---|
| 99 | public struct rcommstate
|
---|
| 100 | {
|
---|
| 101 | public int stage;
|
---|
| 102 | public int[] ia;
|
---|
| 103 | public bool[] ba;
|
---|
| 104 | public double[] ra;
|
---|
| 105 | public AP.Complex[] ca;
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | /********************************************************************
|
---|
| 109 | AP math namespace
|
---|
| 110 | ********************************************************************/
|
---|
| 111 | public class Math
|
---|
| 112 | {
|
---|
[2430] | 113 | //public static System.Random RndObject = new System.Random(System.DateTime.Now.Millisecond);
|
---|
| 114 | public static System.Random RndObject = new System.Random(System.DateTime.Now.Millisecond + 1000*System.DateTime.Now.Second + 60*1000*System.DateTime.Now.Minute);
|
---|
[2154] | 115 |
|
---|
| 116 | public const double MachineEpsilon = 5E-16;
|
---|
| 117 | public const double MaxRealNumber = 1E300;
|
---|
| 118 | public const double MinRealNumber = 1E-300;
|
---|
| 119 |
|
---|
| 120 | public static double RandomReal()
|
---|
| 121 | {
|
---|
| 122 | double r = 0;
|
---|
| 123 | lock(RndObject){ r = RndObject.NextDouble(); }
|
---|
| 124 | return r;
|
---|
| 125 | }
|
---|
| 126 | public static int RandomInteger(int N)
|
---|
| 127 | {
|
---|
| 128 | int r = 0;
|
---|
| 129 | lock(RndObject){ r = RndObject.Next(N); }
|
---|
| 130 | return r;
|
---|
| 131 | }
|
---|
| 132 | public static double Sqr(double X)
|
---|
| 133 | {
|
---|
| 134 | return X*X;
|
---|
| 135 | }
|
---|
| 136 | public static double AbsComplex(Complex z)
|
---|
| 137 | {
|
---|
| 138 | double w;
|
---|
| 139 | double xabs;
|
---|
| 140 | double yabs;
|
---|
| 141 | double v;
|
---|
| 142 |
|
---|
| 143 | xabs = System.Math.Abs(z.x);
|
---|
| 144 | yabs = System.Math.Abs(z.y);
|
---|
| 145 | w = xabs>yabs ? xabs : yabs;
|
---|
| 146 | v = xabs<yabs ? xabs : yabs;
|
---|
| 147 | if( v==0 )
|
---|
| 148 | return w;
|
---|
| 149 | else
|
---|
| 150 | {
|
---|
| 151 | double t = v/w;
|
---|
| 152 | return w*System.Math.Sqrt(1+t*t);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | public static Complex Conj(Complex z)
|
---|
| 156 | {
|
---|
| 157 | return new Complex(z.x, -z.y);
|
---|
| 158 | }
|
---|
| 159 | public static Complex CSqr(Complex z)
|
---|
| 160 | {
|
---|
| 161 | return new Complex(z.x*z.x-z.y*z.y, 2*z.x*z.y);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | }
|
---|
| 165 | }
|
---|