Complex numbers are quite essential mathematical
abstraction: whether one beleives it or not. They are extensively used in
applied mathematics and play an important role in many branches of science.
These models typically require extensive computations involving manipulating
complex numbers according to well-defined arithmetic operations, so we want to
write a program to do the computations. In short we need a new datatype
'Complex'.
The basic
operations on complex nubers are: additon, substraction, multiplication,
division, finding reciprocal, absolute value, square root, conjugate, and
converting to polar form.
Let us write a
program which will create a new datatype complex that will do the above
operations and another program to test our new datatype.
class Complex
{
double r,i;
Complex()
{
r=0;
i=0;
}
Complex(double
real, double imag)
{
r = real;
i = imag;
}
Complex
plus(Complex b)
{
Complex
c=new Complex();
c.r=r+b.r;
c.i=i+b.i;
return c;
}
Complex
minus(Complex b)
{
Complex c=new Complex();
c.r=r-b.r;
c.i=i-b.i;
return c;
}
Complex
times(Complex b)
{
Complex
c=new Complex();
c.r=r*b.r-i*b.i;
c.i=r*b.i+i*b.r;
return c;
}
double abs()
{
double
a=Math.sqrt(r*r+i*i);
return a;
}
double Re()
{
return r;
}
double Im()
{
return i;
}
public String
toString()
{
if(i>=0)
return
Re()+"+"+Im()+"i";
else
return
Re()+"-"+(-Im())+"i";
}
String polar()
{
double
amp,arg;
amp=abs();
arg=Math.atan(i/r);
double
x=Math.PI/arg;
return(amp+"*cis("+x+")");
}
Complex sqrt()
{
Complex
c=new Complex();
c.r=Math.sqrt((abs()+r)/2);
c.i=Math.sqrt((abs()-r)/2);
return c;
}
Complex conjg()
{
Complex
c=new Complex();
c.r=r;
c.i=-i;
return c;
}
Complex
divideBy(Complex b)
{
Complex
cb=new Complex(0,0),c=new Complex(0,0);
double d;
cb=b.conjg();
c.r=r*cb.r-i*cb.i;
c.i=i*cb.r+r*cb.i;
d=cb.r*cb.r+cb.i*cb.i;
c.r /= d;
c.i /= d;
return c;
}
Complex inv()
{
Complex
c=new Complex();
double
d=r*r+i*i;
c=conjg();
c.r=c.r/d;
c.i=c.i/d;
return c;
}
Complex pow(int
a)
{
Complex
c=new Complex(r,i);
int
b=(a>0)?a:-a;
if(a==0)
{
c.r=1;
c.i=0;
}
else
{
for(int
i=1;i<b;i++)
{
c=times(c);
}
}
if(a<0)
c=c.inv();
return c;
}
}
import java.io.*;
class TestComplexNumber
{
public static
void main(String arg[])throws IOException
{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
Complex
A=new Complex(),B=new Complex(),C=new Complex();
System.out.println("Enter
real part and imaginary part of A");
A.r=Double.parseDouble(x.readLine());
A.i=Double.parseDouble(x.readLine());
System.out.println("Enter real part and imaginary part of B");
B.r=Double.parseDouble(x.readLine());
B.i=Double.parseDouble(x.readLine());
System.out.println("Enter a number");
int
n=Integer.parseInt(x.readLine());
System.out.println("\f");
System.out.println("A="+A.toString());
System.out.println("B="+B.toString());
C=A.plus(B);
System.out.print("A+B="+C.toString()+"\n");
C=A.minus(B);
System.out.print("A-B="+C.toString()+"\n");
C=A.times(B);
System.out.print("A*B="+C.toString()+"\n");
C=A.divideBy(B);
System.out.print("A/B="+C.toString()+"\n");
C=A.conjg();
System.out.print("Conjugate of
A="+C.toString()+"\n");
C=B.conjg();
System.out.print("Conjugate of
B="+C.toString()+"\n");
double
a=A.abs();
System.out.print("Absolute value of A="+a+"\n");
double
b=B.abs();
System.out.print("Absolute value of B="+b+"\n");
C=A.inv();
System.out.print("Reciprocal value of
A="+C.toString()+"\n");
C=B.inv();
System.out.print("Reciprocal value of
B="+C.toString()+"\n");
C=A.sqrt();
System.out.print("Square root of
A="+C.toString()+"\n");
C=B.sqrt();
System.out.print("Square root of
B="+C.toString()+"\n");
C=A.pow(n);
System.out.print("A^"+n+"="+C.toString()+"\n");
C=B.pow(n);
System.out.print("B^"+n+"="+C.toString()+"\n");
String
a1=A.polar();
System.out.print("Polar form of A="+a1+"\n");
String
b1=B.polar();
System.out.print("Polar
form of B="+b1+"\n");
}
}
No comments:
Post a Comment