Leaked source code of windows server 2003
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.

363 lines
11 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 _CRTIMP _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 _CRTIMP _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 _CRTIMP _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 _CRTIMP complex<float>;
  159. class _CRTIMP complex<double>;
  160. class _CRTIMP 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. // CLASS complex<float>
  195. class _CRTIMP complex<float> : public _Complex_base<float> {
  196. public:
  197. typedef float _Ty;
  198. explicit complex(const complex<double>&);
  199. explicit complex(const complex<long double>&);
  200. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  201. : _Complex_base<_Ty>(_R, _I) {}
  202. complex<_Ty>& operator=(const _Ty& _X)
  203. {_Re = _X;
  204. _Im = 0;
  205. return (*this); }
  206. };
  207. // CLASS complex<double>
  208. class _CRTIMP complex<double> : public _Complex_base<double> {
  209. public:
  210. typedef double _Ty;
  211. complex(const complex<float>&);
  212. explicit complex(const complex<long double>&);
  213. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  214. : _Complex_base<_Ty>(_R, _I) {}
  215. complex<_Ty>& operator=(const _Ty& _X)
  216. {_Re = _X;
  217. _Im = 0;
  218. return (*this); }
  219. };
  220. // CLASS complex<long double>
  221. class _CRTIMP complex<long double> : public _Complex_base<long double> {
  222. public:
  223. typedef long double _Ty;
  224. complex(const complex<float>&);
  225. complex(const complex<double>&);
  226. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  227. : _Complex_base<_Ty>(_R, _I) {}
  228. complex<_Ty>& operator=(const _Ty& _X)
  229. {_Re = _X;
  230. _Im = 0;
  231. return (*this); }
  232. };
  233. // CONSTRUCTORS FOR complex SPECIALIZATIONS
  234. inline complex<float>::complex(const complex<double>& _X)
  235. : _Complex_base<float>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  236. inline complex<float>::complex(const complex<long double>& _X)
  237. : _Complex_base<float>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  238. inline complex<double>::complex(const complex<float>& _X)
  239. : _Complex_base<double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  240. inline complex<double>::complex(const complex<long double>& _X)
  241. : _Complex_base<double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  242. inline complex<long double>::complex(const complex<float>& _X)
  243. : _Complex_base<long double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  244. inline complex<long double>::complex(const complex<double>& _X)
  245. : _Complex_base<long double>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  246. // TEMPLATE CLASS complex
  247. template<class _Ty>
  248. class complex : public _Complex_base<_Ty> {
  249. public:
  250. complex(const _Ty& _R = 0, const _Ty& _I = 0)
  251. : _Complex_base<_Ty>(_R, _I) {}
  252. typedef _Ty _U;
  253. complex(const complex<_U>& _X)
  254. : _Complex_base<_Ty>((_Ty)_X.real(), (_Ty)_X.imag()) {}
  255. complex<_Ty>& operator=(const complex<_U>& _X)
  256. {_Re = (_Ty)_X.real();
  257. _Im = (_Ty)_X.imag();
  258. return (*this); }
  259. };
  260. // TEMPLATE complex OPERATORS
  261. template<class _Ty, class _U> inline
  262. complex<_Ty>& __cdecl operator+=(
  263. complex<_Ty>& _X,
  264. const complex<_U>& _Y)
  265. {_X.real(_X.real() + (_Ty)_Y.real());
  266. _X.imag(_X.imag() + (_Ty)_Y.imag());
  267. return (_X); }
  268. template<class _Ty, class _U> inline
  269. complex<_Ty>& __cdecl operator-=(
  270. complex<_Ty>& _X,
  271. const complex<_U>& _Y)
  272. {_X.real(_X.real() - (_Ty)_Y.real());
  273. _X.imag(_X.imag() - (_Ty)_Y.imag());
  274. return (_X); }
  275. template<class _Ty, class _U> inline
  276. complex<_Ty>& __cdecl operator*=(
  277. complex<_Ty>& _X,
  278. const complex<_U>& _Y)
  279. {_Ty _Yre = (_Ty)_Y.real();
  280. _Ty _Yim = (_Ty)_Y.imag();
  281. _Ty _W = _X.real() * _Yre - _X.imag() * _Yim;
  282. _X.imag(_X.real() * _Yim + _X.imag() * _Yre);
  283. _X.real(_W);
  284. return (_X); }
  285. template<class _Ty, class _U> inline
  286. complex<_Ty>& __cdecl operator/=(
  287. complex<_Ty>& _X,
  288. const complex<_U>& _Y)
  289. {typedef _Ctr<_Ty> _Myctr;
  290. _Ty _Yre = (_Ty)_Y.real();
  291. _Ty _Yim = (_Ty)_Y.imag();
  292. if (_Myctr::_Isnan(_Yre) || _Myctr::_Isnan(_Yim))
  293. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  294. else if ((_Yim < 0 ? -_Yim : +_Yim)
  295. < (_Yre < 0 ? -_Yre : +_Yre))
  296. {_Ty _Wr = _Yim / _Yre;
  297. _Ty _Wd = _Yre + _Wr * _Yim;
  298. if (_Myctr::_Isnan(_Wd) || _Wd == 0)
  299. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  300. else
  301. {_Ty _W = (_X.real() + _X.imag() * _Wr) / _Wd;
  302. _X.imag((_X.imag() - _X.real() * _Wr) / _Wd);
  303. _X.real(_W); }}
  304. else if (_Yim == 0)
  305. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  306. else
  307. {_Ty _Wr = _Yre / _Yim;
  308. _Ty _Wd = _Yim + _Wr * _Yre;
  309. if (_Myctr::_Isnan(_Wd) || _Wd == 0)
  310. _X.real(_Myctr::_Nanv(_Yre)), _X.imag(_X.real());
  311. else
  312. {_Ty _W = (_X.real() * _Wr + _X.imag()) / _Wd;
  313. _X.imag((_X.imag() * _Wr - _X.real()) / _Wd);
  314. _X.real(_W); }}
  315. return (_X); }
  316. // TEMPLATE FUNCTION operator>>
  317. template<class _E, class _Tr, class _U> inline
  318. basic_istream<_E, _Tr>& __cdecl operator>>(
  319. basic_istream<_E, _Tr>& _I, complex<_U>& _X)
  320. {typedef complex<_U> _Myt;
  321. _E _Ch;
  322. long double _Re, _Im;
  323. if (_I >> _Ch && _Ch != _WIDEN(_E, '('))
  324. _I.putback(_Ch), _I >> _Re, _Im = 0;
  325. else if (_I >> _Re >> _Ch && _Ch != _WIDEN(_E, ','))
  326. if (_Ch == _WIDEN(_E, ')'))
  327. _Im = 0;
  328. else
  329. _I.putback(_Ch), _I.setstate(ios_base::failbit);
  330. else if (_I >> _Im >> _Ch && _Ch != _WIDEN(_E, ')'))
  331. _I.putback(_Ch), _I.setstate(ios_base::failbit);
  332. if (!_I.fail())
  333. _X = _Myt((_U)_Re, (_U)_Im);
  334. return (_I); }
  335. // TEMPLATE FUNCTION operator<<
  336. template<class _E, class _Tr, class _U> inline
  337. basic_ostream<_E, _Tr>& __cdecl operator<<(
  338. basic_ostream<_E, _Tr>& _O, const complex<_U>& _X)
  339. {basic_ostringstream<_E, _Tr, allocator<_E> > _S;
  340. _S.flags(_O.flags());
  341. _S.imbue(_O.getloc());
  342. _S.precision(_O.precision());
  343. _S << _WIDEN(_E, '(') << real(_X) << _WIDEN(_E, ',')
  344. << imag(_X) << _WIDEN(_E, ')');
  345. return (_O << _S.str().c_str()); }
  346. #define _CMPLX(T) complex<T >
  347. #define _CTR(T) _Ctr<T >
  348. #define _TMPLT(T) template<class T >
  349. #include <xcomplex>
  350. _STD_END
  351. #ifdef _MSC_VER
  352. #pragma pack(pop)
  353. #endif /* _MSC_VER */
  354. #endif /* _COMPLEX_ */
  355. /*
  356. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  357. * Consult your license regarding permissions and restrictions.
  358. */