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

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