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