Ticket #6296: complex_mpft.hpp

File complex_mpft.hpp, 6.2 KB (added by anonymous, 11 years ago)
Line 
1// complex standard header
2#pragma once
3#ifndef _COMPLEX_MPFT_
4#define _COMPLEX_MPFT_
5#ifndef RC_INVOKED
6//#include <complex.hpp>
7#include <mpreal.h>
8#include <ymath.h>
9#include <ccomplex> /* dummy if not C99 library */
10#include <cmath>
11#include <sstream>
12
13 #pragma pack(push,_CRT_PACKING)
14 #pragma warning(push,3)
15
16 #pragma warning(disable: 4244)
17
18_C_STD_BEGIN
19 #ifndef _C_COMPLEX_MPFT_T
20 #define _C_COMPLEX_MPFT_T
21
22typedef struct _C_mpft_complex
23 { /* mpft complex */
24 mpfr::mpreal _Val[2];
25 } _C_mpft_complex;
26 #endif /* _C_COMPLEX_MPFT_T */
27_C_STD_END
28
29_STD_BEGIN
30typedef _CSTD _C_mpft_complex _Mcomplex_value;
31
32template<> class complex<mpfr::mpreal>;
33
34 // CLASS _Ctraits<mpfr::mpreal>
35template<> class _Ctraits<mpfr::mpreal>
36 { // complex traits for mpfr::mpreal
37public:
38 typedef mpfr::mpreal _Ty;
39
40 static _Ty _Cosh(_Ty _Left, _Ty _Right)
41 { // return cosh(_Left) * _Right
42 return (_CSTD mpfr::cosh(_Left) * _Right);
43 }
44
45 static short _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
46 { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
47 //_Left * 2 ^ _Exponent
48 return ((short)(mpfr::exp(*_Pleft) * mpfr::ldexp(_Right, _Exponent)).toDouble());
49 }
50
51 static _Ty _Infv(_Ty)
52 { // return infinity
53 return (_CSTD _Inf._Double);
54 }
55
56 static bool _Isinf(_Ty _Left)
57 { // test for infinity
58 return (_CSTD mpfr::isfinite(_Left));
59 }
60
61 static bool _Isnan(_Ty _Left)
62 { // test for NaN
63 return (_CSTD mpfr::isnan(_Left));
64 }
65
66 static _Ty _Nanv(_Ty)
67 { // return NaN
68 return (_CSTD _Nan._Double);
69 }
70
71 static _Ty _Sinh(_Ty _Left, _Ty _Right)
72 { // return sinh(_Left) * _Right
73 return (_CSTD mpfr::sinh(_Left) * _Right);
74 }
75
76 static _Ty atan2(_Ty _Yval, _Ty _Xval)
77 { // return atan(_Yval / _Xval)
78 return (_CSTD mpfr::atan2(_Yval, _Xval));
79 }
80
81 static _Ty cos(_Ty _Left)
82 { // return cos(_Left)
83 return (_CSTD mpfr::cos(_Left));
84 }
85
86 static _Ty exp(_Ty _Left)
87 { // return exp(_Left)
88 return (_CSTD mpfr::exp(_Left));
89 }
90
91 static _Ty ldexp(_Ty _Left, int _Exponent)
92 { // return _Left * 2 ^ _Exponent
93 return (_CSTD mpfr::ldexp(_Left, _Exponent));
94 }
95
96 static _Ty log(_Ty _Left)
97 { // return log(_Left)
98 return (_CSTD mpfr::log(_Left));
99 }
100
101 static _Ty pow(_Ty _Left, _Ty _Right)
102 { // return _Left ^ _Right
103 return (_CSTD mpfr::pow(_Left, _Right));
104 }
105
106 static _Ty sin(_Ty _Left)
107 { // return sin(_Left)
108 return (_CSTD mpfr::sin(_Left));
109 }
110
111 static _Ty sqrt(_Ty _Left)
112 { // return sqrt(_Left)
113 return (_CSTD mpfr::sqrt(_Left));
114 }
115
116 static _Ty tan(_Ty _Left)
117 { // return tan(_Left)
118 return (_CSTD mpfr::tan(_Left));
119 }
120 };
121
122
123 // CLASS complex<mpfr::mpreal>
124template<> class complex<mpfr::mpreal>
125 : public _Complex_base<mpfr::mpreal, _Mcomplex_value>
126 { // complex with mpfr::mpreal components
127public:
128 typedef mpfr::mpreal _Ty;
129 typedef complex<_Ty> _Myt;
130
131 complex(
132 const complex<double>&); // defined below
133
134 operator _Ty() const
135 {
136 return _Val[_RE];
137 }
138
139 complex(const double& _Realval)
140 : _Complex_base<mpfr::mpreal, _Mcomplex_value>(_Realval, 0)
141 { // construct from double component
142 }
143
144 //complex(const double& _Realval = 0, const double& _Imagval = 0)
145 // : _Complex_base<mpfr::mpreal, _Mcomplex_value>(_Realval, _Imagval)
146 // { // construct from mpfr::mpreal component
147 // }
148
149 //complex()
150 // : _Complex_base<mpfr::mpreal, _Mcomplex_value>(0, 0)
151 // { // construct from mpfr::mpreal components
152 // }
153
154 complex(const _Ty& _Realval = 0,
155 const _Ty& _Imagval = 0)
156 : _Complex_base<mpfr::mpreal, _Mcomplex_value>(_Realval, _Imagval)
157 { // construct from mpfr::mpreal components
158 }
159
160 complex(const _Mcomplex_value& _Right)
161 : _Complex_base<mpfr::mpreal, _Mcomplex_value>(_Right._Val[_RE],
162 _Right._Val[_IM])
163 { // construct from mpfr::mpreal complex value
164 }
165
166 complex<_Ty>& operator=(const _Ty& _Right)
167 { // assign real
168 _Val[_RE] = _Right;
169 _Val[_IM] = 0;
170 return (*this);
171 }
172
173 _Myt& operator+=(const _Ty& _Right)
174 { // add real
175 _Val[_RE] = _Val[_RE] + _Right;
176 return (*this);
177 }
178
179 _Myt& operator-=(const _Ty& _Right)
180 { // subtract real
181 _Val[_RE] = _Val[_RE] - _Right;
182 return (*this);
183 }
184
185 _Myt& operator*=(const _Ty& _Right)
186 { // multiply by real
187 _Val[_RE] = _Val[_RE] * _Right;
188 _Val[_IM] = _Val[_IM] * _Right;
189 return (*this);
190 }
191
192 _Myt& operator/=(const _Ty& _Right)
193 { // divide by real
194 _Val[_RE] = _Val[_RE] / _Right;
195 _Val[_IM] = _Val[_IM] / _Right;
196 return (*this);
197 }
198
199 _Myt& operator+=(const _Myt& _Right)
200 { // add other complex
201 this->_Add(_Right);
202 return (*this);
203 }
204
205 _Myt& operator-=(const _Myt& _Right)
206 { // subtract other complex
207 this->_Sub(_Right);
208 return (*this);
209 }
210
211 _Myt& operator*=(const _Myt& _Right)
212 { // multiply by other complex
213 this->_Mul(_Right);
214 return (*this);
215 }
216
217 _Myt& operator/=(const _Myt& _Right)
218 { // divide by other complex
219 this->_Div(_Right);
220 return (*this);
221 }
222
223 template<class _Other> inline
224 _Myt& operator=(const complex<_Other>& _Right)
225 { // assign another complex
226 _Val[_RE] = (_Ty)_Right._Val[_RE];
227 _Val[_IM] = (_Ty)_Right._Val[_IM];
228 return (*this);
229 }
230
231 template<class _Other> inline
232 _Myt& operator+=(const complex<_Other>& _Right)
233 { // add other complex
234 this->_Add(_Right);
235 return (*this);
236 }
237
238 template<class _Other> inline
239 _Myt& operator-=(const complex<_Other>& _Right)
240 { // subtract other complex
241 this->_Sub(_Right);
242 return (*this);
243 }
244
245 template<class _Other> inline
246 _Myt& operator*=(const complex<_Other>& _Right)
247 { // multiply by other complex
248 this->_Mul(_Right);
249 return (*this);
250 }
251
252 template<class _Other> inline
253 _Myt& operator/=(const complex<_Other>& _Right)
254 { // divide by other complex
255 this->_Div(_Right);
256 return (*this);
257 }
258 };
259
260
261 // CONSTRUCTORS FOR complex SPECIALIZATIONS
262inline complex<mpfr::mpreal>::complex(
263 const complex<double>& _Right)
264 : _Complex_base<mpfr::mpreal, _Mcomplex_value>(
265 (_Ty)_Right.real(), (_Ty)_Right.imag())
266 { // construct complex<mpfr::mpreal> from complex<double>
267 }
268
269_STD_END
270
271 #pragma warning(pop)
272 #pragma pack(pop)
273
274#endif /* RC_INVOKED */
275#endif /* _COMPLEX_MPFT_ */