I have the following problem:
I need to extend following class Complex (representing complex number) and add field with exponential form of it, additionaly overload methods from Complex:
class Complex{
public double re;
public double im;
public Complex(double r, double i){
re =r;
im = i;
}
public Complex add(Complex a,Complex b){
return new Complex(a.re + b.re,a.im + b.im );
}
public Complex sub(Complex a,Complex b){
return new Complex(a.re - b.re,a.im - b.im);
}
//more code
}
My class is like following
class ComplexE extends Complex{
public double exponential;
public ComplexE(double a, double b){
re=a;
im = b;
}
public ComplexE(double _exp){
super(0,0);
this.exponential = _exp;
}
public void setExpFromComplex(Complex comp){
double module = Math.sqrt(Math.pow(comp.re,2) + Math.pow(comp.im,2));
double fi = Math.atan2(comp.re,comp.im);
this.exponential = module * Math.pow(Math.E, fi);
}
public ComplexE add(ComplexE a,ComplexE b){
return new ComplexE(a.exponential + b.exponential );
}
}
My questions are: am I doing it right? Does my way of finding exponential form is correct?
Edit:
The math way i tried to use is:
z = |z|*(cos(phi) + i*sin(phi)) = |z|*e^(i*phi).
This doesn’t really make sense. The exponential form is simply an alternative way of expressing a complex number. For example
is the standard real and imaginary component representation that you use in your first class. The exponential form is
where
ris the modulus andthetais the argument whereand
Essentially this is just converting the cartesian coordinates to the equivalent polar representation.
You are storing a double in the second derived class called
exponentialwhich you are trying to calculate but really this value is meaningless. You are calculating the modulus asmodulecorrectly and the argument asfibut it appears you wantexponentialto bewhich is not really meaningful. If you want to store the values of modulus and argument that are correct for your values of
xandyas cartesian real and imaginary components that would make more sense but storing just a doubleexponentialis meaningless unless you have omitted something from the question.It seems that you would really be better off either just calculating the modulus and argument from your
reandimvariables and returning them from a function or storing them as additional member variables.