source: git/Singular/svd/libs/ap.cpp @ aad4ca4

spielwiese
Last change on this file since aad4ca4 was aad4ca4, checked in by Hans Schoenemann <hannes@…>, 6 years ago
format: Warn -> WarnS, trailing spaces
  • Property mode set to 100644
File size: 4.1 KB
Line 
1#include "svd_si.h"
2
3const double ap::machineepsilon = 5E-16;
4const double ap::maxrealnumber  = 1E300;
5const double ap::minrealnumber  = 1E-300;
6
7//
8// ap::complex operations
9//
10bool ap::operator==(const ap::complex& lhs, const ap::complex& rhs)
11{ return lhs.x==rhs.x && lhs.y==rhs.y; }
12
13bool ap::operator!=(const ap::complex& lhs, const ap::complex& rhs)
14{ return lhs.x!=rhs.x || lhs.y!=rhs.y; }
15
16const ap::complex ap::operator+(const ap::complex& lhs)
17{ return lhs; }
18
19const ap::complex ap::operator-(const ap::complex& lhs)
20{ return ap::complex(-lhs.x, -lhs.y); }
21
22const ap::complex ap::operator+(const ap::complex& lhs, const ap::complex& rhs)
23{ ap::complex r = lhs; r += rhs; return r; }
24
25const ap::complex ap::operator+(const ap::complex& lhs, const double& rhs)
26{ ap::complex r = lhs; r += rhs; return r; }
27
28const ap::complex ap::operator+(const double& lhs, const ap::complex& rhs)
29{ ap::complex r = rhs; r += lhs; return r; }
30
31const ap::complex ap::operator-(const ap::complex& lhs, const ap::complex& rhs)
32{ ap::complex r = lhs; r -= rhs; return r; }
33
34const ap::complex ap::operator-(const ap::complex& lhs, const double& rhs)
35{ ap::complex r = lhs; r -= rhs; return r; }
36
37const ap::complex ap::operator-(const double& lhs, const ap::complex& rhs)
38{ ap::complex r = lhs; r -= rhs; return r; }
39
40const 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
43const ap::complex ap::operator*(const ap::complex& lhs, const double& rhs)
44{ return ap::complex(lhs.x*rhs,  lhs.y*rhs); }
45
46const ap::complex ap::operator*(const double& lhs, const ap::complex& rhs)
47{ return ap::complex(lhs*rhs.x,  lhs*rhs.y); }
48
49const 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
71const 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
93const ap::complex ap::operator/(const ap::complex& lhs, const double& rhs)
94{ return ap::complex(lhs.x/rhs, lhs.y/rhs); }
95
96double 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
116const ap::complex ap::conj(const ap::complex &z)
117{ return ap::complex(z.x, -z.y); }
118
119const 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//
125int ap::sign(double x)
126{
127    if( x>0 ) return  1;
128    if( x<0 ) return -1;
129    return 0;
130}
131
132double ap::randomreal()
133{
134    int i = rand();
135    while(i==RAND_MAX)
136        i =rand();
137    return double(i)/double(RAND_MAX);
138}
139
140int ap::randominteger(int maxv)
141{  return rand()%maxv; }
142
143int ap::round(double x)
144{ return int(floor(x+0.5)); }
145
146int ap::trunc(double x)
147{ return int(x>0 ? floor(x) : ceil(x)); }
148
149int ap::ifloor(double x)
150{ return int(floor(x)); }
151
152int ap::iceil(double x)
153{ return int(ceil(x)); }
154
155double ap::pi()
156{ return 3.14159265358979323846; }
157
158double ap::sqr(double x)
159{ return x*x; }
160
161int ap::maxint(int m1, int m2)
162{
163    return m1>m2 ? m1 : m2;
164}
165
166int ap::minint(int m1, int m2)
167{
168    return m1>m2 ? m2 : m1;
169}
170
171double ap::maxreal(double m1, double m2)
172{
173    return m1>m2 ? m1 : m2;
174}
175
176double ap::minreal(double m1, double m2)
177{
178    return m1>m2 ? m2 : m1;
179}
Note: See TracBrowser for help on using the repository browser.