www.pudn.com > sswf-1.7.4-src.zip > complex.asc


/* complex.asc -- written by Alexis WILKE for Made to Order Software, Ltd. (c) 2005-2006 */

/*

Copyright (c) 2005-2006 Made to Order Software, Ltd.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/


package Extension
{
	class Complex
	{
		function Complex(Void) : Complex
		{
			a = 0.0;
			b = 0.0;
		}
		function Complex(real : Double) : Complex
		{
			a = real;
			b = 0;
		}
		function Complex(_a : Double, _b : Double) : Complex
		{
			a = _a;
			b = _b;
		}

// operator overloading
		function "+" (Void) : Complex
		{
			return this;
		}
		function "-" (Void) : Complex
		{
			a = -a;
			b = -b;
			return this;
		}
		function "+=" (c : Complex) : Complex
		{
			a += c.a;
			b += c.b;
			return this;
		}
		function "+" (c : Complex) : Complex
		{
			return Complex(a + c.a, b + c.b);
		}
		function "+=" (c : Double) : Complex
		{
			a += c;
			return this;
		}
		function "+" (c : Double) : Complex
		{
			return Complex(a + c, b);
		}
		function "-=" (c : Complex) : Complex
		{
			a -= c.a;
			b -= c.b;
			return this;
		}
		function "-" (c : Complex) : Complex
		{
			return Complex(a - c.a, b - c.b);
		}
		function "-=" (c : Double) : Complex
		{
			a -= c;
			b -= c;
			return this;
		}
		function "-" (c : Double) : Complex
		{
			return Complex(a - c, b - c);
		}
		function "*=" (c : Complex) : Complex
		{
			var ta : Double, tb : Double;
			ta = a * c.a - b * c.b;
			tb = a * c.b + b * c.a;
			a = ta;
			b = tb;
			return this;
		}
		function "*" (c : Complex) : Complex
		{
			return Complex(a * c.a - b * c.b, a * c.b + b * c.a);
		}
		function "*=" (c : Double) : Complex
		{
			a *= c;
			b *= c;
			return this;
		}
		function "*" (c : Double) : Complex
		{
			return Complex(a * c, b * c);
		}
		function "/=" (c : Complex) : Complex
		{
			var sqr : Double;
			sqr = a * a + b * b;
			a /= sqr;
			b /= sqr;
			return this;
		}
		function "/" (c : Complex) : Complex
		{
			var sqr : Double;
			sqr = a * a + b * b;
			return Complex(a / sqr, b / sqr);
		}
		function "/=" (c : Double) : Complex
		{
			a /= c;
			b /= c;
			return this;
		}
		function "/" (c : Double) : Complex
		{
			return Complex(a / c, b / c);
		}

// comparison
		function "==" (c : Complex) : Complex
		{
			return a == c.a && b == c.b;
		}
		function "==" (c : Double) : Complex
		{
			return a == c && b == 0.0;
		}
		function "===" (c : Complex) : Complex
		{
			return a === c.a && b === c.b;
		}
		function "===" (c : Double) : Complex
		{
			return a === c && b === 0.0;
		}
		function "!=" (c : Complex) : Complex
		{
			return a != c.a || b != c.b;
		}
		function "!=" (c : Double) : Complex
		{
			return a != c || b != 0.0;
		}
		function "!==" (c : Complex) : Complex
		{
			return a !== c.a || b !== c.b;
		}
		function "!==" (c : Double) : Complex
		{
			return a !== c || b !== 0.0;
		}

// transcendentals
		function abs(Void) : Double
		{
			var m : Double = max(abs(a), abs(b));
			if(m == 0) {
				return 0
			}
			var ra : Double = a / m;
			var rb : Double = b / m;
			return m * sqrt(ra ** 2 + rb ** 2);
		}
		function arg(Void) : Double
		{
			return atan2(b, a);
		}
		function conj(Void) : Complex
		{
			return Complex(a, -b);
		}
		function cos(Void) : Complex
		{
			return Complex(cos(a) * cosh(b), -sin(a) * sinh(b));
		}
		function cosh(Void) : Complex
		{
			return Complex(cosh(a) * cos(b), sinh(a) * sin(b));
		}
		function exp(Void) : Complex
		{
			return polar(expr(a), b);
		}
		function imag(Void) : Double
		{
			return b;
		}
		function log(Void) : Complex
		{
			return Complex(log(abs()), arg());
		}
		function log10(Void) : Complex
		{
			return Complex(log() / log(Complex(10)));
		}
		function norm(Void) : Double
		{
			//return a ** 2 + b ** 2;
			return abs() ** 2;
		}
		static function polar(rho : Double, theta : Double)
		{
			return Complex(rho * cos(theta), rho * sin(theta));
		}
		function pow(n : Double) : Complex
		{
			return exp(n * log());
		}
		function pow(c : Complex) : Complex
		{
			return exp(c * log());
		}
		function real(Void) : Double
		{
			return a;
		}
		function sin(Void) : Complex
		{
			return Complex(sin(a) * cosh(b), cos(a) * sinh(b));
		}
		function sinh(Void) : Complex
		{
			return Complex(sinh(a) * cos(b), cosh(a) * sin(b));
		}
		function sqrt(Void) : Complex
		{
			if(a == 0) {
				var t = sqrt(abs(b) / 2);
				return Complex(t, b < 0 ? -t : t);
			}
			else {
				var t = sqrt(2 * (abs() + abs(a)));
				var u = t / 2;
				return x > 0
					? Complex(u, b / t)
					: Complex(abs(b) / t, y < 0 ? -u : u);
			}
		}
		function tan(Void) : Complex
		{
			var s : Complex = sin();
			var c : Complex = cos();
			return s / c;
		}
		function tanh(Void) : Complex
		{
			var s : Complex = sinh();
			var c : Complex = cosh();
			return s / c;
		}

// misc.
		function toString(Void) : String
		{
			return a.toString() + (b == 0 ? "" : " + " + b.toString() + "i");
		}

		var a : Double;
		var b : Double;
	}
}