package Jampack;

/**
 * Znum is a mutable complex variable class.  It is designed to
 * perform complex arithmetic without creating a new Znum at
 * each operation.  Specifically, binary operations have the
 * form c.op(a,b), in which a, b, and c need not be different.
 * The method  places the complex number a.op.b in
 * c.  The method also returns a pointer to c.  Thus the
 * class supports two styles of programming.  For example
 * to compute e = a*b + c*d you can write <p>
 * 
 * z1.Times(a,b) <br>
 * z2.Times(c,d) <br>
 * e.Plus(z1,z2) <p>
 * 
 * or <p>
 *
 * e.Plus(z1.Times(a,b), z2.Times(a,b)) <p>
 *
 * Since objects of class Znum are mutable, the use of the assignment
 * operator "=" with these objects is deprecated. Use Eq. <p>
 *
 * The functions are reasonably resistent to overflow and underflow.
 * But the more complicated ones could almost certainly be improved.

   @version Pre-alpha
   @author G. W. Stewart
*/

public class Znum{

/** Complex 1. */
    public static final Znum ONE = new Znum(1,0);

/** Complex 0. */
    public static final Znum ZERO = new Znum(0,0);

/** Imaginary unit. */
    public static final Znum I = new Znum(0,1);


/** The real part of Znum. */
    public double re;

/** The imaginary part of Znum. */
    public double im;


/**
 * Creates a Znum and initializes it to zero.

   @return a Znum initialized to zero.
*/
   public Znum(){
      re = 0.;
      im = 0.;
   }

/**
 * Creates a Znum and initializes its real and imaginary parts.

   @param     x a double
   @param     y a double
   @return    x + iy
*/

   public Znum(double x, double y){
      re = x;
      im = y;
   }

/**
 * Creates a Znum and initializes its real part.

   @param     x a double
   @return    x + i*0
*/

   public Znum(double x){
      re = x;
      im = 0;
   }

/**
 * Creates a Znum and initializes it to another Znum.

   @param     a a Znum
   @return    a
*/

   public Znum(Znum a){
      re = a.re;
      im = a.im;
   }

/**
 * Tests two Znum'z for equality.

   @param     z1 a Znum
   @param     z2 a Znum
   @return    true if z1=z2, otherwise false
*/

   public boolean IsEqual(Znum z1, Znum z2){
      if (z1.re == z2.re && z1.im == z2.im){
         return true;
      }
      else{
         return false;
      }
   }

/**
 * Resets the real and imaginary parts of a Znum to those of another Znum.

   @param     a a Znum
   @return    this = a;   
*/  

   public Znum Eq(Znum a){
      re = a.re;
      im = a.im;   
      return this;
   }

/**
 * Resets the real and imaginary parts of a Znum.

   @param     a a double
   @param     b a double
   @return    this = a + ib
*/

   public Znum Eq(double a, double b){
      re = a;
      im = b;
      return this;
   }

/**
 * Interchanges the real and imaginary parts of two Znum's.

   @param     a a Znum
   @return    this = a, with a set to the original
              value of this.
*/

   public Znum Exch(Znum a){
      double t;
      t = re; re = a.re; a.re = t;
      t = im; im = a.im; a.im = t;
      return this;
   }

/**
   Computes the 1-norm of a Znum
*/
   public static double abs1(Znum z){
      return Math.abs(z.re) + Math.abs(z.im);
   }

/**
   Computes the absolute value of a Znum.

   @param     z a Znum
   @return    the absolute vaue of Znum
*/

   public static double abs(Znum z){
      double are, aim, rho;
      are = Math.abs(z.re);
      aim = Math.abs(z.im);
      if (are+aim == 0) return 0;
      if (are >= aim){
         rho = aim/are;
         return are*Math.sqrt(1+rho*rho);
      }
      else{
         rho = are/aim;
         return aim*Math.sqrt(1+rho*rho);
      }
   }

/**
 * Computes the conjugate of a Znum.

   @param     a a Znum
   @return    this = conj(a);
*/

   public Znum Conj(Znum a){
      re = a.re;
      im = -a.im;
      return this;
   }

/**
 * Computes unary minus of a Znum.
   @param     a a Znum
   @return    this = -a;
*/

   public Znum Minus(Znum a){
      re = -a.re;
      im = -a.im;
      return this;
   }


/**
 * Computes the sum of two Znum's.

   @param     a a Znum
   @param     b a Znum
   @return    this = a + b
*/
   public Znum Plus(Znum a, Znum b){
      re = a.re + b.re;
      im = a.im + b.im;
      return this;
   }

/**
 * Computes the difference of two Znum's.

   @param     a a Znum
   @param     b a Znum
   @return    this = a - b
*/

   public Znum Minus(Znum a, Znum b){
      re = a.re - b.re;
      im = a.im - b.im;
      return this;
   }

/**
 * Computes the product of two Znum's.

   @param     a a Znum
   @param     b a Znum
   @return    this = ab
*/


   public Znum Times(Znum a, Znum b){
      double tre;
      tre = a.re*b.re - a.im*b.im;
      im = a.im*b.re + a.re*b.im;
      re = tre;
      return this;
   }

/**
 *  Computes the product of a double and a Znum.

   @param     a a double
   @param     b a Znum
   @return    this = ab
*/

   public Znum  Times(double a, Znum b){

      re = a*b.re;
      im = a*b.im;
      return this;
   }

/**
 * Computes the quotient of two Znum's.  Throws a JampackException if
 * the denominator is zero.

   @param     a a Znum
   @param     b a Znum
   @return    this = a/b
   @exception JampackException
              Thrown if b is zero.
*/

   public Znum Div(Znum a, Znum b)
   throws JampackException{
      double avi, t, tre, tim;
         avi = abs(b);
         if (avi == 0){
            throw new JampackException
               ("Divide by zero.");
         }
         avi = 1./avi;
         tre = b.re*avi;
         tim = -b.im*avi;
         t = (a.re*tre - a.im*tim)*avi;
         im = (a.im*tre + a.re*tim)*avi;
         re = t;
         return this;
   }

/**
 * Computes the quotient of a Znum and a double.  Throws a JampackException
 * if the denominator is zero.
   @param     a a Znum
   @param     b a double
   @return    this = a/b
   @exception JampackException
              Thrown if b is zero.
*/

   public Znum Div(Znum a, double b)
   throws JampackException{
      if (b == 0){
         throw new JampackException
            ("Divide by zero.");
      }
      re = a.re/b;
      im = a.im/b;
      return this;
   }

/**
 * Computes the principal value of the square root of a Znum.

   @param     a a Znum
   @param     this = sqrt(a)
*/


   public Znum Sqrt(Znum a){
      double t, tre, tim;

      t = Znum.abs(a);

      if (Math.abs(a.re) <= Math.abs(a.im)){
         // No cancellation in these formulas
         tre = Math.sqrt(0.5*(t+a.re));
         tim = Math.sqrt(0.5*(t-a.re));
      }
      else{
         // Stable computation of the above formulas
         if (a.re > 0){
            tre = t + a.re;
            tim = Math.abs(a.im)*Math.sqrt(0.5/tre);
            tre = Math.sqrt(0.5*tre);
         }
         else{
            tim = t - a.re;
            tre = Math.abs(a.im)*Math.sqrt(0.5/tim);
            tim = Math.sqrt(0.5*tim);
         }
      }
      if (a.im < 0)
         tim = -tim;
      re = tre;
      im = tim;
      return this;

   }

}

