merge master and eb-test branches into commons-dev branch. I was mistakenly still making edits to eb-test branch.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/410abd7f Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/410abd7f Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/410abd7f Branch: refs/heads/master Commit: 410abd7fa4f63d8c2d297b62181ddfeeb44da869 Parents: e2668df ec66bce Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Sun Apr 23 19:42:53 2017 +0200 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Sun Apr 23 19:42:53 2017 +0200 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 244 +++++++++---------- .../commons/numbers/fraction/BigFraction.java | 16 +- .../numbers/fraction/ContinuedFraction.java | 166 +++++++++++++ .../numbers/fraction/BigFractionTest.java | 22 ++ .../numbers/fraction/ContinuedFractionTest.java | 45 ++++ pom.xml | 2 +- src/changes/changes.xml | 8 +- src/site/site.xml | 2 +- 8 files changed, 366 insertions(+), 139 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/410abd7f/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- diff --cc commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java index 42cd0cc,ccd25d1..1189451 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java @@@ -20,11 -20,9 +20,9 @@@ package org.apache.commons.numbers.comp import java.io.Serializable; import java.util.ArrayList; import java.util.List; - import org.apache.commons.numbers.core.Precision; - /** - * Representation of a Complex number, i.e., a number which has both a + * Representation of a Complex number, i.e. a number which has both a * real and imaginary part. * <p> * Implementations of arithmetic operations handle {@code NaN} and @@@ -96,56 -94,8 +94,56 @@@ public class Complex implements Seriali (Double.isInfinite(real) || Double.isInfinite(imaginary)); } + /** + * Creates a Complex from its polar representation. + * <p> + * If either {@code r} or {@code theta} is NaN, or {@code theta} is + * infinite, {@link Complex#NaN} is returned. + * <p> + * If {@code r} is infinite and {@code theta} is finite, infinite or NaN + * values may be returned in parts of the result, following the rules for + * double arithmetic. + * + * <pre> + * Examples: + * {@code + * polar2Complex(INFINITY, \(\pi\)) = INFINITY + INFINITY i + * polar2Complex(INFINITY, 0) = INFINITY + NaN i + * polar2Complex(INFINITY, \(-\frac{\pi}{4}\)) = INFINITY - INFINITY i + * polar2Complex(INFINITY, \(5\frac{\pi}{4}\)) = -INFINITY - INFINITY i } + * </pre> + * + * @param r the modulus of the complex number to create + * @param theta the argument of the complex number to create + * @return {@code Complex} + * @since 1.1 + */ + public Complex polar(double r, double theta) { + checkNotNegative(r); + return new Complex(r * Math.cos(theta), r * Math.sin(theta)); + } + /** - * Returns projection of this Complex number onto the Riemann sphere, ++ * Returns projection of this complex number onto the Riemann sphere, + * i.e. all infinities (including those with an NaN component) + * project onto real infinity, as described in the + * <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/cproj.html"> + * IEEE and ISO C standards</a>. + * <p> + * + * + * @return {@code Complex} projected onto the Riemann sphere. + */ + public Complex proj() { + if (isInfinite) { + return new Complex(Double.POSITIVE_INFINITY); + } else { + return this; + } + } + + /** - * Return the absolute value of this Complex number. + * Return the absolute value of this complex number. * Returns {@code NaN} if either real or imaginary part is {@code NaN} * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN}, * but at least one part is infinite. @@@ -174,19 -125,6 +173,19 @@@ } } + /** - * Return the norm of this Complex number, defined as the square of the magnitude ++ * Return the norm of this complex number, defined as the square of the magnitude + * (Matches C++ 11 standards.) + * Returns {@code NaN} if either real or imaginary part is {@code NaN} + * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN}, + * but at least one part is infinite. + * + * @return the absolute value. + */ + public double norm() { + return abs()*abs(); + } + /** * Returns a {@code Complex} whose value is * {@code (this + addend)}. @@@ -234,14 -171,14 +233,14 @@@ * The conjugate of {@code a + bi} is {@code a - bi}. * <p> * {@link #NaN} is returned if either the real or imaginary -- * part of this Complex number equals {@code Double.NaN}. ++ * part of this complex number equals {@code Double.NaN}. * </p><p> * If the imaginary part is infinite, and the real part is not * {@code NaN}, the returned value has infinite imaginary part * of the opposite sign, e.g. the conjugate of * {@code 1 + POSITIVE_INFINITY i} is {@code 1 - NEGATIVE_INFINITY i}. * </p> -- * @return the conjugate of this Complex object. ++ * @return the conjugate of this complex object. */ public Complex conjugate() { if (isNaN) { @@@ -251,17 -188,6 +250,17 @@@ return createComplex(real, -imaginary); } + /** - * Returns the conjugate of this Complex number. ++ * Returns the conjugate of this complex number. + * C++11 grammar. + * </p> - * @return the conjugate of this Complex object. ++ * @return the conjugate of this complex object. + */ + public Complex conj() { + return conjugate(); + } + + /** * Returns a {@code Complex} whose value is * {@code (this / divisor)}. @@@ -302,10 -228,8 +301,9 @@@ * * @param divisor Value by which this {@code Complex} is to be divided. * @return {@code this / divisor}. + * @if {@code divisor} is {@code null}. */ - public Complex divide(Complex divisor) - { + public Complex divide(Complex divisor) { checkNotNull(divisor); if (isNaN || divisor.isNaN) { return NaN; @@@ -547,20 -457,11 +545,20 @@@ return real; } - /** + /** + * Access the real part (C++ grammar) + * + * @return the real part. + */ + public double real() { + return real; + } + + /** - * Checks whether either or both parts of this Complex number is + * Checks whether either or both parts of this complex number is * {@code NaN}. * - * @return true if either or both parts of this Complex number is + * @return true if either or both parts of this complex number is * {@code NaN}; false otherwise. */ public boolean isNaN() { @@@ -600,10 -501,8 +598,9 @@@ * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. + * @if {@code factor} is {@code null}. */ - public Complex multiply(Complex factor) - { + public Complex multiply(Complex factor) { checkNotNull(factor); if (isNaN || factor.isNaN) { return NaN; @@@ -662,7 -561,7 +659,7 @@@ /** * Returns a {@code Complex} whose value is {@code (-this)}. * Returns {@code NaN} if either real or imaginary -- * part of this Complex number is {@code Double.NaN}. ++ * part of this complex number is {@code Double.NaN}. * * @return {@code -this}. */ @@@ -785,86 -679,8 +777,86 @@@ /** * Compute the + * <a href="http://mathworld.wolfram.com/InverseHyperbolicSine.html" TARGET="_top"> - * inverse hyperbolic sine</a> of this Complex number. ++ * inverse hyperbolic sine</a> of this complex number. + * Implements the formula: + * <p> + * {@code asinh(z) = log(z+sqrt(z^2+1))} + * </p><p> + * Returns {@link Complex#NaN} if either real or imaginary part of the + * input argument is {@code NaN} or infinite.</p> + * - * @return the inverse hyperbolic cosine of this Complex number ++ * @return the inverse hyperbolic cosine of this complex number + * @since 1.2 + */ + public Complex asinh(){ + if (isNaN) { + return NaN; + } + + return square().add(Complex.ONE).sqrt().add(this).log(); + } + + /** + * Compute the + * <a href="http://mathworld.wolfram.com/InverseHyperbolicTangent.html" TARGET="_top"> - * inverse hyperbolic tangent</a> of this Complex number. ++ * inverse hyperbolic tangent</a> of this complex number. + * Implements the formula: + * <p> + * {@code atanh(z) = log((1+z)/(1-z))/2} + * </p><p> + * Returns {@link Complex#NaN} if either real or imaginary part of the + * input argument is {@code NaN} or infinite.</p> + * - * @return the inverse hyperbolic cosine of this Complex number ++ * @return the inverse hyperbolic cosine of this complex number + * @since 1.2 + */ + public Complex atanh(){ + if (isNaN) { + return NaN; + } + + return this.add(Complex.ONE).divide(Complex.ONE.subtract(this)).log().divide(new Complex(2)); + } + /** + * Compute the + * <a href="http://mathworld.wolfram.com/InverseHyperbolicCosine.html" TARGET="_top"> - * inverse hyperbolic cosine</a> of this Complex number. ++ * inverse hyperbolic cosine</a> of this complex number. + * Implements the formula: + * <p> + * {@code acosh(z) = log(z+sqrt(z^2-1))} + * </p><p> + * Returns {@link Complex#NaN} if either real or imaginary part of the + * input argument is {@code NaN} or infinite.</p> + * - * @return the inverse hyperbolic cosine of this Complex number ++ * @return the inverse hyperbolic cosine of this complex number + * @since 1.2 + */ + public Complex acosh() { + if (isNaN) { + return NaN; + } + + return square().subtract(Complex.ONE).sqrt().add(this).log(); + } + + /** - * Compute the square of this Complex number. ++ * Compute the square of this complex number. + * - * @return square of this Complex number ++ * @return square of this complex number + */ + public Complex square(){ + if (isNaN) { + return NaN; + } + + return this.multiply(this); + } + + /** + * Compute the * <a href="http://mathworld.wolfram.com/Cosine.html" TARGET="_top"> - * cosine</a> of this Complex number. + * cosine</a> of this complex number. * Implements the formula: * <p> * {@code cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)i} @@@ -1025,19 -838,7 +1014,19 @@@ } /** + * Compute the base 10 or + * <a href="http://mathworld.wolfram.com/CommonLogarithm.html" TARGET="_top"> - * common logarithm</a> of this Complex number. ++ * common logarithm</a> of this complex number. + * + * @return the base 10 logarithm of <code>this</code>. + */ + public Complex log10() { + return createComplex(Math.log(abs())/Math.log(10), + Math.atan2(imaginary, real)); + } + + /** - * Returns of value of this Complex number raised to the power of {@code x}. + * Returns of value of this complex number raised to the power of {@code x}. * Implements the formula: * <pre> * <code> @@@ -1328,8 -1138,10 +1326,8 @@@ Math.sin(imaginary2) / d); } - - -- /** - * Compute the argument of this Complex number. ++ /** + * Compute the argument of this complex number. * The argument is the angle phi between the positive real axis and * the point representing this number in the complex plane. * The value returned is between -PI (not inclusive) @@@ -1350,28 -1162,7 +1348,17 @@@ } /** - * Compute the argument of this Complex number. - * The argument is the angle phi between the positive real axis and - * the point representing this number in the complex plane. - * The value returned is between -PI (not inclusive) - * and PI (inclusive), with negative values returned for numbers with - * negative imaginary parts. - * <p> - * If either real or imaginary part (or both) is NaN, NaN is returned. - * Infinite parts are handled as {@code Math.atan2} handles them, - * essentially treating finite parts as zero in the presence of an - * infinite coordinate and returning a multiple of pi/4 depending on - * the signs of the infinite parts. - * See the javadoc for {@code Math.atan2} for full details. ++ * Compute the argument of this complex number. ++ * C++11 syntax + * + * @return the argument of {@code this}. + */ + public double arg() { + return getArgument(); + } + + /** - * Computes the n-th roots of this Complex number. + * Computes the n-th roots of this complex number. * The nth roots are defined by the formula: * <pre> * <code> @@@ -1497,27 -1286,16 +1482,28 @@@ } } + + /** + * Check that the argument is positive and throw a RuntimeException + * if it is not. + * @param arg {@code int} to check + */ + private static void checkNotNegative(int arg) { + if (arg <= 0) { + throw new RuntimeException("Complex: Non-positive argument"); + } + } + /** - * Check that the Complex is not null and throw a RuntimeException - * if it is. - * @param arg the Complex to check + * Returns {@code true} if the values are equal according to semantics of + * {@link Double#equals(Object)}. + * + * @param x Value + * @param y Value + * @return {@code new Double(x).equals(new Double(y))} */ - private static void checkNotNull(Complex arg) { - if (arg == null) { - throw new RuntimeException("Complex: Null argument"); - } + private static boolean equals(double x, double y) { + return new Double(x).equals(new Double(y)); } /**