Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
complex.cc
Go to the documentation of this file.
1// -*- C++ -*-
2
32
33
34// complex number `a' raised to an integer power `n'
35// a**n = r**n * (cos(n theta) + i sin (n theta))
37template <class _TypeT>
38complex<_TypeT>
39pow (const complex<_TypeT>& __a, int __n)
40{
41 if (__a == complex<_TypeT>())
42 return complex<_TypeT>(0 == __n);
43
44 if (_TypeT () == __a.imag ()) {
45 if (__a.real () < _TypeT ())
46 return pow (__a, complex<_TypeT>(__n));
47
48 return complex<_TypeT>(_RWSTD_C::pow (__a.real (), __n));
49 }
50
51 _TypeT __r = _RWSTD_C::pow (_TypeT (abs (__a)), __n);
52 _TypeT __th = __n * arg (__a);
53
54 return complex<_TypeT>(__r * _RWSTD_C::cos (__th),
55 __r * _RWSTD_C::sin (__th));
56}
57
58
59// complex number `a' raised to a real power `s'
60// a**s = exp(s * log(a))
62template <class _TypeT>
63complex<_TypeT>
64pow (const complex<_TypeT> &__a, const _TypeT &__s)
65{
66 if (__a == complex<_TypeT>())
67 return complex<_TypeT>(_TypeT () == __s);
68
69 if (_TypeT () == __a.imag ()) {
70 if (__a.real () < _TypeT ())
71 return pow (__a, complex<_TypeT>(__s));
72
73 return complex<_TypeT>(_RWSTD_C::pow (__a.real (), __s));
74 }
75 return exp (__s * log (__a));
76}
77
78
79// real number `s' raised to a complex power `a'
80// s**a = exp(a * log (s))
82template <class _TypeT>
83complex<_TypeT>
84pow (const _TypeT& __s, const complex<_TypeT>& __a)
85{
86 if (_TypeT() == __s)
87 return complex<_TypeT>(complex<_TypeT>() == __a);
88
89 if (__s < _TypeT ())
90 return pow (complex<_TypeT>(__s), __a);
91
92 if (_TypeT () == __a.imag ())
93 return complex<_TypeT>(_RWSTD_C::pow (__s, __a.real ()));
94
95 return complex<_TypeT>(exp(__a * _TypeT (_RWSTD_C::log (__s))));
96}
97
98
99// complex<T> number `a1' raised to `a' complex<T> power `a2'
100// a1**a2 = rho * (cos(phi) + i sin(phi))
101// rho = r1 ** u2 * exp (-v2 * theta1)
102// phi = v2 * log(r1) + u2 * theta1
104template <class _TypeT>
105complex<_TypeT>
106pow (const complex<_TypeT>& __x, const complex<_TypeT>& __y)
107{
108 if (complex<_TypeT>() == __x)
109 return complex<_TypeT>(complex<_TypeT>() == __y);
110
111 _TypeT __r1 = abs (__x);
112 _TypeT __u2 = real (__y);
113 _TypeT __v2 = imag (__y);
114 _TypeT __th1 = arg (__x);
115 _TypeT __rho = _RWSTD_C::pow (__r1, __u2) * _RWSTD_C::exp (-__v2 * __th1);
116 _TypeT __phi = __v2 * _RWSTD_C::log (__r1) + __u2 * __th1;
117
118 return complex<_TypeT>(__rho * _RWSTD_C::cos (__phi),
119 __rho * _RWSTD_C::sin (__phi));
120}
121
122
124template <class _TypeT, class _CharT, class _Traits>
125basic_istream<_CharT, _Traits >&
126operator>> (basic_istream<_CharT, _Traits>& __strm, complex<_TypeT> &__val)
127{
128 // read a complex number in one of the following forms:
129 // "r", "(r)", "(r, i)"
130
131 // lwg issue 156: extraction is performed as a series of simpler
132 // extractions. Therefore, the skipping of whitespace is specified
133 // to be the same for each of the simpler extractions.
134
135 _TypeT __re = _TypeT (),
136 __im = _TypeT ();
137
138 _CharT __ch;
139
140 if (!(__strm >> __ch))
141 return __strm;
142
143 if ('(' == __ch) {
144 if (__strm >> __re && __strm >> __ch) {
145 if (',' == __ch && __strm >> __im)
146 __strm >> __ch;
147
148 // avoid setting failbit if either badbit
149 // or failbit is already set in the stream
150 if (')' != __ch && __strm)
151 __strm.setstate (__strm.failbit);
152 }
153 }
154 else if (__strm.putback (__ch)) {
155 __strm >> __re;
156 }
157
158 if (__strm)
159 __val = complex<_TypeT>(__re, __im);
160
161 return __strm;
162}
163
164
166template <class _TypeT, class _CharT, class _Traits>
167basic_ostream<_CharT, _Traits>&
168operator<< (basic_ostream<_CharT, _Traits >& __strm,
169 const complex<_TypeT>& __val)
170{
171 basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __ss;
172 __ss.flags (__strm.flags ());
173 __ss.imbue (__strm.getloc ());
174 __ss.precision (__strm.precision ());
175 __ss << '(' << __val.real () << ',' << __val.imag () << ')';
176 return __strm << __ss.str ();
177}
178
179
180} // namespace std
T abs(T t)
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _EXPORT
Definition _defs.h:498
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
Definition json.h:5363
int arg
Definition locale.cpp:2806