//Evan Warner //This class stores a complex number and contains methods to implement complex arithmetic public class Complex { //Stores complex number in form x+iy private double x,y; public Complex(double u, double v) { x=u; y=v; } //Returns real part of this public double real() { return x; } //Returns imaginary part of this public double imag() { return y; } //Returns String to be parsed by ComplexFunction class in form {x,y} public String toString() { return "{"+x+","+y+"}"; } //Returns addition of this to another complex number z public Complex add(Complex z) { return new Complex(x+z.real(),y+z.imag()); } //Returns subtraction of another complex number z from this public Complex subtract(Complex z) { return new Complex(x-z.real(),y-z.imag()); } //Returns multiplication of this to another complex number z //Uses algorithm from Numerical Recipes, Second Edition with only three multiplications public Complex multiply(Complex z) { double temp1=x*z.real(); double temp2=y*z.imag(); return new Complex(temp1-temp2,(x+y)*(z.real()+z.imag())-temp1-temp2); } //Returns multiplication of this with a double a public Complex multiply(double a) { return new Complex(x*a,y*a); } //Returns division of another complex number z from this //Uses algorithm from Numerical Recipes, Second Edition public Complex divide(Complex z) { double temp1=z.real()/z.imag(); double temp2=z.imag()/z.real(); if(Math.abs(z.real())>=Math.abs(z.imag())) { double denominator=z.real()+z.imag()*temp2; return new Complex((x+y*temp2)/denominator,(y-x*temp2)/denominator); } else { double denominator=z.real()*temp1+z.imag(); return new Complex((x*temp1+y)/denominator,(y*temp1-x)/denominator); } } //Returns division of this by double a public Complex divide(double a) { return new Complex(x/a,y/a); } //Returns modulus of this; algorithm designed to minimize chances of overflow public double modulus() { if(x==0&&y==0) return 0.0; else if(y==0) return Math.abs(x); else if(x==0) return Math.abs(y); if(Math.abs(y)>=Math.abs(x)) return Math.abs(x)*Math.sqrt(1.0+(y*y)/(x*x)); else return Math.abs(y)*Math.sqrt(1.0+(x*x)/(y*y)); } //Returns argument of this public double argument() { return Math.atan2(y,x); } //Returns square root of this using negative real axis as branch cut (-Pi=Math.abs(y)) w=Math.sqrt(Math.abs(x))*Math.sqrt((1.0+Math.sqrt(1.0+(y*y)/(x*x)))/2.0); else w=Math.sqrt(Math.abs(y))*Math.sqrt((Math.abs(x/y)+Math.sqrt(1.0+(x*x)/(y*y)))/2.0); if(x>=0) return new Complex(w,y/(2*w)); else if(y>=0) return new Complex(Math.abs(y)/(2*w),w); else return new Complex(Math.abs(y)/(2*w),-w); } //Returns this raised to a power given by a double, a public Complex power(double a) { double theta=this.argument(); double r=this.modulus(); Complex temp = new Complex(Math.cos(theta*a),Math.sin(theta*a)).multiply(Math.pow(r,a)); return temp; } //Returns the natural logarithm of this public Complex ln() { double theta=this.argument(); double r=this.modulus(); return new Complex(Math.log(r),theta); } //Returns the exponential function of this public Complex exp() { double etothex=Math.exp(x); return new Complex(etothex*Math.cos(y),etothex*Math.sin(y)); } //The following methods have not yet been completed and will be written as necessary: //This method raises this to another complex number z //Right now, the input has been assumed to be real to work with other programs public Complex power(Complex z) { //THIS IS TEMPORARY PLEASE CHANGE LATER return this.power(z.real()); } public Complex sin() { return null; } public Complex cos() { return null; } public Complex tan() { return null; } public Complex sec() { return null; } public Complex csc() { return null; } public Complex cot() { return null; } }