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
37namespace cxsc {
38
40{
41 *this=rnd(a);
42}
43
45{
46 return *this=rnd(a);
47}
48
49bool operator== (const complex & a, const dotprecision & b) noexcept { return !a.im && a.re==b; }
50bool operator== (const dotprecision & a, const complex & b) noexcept { return !b.im && a==b.re; }
51bool operator!= (const complex & a, const dotprecision & b) noexcept { return !!a.im || a.re!=b; }
52bool operator!= (const dotprecision & a, const complex & b) noexcept { return !!b.im || a!=b.re; }
53
54
55
56// ---- Ausgabefunkt. ---------------------------------------
57
58std::ostream & operator << (std::ostream &s, const complex& a) noexcept
59{
60 s << '('
61 << a.re << ','
62 << a.im
63 << ')';
64 return s;
65}
66std::string & operator << (std::string &s, const complex &a) noexcept
67{
68 s+='(';
69 s << a.re;
70 s+=',';
71 s << a.im;
72 s+=')';
73 return s;
74}
75
76std::istream & operator >> (std::istream &s, complex &a) noexcept
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
107std::string & operator >> (std::string &s, complex &a) noexcept
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
124void operator >>(const std::string &s,complex &a) noexcept
125{
126 std::string r(s);
127 r>>a;
128}
129void operator >>(const char *s,complex &a) noexcept
130{
131 std::string r(s);
132 r>>a;
133}
134
135complex exp(const complex& x) noexcept { return mid(exp(cinterval(x))); }
136complex expm1(const complex& x) noexcept { return mid(expm1(cinterval(x))); }
137complex exp2(const complex& x) noexcept { return mid(exp2(cinterval(x))); }
138complex exp10(const complex& x) noexcept { return mid(exp10(cinterval(x))); }
139complex cos(const complex& x) noexcept { return mid(cos(cinterval(x))); }
140complex sin(const complex& x) noexcept { return mid(sin(cinterval(x))); }
141complex cosh(const complex& x) noexcept { return mid(cosh(cinterval(x))); }
142complex sinh(const complex& x) noexcept { return mid(sinh(cinterval(x))); }
143
144complex tan(const complex& x) noexcept { return mid(tan(cinterval(x))); }
145complex cot(const complex& x) noexcept { return mid(cot(cinterval(x))); }
146complex tanh(const complex& x) noexcept { return mid(tanh(cinterval(x))); }
147complex coth(const complex& x) noexcept { return mid(coth(cinterval(x))); }
148
149real arg(const complex& x) noexcept { return mid(arg(cinterval(x))); }
150real Arg(const complex& x) noexcept { return mid(Arg(cinterval(x))); }
151
152complex ln(const complex& x) noexcept { return mid(ln(cinterval(x))); }
153complex lnp1(const complex& x) noexcept { return mid(lnp1(cinterval(x))); }
154complex log2(const complex& x) noexcept { return mid(log2(cinterval(x))); }
155complex log10(const complex& x) noexcept { return mid(log10(cinterval(x))); }
156
157complex sqr(const complex& x) noexcept { return mid(sqr(cinterval(x))); }
158
159complex sqrt(const complex& x) noexcept { return mid(sqrt(cinterval(x))); }
160complex sqrtp1m1(const complex& x) noexcept { return mid(sqrtp1m1(cinterval(x))); }
161complex sqrt1px2(const complex& x) noexcept { return mid(sqrt1px2(cinterval(x))); }
162complex sqrtx2m1(const complex& x) noexcept { return mid(sqrtx2m1(cinterval(x))); }
163complex sqrt1mx2(const complex& x) noexcept { return mid(sqrt1mx2(cinterval(x))); }
164complex sqrt(const complex& x, int d) noexcept
165{ return mid(sqrt(cinterval(x),d)); }
166
167std::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
179std::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
220complex power_fast(const complex& x,int d) noexcept
221{ return mid(power_fast(cinterval(x),d)); }
222complex power(const complex& x,int d) noexcept
223{ return mid(power(cinterval(x),d)); }
224complex pow(const complex& x, const real& r) noexcept
225{ return mid(pow(cinterval(x),interval(r))); }
226complex pow(const complex& x, const complex& y) noexcept
227{ return mid(pow(cinterval(x),cinterval(y))); }
228
229complex asin(const complex& x) noexcept { return mid(asin(cinterval(x))); }
230complex acos(const complex& x) noexcept { return mid(acos(cinterval(x))); }
231complex asinh(const complex& x) noexcept { return mid(asinh(cinterval(x))); }
232complex acosh(const complex& x) noexcept { return mid(acosh(cinterval(x))); }
233complex atan(const complex& x) noexcept { return mid(atan(cinterval(x))); }
234complex acot(const complex& x) noexcept { return mid(acot(cinterval(x))); }
235complex atanh(const complex& x) noexcept { return mid(atanh(cinterval(x))); }
236complex acoth(const complex& x) noexcept { return mid(acoth(cinterval(x))); }
237
238} // namespace cxsc
239
The Data Type cdotprecision.
Definition cdot.hpp:61
The Scalar Type cinterval.
Definition cinterval.hpp:55
The Scalar Type complex.
Definition complex.hpp:50
complex & operator=(const real &r) noexcept
Implementation of standard assigning operator.
Definition complex.inl:31
complex(void) noexcept
Constructor of class complex.
Definition complex.hpp:59
The Data Type dotprecision.
Definition dot.hpp:112
The Scalar Type interval.
Definition interval.hpp:55
The Scalar Type real.
Definition real.hpp:114
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition cdot.cpp:29
cinterval sqrtp1m1(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:1054
cinterval exp2(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:167
cinterval sqrt1mx2(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:1140
cinterval asinh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:2718
cinterval coth(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:578
cinterval log2(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:898
cinterval power(const cinterval &z, int n) noexcept
Calculates .
Definition cimath.cpp:1941
cinterval log10(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:903
cinterval ln(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:851
cinterval pow(const cinterval &z, const interval &p) noexcept
Calculates .
Definition cimath.cpp:2074
cinterval sinh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:231
cinterval asin(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:2311
cinterval tan(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:393
cinterval exp10(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:172
interval arg(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:741
std::list< cinterval > sqrt_all(const cinterval &z)
Calculates and returns all possible solutions.
Definition cimath.cpp:1176
cinterval acos(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:2553
cinterval sqrtx2m1(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:1109
cinterval acosh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:2732
cinterval cosh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:223
cinterval cos(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:207
cinterval sqrt1px2(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:1071
cinterval exp(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:159
cinterval tanh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:565
cinterval expm1(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:177
cinterval cot(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:538
ivector abs(const cimatrix_subv &mv) noexcept
Returns the absolute value of the matrix.
Definition cimatrix.inl:737
cinterval sqrt(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:1007
cinterval power_fast(const cinterval &z, int n) noexcept
Calculates .
Definition cimath.cpp:1520
cinterval acot(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:3130
cinterval sqr(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:3342
cinterval lnp1(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:867
cvector mid(const cimatrix_subv &mv) noexcept
Returns the middle of the matrix.
Definition cimatrix.inl:739
cinterval atan(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:2938
cinterval atanh(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:3317
interval Arg(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:654
cinterval acoth(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:3330
cinterval sin(const cinterval &z) noexcept
Calculates .
Definition cimath.cpp:215
const interval Pi_interval
Enclosure-Interval for .
Definition interval.cpp:348