commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Luc Maisonobe (JIRA)" <j...@apache.org>
Subject [jira] Commented: (MATH-236) nth-root of complex numbers
Date Fri, 26 Dec 2008 12:37:44 GMT

    [ https://issues.apache.org/jira/browse/MATH-236?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12659234#action_12659234
] 

Luc Maisonobe commented on MATH-236:
------------------------------------

This is an interesting feature, thanks.

Here are a few comments:

* there won't be any 1.3 version, next version will be 2.0
* could you provide unit tests with your patch ?
* couldn't the getPhi method be implemented simpler using Math.atan2(imaginary, real) ?

> 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.


Mime
View raw message