nth-root of complex numbers
---------------------------
Key: MATH-236
URL: https://issues.apache.org/jira/browse/MATH-236
Project: Commons Math
Issue Type: Improvement
Affects Versions: 1.3
Reporter: Bernhard Grünewaldt
Fix For: 1.3
Hello,
I would like to have a simple methods that gives back all nth roots of a
complex number.
Below is the JavaCode for it. I have tested it and it works.
What has to be done is the Exception Handling for NaN and Infinite etc.
You can send me instructions what to do, or you can do it yourself.
I really would like to contribute.
{code:title=org.apache.commons.math.complex.Complex.java|borderStyle=solid}
/**
* Compute the angle phi of this complex number.
* Depending on in which quadrant this complex number lies
* &pi or 2&pi is added. Where x=real and y=imaginary.
*
* Here is a short table for it:
* <pre>
* <code>
* +----------+-------------+------------------+------------------+
* | quadrant | I | II, III | IV |
* +----------+-------------+------------------+------------------+
* | phi | arctan(y/x) | arctan(y/x)+&pi | arctan(y/x)+2&pi |
* +----------+-------------+------------------+------------------+
* </code>
* </pre>
*
* @return the angle phi of this complex number
*/
public double getPhi() {
// the angle phi from arctan(y/x)
double phi = Math.atan(getImaginary()/getReal());
if (getReal() < 0) {
// II and III-quadrant => phi + pi
phi+=Math.PI;
} else if (getImaginary() <0){
// IV-quadrant => phi + 2 * pi
phi+=2*Math.PI;
}
return phi;
}
/**
* Compute the n-th root of this complex number.
* <p>
* For a given n it implements the formula: <pre>
* <code> z_k = pow( abs , 1.0/n ) * (cos(phi + k * 2&pi) + i * (sin(phi +
k * 2&pi)</code></pre></p>
* with <code>k=0,1,...,n-1</code> and <code>pow( abs , 1.0/n )</code> is
the nth root of the absolute-value.
* <p>
*
* @return all nth roots of this complex number as a Collection
*/
public Collection<Complex> nthRoot(int n) {
if (n <= 0) {
throw new IllegalArgumentException("The value for the nth root
has to be positive!");
}
Collection<Complex> result = new ArrayList<Complex>();
// nth root of abs
double nthRootOfAbs = Math.pow( abs() , 1.0/n );
// Compute nth roots of complex number
for (int k=0; k<n;k++) {
// inner part
double innerPart = (getPhi() +k*Math.PI) / n;
double realPart = nthRootOfAbs * Math.cos ( innerPart );
double imaginaryPart = nthRootOfAbs * Math.sin ( innerPart );
result.add(createComplex(realPart, imaginaryPart));
}
return result;
}
/**
* To String now returns human readable Form of this complex number
*
* @return returns a String of the form "real + i * imaginary"
*/
public String toString() {
return getReal() + " + i * " + getImaginary();
}
{code}
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.