Exponential form of complex number
java, math
Solution
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.
Problem
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). ```