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).
The complex exponential. The exponential function is a basic building block for solutions of ODEs. Complex numbers expand the scope of the exponential function, and bring trigonometric functions under its sway.
In exponential notation, a number usually is expressed as a coefficient between one and ten times an integral power of ten, the exponent. To express a number in exponential notation, write it in the form: c × 10n, where c is a number between 1 and 10 (e.g. 1, 2.5, 6.3, 9.8) and n is an integer (e.g. 1, -3, 6, -2).
Recall that the polar form of a complex number z = a + b i is: z = r ( c o s θ + i s i n θ ) So, by substituting Euler's formula to the polar form formula, we get the exponential form of a complex number: z = r e i θ , where is in radians.
Complex numbers have three primary forms: the general form, z=a+ib; the polar form, z=r(cosθ+isinθ); and the exponential form, z=rexp(iθ).
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With