C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
complex.cpp
1 /*
2 ** CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
3 **
4 ** Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
5 ** Universitaet Karlsruhe, Germany
6 ** (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
7 ** Universitaet Wuppertal, Germany
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /* CVS $Id: complex.cpp,v 1.29 2014/01/30 17:23:44 cxsc Exp $ */
25 /* New versions of product(...), quotient(...); Blomquist, 26.10.02; */
26 
27 #include "complex.hpp"
28 #include "dot.hpp"
29 #include "cdot.hpp"
30 #include "real.hpp"
31 #include "rmath.hpp"
32 #include "idot.hpp"
33 #include "interval.hpp"
34 
35 #include "cinterval.hpp"
36 
37 namespace cxsc {
38 
39 complex::complex(const cdotprecision & a) throw()
40 {
41  *this=rnd(a);
42 }
43 
45 {
46  return *this=rnd(a);
47 }
48 
49 bool operator== (const complex & a, const dotprecision & b) throw() { return !a.im && a.re==b; }
50 bool operator== (const dotprecision & a, const complex & b) throw() { return !b.im && a==b.re; }
51 bool operator!= (const complex & a, const dotprecision & b) throw() { return !!a.im || a.re!=b; }
52 bool operator!= (const dotprecision & a, const complex & b) throw() { return !!b.im || a!=b.re; }
53 
54 
55 
56 // ---- Ausgabefunkt. ---------------------------------------
57 
58 std::ostream & operator << (std::ostream &s, const complex& a) throw()
59 {
60  s << '('
61  << a.re << ','
62  << a.im
63  << ')';
64  return s;
65 }
66 std::string & operator << (std::string &s, const complex &a) throw()
67 {
68  s+='(';
69  s << a.re;
70  s+=',';
71  s << a.im;
72  s+=')';
73  return s;
74 }
75 
76 std::istream & operator >> (std::istream &s, complex &a) throw()
77 {
78  char c;
79 
80  skipeolnflag = inpdotflag = true;
81  c = skipwhitespacessinglechar (s, '(');
82  if (inpdotflag)
83  s.putback(c);
84 
85  s >> a.re;
86 
87  skipeolnflag = inpdotflag = true;
88  c = skipwhitespacessinglechar (s, ',');
89  if (inpdotflag) s.putback(c);
90 
91  s >> a.im >> RestoreOpt;
92 
93  if (!waseolnflag)
94  {
95  skipeolnflag = false, inpdotflag = true;
96  c = skipwhitespaces (s);
97  if (inpdotflag && c != ')')
98  s.putback(c);
99  }
100 
101  /*if (a.re > a.im) {
102  errmon (ERR_INTERVAL(EMPTY));
103  } */
104  return s;
105 }
106 
107 std::string & operator >> (std::string &s, complex &a) throw()
108 {
109  s = skipwhitespacessinglechar (s, '(');
110  s >> SaveOpt >> RndDown >> a.re;
111  s = skipwhitespacessinglechar (s, ',');
112  s >> RndUp >> a.im >> RestoreOpt;
113  s = skipwhitespaces (s);
114 
115  if (s[0] == ')')
116  s.erase(0,1);
117 
118  /*if (a.re > a.im) {
119  errmon (ERR_INTERVAL(EMPTY));
120  } */
121  return s;
122 }
123 
124 void operator >>(const std::string &s,complex &a) throw()
125 {
126  std::string r(s);
127  r>>a;
128 }
129 void operator >>(const char *s,complex &a) throw()
130 {
131  std::string r(s);
132  r>>a;
133 }
134 
135 complex exp(const complex& x) throw() { return mid(exp(cinterval(x))); }
136 complex expm1(const complex& x) throw() { return mid(expm1(cinterval(x))); }
137 complex exp2(const complex& x) throw() { return mid(exp2(cinterval(x))); }
138 complex exp10(const complex& x) throw() { return mid(exp10(cinterval(x))); }
139 complex cos(const complex& x) throw() { return mid(cos(cinterval(x))); }
140 complex sin(const complex& x) throw() { return mid(sin(cinterval(x))); }
141 complex cosh(const complex& x) throw() { return mid(cosh(cinterval(x))); }
142 complex sinh(const complex& x) throw() { return mid(sinh(cinterval(x))); }
143 
144 complex tan(const complex& x) throw() { return mid(tan(cinterval(x))); }
145 complex cot(const complex& x) throw() { return mid(cot(cinterval(x))); }
146 complex tanh(const complex& x) throw() { return mid(tanh(cinterval(x))); }
147 complex coth(const complex& x) throw() { return mid(coth(cinterval(x))); }
148 
149 real arg(const complex& x) throw() { return mid(arg(cinterval(x))); }
150 real Arg(const complex& x) throw() { return mid(Arg(cinterval(x))); }
151 
152 complex ln(const complex& x) throw() { return mid(ln(cinterval(x))); }
153 complex lnp1(const complex& x) throw() { return mid(lnp1(cinterval(x))); }
154 complex log2(const complex& x) throw() { return mid(log2(cinterval(x))); }
155 complex log10(const complex& x) throw() { return mid(log10(cinterval(x))); }
156 
157 complex sqr(const complex& x) throw() { return mid(sqr(cinterval(x))); }
158 
159 complex sqrt(const complex& x) throw() { return mid(sqrt(cinterval(x))); }
160 complex sqrtp1m1(const complex& x) throw() { return mid(sqrtp1m1(cinterval(x))); }
161 complex sqrt1px2(const complex& x) throw() { return mid(sqrt1px2(cinterval(x))); }
162 complex sqrtx2m1(const complex& x) throw() { return mid(sqrtx2m1(cinterval(x))); }
163 complex sqrt1mx2(const complex& x) throw() { return mid(sqrt1mx2(cinterval(x))); }
164 complex sqrt(const complex& x, int d) throw()
165 { return mid(sqrt(cinterval(x),d)); }
166 
167 std::list<complex> sqrt_all( const complex& c )
168 {
169  complex z;
170  z = sqrt(c);
171 
172  std::list<complex> res;
173  res.push_back( z );
174  res.push_back( -z );
175 
176  return res;
177 } // end sqrt_all
178 
179 std::list<complex> sqrt_all( const complex& z, int n )
180  //
181 // sqrt_all(z,n) computes a list of n values approximating all n-th roots of z
182  //
183 // For n >=3, computing the optimal approximations is very expensive
184 // and thus not considered cost-effective.
185  //
186 // Hence, the polar form is used to calculate sqrt_all(z,n)
187  //
188 {
189  std::list<complex> res;
190 
191  if( n == 0 )
192  {
193  res.push_back( complex(1,0) );
194  return res;
195  }
196  else if( n == 1 )
197  {
198  res.push_back(z);
199  return res;
200  }
201  else if( n == 2 ) return sqrt_all( z );
202  else
203  {
204  real
205  arg_z = arg( z ), root_abs_z = sqrt( abs( z ), n );
206 
207  for(int k = 0; k < n; k++)
208  {
209  real arg_k = ( arg_z + 2 * k * mid(Pi_interval) ) / n;
210 
211  res.push_back( complex( root_abs_z * cos( arg_k ),
212  root_abs_z * sin( arg_k ) ) );
213  }
214  return res;
215  }
216 }
217 //-- end sqrt_all -------------------------------------------------------------
218 
219 
220 complex power_fast(const complex& x,int d) throw()
221 { return mid(power_fast(cinterval(x),d)); }
222 complex power(const complex& x,int d) throw()
223 { return mid(power(cinterval(x),d)); }
224 complex pow(const complex& x, const real& r) throw()
225 { return mid(pow(cinterval(x),interval(r))); }
226 complex pow(const complex& x, const complex& y) throw()
227 { return mid(pow(cinterval(x),cinterval(y))); }
228 
229 complex asin(const complex& x) throw() { return mid(asin(cinterval(x))); }
230 complex acos(const complex& x) throw() { return mid(acos(cinterval(x))); }
231 complex asinh(const complex& x) throw() { return mid(asinh(cinterval(x))); }
232 complex acosh(const complex& x) throw() { return mid(acosh(cinterval(x))); }
233 complex atan(const complex& x) throw() { return mid(atan(cinterval(x))); }
234 complex acot(const complex& x) throw() { return mid(acot(cinterval(x))); }
235 complex atanh(const complex& x) throw() { return mid(atanh(cinterval(x))); }
236 complex acoth(const complex& x) throw() { return mid(acoth(cinterval(x))); }
237 
238 } // namespace cxsc
239 
cinterval sqrtx2m1(const cinterval &z)
Calculates .
Definition: cimath.cpp:1109
complex & operator=(const real &r)
Implementation of standard assigning operator.
Definition: complex.inl:31
cinterval sqrt1px2(const cinterval &z)
Calculates .
Definition: cimath.cpp:1071
The Data Type dotprecision.
Definition: dot.hpp:111
cinterval log2(const cinterval &z)
Calculates .
Definition: cimath.cpp:898
cinterval ln(const cinterval &z)
Calculates .
Definition: cimath.cpp:851
cinterval sin(const cinterval &z)
Calculates .
Definition: cimath.cpp:215
cinterval cot(const cinterval &z)
Calculates .
Definition: cimath.cpp:538
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29
cinterval acot(const cinterval &z)
Calculates .
Definition: cimath.cpp:3130
cvector mid(const cimatrix_subv &mv)
Returns the middle of the matrix.
Definition: cimatrix.inl:739
The Scalar Type interval.
Definition: interval.hpp:54
cinterval atan(const cinterval &z)
Calculates .
Definition: cimath.cpp:2938
cinterval acos(const cinterval &z)
Calculates .
Definition: cimath.cpp:2553
cinterval sqr(const cinterval &z)
Calculates .
Definition: cimath.cpp:3342
cinterval expm1(const cinterval &z)
Calculates .
Definition: cimath.cpp:177
cinterval tanh(const cinterval &z)
Calculates .
Definition: cimath.cpp:565
cinterval asinh(const cinterval &z)
Calculates .
Definition: cimath.cpp:2718
cinterval log10(const cinterval &z)
Calculates .
Definition: cimath.cpp:903
cinterval sqrt1mx2(const cinterval &z)
Calculates .
Definition: cimath.cpp:1140
The Data Type cdotprecision.
Definition: cdot.hpp:60
cinterval lnp1(const cinterval &z)
Calculates .
Definition: cimath.cpp:867
The Scalar Type cinterval.
Definition: cinterval.hpp:54
std::list< cinterval > sqrt_all(const cinterval &z)
Calculates and returns all possible solutions.
Definition: cimath.cpp:1176
cinterval sqrt(const cinterval &z)
Calculates .
Definition: cimath.cpp:1007
The Scalar Type complex.
Definition: complex.hpp:49
cinterval cos(const cinterval &z)
Calculates .
Definition: cimath.cpp:207
cinterval atanh(const cinterval &z)
Calculates .
Definition: cimath.cpp:3317
cinterval power_fast(const cinterval &z, int n)
Calculates .
Definition: cimath.cpp:1520
cinterval exp10(const cinterval &z)
Calculates .
Definition: cimath.cpp:172
cinterval sqrtp1m1(const cinterval &z)
Calculates .
Definition: cimath.cpp:1054
const interval Pi_interval
Enclosure-Interval for .
Definition: interval.cpp:348
cinterval exp(const cinterval &z)
Calculates .
Definition: cimath.cpp:159
cinterval cosh(const cinterval &z)
Calculates .
Definition: cimath.cpp:223
cinterval tan(const cinterval &z)
Calculates .
Definition: cimath.cpp:393
complex(void)
Constructor of class complex.
Definition: complex.hpp:59
cinterval sinh(const cinterval &z)
Calculates .
Definition: cimath.cpp:231
cinterval exp2(const cinterval &z)
Calculates .
Definition: cimath.cpp:167
interval Arg(const cinterval &z)
Calculates .
Definition: cimath.cpp:654
cinterval asin(const cinterval &z)
Calculates .
Definition: cimath.cpp:2311
cinterval acoth(const cinterval &z)
Calculates .
Definition: cimath.cpp:3330
cinterval coth(const cinterval &z)
Calculates .
Definition: cimath.cpp:578
cinterval pow(const cinterval &z, const interval &p)
Calculates .
Definition: cimath.cpp:2074
interval arg(const cinterval &z)
Calculates .
Definition: cimath.cpp:741
The Scalar Type real.
Definition: real.hpp:113
cinterval power(const cinterval &z, int n)
Calculates .
Definition: cimath.cpp:1941
ivector abs(const cimatrix_subv &mv)
Returns the absolute value of the matrix.
Definition: cimatrix.inl:737
cinterval acosh(const cinterval &z)
Calculates .
Definition: cimath.cpp:2732