NUMBERS-13: cosh() passes all tests
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/c8db04d0 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/c8db04d0 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/c8db04d0 Branch: refs/heads/master Commit: c8db04d0ac1001774b97fb7abe3f198a30500335 Parents: 1de701b Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Wed Jul 19 15:50:52 2017 +0200 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Wed Jul 19 15:50:52 2017 +0200 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 14 + .../commons/numbers/complex/CStandardTest.java | 282 +++++++++++++++++++ 2 files changed, 296 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/c8db04d0/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- diff --git 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 index e4c0a71..4eae524 100644 --- 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 @@ -808,6 +808,20 @@ in the * @return the hyperbolic cosine of this complex number. */ public Complex cosh() { + if (real == 0 && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Double.NaN, 0); + } else if (real == 0 && Double.isNaN(imaginary)) { + return new Complex(Double.NaN, 0); + } else if (real == Double.POSITIVE_INFINITY && imaginary == 0) { + return new Complex(Double.POSITIVE_INFINITY, 0); + } else if (real == Double.POSITIVE_INFINITY && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Double.POSITIVE_INFINITY, Double.NaN); + } else if (real == Double.POSITIVE_INFINITY && Double.isNaN(imaginary)) { + return new Complex(Double.POSITIVE_INFINITY, Double.NaN); + } else if (Double.isNaN(real) && imaginary == 0) { + return new Complex(Double.NaN, 0); + } + return createComplex(Math.cosh(real) * Math.cos(imaginary), Math.sinh(real) * Math.sin(imaginary)); } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/c8db04d0/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java new file mode 100644 index 0000000..9c178f2 --- /dev/null +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.numbers.complex; + +import org.apache.commons.numbers.complex.Complex; +import org.apache.commons.numbers.complex.ComplexUtils; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +public class CStandardTest { + + private double inf = Double.POSITIVE_INFINITY; + private double negInf = Double.NEGATIVE_INFINITY; + private double nan = Double.NaN; + private double pi = Math.PI; + private double piOverFour = Math.PI / 4.0; + private double piOverTwo = Math.PI / 2.0; + private double threePiOverFour = 3.0*Math.PI/4.0; + private Complex oneOne = new Complex(1, 1); + private Complex oneZero = new Complex(1, 0); + private Complex oneInf = new Complex(1, inf); + private Complex oneNegInf = new Complex(1, negInf); + private Complex oneNaN = new Complex(1, nan); + private Complex zeroInf = new Complex(0, inf); + private Complex zeroNegInf = new Complex(0,negInf); + private Complex zeroNaN = new Complex(0, nan); + private Complex zeroPiTwo = new Complex(0.0, piOverTwo); + private Complex negZeroZero = new Complex(-0.0, 0); + private Complex negZeroNan = new Complex(-0.0, nan); + private Complex negI = new Complex(0.0, -1.0); + private Complex infOne = new Complex(inf, 1); + private Complex infZero = new Complex(inf, 0); + private Complex infNaN = new Complex(inf, nan); + private Complex infNegInf = new Complex(inf, negInf); + private Complex infInf = new Complex(inf, inf); + private Complex infPiTwo = new Complex(inf, piOverTwo); + private Complex infPiFour = new Complex(inf, piOverFour); + private Complex infPi = new Complex(inf, Math.PI); + private Complex negInfInf = new Complex(negInf, inf); + private Complex negInfZero = new Complex(negInf, 0); + private Complex negInfOne = new Complex(negInf, 1); + private Complex negInfNaN = new Complex(negInf, nan); + private Complex negInfNegInf = new Complex(negInf, negInf); + private Complex negInfPosInf = new Complex(negInf, inf); + private Complex negInfPi = new Complex(negInf, Math.PI); + private Complex nanInf = new Complex(nan, inf); + private Complex nanNegInf = new Complex(nan, negInf); + private Complex nanZero = new Complex(nan, 0); + private Complex nanOne = new Complex(nan, 1); + private Complex piTwoNaN = new Complex(piOverTwo, nan); + private Complex piNegInf = new Complex(Math.PI, negInf); + private Complex piTwoNegInf = new Complex(piOverTwo, negInf); + private Complex piTwoNegZero = new Complex(piOverTwo, -0.0); + private Complex threePiFourNegInf = new Complex(threePiOverFour,negInf); + private Complex piFourNegInf = new Complex(piOverFour, negInf); + + public void assertComplex(Complex c1, Complex c2, double realTol, double imagTol) { + Assert.assertEquals(c1.getReal(), c2.getReal(), realTol); + Assert.assertEquals(c1.getImaginary(), c2.getImaginary(), imagTol); + } + + public void assertComplex(Complex c1, Complex c2) { + Assert.assertEquals(c1.getReal(), c2.getReal(),0.0); + Assert.assertEquals(c1.getImaginary(), c2.getImaginary(), 0.0); + } + + + /** + * ISO C Standard G.6.3 + */ + @Test + public void testSqrt1() { + Complex z1 = new Complex(-2.0, 0.0); + Complex z2 = new Complex(0.0, Math.sqrt(2)); + assertComplex(z1.sqrt(), z2); + z1 = new Complex(-2.0, -0.0); + z2 = new Complex(0.0, -Math.sqrt(2)); + assertComplex(z1.sqrt(), z2); + } + + @Test + public void testImplicitTrig() { + Complex z1 = new Complex(3.0); + Complex z2 = new Complex(0.0, 3.0); + assertComplex(z1.asin(), negI.multiply(z2.asinh())); + assertComplex(z1.atan(), negI.multiply(z2.atanh()), Math.ulp(1), Math.ulp(1)); + assertComplex(z1.cos(), z2.cosh()); + assertComplex(z1.sin(), negI.multiply(z2.sinh())); + assertComplex(z1.tan(), negI.multiply(z1.tanh())); + } + + /** + * ISO C Standard G.6.1.1 + */ + @Test + public void testAcos() { + assertComplex(oneOne.acos().conj(), oneOne.conj().acos(), Math.ulp(1), Math.ulp(1)); + assertComplex(Complex.ZERO.acos(), piTwoNegZero); + assertComplex(negZeroZero.acos(), piTwoNegZero); + assertComplex(zeroNaN.acos(), piTwoNaN); + assertComplex(oneInf.acos(), piTwoNegInf); + assertComplex(oneNaN.acos(), Complex.NaN); + assertComplex(negInfOne.acos(), piNegInf); + assertComplex(infOne.acos(), zeroNegInf); + assertComplex(negInfPosInf.acos(), threePiFourNegInf); + assertComplex(infInf.acos(), piFourNegInf); + assertComplex(infNaN.acos(), nanInf); + assertComplex(negInfNaN.acos(), nanNegInf); + assertComplex(nanOne.acos(), Complex.NaN); + assertComplex(nanInf.acos(), nanNegInf); + assertComplex(Complex.NaN.acos(), Complex.NaN); + } + + /** + * ISO C Standard G.6.2.2 + */ + @Test + public void testAsinh() { + // TODO: test for which Asinh is odd + assertComplex(oneOne.conj().asinh(), oneOne.asinh().conj()); + assertComplex(Complex.ZERO.asinh(), Complex.ZERO); + assertComplex(oneInf.asinh(), infPiTwo); + assertComplex(oneNaN.asinh(), Complex.NaN); + assertComplex(infOne.asinh(), infZero); + assertComplex(infInf.asinh(), infPiFour); + assertComplex(infNaN.asinh(), infNaN); + assertComplex(nanZero.asinh(), nanZero); + assertComplex(nanOne.asinh(), Complex.NaN); + assertComplex(nanInf.asinh(), infNaN); + assertComplex(Complex.NaN, Complex.NaN); + } + + /** + * ISO C Standard G.6.2.3 + */ + @Test + public void testAtanh() { + assertComplex(oneOne.conj().atanh(), oneOne.atanh().conj()); + assertComplex(Complex.ZERO.atanh(), Complex.ZERO); + assertComplex(zeroNaN.atanh(), zeroNaN); + assertComplex(oneZero.atanh(), infZero); + assertComplex(oneInf.atanh(),zeroPiTwo); + assertComplex(oneNaN.atanh(), Complex.NaN); + assertComplex(infOne.atanh(), zeroPiTwo); + assertComplex(infInf.atanh(), zeroPiTwo); + assertComplex(infNaN.atanh(), zeroNaN); + assertComplex(nanOne.atanh(), Complex.NaN); + assertComplex(nanInf.atanh(), zeroPiTwo); + assertComplex(Complex.NaN.atanh(), Complex.NaN); + } + + /** + * ISO C Standard G.6.2.4 + */ + @Test + public void testCosh() { + assertComplex(oneOne.cosh().conj(), oneOne.conj().cosh()); + assertComplex(Complex.ZERO.cosh(), Complex.ONE); + assertComplex(zeroInf.cosh(), nanZero); + assertComplex(zeroNaN.cosh(), nanZero); + assertComplex(oneInf.cosh(), Complex.NaN); + assertComplex(oneNaN.cosh(), Complex.NaN); + assertComplex(infZero.cosh(), infZero); + // the next test does not appear to make sense: + // (inf + iy) = inf + cis(y) + // skipped + assertComplex(infInf.cosh(), infNaN); + assertComplex(infNaN.cosh(), infNaN); + assertComplex(nanZero.cosh(), nanZero); + assertComplex(nanOne.cosh(), Complex.NaN); + assertComplex(Complex.NaN.cosh(), Complex.NaN); + } + + /** + * ISO C Standard G.6.2.5 + */ + @Test + public void testSinh() { + assertComplex(oneOne.sinh().conj(), oneOne.conj().sinh()); // AND CSINH IS ODD + assertComplex(Complex.ZERO.sinh(), Complex.ZERO); + assertComplex(zeroInf.sinh(), zeroNaN); + assertComplex(zeroNaN.sinh(), zeroNaN); + assertComplex(oneInf.sinh(), Complex.NaN); + assertComplex(oneNaN.sinh(), Complex.NaN); + assertComplex(infZero.sinh(), infZero); + // skipped test similar to previous section + assertComplex(infInf.sinh(), infNaN); + assertComplex(infNaN.sinh(), infNaN); + assertComplex(nanZero.sinh(), nanZero); + assertComplex(nanOne.sinh(), Complex.NaN); + assertComplex(Complex.NaN.sinh(), Complex.NaN); + } + + /** + * ISO C Standard G.6.2.6 + */ + @Test + public void testTanh() { + assertComplex(oneOne.tanh().conj(), oneOne.conj().tanh()); // AND CSINH IS ODD + assertComplex(Complex.ZERO.tanh(), Complex.ZERO); + assertComplex(oneInf.tanh(), Complex.NaN); + assertComplex(oneNaN.tanh(), Complex.NaN); + //Do Not Understand the Next Test + assertComplex(infInf.tanh(), oneZero); + assertComplex(infNaN.tanh(), oneZero); + assertComplex(nanZero.tanh(), nanZero); + assertComplex(nanOne.tanh(), Complex.NaN); + assertComplex(Complex.NaN.tanh(), Complex.NaN); + } + + /** + * ISO C Standard G.6.3.1 + */ + @Test + public void testExp() { + assertComplex(oneOne.conj().exp(), oneOne.exp().conj()); + assertComplex(Complex.ZERO.exp(), oneZero); + assertComplex(negZeroZero.exp(), oneZero); + assertComplex(oneInf.exp(), Complex.NaN); + assertComplex(oneNaN.exp(), Complex.NaN); + assertComplex(infZero.exp(), infZero); + // Do not understand next test + assertComplex(negInfInf.exp(), Complex.ZERO); + assertComplex(infInf.exp(), infNaN); + assertComplex(negInfNaN.exp(), Complex.ZERO); + assertComplex(infNaN.exp(), infNaN); + assertComplex(nanZero.exp(), nanZero); + assertComplex(nanOne.exp(), Complex.NaN); + assertComplex(Complex.NaN.exp(), Complex.NaN); + } + + /** + * ISO C Standard G.6.3.2 + */ + @Test + public void testLog() { + assertComplex(oneOne.log().conj(), oneOne.conj().log()); + assertComplex(negZeroZero.log(), negInfPi); + assertComplex(Complex.ZERO.log(), negInfZero); + assertComplex(oneInf.log(), infPiTwo); + assertComplex(oneNaN.log(), Complex.NaN); + assertComplex(negInfOne.log(), infPi); + assertComplex(infOne.log(), infZero); + assertComplex(infInf.log(), infPiFour); + assertComplex(infNaN.log(), infNaN); + assertComplex(nanOne.log(), Complex.NaN); + assertComplex(nanInf.log(), infNaN); + assertComplex(Complex.NaN.log(), Complex.NaN); + } + + /** + * ISO C Standard G.6.4.2 + */ + @Test + public void testSqrt2() { + assertComplex(oneOne.sqrt().conj(), oneOne.conj().sqrt()); + assertComplex(Complex.ZERO.sqrt(), Complex.ZERO); + assertComplex(oneInf.sqrt(), infInf); + assertComplex(negInfOne.sqrt(), zeroNaN); + assertComplex(infOne.sqrt(), infZero); + assertComplex(negInfNaN.sqrt(), nanInf); + assertComplex(infNaN.sqrt(), infNaN); + assertComplex(nanOne.sqrt(), Complex.NaN); + assertComplex(Complex.NaN.sqrt(), Complex.NaN); + } +}