This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/master by this push: new 9b3c706 Use identities for tanh() with real / imaginary parts of zero. 9b3c706 is described below commit 9b3c706a83cf7d46589af344e14ccdaa0d4cd8b8 Author: aherbert <aherb...@apache.org> AuthorDate: Tue Dec 10 16:51:12 2019 +0000 Use identities for tanh() with real / imaginary parts of zero. real only the formula reduces to: sinh x / (1 + cosh x) = tanh(x/2) imaginary only the formula reduces to: sin x / (1 + cos x) = tan(x/2) --- .../main/java/org/apache/commons/numbers/complex/Complex.java | 11 ++++++++++- .../org/apache/commons/numbers/complex/CReferenceTest.java | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) 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 f6f4681..c9cd5ea 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 @@ -1816,13 +1816,22 @@ public final class Complex implements Serializable { */ private static Complex tanh(double real, double imaginary, ComplexConstructor constructor) { // Math.cos and Math.sin return NaN for infinity. - // Perform edge-condition checks on the twice the imaginary value. + // Perform edge-condition checks on twice the imaginary value. // This handles very big imaginary numbers as infinite. final double imaginary2 = 2 * imaginary; if (Double.isFinite(real)) { if (Double.isFinite(imaginary2)) { + if (real == 0) { + // Identity: sin x / (1 + cos x) = tan(x/2) + return constructor.create(real, Math.tan(imaginary)); + } + if (imaginary == 0) { + // Identity: sinh x / (1 + cosh x) = tanh(x/2) + return constructor.create(Math.tanh(real), imaginary); + } + final double real2 = 2 * real; // Math.cosh returns positive infinity for infinity. diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java index caddc73..e11a4e3 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java @@ -302,7 +302,7 @@ public class CReferenceTest { @Test public void testTanh() { // Odd function: negative real cases defined by positive real cases - assertOperation("tanh", Complex::tanh, 34); + assertOperation("tanh", Complex::tanh, 3); } @Test