Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
10 KiB

  1. // complex standard header
  2. #ifndef _COMPLEX_
  3. #define _COMPLEX_
  4. #include <ymath.h>
  5. #include <cmath>
  6. #include <sstream>
  7. #include <xutility>
  8. #ifdef _MSC_VER
  9. #pragma pack(push,8)
  10. #endif /* _MSC_VER */
  11. _STD_BEGIN
  12. #define __STD_COMPLEX
  13. // TEMPLATE CLASS _Ctr
  14. template<class _Ty> class _Ctr {
  15. public:
  16. static _Ty _Cosh(_Ty _X, _Ty _Y)
  17. {return (::_Cosh((double)_X, (double)_Y)); }
  18. static short _Exp(_Ty *_P, _Ty _Y, short _E)
  19. {double _W = (double)*_P;
  20. short _Ans = ::_Exp(&_W, (double)_Y, _E);
  21. *_P = (_Ty)_W;
  22. return (_Ans); }
  23. static _Ty _Infv(_Ty)
  24. {return (_Inf._D); }
  25. static bool _Isinf(_Ty _X)
  26. {double _W = (double)_X;
  27. return (_Dtest(&_W) == _INFCODE); }
  28. static bool _Isnan(_Ty _X)
  29. {double _W = (double)_X;
  30. return (_Dtest(&_W) == _NANCODE); }
  31. static _Ty _Nanv(_Ty)
  32. {return (_Nan._D); }
  33. static _Ty _Sinh(_Ty _X, _Ty _Y)
  34. {return (::_Sinh((double)_X, (double)_Y)); }
  35. static _Ty atan2(_Ty _Y, _Ty _X)
  36. {return (::atan2((double)_Y, (double)_X)); }
  37. static _Ty cos(_Ty _X)
  38. {return (::cos((double)_X)); }
  39. static _Ty exp(_Ty _X)
  40. {return (::exp((double)_X)); }
  41. static _Ty ldexp(_Ty _R, int _E)
  42. {return (::ldexp((double)_R, _E)); }
  43. static _Ty log(_Ty _X)
  44. {return (::log((double)_X)); }
  45. static _Ty pow(_Ty _X, _Ty _Y)
  46. {return (::pow((double)_X, (double)_Y)); }
  47. static _Ty sin(_Ty _X)
  48. {return (::sin((double)_X)); }
  49. static _Ty sqrt(_Ty _X)
  50. {return (::sqrt((double)_X)); }
  51. };
  52. // CLASS _Ctr<long double>
  53. class _CRTIMP2 _Ctr<long double> {
  54. public:
  55. typedef long double _Ty;
  56. static _Ty _Cosh(_Ty _X, _Ty _Y)
  57. {return (_LCosh(_X, _Y)); }
  58. static short _Exp(_Ty *_P, _Ty _Y, short _E)
  59. {return (_LExp(_P, _Y, _E)); }
  60. static _Ty _Infv(_Ty)
  61. {return (_LInf._L); }
  62. static bool _Isinf(_Ty _X)
  63. {return (_LDtest(&_X) == _INFCODE); }
  64. static bool _Isnan(_Ty _X)
  65. {return (_LDtest(&_X) == _NANCODE); }
  66. static _Ty _Nanv(_Ty)
  67. {return (_LNan._L); }
  68. static _Ty _Sinh(_Ty _X, _Ty _Y)
  69. {return (_LSinh(_X, _Y)); }
  70. static _Ty atan2(_Ty _Y, _Ty _X)
  71. {return (atan2l(_Y, _X)); }
  72. static _Ty cos(_Ty _X)
  73. {return (cosl(_X)); }
  74. static _Ty exp(_Ty _X)
  75. {return (expl(_X)); }
  76. static _Ty ldexp(_Ty _R, int _E)
  77. {return (ldexpl(_R, _E)); }
  78. static _Ty log(_Ty _X)
  79. {return (logl(_X)); }
  80. static _Ty pow(_Ty _X, _Ty _Y)
  81. {return (powl(_X, _Y)); }
  82. static _Ty sin(_Ty _X)
  83. {return (sinl(_X)); }
  84. static _Ty sqrt(_Ty _X)
  85. {return (sqrtl(_X)); }
  86. };
  87. // CLASS _Ctr<double>
  88. class _CRTIMP2 _Ctr<double> {
  89. public:
  90. typedef double _Ty;
  91. static _Ty _Cosh(_Ty _X, _Ty _Y)
  92. {return (::_Cosh(_X, _Y)); }
  93. static short _Exp(_Ty *_P, _Ty _Y, short _E)
  94. {return (::_Exp(_P, _Y, _E)); }
  95. static _Ty _Infv(_Ty)
  96. {return (_Inf._D); }
  97. static bool _Isinf(_Ty _X)
  98. {return (_Dtest(&_X) == _INFCODE); }
  99. static bool _Isnan(_Ty _X)
  100. {return (_Dtest(&_X) == _NANCODE); }
  101. static _Ty _Nanv(_Ty)
  102. {return (_Nan._D); }
  103. static _Ty _Sinh(_Ty _X, _Ty _Y)
  104. {return (::_Sinh(_X, _Y)); }
  105. static _Ty atan2(_Ty _Y, _Ty _X)
  106. {return (::atan2(_Y, _X)); }
  107. static _Ty cos(_Ty _X)
  108. {return (::cos(_X)); }
  109. static _Ty exp(_Ty _X)
  110. {return (::exp(_X)); }
  111. static _Ty ldexp(_Ty _R, int _E)
  112. {return (::ldexp(_R, _E)); }
  113. static _Ty log(_Ty _X)
  114. {return (::log(_X)); }
  115. static _Ty pow(_Ty _X, _Ty _Y)
  116. {return (::pow(_X, _Y)); }
  117. static _Ty sin(_Ty _X)
  118. {return (::sin(_X)); }
  119. static _Ty sqrt(_Ty _X)
  120. {return (::sqrt(_X)); }
  121. };
  122. class _CRTIMP2 _Ctr<float> {
  123. public:
  124. typedef float _Ty;
  125. static _Ty _Cosh(_Ty _X, _Ty _Y)
  126. {return (_FCosh(_X, _Y)); }
  127. static short _Exp(_Ty *_P, _Ty _Y, short _E)
  128. {return (_FExp(_P, _Y, _E)); }
  129. static _Ty _Infv(_Ty)
  130. {return (_FInf._F); }
  131. static bool _Isinf(_Ty _X)
  132. {return (_FDtest(&_X) == _INFCODE); }
  133. static bool _Isnan(_Ty _X)
  134. {return (_FDtest(&_X) == _NANCODE); }
  135. static _Ty _Nanv(_Ty)
  136. {return (_FNan._F); }
  137. static _Ty _Sinh(_Ty _X, _Ty _Y)
  138. {return (_FSinh(_X, _Y)); }
  139. static _Ty atan2(_Ty _Y, _Ty _X)
  140. {return (atan2f(_Y, _X)); }
  141. static _Ty cos(_Ty _X)
  142. {return (cosf(_X)); }
  143. static _Ty exp(_Ty _X)
  144. {return (expf(_X)); }
  145. static _Ty ldexp(_Ty _R, int _E)
  146. {return (ldexpf(_R, _E)); }
  147. static _Ty log(_Ty _X)
  148. {return (logf(_X)); }
  149. static _Ty pow(_Ty _X, _Ty _Y)
  150. {return (powf(_X, _Y)); }
  151. static _Ty sin(_Ty _X)
  152. {return (sinf(_X)); }
  153. static _Ty sqrt(_Ty _X)
  154. {return (sqrtf(_X)); }
  155. };
  156. // TEMPLATE CLASS _Complex_base
  157. template<class _Ty> class complex;
  158. class _CRTIMP2 complex<float>;
  159. class _CRTIMP2 complex<double>;
  160. class _CRTIMP2 complex<long double>;
  161. template<class _Ty>
  162. class _Complex_base {
  163. public:
  164. typedef _Complex_base<_Ty> _Myt;
  165. typedef _Ctr<_Ty> _Myctr;
  166. typedef _Ty value_type;
  167. _Complex_base(const _Ty& _R, const _Ty& _I)
  168. : _Re(_R), _Im(_I) {}
  169. _Myt& operator+=(const _Ty& _X)
  170. {_Re = _Re + _X;
  171. return (*this); }
  172. _Myt& operator-=(const _Ty& _X)
  173. {_Re = _Re - _X;
  174. return (*this); }
  175. _Myt& operator*=(const _Ty& _X)
  176. {_Re = _Re * _X;
  177. _Im = _Im * _X;
  178. return (*this); }
  179. _Myt& operator/=(const _Ty& _X)
  180. {_Re = _Re / _X;
  181. _Im = _Im / _X;
  182. return (*this); }
  183. _Ty real(const _Ty& _X)
  184. {return (_Re = _X); }
  185. _Ty imag(const _Ty& _X)
  186. {return (_Im = _X); }
  187. _Ty real() const
  188. {return (_Re); }
  189. _Ty imag() const
  190. {return (_Im); }
  191. protected:
  192. _Ty _Re, _Im;
  193. };
  194. #ifdef __FORCE_INSTANCE
  195. template class _CRTIMP2 _Complex_base<float>;
  196. template class _CRTIMP2 _Complex_base<double>;
  197. template class _CRTIMP2 _Complex_base<long double>;
  198. #endif // __FORCE_INSTANCE
  199. // CLASS complex<float>
  200. class _CRTIMP2 complex<float> : public _Complex_base<float> {
  201. public:
  202. typedef float _Ty;
  203. explicit complex(const complex<double>&);
  204. explicit complex(const complex<long double>&);
  205. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  206. : _Complex_base<_Ty>(_R, _I) {}
  207. complex<_Ty>& operator=(const _Ty& _X)
  208. {_Re = _X;
  209. _Im = 0;
  210. return (*this); }
  211. };
  212. // CLASS complex<double>
  213. class _CRTIMP2 complex<double> : public _Complex_base<double> {
  214. public:
  215. typedef double _Ty;
  216. complex(const complex<float>&);
  217. explicit complex(const complex<long double>&);
  218. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  219. : _Complex_base<_Ty>(_R, _I) {}
  220. complex<_Ty>& operator=(const _Ty& _X)
  221. {_Re = _X;
  222. _Im = 0;
  223. return (*this); }
  224. };
  225. // CLASS complex<long double>
  226. class _CRTIMP2 complex<long double> : public _Complex_base<long double> {
  227. public:
  228. typedef long double _Ty;
  229. complex(const complex<float>&);
  230. complex(const complex<double>&);
  231. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  232. : _Complex_base<_Ty>(_R, _I) {}
  233. complex<_Ty>& operator=(const _Ty& _X)
  234. {_Re = _X;
  235. _Im = 0;
  236. return (*this); }
  237. };
  238. // CONSTRUCTORS FOR complex SPECIALIZATIONS
  239. inline complex<float>::complex(const complex<double>& _X)
  240. : _Complex_base<float>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  241. inline complex<float>::complex(const complex<long double>& _X)
  242. : _Complex_base<float>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  243. inline complex<double>::complex(const complex<float>& _X)
  244. : _Complex_base<double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  245. inline complex<double>::complex(const complex<long double>& _X)
  246. : _Complex_base<double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  247. inline complex<long double>::complex(const complex<float>& _X)
  248. : _Complex_base<long double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  249. inline complex<long double>::complex(const complex<double>& _X)
  250. : _Complex_base<long double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  251. // TEMPLATE CLASS complex
  252. template<class _Ty>
  253. class complex : public _Complex_base<_Ty> {
  254. public:
  255. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  256. : _Complex_base<_Ty>(_R, _I) {}
  257. typedef _Ty _U;
  258. complex(const complex<_U>& _X)
  259. : _Complex_base<_Ty>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  260. complex<_Ty>& operator=(const complex<_U>& _X)
  261. {_Re = (_Ty)_X.real();
  262. _Im = (_Ty)_X.imag();
  263. return (*this); }
  264. };
  265. // TEMPLATE complex OPERATORS
  266. template<class _Ty, class _U> inline
  267. complex<_Ty>& __cdecl operator+=(
  268. complex<_Ty>& _X,
  269. const complex<_U>& _Y)
  270. {_X.real(_X.real() + (_Ty)_Y.real());
  271. _X.imag(_X.imag() + (_Ty)_Y.imag());
  272. return (_X); }
  273. template<class _Ty, class _U> inline
  274. complex<_Ty>& __cdecl operator-=(
  275. complex<_Ty>& _X,
  276. const complex<_U>& _Y)
  277. {_X.real(_X.real() - (_Ty)_Y.real());
  278. _X.imag(_X.imag() - (_Ty)_Y.imag());
  279. return (_X); }
  280. template<class _Ty, class _U> inline
  281. complex<_Ty>& __cdecl operator*=(
  282. complex<_Ty>& _X,
  283. const complex<_U>& _Y)
  284. {_Ty _Yre = (_Ty)_Y.real();
  285. _Ty _Yim = (_Ty)_Y.imag();
  286. _Ty _W = _X.real() * _Yre - _X.imag() * _Yim;
  287. _X.imag(_X.real() * _Yim + _X.imag() * _Yre);
  288. _X.real(_W);
  289. return (_X); }
  290. template<class _Ty, class _U> inline
  291. complex<_Ty>& __cdecl operator/=(
  292. complex<_Ty>& _X,
  293. const complex<_U>& _Y)
  294. {typedef _Ctr<_Ty> _Myctr;
  295. _Ty _Yre = (_Ty)_Y.real();
  296. _Ty _Yim = (_Ty)_Y.imag();
  297. if (_Myctr::_Isnan(_Yre) || _Myctr::_Isnan(_Yim))
  298. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  299. else if ((_Yim < 0 ? -_Yim : +_Yim)
  300. < (_Yre < 0 ? -_Yre : +_Yre))
  301. {_Ty _Wr = _Yim / _Yre;
  302. _Ty _Wd = _Yre + _Wr * _Yim;
  303. if (_Myctr::_Isnan(_Wd) || _Wd == 0)
  304. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  305. else
  306. {_Ty _W = (_X.real() + _X.imag() * _Wr) / _Wd;
  307. _X.imag((_X.imag() - _X.real() * _Wr) / _Wd);
  308. _X.real(_W); }}
  309. else if (_Yim == 0)
  310. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  311. else
  312. {_Ty _Wr = _Yre / _Yim;
  313. _Ty _Wd = _Yim + _Wr * _Yre;
  314. if (_Myctr::_Isnan(_Wd) || _Wd == 0)
  315. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  316. else
  317. {_Ty _W = (_X.real() * _Wr + _X.imag()) / _Wd;
  318. _X.imag((_X.imag() * _Wr - _X.real()) / _Wd);
  319. _X.real(_W); }}
  320. return (_X); }
  321. // TEMPLATE FUNCTION operator>>
  322. template<class _E, class _Tr, class _U> inline
  323. basic_istream<_E, _Tr>& __cdecl operator>>(
  324. basic_istream<_E, _Tr>& _I, complex<_U>& _X)
  325. {typedef complex<_U> _Myt;
  326. _E _Ch;
  327. long double _Re, _Im;
  328. if (_I >> _Ch && _Ch != '(')
  329. _I.putback(_Ch), _I >> _Re, _Im = 0;
  330. else if (_I >> _Re >> _Ch && _Ch != ',')
  331. if (_Ch == ')')
  332. _Im = 0;
  333. else
  334. _I.putback(_Ch), _I.setstate(ios_base::failbit);
  335. else if (_I >> _Im >> _Ch && _Ch != ')')
  336. _I.putback(_Ch), _I.setstate(ios_base::failbit);
  337. if (!_I.fail())
  338. _X = _Myt((_U)_Re, (_U)_Im);
  339. return (_I); }
  340. // TEMPLATE FUNCTION operator<<
  341. template<class _E, class _Tr, class _U> inline
  342. basic_ostream<_E, _Tr>& __cdecl operator<<(
  343. basic_ostream<_E, _Tr>& _O, const complex<_U>& _X)
  344. {basic_ostringstream<_E, _Tr, allocator<_E> > _S;
  345. _S.flags(_O.flags());
  346. _S.imbue(_O.getloc());
  347. _S.precision(_O.precision());
  348. _S << '(' << real(_X) << ',' << imag(_X) << ')';
  349. return (_O << _S.str().c_str()); }
  350. #define _CMPLX(T) complex<T >
  351. #define _CTR(T) _Ctr<T >
  352. #define _TMPLT(T) template<class T >
  353. #include <xcomplex>
  354. _STD_END
  355. #ifdef _MSC_VER
  356. #pragma pack(pop)
  357. #endif /* _MSC_VER */
  358. #endif /* _COMPLEX_ */
  359. /*
  360. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  361. * Consult your license regarding permissions and restrictions.
  362. */