Reputation: 733
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).
Upvotes: 5
Views: 4654
Reputation: 21351
This doesn't really make sense. The exponential form is simply an alternative way of expressing a complex number. For example
z = x + iy
is the standard real and imaginary component representation that you use in your first class. The exponential form is
z = r * exp(i * theta)
where r
is the modulus and theta
is the argument where
r = sqrt(x*x + y*y)
and
z = r*( cos(theta) + i*sin(theta))
Essentially this is just converting the cartesian coordinates to the equivalent polar representation.
You are storing a double in the second derived class called exponential
which you are trying to calculate but really this value is meaningless. You are calculating the modulus as module
correctly and the argument as fi
but it appears you want exponential
to be
module*exp(fi)
which is not really meaningful. If you want to store the values of modulus and argument that are correct for your values of x
and y
as cartesian real and imaginary components that would make more sense but storing just a double exponential
is 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 re
and im
variables and returning them from a function or storing them as additional member variables.
Upvotes: 5
Reputation: 32333
You have a number of mistakes. First of all, I assume by exponential you mean polar coordinates, i.e. r*e^(i*theta)
. Except rectangular complex numbers and polar complex numbers are ISOMORPHIC, that is, you can convert one to the other and back again.
This suggests that you shouldn't have exponential complex numbers inherit from regular complex numbers... the difference is in the IMPLEMENTATION, i.e. how the numbers are stored in memory. In other words, your interface:
public interface IComplex {
public double getReal();
public double getImag();
public double getR();
public double getTheta();
public IComplex add(IComplex first, IComplex second);
public IComplex sub(IComplex first, IComplex second);
// Etc.
}
And the rectangular implementation:
public class RectComplex implements IComplex {
private double realPart; // note privacy... use getters and setters
private double imagPart;
public RealComplex (double real, double imag) {
realPart = real;
imagPart = imag;
}
public double getReal() {
return realPart;
}
public double getR() {
return Math.sqrt(realPart * realPart + imagPart * imagPart);
}
// Etc.
}
And the exponential implementation:
public class ExpComplex implements IComplex {
private double r;
private double theta;
public double getReal() {
return r * Math.cos(theta);
}
public double getR() {
return r;
}
// Etc.
}
This prevents redundant fields. Think about the difference between an ArrayList
and a LinkedList
. They're both lists, (the above are both Complex numbers), but they work totally differently from an implementation perspective. But any code that USES these classes wouldn't care, but they would choose the correct one based on performance, probably. Make sense?
Upvotes: 6