/*
 * Copyright (C) 2001-2005 Nathan Ingersoll
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the ferite software nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

uses "math";

/**
 * @namespace Math
 * @modifies Math
 */
namespace modifies Math
{
  /**
   * @class Complex
   * @brief Provides methods for manipulating complex numbers
   */
    class Complex
    {
       /* APH: Why is this stored in the class instead of the namespace? */
       final number sqrt2 = 1.41421356;

       number real;
       number imaginary;

     /**
      * @function Complex
      * @brief The constructor of the Math.Complex class
      * @declaration function Complex( number r, number i )
      * @param number r The real part of the complex number
      * @param number i The imaginary part of the complex number
      * @description Creates a complex number object with the specified real
      *              and imaginary parts.
      */
       function constructor(number r, number i)
       {
           .real = r;
           .imaginary = i;
       }

     /**
      * @function conjugate
      * @brief Determines the complex conjugate of a complex number
      * @param object c the complex variable to determine the conjugate of
      * @description Stores the complex conjugate of c into the calling
      *              object.
      */
       function conjugate(object c)
       {
           .real = c.real;
           .imaginary = -c.imaginary;
       }

     /**
      * @function add
      * @brief Adds two complex numbers
      * @param object a The first complex number
      * @param object b The second complex number
      * @description Adds the complex numbers a and b and stores the result
      *              into the calling object.
      */
       function add(object a, object b)
       {
           .real = a.real + b.real;
           .imaginary = a.imaginary + b.imaginary;
       }

     /**
      * @function sub
      * @brief Subtracts complex number b from complex number a
      * @param object a The minuend
      * @param object b The subtrahend
      * @description Subtracts complex number b from complex number a and
      *              stores the result into the calling object.
      */
       function sub(object a, object b)
       {
           .real = a.real - b.real;
           .imaginary = a.imaginary - b.imaginary;
       }

     /**
      * @function mult
      * @brief Multiplies two complex numbers
      * @param object a The first complex number
      * @param object b The second complex number
      * @description Multiplies the complex numbers a and b and stores the
      *              results into the calling object.
      */
       function mult(object a, object b)
       {
           number rcoeff, icoeff;

           rcoeff = a.real * b.real;
           rcoeff -= a.imaginary * b.imaginary;

           icoeff = a.imaginary * b.real;
           icoeff += a.real * b.imaginary;

           .real = rcoeff;
           .imaginary = icoeff;
       }

     /**
      * @function div
      * @brief Divides complex number a by complex number b
      * @param object a The numerator
      * @param object b The denominator
      * @description Divides the complex number a by the complex number b and
      *              stores the result in the calling object.
      */
       function div(object a, object b)
       {
           object numer, denom;

       /*
        * Determine the numerators and denominators of the fractions.
        */
           numer = new Math.Complex(0, 0);
           denom = new Math.Complex(0, 0);

       /*
        * Multiply the numerator @a by the complex conjugate of the
        * denominator.
        */
           denom.conjugate(b);
           numer.mult(a, denom);

       /*
        * Now multiply the denominator @b by it's complex conjugate.
        */
           denom.mult(b, denom);

       /*
        * At this point all values of the denominator are real, so
        * we can simply perform the division of each part of the
        * numerator.
        */
           .real = numer.real / denom.real;
           .imaginary = numer.imaginary / denom.real;
       }

     /**
      * @function modulus
      * @brief Calculates the modulus of this complex number object 
      * @return The modulus of the complex number as an ordinary number
      */
       function modulus()
       {
           return Math.sqrt((.real * .real) +
                            (.imaginary * .imaginary));
       }

     /**
      * @function sqrt
      * @brief Determines the principal square root of a complex number
      * @param object a The complex number to determine the square root of
      * @description Computes the principal square root of complex number a and
      *              stores the result in the calling object.
      */
       function sqrt(object a)
       {
           number val;
           number len;

       /*
        * Precompute two variables needed to calculate both the real
        * and imaginary parts of the square root.
        */
           val = .sqrt2 / 2;
           len = a.modulus();

       /*
        * Determine the real portion of the sqrt.
        */
           .real = val * Math.sqrt(len + a.real);

       /*
        * Find the imaginary part and determine its sign
        */
           .imaginary = val * Math.sqrt(len - a.real);
           if (a.imaginary < 0)
             .imaginary = -.imaginary;
       }

     /**
      * @function copy
      * @brief Copies the contents of a complex variable
      * @param object a The complex number to copy
      * @description Copies complex number a into the calling object.
      */
       function copy(object a)
       {
           .real = a.real;
           .imaginary = a.imaginary;
       }

     /**
      * @function toString
      * @brief Generates a string representation of this complex number
      * @return A string representation of this complex number
      */
       function toString()
       {
           string str = "" + .real;
           str += "+" + .imaginary + "i";

           return str;
       }
    }
  /**
   * @end
   */
}
/**
 * @end
 */
