// xcomplex internal header
#ifndef _XCOMPLEX_
#define _XCOMPLEX_

#pragma info(none)
#ifndef __CHKHDR__
#  pragma report(level, E)
#endif
#pragma info(restore)

#if __IBMCPP__ < 400
#error This file to be used only with IBM VisualAge C++ v4 and later compilers.
#endif /* __IBMCPP__ */

#pragma namemangling(ansi)

   /********************************************************************/
   /*  <xcomplex> header file                                          */
   /*                                                                  */
   /*  VisualAge C++, Version 4.0                                      */
   /*    Licensed Material - Property of IBM                           */
   /*                                                                  */
   /*  5801-AAR and Other Materials                                    */
   /*                                                                  */
   /*  (c) Copyright IBM Corp 1991, 1998. All rights reserved.         */
   /*                                                                  */
   /*                                                                  */
   /*  Licensed Materials.                                             */
   /*                                                                  */
   /*  Dinkum C++ Library                                              */
   /*  Copyright (c) 1998.  Licensed to IBM Corp. and its suppliers.   */
   /*                                                                  */
   /********************************************************************/

		// TEMPLATE FUNCTION imag
_TMPLT(_Ty) inline
	_Ty imag(const _CMPLX(_Ty)& _X)
	{return (_X.imag()); }
		// TEMPLATE FUNCTION real
_TMPLT(_Ty) inline
	_Ty real(const _CMPLX(_Ty)& _X)
	{return (_X.real()); }
		// TEMPLATE FUNCTION _Fabs
_TMPLT(_Ty) inline
	_Ty _Fabs(const _CMPLX(_Ty)& _X, int *_Pexp)
	{*_Pexp = 0;
	_Ty _A = real(_X);
	_Ty _B = imag(_X);
	if (_CTR(_Ty)::_Isnan(_A))
		return (_A);
	else if (_CTR(_Ty)::_Isnan(_B))
		return (_B);
	else
		{if (_A < 0)
			_A = -_A;
		if (_B < 0)
			_B = -_B;
		if (_A < _B)
			{_Ty _W = _A;
			_A = _B, _B = _W; }
		if (_A == 0 || _CTR(_Ty)::_Isinf(_A))
			return (_A);
		if (1 <= _A)
			*_Pexp = 2, _A = _A * 0.25, _B = _B * 0.25;
		else
			*_Pexp = -2, _A = _A * 4, _B = _B * 4;
		_Ty _W = _A - _B;
		if (_W == _A)
			return (_A);
		else if (_B < _W)
			{const _Ty _Q = _A / _B;
			return (_A + _B
				/ (_Q + _CTR(_Ty)::sqrt(_Q * _Q + 1))); }
		else
			{static const _Ty _R2 = (_Ty)1.4142135623730950488L;
			static const _Ty _Xh = (_Ty)2.4142L;
			static const _Ty _Xl =
				(_Ty)0.0000135623730950488016887L;
			const _Ty _Q = _W / _B;
			const _Ty _R = (_Q + 2) * _Q;
			const _Ty _S = _R / (_R2 + _CTR(_Ty)::sqrt(_R + 2))
				+ _Xl + _Q + _Xh;
			return (_A + _B / _S); }}}
		// TEMPLATE FUNCTION operator+
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator+(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W += _R); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator+(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{_CMPLX(_Ty) _W(_L);
	_W.real(_W.real() + _R);
	return (_W); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator+(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W += _R); }
		// TEMPLATE FUNCTION operator-
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator-(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W -= _R); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator-(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{_CMPLX(_Ty) _W(_L);
	_W.real(_W.real() - _R);
	return (_W); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator-(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W -= _R); }
		// TEMPLATE FUNCTION operator*
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator*(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W *= _R); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator*(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{_CMPLX(_Ty) _W(_L);
	_W.real(_W.real() * _R);
	_W.imag(_W.imag() * _R);
	return (_W); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator*(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W *= _R); }
		// TEMPLATE FUNCTION operator/
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator/(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W /= _R); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator/(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{_CMPLX(_Ty) _W(_L);
	_W.real(_W.real() / _R);
	_W.imag(_W.imag() / _R);
	return (_W); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator/(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{_CMPLX(_Ty) _W(_L);
	return (_W /= _R); }
		// TEMPLATE FUNCTION UNARY operator+
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator+(const _CMPLX(_Ty)& _L)
	{return (_CMPLX(_Ty)(_L)); }
		// TEMPLATE FUNCTION UNARY operator-
_TMPLT(_Ty) inline
	_CMPLX(_Ty) operator-(const _CMPLX(_Ty)& _L)
	{return (_CMPLX(_Ty)(-real(_L), -imag(_L))); }
		// TEMPLATE FUNCTION operator==
_TMPLT(_Ty) inline
	bool operator==(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{return (real(_L) == real(_R) && imag(_L) == imag(_R)); }
_TMPLT(_Ty) inline
	bool operator==(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{return (real(_L) == _R && imag(_L) == 0); }
_TMPLT(_Ty) inline
	bool operator==(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{return (_L == real(_R) && 0 == imag(_R)); }
_TMPLT(_Ty) inline
	bool operator!=(const _CMPLX(_Ty)& _L,
		const _CMPLX(_Ty)& _R)
	{return (!(_L == _R)); }
_TMPLT(_Ty) inline
	bool operator!=(const _CMPLX(_Ty)& _L,
		const _Ty& _R)
	{return (!(_L == _R)); }
_TMPLT(_Ty) inline
	bool operator!=(const _Ty& _L,
		const _CMPLX(_Ty)& _R)
	{return (!(_L == _R)); }
		// TEMPLATE FUNCTION abs
_TMPLT(_Ty) inline
	_Ty abs(const _CMPLX(_Ty)& _X)
	{int _Xexp;
	_Ty _Rho = _Fabs(_X, &_Xexp);
	if (_Xexp == 0)
		return (_Rho);
	else
		return (_CTR(_Ty)::ldexp(_Rho, _Xexp)); }
		// TEMPLATE FUNCTION arg
_TMPLT(_Ty) inline
	_Ty arg(const _CMPLX(_Ty)& _X)
	{return (_CTR(_Ty)::atan2(imag(_X), real(_X))); }
		// TEMPLATE FUNCTION conj
_TMPLT(_Ty) inline
	_CMPLX(_Ty) conj(const _CMPLX(_Ty)& _X)
	{return (_CMPLX(_Ty)(real(_X), -imag(_X))); }
		// TEMPLATE FUNCTION cos
_TMPLT(_Ty) inline
	_CMPLX(_Ty) cos(const _CMPLX(_Ty)& _X)
	{return (_CMPLX(_Ty)(
		_CTR(_Ty)::_Cosh(imag(_X), _CTR(_Ty)::cos(real(_X))),
		-_CTR(_Ty)::_Sinh(imag(_X),
			_CTR(_Ty)::sin(real(_X))))); }
		// TEMPLATE FUNCTION cosh
_TMPLT(_Ty) inline
	_CMPLX(_Ty) cosh(const _CMPLX(_Ty)& _X)
	{return (_CMPLX(_Ty)(
		_CTR(_Ty)::_Cosh(real(_X), _CTR(_Ty)::cos(imag(_X))),
		_CTR(_Ty)::_Sinh(real(_X), _CTR(_Ty)::sin(imag(_X))))); }
		// TEMPLATE FUNCTION exp
_TMPLT(_Ty) inline
	_CMPLX(_Ty) exp(const _CMPLX(_Ty)& _X)
	{_Ty _Re(real(_X)), _Im(real(_X));
	_CTR(_Ty)::_Exp(&_Re, _CTR(_Ty)::cos(imag(_X)), 0);
	_CTR(_Ty)::_Exp(&_Im, _CTR(_Ty)::sin(imag(_X)), 0);
	return (_CMPLX(_Ty)(_Re, _Im)); }
		// TEMPLATE FUNCTION log
_TMPLT(_Ty) inline
	_CMPLX(_Ty) log(const _CMPLX(_Ty)& _X)
	{int _Xexp;
	_Ty _Rho = _Fabs(_X, &_Xexp);
	if (_CTR(_Ty)::_Isnan(_Rho))
		return (_CMPLX(_Ty)(_Rho, _Rho));
	else
		{static const _Ty _Cm = (_Ty)(22713.0 / 32768.0);
		static const _Ty _Cl = (_Ty)1.428606820309417232e-6L;
		_Ty _Xn = _Xexp;
		_CMPLX(_Ty) _W(_Rho == 0 ? -_CTR(_Ty)::_Infv(_Rho)
			: _CTR(_Ty)::_Isinf(_Rho) ? _Rho
			: _CTR(_Ty)::log(_Rho) + _Xn * _Cl + _Xn * _Cm,
				_CTR(_Ty)::atan2(imag(_X), real(_X)));
		return (_W); }}
		// TEMPLATE FUNCTION log10
_TMPLT(_Ty) inline
	_CMPLX(_Ty) log10(const _CMPLX(_Ty)& _X)
	{return (log(_X) * (_Ty)0.4342944819032518276511289L); }
		// TEMPLATE FUNCTION norm
_TMPLT(_Ty) inline
	_Ty norm(const _CMPLX(_Ty)& _X)
	{return (real(_X) * real(_X) + imag(_X) * imag(_X)); }
		// TEMPLATE FUNCTION polar
_TMPLT(_Ty) inline
	_CMPLX(_Ty) polar(const _Ty& _Rho, const _Ty& _Theta)
	{return (_CMPLX(_Ty)(_Rho * _CTR(_Ty)::cos(_Theta),
		_Rho * _CTR(_Ty)::sin(_Theta))); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) polar(const _Ty& _Rho)
	{return (polar(_Rho, (_Ty)0)); }
		// TEMPLATE FUNCTION pow
_TMPLT(_Ty) inline
	_CMPLX(_Ty) pow(const _CMPLX(_Ty)& _X, const _Ty& _Y)
	{if (imag(_X) == 0)
		return (_CMPLX(_Ty)(_CTR(_Ty)::pow(real(_X), _Y)));
	else
		return (exp(_Y * log(_X))); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) pow(const _CMPLX(_Ty)& _X, int _Y)
	{if (imag(_X) == 0)
		return (_CMPLX(_Ty)(_CTR(_Ty)::pow(real(_X), _Y)));
	else
		{_CMPLX(_Ty) _W = _X;
		unsigned int _N = _Y;
		if (_Y < 0)
			_N = -_N;
		for (_CMPLX(_Ty) _Z = _CMPLX(_Ty)(1); ; _W *= _W)
			{if ((_N & 1) != 0)
				_Z *= _W;
			if ((_N >>= 1) == 0)
				return (_Y < 0 ? _CMPLX(_Ty)(1) / _Z : _Z); }}}
_TMPLT(_Ty) inline
	_CMPLX(_Ty) pow(const _Ty& _X, const _CMPLX(_Ty)& _Y)
	{if (imag(_Y) == 0)
		return (_CMPLX(_Ty)(_CTR(_Ty)::pow(_X, real(_Y))));
	else
		return (exp(_Y * _CTR(_Ty)::log(_X))); }
_TMPLT(_Ty) inline
	_CMPLX(_Ty) pow(const _CMPLX(_Ty)& _X,
		const _CMPLX(_Ty)& _Y)
	{if (imag(_Y) == 0)
		return (pow(_X, real(_Y)));
	else if (imag(_X) == 0)
		return (_CMPLX(_Ty)(pow(real(_X), _Y)));
	else
		return (exp(_Y * log(_X))); }
		// TEMPLATE FUNCTION sin
_TMPLT(_Ty) inline
	_CMPLX(_Ty) sin(const _CMPLX(_Ty)& _X)
	{return (_CMPLX(_Ty)(
		_CTR(_Ty)::_Cosh(imag(_X), _CTR(_Ty)::sin(real(_X))),
		_CTR(_Ty)::_Sinh(imag(_X), _CTR(_Ty)::cos(real(_X))))); }
		// TEMPLATE FUNCTION sinh
_TMPLT(_Ty) inline
	_CMPLX(_Ty) sinh(const _CMPLX(_Ty)& _X)
	{return (_CMPLX(_Ty)(
		_CTR(_Ty)::_Sinh(real(_X), _CTR(_Ty)::cos(imag(_X))),
		_CTR(_Ty)::_Cosh(real(_X), _CTR(_Ty)::sin(imag(_X))))); }
		// TEMPLATE FUNCTION sqrt
_TMPLT(_Ty) inline
	_CMPLX(_Ty) sqrt(const _CMPLX(_Ty)& _X)
	{int _Xexp;
	_Ty _Rho = _Fabs(_X, &_Xexp);
	if (_Xexp == 0)
		return (_CMPLX(_Ty)(_Rho, _Rho));
	else
		{_Ty _Remag = _CTR(_Ty)::ldexp(real(_X) < 0
			? - real(_X) : real(_X), -_Xexp);
		_Rho = _CTR(_Ty)::ldexp(_CTR(_Ty)::sqrt(
			2 * (_Remag + _Rho)), _Xexp / 2 - 1);
		if (0 <= real(_X))
			return (_CMPLX(_Ty)(_Rho, imag(_X) / (2 * _Rho)));
		else if (imag(_X) < 0)
			return (_CMPLX(_Ty)(-imag(_X) / (2 * _Rho), -_Rho));
		else
			return (_CMPLX(_Ty)(imag(_X) / (2 * _Rho),
				_Rho)); }}
		// TEMPLATE FUNCTION tanh
_TMPLT(_Ty) inline
	_CMPLX(_Ty) tanh(const _CMPLX(_Ty)& _X)
	{_Ty _Tv = _CTR(_Ty)::tan(imag(_X));
	_Ty _Sv = _CTR(_Ty)::_Sinh(real(_X), (_Ty)(1));
	_Ty _Bv = _Sv *((_Ty)(1) + _Tv * _Tv);
	_Ty _Dv = (_Ty)(1) + _Bv * _Sv;
	return (_CMPLX(_Ty)((_CTR(_Ty)::sqrt((_Ty)(1) + _Sv * _Sv))
		* _Bv / _Dv, _Tv / _Dv)); }
		// TEMPLATE FUNCTION tan
_TMPLT(_Ty) inline
	_CMPLX(_Ty) tan(const _CMPLX(_Ty)& _X)
	{_CMPLX(_Ty) _Z(tanh(_CMPLX(_Ty)(-imag(_X), real(_X))));
	return (_CMPLX(_Ty)(imag(_Z), -real(_Z))); }

#pragma namemangling()

#pragma info(none)
#ifndef __CHKHDR__
#  pragma report(pop)
#endif
#pragma info(restore)

#endif /* _XCOMPLEX_ */

/*
 * Copyright (c) 1996-1998 by P.J. Plauger.  ALL RIGHTS RESERVED. 
 * Consult your license regarding permissions and restrictions.
V2.2:0081 */
