1 | #include "svd_si.h" |
---|
2 | |
---|
3 | const double ap::machineepsilon = 5E-16; |
---|
4 | const double ap::maxrealnumber = 1E300; |
---|
5 | const double ap::minrealnumber = 1E-300; |
---|
6 | |
---|
7 | // |
---|
8 | // ap::complex operations |
---|
9 | // |
---|
10 | bool ap::operator==(const ap::complex& lhs, const ap::complex& rhs) |
---|
11 | { return lhs.x==rhs.x && lhs.y==rhs.y; } |
---|
12 | |
---|
13 | bool ap::operator!=(const ap::complex& lhs, const ap::complex& rhs) |
---|
14 | { return lhs.x!=rhs.x || lhs.y!=rhs.y; } |
---|
15 | |
---|
16 | const ap::complex ap::operator+(const ap::complex& lhs) |
---|
17 | { return lhs; } |
---|
18 | |
---|
19 | const ap::complex ap::operator-(const ap::complex& lhs) |
---|
20 | { return ap::complex(-lhs.x, -lhs.y); } |
---|
21 | |
---|
22 | const ap::complex ap::operator+(const ap::complex& lhs, const ap::complex& rhs) |
---|
23 | { ap::complex r = lhs; r += rhs; return r; } |
---|
24 | |
---|
25 | const ap::complex ap::operator+(const ap::complex& lhs, const double& rhs) |
---|
26 | { ap::complex r = lhs; r += rhs; return r; } |
---|
27 | |
---|
28 | const ap::complex ap::operator+(const double& lhs, const ap::complex& rhs) |
---|
29 | { ap::complex r = rhs; r += lhs; return r; } |
---|
30 | |
---|
31 | const ap::complex ap::operator-(const ap::complex& lhs, const ap::complex& rhs) |
---|
32 | { ap::complex r = lhs; r -= rhs; return r; } |
---|
33 | |
---|
34 | const ap::complex ap::operator-(const ap::complex& lhs, const double& rhs) |
---|
35 | { ap::complex r = lhs; r -= rhs; return r; } |
---|
36 | |
---|
37 | const ap::complex ap::operator-(const double& lhs, const ap::complex& rhs) |
---|
38 | { ap::complex r = lhs; r -= rhs; return r; } |
---|
39 | |
---|
40 | const ap::complex ap::operator*(const ap::complex& lhs, const ap::complex& rhs) |
---|
41 | { return ap::complex(lhs.x*rhs.x - lhs.y*rhs.y, lhs.x*rhs.y + lhs.y*rhs.x); } |
---|
42 | |
---|
43 | const ap::complex ap::operator*(const ap::complex& lhs, const double& rhs) |
---|
44 | { return ap::complex(lhs.x*rhs, lhs.y*rhs); } |
---|
45 | |
---|
46 | const ap::complex ap::operator*(const double& lhs, const ap::complex& rhs) |
---|
47 | { return ap::complex(lhs*rhs.x, lhs*rhs.y); } |
---|
48 | |
---|
49 | const ap::complex ap::operator/(const ap::complex& lhs, const ap::complex& rhs) |
---|
50 | { |
---|
51 | ap::complex result; |
---|
52 | double e; |
---|
53 | double f; |
---|
54 | if( fabs(rhs.y)<fabs(rhs.x) ) |
---|
55 | { |
---|
56 | e = rhs.y/rhs.x; |
---|
57 | f = rhs.x+rhs.y*e; |
---|
58 | result.x = (lhs.x+lhs.y*e)/f; |
---|
59 | result.y = (lhs.y-lhs.x*e)/f; |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | e = rhs.x/rhs.y; |
---|
64 | f = rhs.y+rhs.x*e; |
---|
65 | result.x = (lhs.y+lhs.x*e)/f; |
---|
66 | result.y = (-lhs.x+lhs.y*e)/f; |
---|
67 | } |
---|
68 | return result; |
---|
69 | } |
---|
70 | |
---|
71 | const ap::complex ap::operator/(const double& lhs, const ap::complex& rhs) |
---|
72 | { |
---|
73 | ap::complex result; |
---|
74 | double e; |
---|
75 | double f; |
---|
76 | if( fabs(rhs.y)<fabs(rhs.x) ) |
---|
77 | { |
---|
78 | e = rhs.y/rhs.x; |
---|
79 | f = rhs.x+rhs.y*e; |
---|
80 | result.x = lhs/f; |
---|
81 | result.y = -lhs*e/f; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | e = rhs.x/rhs.y; |
---|
86 | f = rhs.y+rhs.x*e; |
---|
87 | result.x = lhs*e/f; |
---|
88 | result.y = -lhs/f; |
---|
89 | } |
---|
90 | return result; |
---|
91 | } |
---|
92 | |
---|
93 | const ap::complex ap::operator/(const ap::complex& lhs, const double& rhs) |
---|
94 | { return ap::complex(lhs.x/rhs, lhs.y/rhs); } |
---|
95 | |
---|
96 | double ap::abscomplex(const ap::complex &z) |
---|
97 | { |
---|
98 | double w; |
---|
99 | double xabs; |
---|
100 | double yabs; |
---|
101 | double v; |
---|
102 | |
---|
103 | xabs = fabs(z.x); |
---|
104 | yabs = fabs(z.y); |
---|
105 | w = xabs>yabs ? xabs : yabs; |
---|
106 | v = xabs<yabs ? xabs : yabs; |
---|
107 | if( v==0 ) |
---|
108 | return w; |
---|
109 | else |
---|
110 | { |
---|
111 | double t = v/w; |
---|
112 | return w*sqrt(1+t*t); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | const ap::complex ap::conj(const ap::complex &z) |
---|
117 | { return ap::complex(z.x, -z.y); } |
---|
118 | |
---|
119 | const ap::complex ap::csqr(const ap::complex &z) |
---|
120 | { return ap::complex(z.x*z.x-z.y*z.y, 2*z.x*z.y); } |
---|
121 | |
---|
122 | // |
---|
123 | // standard functions |
---|
124 | // |
---|
125 | int ap::sign(double x) |
---|
126 | { |
---|
127 | if( x>0 ) return 1; |
---|
128 | if( x<0 ) return -1; |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | double ap::randomreal() |
---|
133 | { |
---|
134 | int i = rand(); |
---|
135 | while(i==RAND_MAX) |
---|
136 | i =rand(); |
---|
137 | return double(i)/double(RAND_MAX); |
---|
138 | } |
---|
139 | |
---|
140 | int ap::randominteger(int maxv) |
---|
141 | { return rand()%maxv; } |
---|
142 | |
---|
143 | int ap::round(double x) |
---|
144 | { return int(floor(x+0.5)); } |
---|
145 | |
---|
146 | int ap::trunc(double x) |
---|
147 | { return int(x>0 ? floor(x) : ceil(x)); } |
---|
148 | |
---|
149 | int ap::ifloor(double x) |
---|
150 | { return int(floor(x)); } |
---|
151 | |
---|
152 | int ap::iceil(double x) |
---|
153 | { return int(ceil(x)); } |
---|
154 | |
---|
155 | double ap::pi() |
---|
156 | { return 3.14159265358979323846; } |
---|
157 | |
---|
158 | double ap::sqr(double x) |
---|
159 | { return x*x; } |
---|
160 | |
---|
161 | int ap::maxint(int m1, int m2) |
---|
162 | { |
---|
163 | return m1>m2 ? m1 : m2; |
---|
164 | } |
---|
165 | |
---|
166 | int ap::minint(int m1, int m2) |
---|
167 | { |
---|
168 | return m1>m2 ? m2 : m1; |
---|
169 | } |
---|
170 | |
---|
171 | double ap::maxreal(double m1, double m2) |
---|
172 | { |
---|
173 | return m1>m2 ? m1 : m2; |
---|
174 | } |
---|
175 | |
---|
176 | double ap::minreal(double m1, double m2) |
---|
177 | { |
---|
178 | return m1>m2 ? m2 : m1; |
---|
179 | } |
---|