Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/ALGLIB/ap.cs @ 4593

Last change on this file since 4593 was 2430, checked in by gkronber, 15 years ago

Moved ALGLIB code into a separate plugin. #783

File size: 5.2 KB
Line 
1/*************************************************************************
2AP library
3Copyright (c) 2003-2009 Sergey Bochkanov (ALGLIB project).
4
5>>> LICENSE >>>
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation (www.fsf.org); either version 2 of the
9License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16A copy of the GNU General Public License is available at
17http://www.fsf.org/licensing/licenses
18
19>>> END OF LICENSE >>>
20*************************************************************************/
21namespace 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    {
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);
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}
Note: See TracBrowser for help on using the repository browser.