import java.io.*; // class complex // complex number abstraction // class complex { // constructors public complex() { nreal=0; nimag=0; } public complex(double nre,double nim) { nreal=nre; nimag=nim; } public complex(double numer) { nreal=numer; nimag=0; } public complex(complex value ) { nreal=value.real(); nimag=value.imaginary(); } // accessor functions public double real() { return nreal; } public double imaginary() { return nimag; } public double R() { return Math.sqrt(nreal*nreal+nimag*nimag); } public double theta() { return Math.atan2(nimag,nreal); } public double dtheta() { return Math.atan2(nimag,nreal)*45.0/Math.atan(1.0); } // assignments public void assign(complex right) { nreal=right.real(); nimag=right.imaginary(); } public void add(complex right) { nimag = nimag + right.imaginary(); nreal = nreal + right.real(); } public void substract(complex right) { nimag = nimag - right.imaginary(); nreal = nreal - right.real(); } public void multiply(complex right ) { nreal = nreal*right.real() - nimag*right.imaginary(); nimag = nreal*right.imaginary() + nimag*right.real(); } public void divide(complex right ) { double a=nreal*nreal+nimag*nimag; nreal = ( nreal*right.real() + nimag*right.imaginary())/a; nimag = (-nreal*right.imaginary() + nimag*right.real())/a; } public static complex add(complex left, complex right) { // return sum of two complex numbers double r1=(left.real() + right.real()); double i1=(left.imaginary() + right.imaginary()); complex result; result=new complex(r1,i1); return result; } public static complex substract(complex left, complex right) { // return substraction of two complex numbers complex result; result=new complex((left.real() - right.real()), (left.imaginary() - right.imaginary())); return result; } public static complex multiply(complex left, complex right) { // return multiplication of two complex numbers complex result; result=new complex ((left.real()*right.real() - left.imaginary()*right.imaginary()), (left.real()*right.imaginary() + left.imaginary()*right.real())); return result; } public static complex divide(complex left, complex right) { // return division of two complex numbers double a=right.real()*right.real()+right.imaginary()*right.imaginary(); complex result; result=new complex ((left.real()*right.real() + left.imaginary()*right.imaginary())/a, (-left.real()*right.imaginary() + left.imaginary()*right.real())/a); return result; } public static complex pow(complex left, double right) { // return sum of two complex numbers double Rad,th; Rad=Math.pow(left.R(),right); th=right*left.theta(); complex result; result =new complex((Rad*Math.cos(th) ), (Rad*Math.sin(th) ) ); return result; } public boolean smaller(complex left,complex right) { // less then comparison of two complex numbers return (left.R() < right.R()); } public boolean smaller_equal(complex left,complex right) { // less then and equal comparison of two complex numbers return (left.R() <= right.R()); } public boolean greater(complex left,complex right) { // greater then comparison of two complex numbers return left.R() > right.R(); } public boolean greater_equal(complex left,complex right) { // greater then and equal comparison of two complex numbers return left.R() >= right.R(); } public boolean equal(complex left,complex right) { // equal comparison of two complex numbers return left.R() == right.R(); } public boolean not_equal(complex left,complex right) { // not equal comparison of two complex numbers return left.R() != right.R(); } public static String toString(complex value) { String b=""; if(Math.abs(value.imaginary())!=1) { if(value.imaginary() >= 0) b=b+"("+value.real()+" + "+value.imaginary()+"i )"; else b=b+"("+value.real()+" - "+(-value.imaginary())+"i )"; } else { if(value.imaginary() >= 0) b=b+"("+value.real()+" + i )"; else b=b+"("+value.real()+" - i )"; } return b; } public String toString() { String b=""; if(Math.abs(imaginary())!=1) { if(imaginary() >= 0) b=b+"("+real()+" + "+imaginary()+"i )"; else b=b+"("+real()+" - "+(-imaginary())+"i )"; } else { if(imaginary() >= 0) b=b+"("+real()+" + i )"; else b=b+"("+real()+" - i )"; } return b; } // data areas public double nreal; public double nimag; };