You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/02/28 02:45:50 UTC
[1/5] [math] MATH-1416: Depend on "Commons Numbers".
Repository: commons-math
Updated Branches:
refs/heads/master c965f1c7f -> 583d9ec86
MATH-1416: Depend on "Commons Numbers".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0f25f11c
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0f25f11c
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0f25f11c
Branch: refs/heads/master
Commit: 0f25f11ce63fe6d6096a13bb2ac00b8c3f98ef62
Parents: c965f1c
Author: Gilles <er...@apache.org>
Authored: Wed Feb 28 02:07:37 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Feb 28 02:07:37 2018 +0100
----------------------------------------------------------------------
pom.xml | 6 ++++++
.../org/apache/commons/math4/linear/EigenDecomposition.java | 4 ++--
2 files changed, 8 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0f25f11c/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 1b9b97f..2e6bae8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -379,6 +379,12 @@
<dependency>
<groupId>org.apache.commons</groupId>
+ <artifactId>commons-numbers-complex</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.commons</groupId>
<artifactId>commons-numbers-gamma</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0f25f11c/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java b/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java
index 75e9614..53cbf8a 100644
--- a/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java
+++ b/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java
@@ -17,14 +17,14 @@
package org.apache.commons.math4.linear;
-import org.apache.commons.math4.complex.Complex;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.Precision;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
import org.apache.commons.math4.exception.MaxCountExceededException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.math4.util.FastMath;
-import org.apache.commons.numbers.core.Precision;
/**
* Calculates the eigen decomposition of a real matrix.
[4/5] [math] MATH-1416: Depend on "Commons Numbers".
Posted by er...@apache.org.
MATH-1416: Depend on "Commons Numbers".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/35378d9c
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/35378d9c
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/35378d9c
Branch: refs/heads/master
Commit: 35378d9c4a3a656b881e77fb405fabd22f7803eb
Parents: 56b28f3
Author: Gilles <er...@apache.org>
Authored: Wed Feb 28 03:32:19 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Feb 28 03:32:19 2018 +0100
----------------------------------------------------------------------
.../commons/math4/complex/ComplexFormat.java | 1 +
.../commons/math4/complex/ComplexUtils.java | 1 +
.../org/apache/commons/math4/TestUtils.java | 4 +-
.../complex/ComplexFormatAbstractTest.java | 8 +-
.../commons/math4/complex/ComplexTest.java | 1491 ------------------
.../commons/math4/complex/ComplexUtilsTest.java | 17 +-
6 files changed, 15 insertions(+), 1507 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/main/java/org/apache/commons/math4/complex/ComplexFormat.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/complex/ComplexFormat.java b/src/main/java/org/apache/commons/math4/complex/ComplexFormat.java
index f1ca864..87b6edd 100644
--- a/src/main/java/org/apache/commons/math4/complex/ComplexFormat.java
+++ b/src/main/java/org/apache/commons/math4/complex/ComplexFormat.java
@@ -22,6 +22,7 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.MathParseException;
import org.apache.commons.math4.exception.NoDataException;
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/main/java/org/apache/commons/math4/complex/ComplexUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/complex/ComplexUtils.java b/src/main/java/org/apache/commons/math4/complex/ComplexUtils.java
index a95c099..13410bd 100644
--- a/src/main/java/org/apache/commons/math4/complex/ComplexUtils.java
+++ b/src/main/java/org/apache/commons/math4/complex/ComplexUtils.java
@@ -17,6 +17,7 @@
package org.apache.commons.math4.complex;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/test/java/org/apache/commons/math4/TestUtils.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/TestUtils.java b/src/test/java/org/apache/commons/math4/TestUtils.java
index 992e0d8..75fa790 100644
--- a/src/test/java/org/apache/commons/math4/TestUtils.java
+++ b/src/test/java/org/apache/commons/math4/TestUtils.java
@@ -23,8 +23,9 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.Precision;
import org.apache.commons.math4.FieldElement;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.complex.ComplexFormat;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.linear.FieldMatrix;
@@ -32,7 +33,6 @@ import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.linear.RealVector;
import org.apache.commons.math4.stat.inference.ChiSquareTest;
import org.apache.commons.math4.util.FastMath;
-import org.apache.commons.numbers.core.Precision;
import org.junit.Assert;
/**
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/test/java/org/apache/commons/math4/complex/ComplexFormatAbstractTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/complex/ComplexFormatAbstractTest.java b/src/test/java/org/apache/commons/math4/complex/ComplexFormatAbstractTest.java
index dd53848..e74da21 100644
--- a/src/test/java/org/apache/commons/math4/complex/ComplexFormatAbstractTest.java
+++ b/src/test/java/org/apache/commons/math4/complex/ComplexFormatAbstractTest.java
@@ -22,14 +22,12 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Locale;
-
+import org.junit.Test;
+import org.junit.Assert;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NoDataException;
import org.apache.commons.math4.exception.NullArgumentException;
-import org.junit.Test;
-import org.junit.Assert;
-import org.apache.commons.math4.complex.Complex;
-import org.apache.commons.math4.complex.ComplexFormat;
import org.apache.commons.math4.util.FastMath;
public abstract class ComplexFormatAbstractTest {
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/test/java/org/apache/commons/math4/complex/ComplexTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/complex/ComplexTest.java b/src/test/java/org/apache/commons/math4/complex/ComplexTest.java
deleted file mode 100644
index 9032692..0000000
--- a/src/test/java/org/apache/commons/math4/complex/ComplexTest.java
+++ /dev/null
@@ -1,1491 +0,0 @@
-/*
- * 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.math4.complex;
-
-import java.util.List;
-
-import org.apache.commons.math4.TestUtils;
-import org.apache.commons.math4.complex.Complex;
-import org.apache.commons.math4.complex.ComplexUtils;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Ignore;
-import org.junit.Test;
-
-
-/**
- */
-public class ComplexTest {
-
-
- private double inf = Double.POSITIVE_INFINITY;
- private double neginf = Double.NEGATIVE_INFINITY;
- private double nan = Double.NaN;
- private double pi = FastMath.PI;
- private Complex oneInf = new Complex(1, inf);
- private Complex oneNegInf = new Complex(1, neginf);
- 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 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 oneNaN = new Complex(1, nan);
- private Complex zeroInf = new Complex(0, inf);
- private Complex zeroNaN = new Complex(0, nan);
- private Complex nanInf = new Complex(nan, inf);
- private Complex nanNegInf = new Complex(nan, neginf);
- private Complex nanZero = new Complex(nan, 0);
-
- @Test
- public void testConstructor() {
- Complex z = new Complex(3.0, 4.0);
- Assert.assertEquals(3.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(4.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testConstructorNaN() {
- Complex z = new Complex(3.0, Double.NaN);
- Assert.assertTrue(z.isNaN());
-
- z = new Complex(nan, 4.0);
- Assert.assertTrue(z.isNaN());
-
- z = new Complex(3.0, 4.0);
- Assert.assertFalse(z.isNaN());
- }
-
- @Test
- public void testAbs() {
- Complex z = new Complex(3.0, 4.0);
- Assert.assertEquals(5.0, z.abs(), 1.0e-5);
- }
-
- @Test
- public void testAbsNaN() {
- Assert.assertTrue(Double.isNaN(Complex.NaN.abs()));
- Complex z = new Complex(inf, nan);
- Assert.assertTrue(Double.isNaN(z.abs()));
- }
-
- @Test
- public void testAbsInfinite() {
- Complex z = new Complex(inf, 0);
- Assert.assertEquals(inf, z.abs(), 0);
- z = new Complex(0, neginf);
- Assert.assertEquals(inf, z.abs(), 0);
- z = new Complex(inf, neginf);
- Assert.assertEquals(inf, z.abs(), 0);
- }
-
- @Test
- public void testAdd() {
- Complex x = new Complex(3.0, 4.0);
- Complex y = new Complex(5.0, 6.0);
- Complex z = x.add(y);
- Assert.assertEquals(8.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(10.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testAddNaN() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.add(Complex.NaN);
- Assert.assertSame(Complex.NaN, z);
- z = new Complex(1, nan);
- Complex w = x.add(z);
- Assert.assertSame(Complex.NaN, w);
- }
-
- @Test
- public void testAddInf() {
- Complex x = new Complex(1, 1);
- Complex z = new Complex(inf, 0);
- Complex w = x.add(z);
- Assert.assertEquals(w.getImaginary(), 1, 0);
- Assert.assertEquals(inf, w.getReal(), 0);
-
- x = new Complex(neginf, 0);
- Assert.assertTrue(Double.isNaN(x.add(z).getReal()));
- }
-
-
- @Test
- public void testScalarAdd() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = 2.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.add(yComplex), x.add(yDouble));
- }
-
- @Test
- public void testScalarAddNaN() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = Double.NaN;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.add(yComplex), x.add(yDouble));
- }
-
- @Test
- public void testScalarAddInf() {
- Complex x = new Complex(1, 1);
- double yDouble = Double.POSITIVE_INFINITY;
-
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.add(yComplex), x.add(yDouble));
-
- x = new Complex(neginf, 0);
- Assert.assertEquals(x.add(yComplex), x.add(yDouble));
- }
-
- @Test
- public void testConjugate() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.conjugate();
- Assert.assertEquals(3.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testConjugateNaN() {
- Complex z = Complex.NaN.conjugate();
- Assert.assertTrue(z.isNaN());
- }
-
- @Test
- public void testConjugateInfiinite() {
- Complex z = new Complex(0, inf);
- Assert.assertEquals(neginf, z.conjugate().getImaginary(), 0);
- z = new Complex(0, neginf);
- Assert.assertEquals(inf, z.conjugate().getImaginary(), 0);
- }
-
- @Test
- public void testDivide() {
- Complex x = new Complex(3.0, 4.0);
- Complex y = new Complex(5.0, 6.0);
- Complex z = x.divide(y);
- Assert.assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testDivideReal() {
- Complex x = new Complex(2d, 3d);
- Complex y = new Complex(2d, 0d);
- Assert.assertEquals(new Complex(1d, 1.5), x.divide(y));
-
- }
-
- @Test
- public void testDivideImaginary() {
- Complex x = new Complex(2d, 3d);
- Complex y = new Complex(0d, 2d);
- Assert.assertEquals(new Complex(1.5d, -1d), x.divide(y));
- }
-
- @Test
- public void testDivideInf() {
- Complex x = new Complex(3, 4);
- Complex w = new Complex(neginf, inf);
- Assert.assertTrue(x.divide(w).equals(Complex.ZERO));
-
- Complex z = w.divide(x);
- Assert.assertTrue(Double.isNaN(z.getReal()));
- Assert.assertEquals(inf, z.getImaginary(), 0);
-
- w = new Complex(inf, inf);
- z = w.divide(x);
- Assert.assertTrue(Double.isNaN(z.getImaginary()));
- Assert.assertEquals(inf, z.getReal(), 0);
-
- w = new Complex(1, inf);
- z = w.divide(w);
- Assert.assertTrue(Double.isNaN(z.getReal()));
- Assert.assertTrue(Double.isNaN(z.getImaginary()));
- }
-
- @Test
- public void testDivideZero() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.divide(Complex.ZERO);
- // Assert.assertEquals(z, Complex.INF); // See MATH-657
- Assert.assertEquals(z, Complex.NaN);
- }
-
- @Test
- public void testDivideZeroZero() {
- Complex x = new Complex(0.0, 0.0);
- Complex z = x.divide(Complex.ZERO);
- Assert.assertEquals(z, Complex.NaN);
- }
-
- @Test
- public void testDivideNaN() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.divide(Complex.NaN);
- Assert.assertTrue(z.isNaN());
- }
-
- @Test
- public void testDivideNaNInf() {
- Complex z = oneInf.divide(Complex.ONE);
- Assert.assertTrue(Double.isNaN(z.getReal()));
- Assert.assertEquals(inf, z.getImaginary(), 0);
-
- z = negInfNegInf.divide(oneNaN);
- Assert.assertTrue(Double.isNaN(z.getReal()));
- Assert.assertTrue(Double.isNaN(z.getImaginary()));
-
- z = negInfInf.divide(Complex.ONE);
- Assert.assertTrue(Double.isNaN(z.getReal()));
- Assert.assertTrue(Double.isNaN(z.getImaginary()));
- }
-
- @Test
- public void testScalarDivide() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = 2.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.divide(yComplex), x.divide(yDouble));
- }
-
- @Test
- public void testScalarDivideNaN() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = Double.NaN;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.divide(yComplex), x.divide(yDouble));
- }
-
- @Test
- public void testScalarDivideInf() {
- Complex x = new Complex(1,1);
- double yDouble = Double.POSITIVE_INFINITY;
- Complex yComplex = new Complex(yDouble);
- TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0);
-
- yDouble = Double.NEGATIVE_INFINITY;
- yComplex = new Complex(yDouble);
- TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0);
-
- x = new Complex(1, Double.NEGATIVE_INFINITY);
- TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0);
- }
-
- @Test
- public void testScalarDivideZero() {
- Complex x = new Complex(1,1);
- TestUtils.assertEquals(x.divide(Complex.ZERO), x.divide(0), 0);
- }
-
- @Test
- public void testReciprocal() {
- Complex z = new Complex(5.0, 6.0);
- Complex act = z.reciprocal();
- double expRe = 5.0 / 61.0;
- double expIm = -6.0 / 61.0;
- Assert.assertEquals(expRe, act.getReal(), FastMath.ulp(expRe));
- Assert.assertEquals(expIm, act.getImaginary(), FastMath.ulp(expIm));
- }
-
- @Test
- public void testReciprocalReal() {
- Complex z = new Complex(-2.0, 0.0);
- Assert.assertTrue(Complex.equals(new Complex(-0.5, 0.0), z.reciprocal()));
- }
-
- @Test
- public void testReciprocalImaginary() {
- Complex z = new Complex(0.0, -2.0);
- Assert.assertEquals(new Complex(0.0, 0.5), z.reciprocal());
- }
-
- @Test
- public void testReciprocalInf() {
- Complex z = new Complex(neginf, inf);
- Assert.assertTrue(z.reciprocal().equals(Complex.ZERO));
-
- z = new Complex(1, inf).reciprocal();
- Assert.assertEquals(z, Complex.ZERO);
- }
-
- @Test
- public void testReciprocalZero() {
- Assert.assertEquals(Complex.ZERO.reciprocal(), Complex.INF);
- }
-
- @Test
- public void testReciprocalNaN() {
- Assert.assertTrue(Complex.NaN.reciprocal().isNaN());
- }
-
- @Test
- public void testMultiply() {
- Complex x = new Complex(3.0, 4.0);
- Complex y = new Complex(5.0, 6.0);
- Complex z = x.multiply(y);
- Assert.assertEquals(-9.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(38.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testMultiplyNaN() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.multiply(Complex.NaN);
- Assert.assertSame(Complex.NaN, z);
- z = Complex.NaN.multiply(5);
- Assert.assertSame(Complex.NaN, z);
- }
-
- @Test
- public void testMultiplyInfInf() {
- // Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620
- Assert.assertTrue(infInf.multiply(infInf).isInfinite());
- }
-
- @Test
- public void testMultiplyNaNInf() {
- Complex z = new Complex(1,1);
- Complex w = z.multiply(infOne);
- Assert.assertEquals(w.getReal(), inf, 0);
- Assert.assertEquals(w.getImaginary(), inf, 0);
-
- // [MATH-164]
- Assert.assertTrue(new Complex( 1,0).multiply(infInf).equals(Complex.INF));
- Assert.assertTrue(new Complex(-1,0).multiply(infInf).equals(Complex.INF));
- Assert.assertTrue(new Complex( 1,0).multiply(negInfZero).equals(Complex.INF));
-
- w = oneInf.multiply(oneNegInf);
- Assert.assertEquals(w.getReal(), inf, 0);
- Assert.assertEquals(w.getImaginary(), inf, 0);
-
- w = negInfNegInf.multiply(oneNaN);
- Assert.assertTrue(Double.isNaN(w.getReal()));
- Assert.assertTrue(Double.isNaN(w.getImaginary()));
-
- z = new Complex(1, neginf);
- Assert.assertSame(Complex.INF, z.multiply(z));
- }
-
- @Test
- public void testScalarMultiply() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = 2.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
- int zInt = -5;
- Complex zComplex = new Complex(zInt);
- Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt));
- }
-
- @Test
- public void testScalarMultiplyNaN() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = Double.NaN;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
- }
-
- @Test
- public void testScalarMultiplyInf() {
- Complex x = new Complex(1, 1);
- double yDouble = Double.POSITIVE_INFINITY;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
-
- yDouble = Double.NEGATIVE_INFINITY;
- yComplex = new Complex(yDouble);
- Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
- }
-
- @Test
- public void testNegate() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.negate();
- Assert.assertEquals(-3.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testNegateNaN() {
- Complex z = Complex.NaN.negate();
- Assert.assertTrue(z.isNaN());
- }
-
- @Test
- public void testSubtract() {
- Complex x = new Complex(3.0, 4.0);
- Complex y = new Complex(5.0, 6.0);
- Complex z = x.subtract(y);
- Assert.assertEquals(-2.0, z.getReal(), 1.0e-5);
- Assert.assertEquals(-2.0, z.getImaginary(), 1.0e-5);
- }
-
- @Test
- public void testSubtractNaN() {
- Complex x = new Complex(3.0, 4.0);
- Complex z = x.subtract(Complex.NaN);
- Assert.assertSame(Complex.NaN, z);
- z = new Complex(1, nan);
- Complex w = x.subtract(z);
- Assert.assertSame(Complex.NaN, w);
- }
-
- @Test
- public void testSubtractInf() {
- Complex x = new Complex(1, 1);
- Complex z = new Complex(neginf, 0);
- Complex w = x.subtract(z);
- Assert.assertEquals(w.getImaginary(), 1, 0);
- Assert.assertEquals(inf, w.getReal(), 0);
-
- x = new Complex(neginf, 0);
- Assert.assertTrue(Double.isNaN(x.subtract(z).getReal()));
- }
-
- @Test
- public void testScalarSubtract() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = 2.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble));
- }
-
- @Test
- public void testScalarSubtractNaN() {
- Complex x = new Complex(3.0, 4.0);
- double yDouble = Double.NaN;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble));
- }
-
- @Test
- public void testScalarSubtractInf() {
- Complex x = new Complex(1, 1);
- double yDouble = Double.POSITIVE_INFINITY;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble));
-
- x = new Complex(neginf, 0);
- Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble));
- }
-
-
- @Test
- public void testEqualsNull() {
- Complex x = new Complex(3.0, 4.0);
- Assert.assertFalse(x.equals(null));
- }
-
- @Test(expected=NullPointerException.class)
- public void testFloatingPointEqualsPrecondition1() {
- Complex.equals(new Complex(3.0, 4.0), null, 3);
- }
- @Test(expected=NullPointerException.class)
- public void testFloatingPointEqualsPrecondition2() {
- Complex.equals(null, new Complex(3.0, 4.0), 3);
- }
-
- @Test
- public void testEqualsClass() {
- Complex x = new Complex(3.0, 4.0);
- Assert.assertFalse(x.equals(this));
- }
-
- @Test
- public void testEqualsSame() {
- Complex x = new Complex(3.0, 4.0);
- Assert.assertTrue(x.equals(x));
- }
-
- @Test
- public void testFloatingPointEquals() {
- double re = -3.21;
- double im = 456789e10;
-
- final Complex x = new Complex(re, im);
- Complex y = new Complex(re, im);
-
- Assert.assertTrue(x.equals(y));
- Assert.assertTrue(Complex.equals(x, y));
-
- final int maxUlps = 5;
- for (int i = 0; i < maxUlps; i++) {
- re = FastMath.nextUp(re);
- im = FastMath.nextUp(im);
- }
- y = new Complex(re, im);
- Assert.assertTrue(Complex.equals(x, y, maxUlps));
-
- re = FastMath.nextUp(re);
- im = FastMath.nextUp(im);
- y = new Complex(re, im);
- Assert.assertFalse(Complex.equals(x, y, maxUlps));
- }
-
- @Test
- public void testFloatingPointEqualsNaN() {
- Complex c = new Complex(Double.NaN, 1);
- Assert.assertFalse(Complex.equals(c, c));
-
- c = new Complex(1, Double.NaN);
- Assert.assertFalse(Complex.equals(c, c));
- }
-
- @Test
- public void testFloatingPointEqualsWithAllowedDelta() {
- final double re = 153.0000;
- final double im = 152.9375;
- final double tol1 = 0.0625;
- final Complex x = new Complex(re, im);
- final Complex y = new Complex(re + tol1, im + tol1);
- Assert.assertTrue(Complex.equals(x, y, tol1));
-
- final double tol2 = 0.0624;
- Assert.assertFalse(Complex.equals(x, y, tol2));
- }
-
- @Test
- public void testFloatingPointEqualsWithAllowedDeltaNaN() {
- final Complex x = new Complex(0, Double.NaN);
- final Complex y = new Complex(Double.NaN, 0);
- Assert.assertFalse(Complex.equals(x, Complex.ZERO, 0.1));
- Assert.assertFalse(Complex.equals(x, x, 0.1));
- Assert.assertFalse(Complex.equals(x, y, 0.1));
- }
-
- @Test
- public void testFloatingPointEqualsWithRelativeTolerance() {
- final double tol = 1e-4;
- final double re = 1;
- final double im = 1e10;
-
- final double f = 1 + tol;
- final Complex x = new Complex(re, im);
- final Complex y = new Complex(re * f, im * f);
- Assert.assertTrue(Complex.equalsWithRelativeTolerance(x, y, tol));
- }
-
- @Test
- public void testFloatingPointEqualsWithRelativeToleranceNaN() {
- final Complex x = new Complex(0, Double.NaN);
- final Complex y = new Complex(Double.NaN, 0);
- Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, Complex.ZERO, 0.1));
- Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, x, 0.1));
- Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, y, 0.1));
- }
-
- @Test
- public void testEqualsTrue() {
- Complex x = new Complex(3.0, 4.0);
- Complex y = new Complex(3.0, 4.0);
- Assert.assertTrue(x.equals(y));
- }
-
- @Test
- public void testEqualsRealDifference() {
- Complex x = new Complex(0.0, 0.0);
- Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
- Assert.assertFalse(x.equals(y));
- }
-
- @Test
- public void testEqualsImaginaryDifference() {
- Complex x = new Complex(0.0, 0.0);
- Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
- Assert.assertFalse(x.equals(y));
- }
-
- @Test
- public void testEqualsNaN() {
- Complex realNaN = new Complex(Double.NaN, 0.0);
- Complex imaginaryNaN = new Complex(0.0, Double.NaN);
- Complex complexNaN = Complex.NaN;
- Assert.assertTrue(realNaN.equals(imaginaryNaN));
- Assert.assertTrue(imaginaryNaN.equals(complexNaN));
- Assert.assertTrue(realNaN.equals(complexNaN));
- }
-
- @Test
- public void testHashCode() {
- Complex x = new Complex(0.0, 0.0);
- Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
- Assert.assertFalse(x.hashCode()==y.hashCode());
- y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
- Assert.assertFalse(x.hashCode()==y.hashCode());
- Complex realNaN = new Complex(Double.NaN, 0.0);
- Complex imaginaryNaN = new Complex(0.0, Double.NaN);
- Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
- Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
-
- // MATH-1118
- // "equals" and "hashCode" must be compatible: if two objects have
- // different hash codes, "equals" must return false.
- final String msg = "'equals' not compatible with 'hashCode'";
-
- x = new Complex(0.0, 0.0);
- y = new Complex(0.0, -0.0);
- Assert.assertTrue(x.hashCode() != y.hashCode());
- Assert.assertFalse(msg, x.equals(y));
-
- x = new Complex(0.0, 0.0);
- y = new Complex(-0.0, 0.0);
- Assert.assertTrue(x.hashCode() != y.hashCode());
- Assert.assertFalse(msg, x.equals(y));
- }
-
- @Test
- public void testAcos() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(0.936812, -2.30551);
- TestUtils.assertEquals(expected, z.acos(), 1.0e-5);
- TestUtils.assertEquals(new Complex(FastMath.acos(0), 0),
- Complex.ZERO.acos(), 1.0e-12);
- }
-
- @Test
- public void testAcosInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.acos());
- TestUtils.assertSame(Complex.NaN, oneNegInf.acos());
- TestUtils.assertSame(Complex.NaN, infOne.acos());
- TestUtils.assertSame(Complex.NaN, negInfOne.acos());
- TestUtils.assertSame(Complex.NaN, infInf.acos());
- TestUtils.assertSame(Complex.NaN, infNegInf.acos());
- TestUtils.assertSame(Complex.NaN, negInfInf.acos());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.acos());
- }
-
- @Test
- public void testAcosNaN() {
- Assert.assertTrue(Complex.NaN.acos().isNaN());
- }
-
- @Test
- public void testAsin() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(0.633984, 2.30551);
- TestUtils.assertEquals(expected, z.asin(), 1.0e-5);
- }
-
- @Test
- public void testAsinNaN() {
- Assert.assertTrue(Complex.NaN.asin().isNaN());
- }
-
- @Test
- public void testAsinInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.asin());
- TestUtils.assertSame(Complex.NaN, oneNegInf.asin());
- TestUtils.assertSame(Complex.NaN, infOne.asin());
- TestUtils.assertSame(Complex.NaN, negInfOne.asin());
- TestUtils.assertSame(Complex.NaN, infInf.asin());
- TestUtils.assertSame(Complex.NaN, infNegInf.asin());
- TestUtils.assertSame(Complex.NaN, negInfInf.asin());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.asin());
- }
-
-
- @Test
- public void testAtan() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(1.44831, 0.158997);
- TestUtils.assertEquals(expected, z.atan(), 1.0e-5);
- }
-
- @Test
- public void testAtanInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.atan());
- TestUtils.assertSame(Complex.NaN, oneNegInf.atan());
- TestUtils.assertSame(Complex.NaN, infOne.atan());
- TestUtils.assertSame(Complex.NaN, negInfOne.atan());
- TestUtils.assertSame(Complex.NaN, infInf.atan());
- TestUtils.assertSame(Complex.NaN, infNegInf.atan());
- TestUtils.assertSame(Complex.NaN, negInfInf.atan());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.atan());
- }
-
- @Test
- public void testAtanI() {
- Assert.assertTrue(Complex.I.atan().isNaN());
- }
-
- @Test
- public void testAtanNaN() {
- Assert.assertTrue(Complex.NaN.atan().isNaN());
- }
-
- @Test
- public void testCos() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(-27.03495, -3.851153);
- TestUtils.assertEquals(expected, z.cos(), 1.0e-5);
- }
-
- @Test
- public void testCosNaN() {
- Assert.assertTrue(Complex.NaN.cos().isNaN());
- }
-
- @Test
- public void testCosInf() {
- TestUtils.assertSame(infNegInf, oneInf.cos());
- TestUtils.assertSame(infInf, oneNegInf.cos());
- TestUtils.assertSame(Complex.NaN, infOne.cos());
- TestUtils.assertSame(Complex.NaN, negInfOne.cos());
- TestUtils.assertSame(Complex.NaN, infInf.cos());
- TestUtils.assertSame(Complex.NaN, infNegInf.cos());
- TestUtils.assertSame(Complex.NaN, negInfInf.cos());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.cos());
- }
-
- @Test
- public void testCosh() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(-6.58066, -7.58155);
- TestUtils.assertEquals(expected, z.cosh(), 1.0e-5);
- }
-
- @Test
- public void testCoshNaN() {
- Assert.assertTrue(Complex.NaN.cosh().isNaN());
- }
-
- @Test
- public void testCoshInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.cosh());
- TestUtils.assertSame(Complex.NaN, oneNegInf.cosh());
- TestUtils.assertSame(infInf, infOne.cosh());
- TestUtils.assertSame(infNegInf, negInfOne.cosh());
- TestUtils.assertSame(Complex.NaN, infInf.cosh());
- TestUtils.assertSame(Complex.NaN, infNegInf.cosh());
- TestUtils.assertSame(Complex.NaN, negInfInf.cosh());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.cosh());
- }
-
- @Test
- public void testExp() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(-13.12878, -15.20078);
- TestUtils.assertEquals(expected, z.exp(), 1.0e-5);
- TestUtils.assertEquals(Complex.ONE,
- Complex.ZERO.exp(), 10e-12);
- Complex iPi = Complex.I.multiply(new Complex(pi,0));
- TestUtils.assertEquals(Complex.ONE.negate(),
- iPi.exp(), 10e-12);
- }
-
- @Test
- public void testExpNaN() {
- Assert.assertTrue(Complex.NaN.exp().isNaN());
- }
-
- @Test
- public void testExpInf1() {
- TestUtils.assertSame(Complex.NaN, oneInf.exp());
- }
-
- @Test
- public void testExpInf2() {
- TestUtils.assertSame(Complex.NaN, oneNegInf.exp());
- }
-
- @Test
- public void testExpInf3() {
- TestUtils.assertSame(infInf, infOne.exp());
- }
-
- @Test
- @Ignore
- public void testJava() {// TODO more debug
- System.out.println(">>testJava()");
- // FastMathTest#testExpSpecialCases() checks the following:
- // Assert.assertEquals("exp of -infinity should be 0.0", 0.0, FastMath.exp(Double.NEGATIVE_INFINITY), Precision.EPSILON);
- // Let's check how well Math works:
- System.out.println("Math.exp="+Math.exp(Double.NEGATIVE_INFINITY));
- String props[] = {
- "java.version", // Java Runtime Environment version
- "java.vendor", // Java Runtime Environment vendor
- "java.vm.specification.version", // Java Virtual Machine specification version
- "java.vm.specification.vendor", // Java Virtual Machine specification vendor
- "java.vm.specification.name", // Java Virtual Machine specification name
- "java.vm.version", // Java Virtual Machine implementation version
- "java.vm.vendor", // Java Virtual Machine implementation vendor
- "java.vm.name", // Java Virtual Machine implementation name
- "java.specification.version", // Java Runtime Environment specification version
- "java.specification.vendor", // Java Runtime Environment specification vendor
- "java.specification.name", // Java Runtime Environment specification name
- "java.class.version", // Java class format version number
- };
- for(String t : props) {
- System.out.println(t + "=" + System.getProperty(t));
- }
- System.out.println("<<testJava()");
- }
-
- @Test
- public void testExpInf4() {
- final Complex exp = negInfOne.exp();
- TestUtils.assertSame(Complex.ZERO, exp);
- }
-
- @Test
- public void testExpInf5() {
- TestUtils.assertSame(Complex.NaN, infInf.exp());
- }
-
- @Test
- public void testExpInf6() {
- TestUtils.assertSame(Complex.NaN, infNegInf.exp());
- }
-
- @Test
- public void testExpInf7() {
- TestUtils.assertSame(Complex.NaN, negInfInf.exp());
- }
-
- @Test
- public void testExpInf8() {
- TestUtils.assertSame(Complex.NaN, negInfNegInf.exp());
- }
-
- @Test
- public void testLog() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(1.60944, 0.927295);
- TestUtils.assertEquals(expected, z.log(), 1.0e-5);
- }
-
- @Test
- public void testLogNaN() {
- Assert.assertTrue(Complex.NaN.log().isNaN());
- }
-
- @Test
- public void testLogInf() {
- TestUtils.assertEquals(new Complex(inf, pi / 2),
- oneInf.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, -pi / 2),
- oneNegInf.log(), 10e-12);
- TestUtils.assertEquals(infZero, infOne.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, pi),
- negInfOne.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, pi / 4),
- infInf.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, -pi / 4),
- infNegInf.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, 3d * pi / 4),
- negInfInf.log(), 10e-12);
- TestUtils.assertEquals(new Complex(inf, - 3d * pi / 4),
- negInfNegInf.log(), 10e-12);
- }
-
- @Test
- public void testLogZero() {
- TestUtils.assertSame(negInfZero, Complex.ZERO.log());
- }
-
- @Test
- public void testPow() {
- Complex x = new Complex(3, 4);
- Complex y = new Complex(5, 6);
- Complex expected = new Complex(-1.860893, 11.83677);
- TestUtils.assertEquals(expected, x.pow(y), 1.0e-5);
- }
-
- @Test
- public void testPowNaNBase() {
- Complex x = new Complex(3, 4);
- Assert.assertTrue(Complex.NaN.pow(x).isNaN());
- }
-
- @Test
- public void testPowNaNExponent() {
- Complex x = new Complex(3, 4);
- Assert.assertTrue(x.pow(Complex.NaN).isNaN());
- }
-
- @Test
- public void testPowInf() {
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneInf));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneNegInf));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infOne));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infInf));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infNegInf));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfInf));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfNegInf));
- TestUtils.assertSame(Complex.NaN,infOne.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,negInfOne.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,infInf.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,negInfInf.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infNegInf));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(negInfNegInf));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infInf));
- TestUtils.assertSame(Complex.NaN,infInf.pow(infNegInf));
- TestUtils.assertSame(Complex.NaN,infInf.pow(negInfNegInf));
- TestUtils.assertSame(Complex.NaN,infInf.pow(infInf));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(infNegInf));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(negInfNegInf));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(infInf));
- }
-
- @Test
- public void testPowZero() {
- TestUtils.assertSame(Complex.NaN,
- Complex.ZERO.pow(Complex.ONE));
- TestUtils.assertSame(Complex.NaN,
- Complex.ZERO.pow(Complex.ZERO));
- TestUtils.assertSame(Complex.NaN,
- Complex.ZERO.pow(Complex.I));
- TestUtils.assertEquals(Complex.ONE,
- Complex.ONE.pow(Complex.ZERO), 10e-12);
- TestUtils.assertEquals(Complex.ONE,
- Complex.I.pow(Complex.ZERO), 10e-12);
- TestUtils.assertEquals(Complex.ONE,
- new Complex(-1, 3).pow(Complex.ZERO), 10e-12);
- }
-
- @Test
- public void testScalarPow() {
- Complex x = new Complex(3, 4);
- double yDouble = 5.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.pow(yComplex), x.pow(yDouble));
- }
-
- @Test
- public void testScalarPowNaNBase() {
- Complex x = Complex.NaN;
- double yDouble = 5.0;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.pow(yComplex), x.pow(yDouble));
- }
-
- @Test
- public void testScalarPowNaNExponent() {
- Complex x = new Complex(3, 4);
- double yDouble = Double.NaN;
- Complex yComplex = new Complex(yDouble);
- Assert.assertEquals(x.pow(yComplex), x.pow(yDouble));
- }
-
- @Test
- public void testScalarPowInf() {
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.POSITIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.NEGATIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,infOne.pow(1.0));
- TestUtils.assertSame(Complex.NaN,negInfOne.pow(1.0));
- TestUtils.assertSame(Complex.NaN,infInf.pow(1.0));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(1.0));
- TestUtils.assertSame(Complex.NaN,negInfInf.pow(10));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(1.0));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,infInf.pow(Double.POSITIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,infInf.pow(Double.NEGATIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.NEGATIVE_INFINITY));
- TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.POSITIVE_INFINITY));
- }
-
- @Test
- public void testScalarPowZero() {
- TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(1.0));
- TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(0.0));
- TestUtils.assertEquals(Complex.ONE, Complex.ONE.pow(0.0), 10e-12);
- TestUtils.assertEquals(Complex.ONE, Complex.I.pow(0.0), 10e-12);
- TestUtils.assertEquals(Complex.ONE, new Complex(-1, 3).pow(0.0), 10e-12);
- }
-
- @Test(expected=NullArgumentException.class)
- public void testpowNull() {
- Complex.ONE.pow(null);
- }
-
- @Test
- public void testSin() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(3.853738, -27.01681);
- TestUtils.assertEquals(expected, z.sin(), 1.0e-5);
- }
-
- @Test
- public void testSinInf() {
- TestUtils.assertSame(infInf, oneInf.sin());
- TestUtils.assertSame(infNegInf, oneNegInf.sin());
- TestUtils.assertSame(Complex.NaN, infOne.sin());
- TestUtils.assertSame(Complex.NaN, negInfOne.sin());
- TestUtils.assertSame(Complex.NaN, infInf.sin());
- TestUtils.assertSame(Complex.NaN, infNegInf.sin());
- TestUtils.assertSame(Complex.NaN, negInfInf.sin());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.sin());
- }
-
- @Test
- public void testSinNaN() {
- Assert.assertTrue(Complex.NaN.sin().isNaN());
- }
-
- @Test
- public void testSinh() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(-6.54812, -7.61923);
- TestUtils.assertEquals(expected, z.sinh(), 1.0e-5);
- }
-
- @Test
- public void testSinhNaN() {
- Assert.assertTrue(Complex.NaN.sinh().isNaN());
- }
-
- @Test
- public void testSinhInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.sinh());
- TestUtils.assertSame(Complex.NaN, oneNegInf.sinh());
- TestUtils.assertSame(infInf, infOne.sinh());
- TestUtils.assertSame(negInfInf, negInfOne.sinh());
- TestUtils.assertSame(Complex.NaN, infInf.sinh());
- TestUtils.assertSame(Complex.NaN, infNegInf.sinh());
- TestUtils.assertSame(Complex.NaN, negInfInf.sinh());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.sinh());
- }
-
- @Test
- public void testSqrtRealPositive() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(2, 1);
- TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5);
- }
-
- @Test
- public void testSqrtRealZero() {
- Complex z = new Complex(0.0, 4);
- Complex expected = new Complex(1.41421, 1.41421);
- TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5);
- }
-
- @Test
- public void testSqrtRealNegative() {
- Complex z = new Complex(-3.0, 4);
- Complex expected = new Complex(1, 2);
- TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5);
- }
-
- @Test
- public void testSqrtImaginaryZero() {
- Complex z = new Complex(-3.0, 0.0);
- Complex expected = new Complex(0.0, 1.73205);
- TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5);
- }
-
- @Test
- public void testSqrtImaginaryNegative() {
- Complex z = new Complex(-3.0, -4.0);
- Complex expected = new Complex(1.0, -2.0);
- TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5);
- }
-
- @Test
- public void testSqrtPolar() {
- double r = 1;
- for (int i = 0; i < 5; i++) {
- r += i;
- double theta = 0;
- for (int j =0; j < 11; j++) {
- theta += pi /12;
- Complex z = ComplexUtils.polar2Complex(r, theta);
- Complex sqrtz = ComplexUtils.polar2Complex(FastMath.sqrt(r), theta / 2);
- TestUtils.assertEquals(sqrtz, z.sqrt(), 10e-12);
- }
- }
- }
-
- @Test
- public void testSqrtNaN() {
- Assert.assertTrue(Complex.NaN.sqrt().isNaN());
- }
-
- @Test
- public void testSqrtInf() {
- TestUtils.assertSame(infNaN, oneInf.sqrt());
- TestUtils.assertSame(infNaN, oneNegInf.sqrt());
- TestUtils.assertSame(infZero, infOne.sqrt());
- TestUtils.assertSame(zeroInf, negInfOne.sqrt());
- TestUtils.assertSame(infNaN, infInf.sqrt());
- TestUtils.assertSame(infNaN, infNegInf.sqrt());
- TestUtils.assertSame(nanInf, negInfInf.sqrt());
- TestUtils.assertSame(nanNegInf, negInfNegInf.sqrt());
- }
-
- @Test
- public void testSqrt1z() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(4.08033, -2.94094);
- TestUtils.assertEquals(expected, z.sqrt1z(), 1.0e-5);
- }
-
- @Test
- public void testSqrt1zNaN() {
- Assert.assertTrue(Complex.NaN.sqrt1z().isNaN());
- }
-
- @Test
- public void testTan() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(-0.000187346, 0.999356);
- TestUtils.assertEquals(expected, z.tan(), 1.0e-5);
- /* Check that no overflow occurs (MATH-722) */
- Complex actual = new Complex(3.0, 1E10).tan();
- expected = new Complex(0, 1);
- TestUtils.assertEquals(expected, actual, 1.0e-5);
- actual = new Complex(3.0, -1E10).tan();
- expected = new Complex(0, -1);
- TestUtils.assertEquals(expected, actual, 1.0e-5);
- }
-
- @Test
- public void testTanNaN() {
- Assert.assertTrue(Complex.NaN.tan().isNaN());
- }
-
- @Test
- public void testTanInf() {
- TestUtils.assertSame(Complex.valueOf(0.0, 1.0), oneInf.tan());
- TestUtils.assertSame(Complex.valueOf(0.0, -1.0), oneNegInf.tan());
- TestUtils.assertSame(Complex.NaN, infOne.tan());
- TestUtils.assertSame(Complex.NaN, negInfOne.tan());
- TestUtils.assertSame(Complex.NaN, infInf.tan());
- TestUtils.assertSame(Complex.NaN, infNegInf.tan());
- TestUtils.assertSame(Complex.NaN, negInfInf.tan());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.tan());
- }
-
- @Test
- public void testTanCritical() {
- TestUtils.assertSame(infNaN, new Complex(pi/2, 0).tan());
- TestUtils.assertSame(negInfNaN, new Complex(-pi/2, 0).tan());
- }
-
- @Test
- public void testTanh() {
- Complex z = new Complex(3, 4);
- Complex expected = new Complex(1.00071, 0.00490826);
- TestUtils.assertEquals(expected, z.tanh(), 1.0e-5);
- /* Check that no overflow occurs (MATH-722) */
- Complex actual = new Complex(1E10, 3.0).tanh();
- expected = new Complex(1, 0);
- TestUtils.assertEquals(expected, actual, 1.0e-5);
- actual = new Complex(-1E10, 3.0).tanh();
- expected = new Complex(-1, 0);
- TestUtils.assertEquals(expected, actual, 1.0e-5);
- }
-
- @Test
- public void testTanhNaN() {
- Assert.assertTrue(Complex.NaN.tanh().isNaN());
- }
-
- @Test
- public void testTanhInf() {
- TestUtils.assertSame(Complex.NaN, oneInf.tanh());
- TestUtils.assertSame(Complex.NaN, oneNegInf.tanh());
- TestUtils.assertSame(Complex.valueOf(1.0, 0.0), infOne.tanh());
- TestUtils.assertSame(Complex.valueOf(-1.0, 0.0), negInfOne.tanh());
- TestUtils.assertSame(Complex.NaN, infInf.tanh());
- TestUtils.assertSame(Complex.NaN, infNegInf.tanh());
- TestUtils.assertSame(Complex.NaN, negInfInf.tanh());
- TestUtils.assertSame(Complex.NaN, negInfNegInf.tanh());
- }
-
- @Test
- public void testTanhCritical() {
- TestUtils.assertSame(nanInf, new Complex(0, pi/2).tanh());
- }
-
- /** test issue MATH-221 */
- @Test
- public void testMath221() {
- Assert.assertTrue(Complex.equals(new Complex(0,-1),
- new Complex(0,1).multiply(new Complex(-1,0))));
- }
-
- /**
- * Test: computing <b>third roots</b> of z.
- * <pre>
- * <code>
- * <b>z = -2 + 2 * i</b>
- * => z_0 = 1 + i
- * => z_1 = -1.3660 + 0.3660 * i
- * => z_2 = 0.3660 - 1.3660 * i
- * </code>
- * </pre>
- */
- @Test
- public void testNthRoot_normal_thirdRoot() {
- // The complex number we want to compute all third-roots for.
- Complex z = new Complex(-2,2);
- // The List holding all third roots
- Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]);
- // Returned Collection must not be empty!
- Assert.assertEquals(3, thirdRootsOfZ.length);
- // test z_0
- Assert.assertEquals(1.0, thirdRootsOfZ[0].getReal(), 1.0e-5);
- Assert.assertEquals(1.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5);
- // test z_1
- Assert.assertEquals(-1.3660254037844386, thirdRootsOfZ[1].getReal(), 1.0e-5);
- Assert.assertEquals(0.36602540378443843, thirdRootsOfZ[1].getImaginary(), 1.0e-5);
- // test z_2
- Assert.assertEquals(0.366025403784439, thirdRootsOfZ[2].getReal(), 1.0e-5);
- Assert.assertEquals(-1.3660254037844384, thirdRootsOfZ[2].getImaginary(), 1.0e-5);
- }
-
-
- /**
- * Test: computing <b>fourth roots</b> of z.
- * <pre>
- * <code>
- * <b>z = 5 - 2 * i</b>
- * => z_0 = 1.5164 - 0.1446 * i
- * => z_1 = 0.1446 + 1.5164 * i
- * => z_2 = -1.5164 + 0.1446 * i
- * => z_3 = -1.5164 - 0.1446 * i
- * </code>
- * </pre>
- */
- @Test
- public void testNthRoot_normal_fourthRoot() {
- // The complex number we want to compute all third-roots for.
- Complex z = new Complex(5,-2);
- // The List holding all fourth roots
- Complex[] fourthRootsOfZ = z.nthRoot(4).toArray(new Complex[0]);
- // Returned Collection must not be empty!
- Assert.assertEquals(4, fourthRootsOfZ.length);
- // test z_0
- Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[0].getReal(), 1.0e-5);
- Assert.assertEquals(-0.14469266210702247, fourthRootsOfZ[0].getImaginary(), 1.0e-5);
- // test z_1
- Assert.assertEquals(0.14469266210702256, fourthRootsOfZ[1].getReal(), 1.0e-5);
- Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[1].getImaginary(), 1.0e-5);
- // test z_2
- Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[2].getReal(), 1.0e-5);
- Assert.assertEquals(0.14469266210702267, fourthRootsOfZ[2].getImaginary(), 1.0e-5);
- // test z_3
- Assert.assertEquals(-0.14469266210702275, fourthRootsOfZ[3].getReal(), 1.0e-5);
- Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[3].getImaginary(), 1.0e-5);
- }
-
- /**
- * Test: computing <b>third roots</b> of z.
- * <pre>
- * <code>
- * <b>z = 8</b>
- * => z_0 = 2
- * => z_1 = -1 + 1.73205 * i
- * => z_2 = -1 - 1.73205 * i
- * </code>
- * </pre>
- */
- @Test
- public void testNthRoot_cornercase_thirdRoot_imaginaryPartEmpty() {
- // The number 8 has three third roots. One we all already know is the number 2.
- // But there are two more complex roots.
- Complex z = new Complex(8,0);
- // The List holding all third roots
- Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]);
- // Returned Collection must not be empty!
- Assert.assertEquals(3, thirdRootsOfZ.length);
- // test z_0
- Assert.assertEquals(2.0, thirdRootsOfZ[0].getReal(), 1.0e-5);
- Assert.assertEquals(0.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5);
- // test z_1
- Assert.assertEquals(-1.0, thirdRootsOfZ[1].getReal(), 1.0e-5);
- Assert.assertEquals(1.7320508075688774, thirdRootsOfZ[1].getImaginary(), 1.0e-5);
- // test z_2
- Assert.assertEquals(-1.0, thirdRootsOfZ[2].getReal(), 1.0e-5);
- Assert.assertEquals(-1.732050807568877, thirdRootsOfZ[2].getImaginary(), 1.0e-5);
- }
-
-
- /**
- * Test: computing <b>third roots</b> of z with real part 0.
- * <pre>
- * <code>
- * <b>z = 2 * i</b>
- * => z_0 = 1.0911 + 0.6299 * i
- * => z_1 = -1.0911 + 0.6299 * i
- * => z_2 = -2.3144 - 1.2599 * i
- * </code>
- * </pre>
- */
- @Test
- public void testNthRoot_cornercase_thirdRoot_realPartZero() {
- // complex number with only imaginary part
- Complex z = new Complex(0,2);
- // The List holding all third roots
- Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]);
- // Returned Collection must not be empty!
- Assert.assertEquals(3, thirdRootsOfZ.length);
- // test z_0
- Assert.assertEquals(1.0911236359717216, thirdRootsOfZ[0].getReal(), 1.0e-5);
- Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[0].getImaginary(), 1.0e-5);
- // test z_1
- Assert.assertEquals(-1.0911236359717216, thirdRootsOfZ[1].getReal(), 1.0e-5);
- Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[1].getImaginary(), 1.0e-5);
- // test z_2
- Assert.assertEquals(-2.3144374213981936E-16, thirdRootsOfZ[2].getReal(), 1.0e-5);
- Assert.assertEquals(-1.2599210498948732, thirdRootsOfZ[2].getImaginary(), 1.0e-5);
- }
-
- /**
- * Test cornercases with NaN and Infinity.
- */
- @Test
- public void testNthRoot_cornercase_NAN_Inf() {
- // NaN + finite -> NaN
- List<Complex> roots = oneNaN.nthRoot(3);
- Assert.assertEquals(1,roots.size());
- Assert.assertEquals(Complex.NaN, roots.get(0));
-
- roots = nanZero.nthRoot(3);
- Assert.assertEquals(1,roots.size());
- Assert.assertEquals(Complex.NaN, roots.get(0));
-
- // NaN + infinite -> NaN
- roots = nanInf.nthRoot(3);
- Assert.assertEquals(1,roots.size());
- Assert.assertEquals(Complex.NaN, roots.get(0));
-
- // finite + infinite -> Inf
- roots = oneInf.nthRoot(3);
- Assert.assertEquals(1,roots.size());
- Assert.assertEquals(Complex.INF, roots.get(0));
-
- // infinite + infinite -> Inf
- roots = negInfInf.nthRoot(3);
- Assert.assertEquals(1,roots.size());
- Assert.assertEquals(Complex.INF, roots.get(0));
- }
-
- /**
- * Test standard values
- */
- @Test
- public void testGetArgument() {
- Complex z = new Complex(1, 0);
- Assert.assertEquals(0.0, z.getArgument(), 1.0e-12);
-
- z = new Complex(1, 1);
- Assert.assertEquals(FastMath.PI/4, z.getArgument(), 1.0e-12);
-
- z = new Complex(0, 1);
- Assert.assertEquals(FastMath.PI/2, z.getArgument(), 1.0e-12);
-
- z = new Complex(-1, 1);
- Assert.assertEquals(3 * FastMath.PI/4, z.getArgument(), 1.0e-12);
-
- z = new Complex(-1, 0);
- Assert.assertEquals(FastMath.PI, z.getArgument(), 1.0e-12);
-
- z = new Complex(-1, -1);
- Assert.assertEquals(-3 * FastMath.PI/4, z.getArgument(), 1.0e-12);
-
- z = new Complex(0, -1);
- Assert.assertEquals(-FastMath.PI/2, z.getArgument(), 1.0e-12);
-
- z = new Complex(1, -1);
- Assert.assertEquals(-FastMath.PI/4, z.getArgument(), 1.0e-12);
-
- }
-
- /**
- * Verify atan2-style handling of infinite parts
- */
- @Test
- public void testGetArgumentInf() {
- Assert.assertEquals(FastMath.PI/4, infInf.getArgument(), 1.0e-12);
- Assert.assertEquals(FastMath.PI/2, oneInf.getArgument(), 1.0e-12);
- Assert.assertEquals(0.0, infOne.getArgument(), 1.0e-12);
- Assert.assertEquals(FastMath.PI/2, zeroInf.getArgument(), 1.0e-12);
- Assert.assertEquals(0.0, infZero.getArgument(), 1.0e-12);
- Assert.assertEquals(FastMath.PI, negInfOne.getArgument(), 1.0e-12);
- Assert.assertEquals(-3.0*FastMath.PI/4, negInfNegInf.getArgument(), 1.0e-12);
- Assert.assertEquals(-FastMath.PI/2, oneNegInf.getArgument(), 1.0e-12);
- }
-
- /**
- * Verify that either part NaN results in NaN
- */
- @Test
- public void testGetArgumentNaN() {
- Assert.assertTrue(Double.isNaN(nanZero.getArgument()));
- Assert.assertTrue(Double.isNaN(zeroNaN.getArgument()));
- Assert.assertTrue(Double.isNaN(Complex.NaN.getArgument()));
- }
-
- @Test
- public void testSerial() {
- Complex z = new Complex(3.0, 4.0);
- Assert.assertEquals(z, TestUtils.serializeAndRecover(z));
- Complex ncmplx = (Complex)TestUtils.serializeAndRecover(oneNaN);
- Assert.assertEquals(nanZero, ncmplx);
- Assert.assertTrue(ncmplx.isNaN());
- Complex infcmplx = (Complex)TestUtils.serializeAndRecover(infInf);
- Assert.assertEquals(infInf, infcmplx);
- Assert.assertTrue(infcmplx.isInfinite());
- TestComplex tz = new TestComplex(3.0, 4.0);
- Assert.assertEquals(tz, TestUtils.serializeAndRecover(tz));
- TestComplex ntcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(oneNaN));
- Assert.assertEquals(nanZero, ntcmplx);
- Assert.assertTrue(ntcmplx.isNaN());
- TestComplex inftcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(infInf));
- Assert.assertEquals(infInf, inftcmplx);
- Assert.assertTrue(inftcmplx.isInfinite());
- }
-
- /**
- * Class to test extending Complex
- */
- public static class TestComplex extends Complex {
-
- /**
- * Serialization identifier.
- */
- private static final long serialVersionUID = 3268726724160389237L;
-
- public TestComplex(double real, double imaginary) {
- super(real, imaginary);
- }
-
- public TestComplex(Complex other){
- this(other.getReal(), other.getImaginary());
- }
-
- @Override
- protected TestComplex createComplex(double real, double imaginary){
- return new TestComplex(real, imaginary);
- }
-
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/35378d9c/src/test/java/org/apache/commons/math4/complex/ComplexUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/complex/ComplexUtilsTest.java b/src/test/java/org/apache/commons/math4/complex/ComplexUtilsTest.java
index fbff674..5d49afe 100644
--- a/src/test/java/org/apache/commons/math4/complex/ComplexUtilsTest.java
+++ b/src/test/java/org/apache/commons/math4/complex/ComplexUtilsTest.java
@@ -17,9 +17,8 @@
package org.apache.commons.math4.complex;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.TestUtils;
-import org.apache.commons.math4.complex.Complex;
-import org.apache.commons.math4.complex.ComplexUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.IntegerSequence;
@@ -218,17 +217,17 @@ public class ComplexUtilsTest {
@Test
public void testPolar2ComplexNaN() {
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1));
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan));
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, nan));
+ Assert.assertTrue(ComplexUtils.polar2Complex(nan, 1).isNaN());
+ Assert.assertTrue(ComplexUtils.polar2Complex(1, nan).isNaN());
+ Assert.assertTrue(ComplexUtils.polar2Complex(nan, nan).isNaN());
}
@Test
public void testPolar2ComplexInf() {
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf));
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, negInf));
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf));
- TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, negInf));
+ Assert.assertTrue(ComplexUtils.polar2Complex(1, inf).isNaN());
+ Assert.assertTrue(ComplexUtils.polar2Complex(1, negInf).isNaN());
+ Assert.assertTrue(ComplexUtils.polar2Complex(inf, inf).isNaN());
+ Assert.assertTrue(ComplexUtils.polar2Complex(inf, negInf).isNaN());
TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi / 4));
TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi / 4));
[2/5] [math] MATH-1416: Depend on "Commons Numbers".
Posted by er...@apache.org.
MATH-1416: Depend on "Commons Numbers".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/db37a6d8
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/db37a6d8
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/db37a6d8
Branch: refs/heads/master
Commit: db37a6d84458bdc59570f40a678ab5818d55bd99
Parents: 0f25f11
Author: Gilles <er...@apache.org>
Authored: Wed Feb 28 02:29:31 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Feb 28 02:29:31 2018 +0100
----------------------------------------------------------------------
.../apache/commons/math4/transform/FastCosineTransformer.java | 4 ++--
.../apache/commons/math4/transform/FastFourierTransformer.java | 4 ++--
.../org/apache/commons/math4/transform/FastSineTransformer.java | 4 ++--
.../java/org/apache/commons/math4/transform/TransformUtils.java | 2 +-
.../commons/math4/transform/FastFourierTransformerTest.java | 2 +-
5 files changed, 8 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/db37a6d8/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java
index c2c4562..7515009 100644
--- a/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java
+++ b/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java
@@ -18,12 +18,12 @@ package org.apache.commons.math4.transform;
import java.io.Serializable;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.analysis.FunctionUtils;
import org.apache.commons.math4.analysis.UnivariateFunction;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
-import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.util.FastMath;
/**
http://git-wip-us.apache.org/repos/asf/commons-math/blob/db37a6d8/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java
index 522ea64..bd97ea8 100644
--- a/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java
+++ b/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java
@@ -17,14 +17,14 @@
package org.apache.commons.math4.transform;
import java.io.Serializable;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.analysis.FunctionUtils;
import org.apache.commons.math4.analysis.UnivariateFunction;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.MathIllegalStateException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
-import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
http://git-wip-us.apache.org/repos/asf/commons-math/blob/db37a6d8/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java
index b4b27ec..1b4df47 100644
--- a/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java
+++ b/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java
@@ -18,12 +18,12 @@ package org.apache.commons.math4.transform;
import java.io.Serializable;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.analysis.FunctionUtils;
import org.apache.commons.math4.analysis.UnivariateFunction;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
-import org.apache.commons.numbers.core.ArithmeticUtils;
import org.apache.commons.math4.util.FastMath;
/**
http://git-wip-us.apache.org/repos/asf/commons-math/blob/db37a6d8/src/main/java/org/apache/commons/math4/transform/TransformUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/transform/TransformUtils.java b/src/main/java/org/apache/commons/math4/transform/TransformUtils.java
index 27915b2..c37d03f 100644
--- a/src/main/java/org/apache/commons/math4/transform/TransformUtils.java
+++ b/src/main/java/org/apache/commons/math4/transform/TransformUtils.java
@@ -18,7 +18,7 @@ package org.apache.commons.math4.transform;
import java.util.Arrays;
-import org.apache.commons.math4.complex.Complex;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
http://git-wip-us.apache.org/repos/asf/commons-math/blob/db37a6d8/src/test/java/org/apache/commons/math4/transform/FastFourierTransformerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/transform/FastFourierTransformerTest.java b/src/test/java/org/apache/commons/math4/transform/FastFourierTransformerTest.java
index e520269..54a8854 100644
--- a/src/test/java/org/apache/commons/math4/transform/FastFourierTransformerTest.java
+++ b/src/test/java/org/apache/commons/math4/transform/FastFourierTransformerTest.java
@@ -18,10 +18,10 @@ package org.apache.commons.math4.transform;
import java.util.Random;
+import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.analysis.function.Sin;
import org.apache.commons.math4.analysis.function.Sinc;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
[3/5] [math] MATH-1416: Depend on "Commons Numbers".
Posted by er...@apache.org.
MATH-1416: Depend on "Commons Numbers".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/56b28f34
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/56b28f34
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/56b28f34
Branch: refs/heads/master
Commit: 56b28f34d934efa0db1b753e83161fe12f8e44b1
Parents: db37a6d
Author: Gilles <er...@apache.org>
Authored: Wed Feb 28 02:41:47 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Feb 28 02:41:47 2018 +0100
----------------------------------------------------------------------
.../math4/analysis/solvers/LaguerreSolver.java | 4 +--
.../analysis/solvers/LaguerreSolverTest.java | 37 ++++++++++++++++++--
2 files changed, 36 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/56b28f34/src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java b/src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java
index ab01885..f0ecdc8 100644
--- a/src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java
+++ b/src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java
@@ -16,9 +16,9 @@
*/
package org.apache.commons.math4.analysis.solvers;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.complex.ComplexUtils;
import org.apache.commons.math4.analysis.polynomials.PolynomialFunction;
-import org.apache.commons.math4.complex.Complex;
-import org.apache.commons.math4.complex.ComplexUtils;
import org.apache.commons.math4.exception.NoBracketingException;
import org.apache.commons.math4.exception.NoDataException;
import org.apache.commons.math4.exception.NullArgumentException;
http://git-wip-us.apache.org/repos/asf/commons-math/blob/56b28f34/src/test/java/org/apache/commons/math4/analysis/solvers/LaguerreSolverTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/analysis/solvers/LaguerreSolverTest.java b/src/test/java/org/apache/commons/math4/analysis/solvers/LaguerreSolverTest.java
index 6100710..caee1cc 100644
--- a/src/test/java/org/apache/commons/math4/analysis/solvers/LaguerreSolverTest.java
+++ b/src/test/java/org/apache/commons/math4/analysis/solvers/LaguerreSolverTest.java
@@ -16,10 +16,10 @@
*/
package org.apache.commons.math4.analysis.solvers;
-import org.apache.commons.math4.TestUtils;
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.core.Precision;
import org.apache.commons.math4.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math4.analysis.solvers.LaguerreSolver;
-import org.apache.commons.math4.complex.Complex;
import org.apache.commons.math4.exception.NoBracketingException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.util.FastMath;
@@ -129,7 +129,7 @@ public final class LaguerreSolverTest {
new Complex(0.5, -0.5 * FastMath.sqrt(3.0)) }) {
final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
FastMath.abs(expected.abs() * solver.getRelativeAccuracy()));
- TestUtils.assertContains(result, expected, tolerance);
+ assertContains(result, expected, tolerance);
}
}
@@ -157,4 +157,35 @@ public final class LaguerreSolverTest {
// expected
}
}
+
+ /**
+ * Fails iff values does not contain a number within epsilon of z.
+ *
+ * @param msg message to return with failure
+ * @param values complex array to search
+ * @param z value sought
+ * @param epsilon tolerance
+ */
+ private static void assertContains(String msg, Complex[] values,
+ Complex z, double epsilon) {
+ for (Complex value : values) {
+ if (Precision.equals(value.getReal(), z.getReal(), epsilon) &&
+ Precision.equals(value.getImaginary(), z.getImaginary(), epsilon)) {
+ return;
+ }
+ }
+ Assert.fail(msg + " Unable to find " + z);
+ }
+
+ /**
+ * Fails iff values does not contain a number within epsilon of z.
+ *
+ * @param values complex array to search
+ * @param z value sought
+ * @param epsilon tolerance
+ */
+ private static void assertContains(Complex[] values,
+ Complex z, double epsilon) {
+ assertContains(null, values, z, epsilon);
+ }
}
[5/5] [math] Removed unused classes.
Posted by er...@apache.org.
Removed unused classes.
Class "Complex" is replaced by its equivalent in "Commons Numbers".
Class "ComplexField" has no replacement: see ongoing discussion on the
"dev" ML (and issue NUMBERS-51 on JIRA).
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/583d9ec8
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/583d9ec8
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/583d9ec8
Branch: refs/heads/master
Commit: 583d9ec8647a7f667bb8f22cecf9859187149ade
Parents: 35378d9
Author: Gilles <er...@apache.org>
Authored: Wed Feb 28 03:37:54 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Feb 28 03:37:54 2018 +0100
----------------------------------------------------------------------
.../apache/commons/math4/complex/Complex.java | 1308 ------------------
.../commons/math4/complex/ComplexField.java | 86 --
.../commons/math4/complex/ComplexFieldTest.java | 44 -
3 files changed, 1438 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/583d9ec8/src/main/java/org/apache/commons/math4/complex/Complex.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/complex/Complex.java b/src/main/java/org/apache/commons/math4/complex/Complex.java
deleted file mode 100644
index f283397..0000000
--- a/src/main/java/org/apache/commons/math4/complex/Complex.java
+++ /dev/null
@@ -1,1308 +0,0 @@
-/*
- * 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.math4.complex;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.math4.FieldElement;
-import org.apache.commons.math4.exception.NotPositiveException;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.exception.util.LocalizedFormats;
-import org.apache.commons.math4.util.FastMath;
-import org.apache.commons.math4.util.MathUtils;
-import org.apache.commons.numbers.core.Precision;
-
-/**
- * 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
- * infinite values according to the rules for {@link java.lang.Double}, i.e.
- * {@link #equals} is an equivalence relation for all instances that have
- * a {@code NaN} in either real or imaginary part, e.g. the following are
- * considered equal:
- * <ul>
- * <li>{@code 1 + NaNi}</li>
- * <li>{@code NaN + i}</li>
- * <li>{@code NaN + NaNi}</li>
- * </ul><p>
- * Note that this contradicts the IEEE-754 standard for floating
- * point numbers (according to which the test {@code x == x} must fail if
- * {@code x} is {@code NaN}). The method
- * {@link org.apache.commons.numbers.core.Precision#equals(double,double,int)
- * equals for primitive double} in {@link org.apache.commons.numbers.core.Precision}
- * conforms with IEEE-754 while this class conforms with the standard behavior
- * for Java object types.</p>
- *
- */
-public class Complex implements FieldElement<Complex>, Serializable {
- /** The square root of -1. A number representing "0.0 + 1.0i" */
- public static final Complex I = new Complex(0.0, 1.0);
- // CHECKSTYLE: stop ConstantName
- /** A complex number representing "NaN + NaNi" */
- public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
- // CHECKSTYLE: resume ConstantName
- /** A complex number representing "+INF + INFi" */
- public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
- /** A complex number representing "1.0 + 0.0i" */
- public static final Complex ONE = new Complex(1.0, 0.0);
- /** A complex number representing "0.0 + 0.0i" */
- public static final Complex ZERO = new Complex(0.0, 0.0);
-
- /** Serializable version identifier */
- private static final long serialVersionUID = -6195664516687396620L;
-
- /** The imaginary part. */
- private final double imaginary;
- /** The real part. */
- private final double real;
- /** Record whether this complex number is equal to NaN. */
- private final transient boolean isNaN;
- /** Record whether this complex number is infinite. */
- private final transient boolean isInfinite;
-
- /**
- * Create a complex number given only the real part.
- *
- * @param real Real part.
- */
- public Complex(double real) {
- this(real, 0.0);
- }
-
- /**
- * Create a complex number given the real and imaginary parts.
- *
- * @param real Real part.
- * @param imaginary Imaginary part.
- */
- public Complex(double real, double imaginary) {
- this.real = real;
- this.imaginary = imaginary;
-
- isNaN = Double.isNaN(real) || Double.isNaN(imaginary);
- isInfinite = !isNaN &&
- (Double.isInfinite(real) || Double.isInfinite(imaginary));
- }
-
- /**
- * 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.
- *
- * @return the absolute value.
- */
- public double abs() {
- if (isNaN) {
- return Double.NaN;
- }
- if (isInfinite()) {
- return Double.POSITIVE_INFINITY;
- }
- if (FastMath.abs(real) < FastMath.abs(imaginary)) {
- if (imaginary == 0.0) {
- return FastMath.abs(real);
- }
- double q = real / imaginary;
- return FastMath.abs(imaginary) * FastMath.sqrt(1 + q * q);
- } else {
- if (real == 0.0) {
- return FastMath.abs(imaginary);
- }
- double q = imaginary / real;
- return FastMath.abs(real) * FastMath.sqrt(1 + q * q);
- }
- }
-
- /**
- * Returns a {@code Complex} whose value is
- * {@code (this + addend)}.
- * Uses the definitional formula
- * <p>
- * {@code (a + bi) + (c + di) = (a+c) + (b+d)i}
- * </p>
- * If either {@code this} or {@code addend} has a {@code NaN} value in
- * either part, {@link #NaN} is returned; otherwise {@code Infinite}
- * and {@code NaN} values are returned in the parts of the result
- * according to the rules for {@link java.lang.Double} arithmetic.
- *
- * @param addend Value to be added to this {@code Complex}.
- * @return {@code this + addend}.
- * @throws NullArgumentException if {@code addend} is {@code null}.
- */
- @Override
- public Complex add(Complex addend) throws NullArgumentException {
- MathUtils.checkNotNull(addend);
- if (isNaN || addend.isNaN) {
- return NaN;
- }
-
- return createComplex(real + addend.getReal(),
- imaginary + addend.getImaginary());
- }
-
- /**
- * Returns a {@code Complex} whose value is {@code (this + addend)},
- * with {@code addend} interpreted as a real number.
- *
- * @param addend Value to be added to this {@code Complex}.
- * @return {@code this + addend}.
- * @see #add(Complex)
- */
- public Complex add(double addend) {
- if (isNaN || Double.isNaN(addend)) {
- return NaN;
- }
-
- return createComplex(real + addend, imaginary);
- }
-
- /**
- * Returns the conjugate of this complex number.
- * 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}.
- * </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.
- */
- public Complex conjugate() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(real, -imaginary);
- }
-
- /**
- * Returns a {@code Complex} whose value is
- * {@code (this / divisor)}.
- * Implements the definitional formula
- * <pre>
- * <code>
- * a + bi ac + bd + (bc - ad)i
- * ----------- = -------------------------
- * c + di c<sup>2</sup> + d<sup>2</sup>
- * </code>
- * </pre>
- * but uses
- * <a href="http://doi.acm.org/10.1145/1039813.1039814">
- * prescaling of operands</a> to limit the effects of overflows and
- * underflows in the computation.
- * <p>
- * {@code Infinite} and {@code NaN} values are handled according to the
- * following rules, applied in the order presented:
- * <ul>
- * <li>If either {@code this} or {@code divisor} has a {@code NaN} value
- * in either part, {@link #NaN} is returned.
- * </li>
- * <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned.
- * </li>
- * <li>If {@code this} and {@code divisor} are both infinite,
- * {@link #NaN} is returned.
- * </li>
- * <li>If {@code this} is finite (i.e., has no {@code Infinite} or
- * {@code NaN} parts) and {@code divisor} is infinite (one or both parts
- * infinite), {@link #ZERO} is returned.
- * </li>
- * <li>If {@code this} is infinite and {@code divisor} is finite,
- * {@code NaN} values are returned in the parts of the result if the
- * {@link java.lang.Double} rules applied to the definitional formula
- * force {@code NaN} results.
- * </li>
- * </ul>
- *
- * @param divisor Value by which this {@code Complex} is to be divided.
- * @return {@code this / divisor}.
- * @throws NullArgumentException if {@code divisor} is {@code null}.
- */
- @Override
- public Complex divide(Complex divisor)
- throws NullArgumentException {
- MathUtils.checkNotNull(divisor);
- if (isNaN || divisor.isNaN) {
- return NaN;
- }
-
- final double c = divisor.getReal();
- final double d = divisor.getImaginary();
- if (c == 0.0 && d == 0.0) {
- return NaN;
- }
-
- if (divisor.isInfinite() && !isInfinite()) {
- return ZERO;
- }
-
- if (FastMath.abs(c) < FastMath.abs(d)) {
- double q = c / d;
- double denominator = c * q + d;
- return createComplex((real * q + imaginary) / denominator,
- (imaginary * q - real) / denominator);
- } else {
- double q = d / c;
- double denominator = d * q + c;
- return createComplex((imaginary * q + real) / denominator,
- (imaginary - real * q) / denominator);
- }
- }
-
- /**
- * Returns a {@code Complex} whose value is {@code (this / divisor)},
- * with {@code divisor} interpreted as a real number.
- *
- * @param divisor Value by which this {@code Complex} is to be divided.
- * @return {@code this / divisor}.
- * @see #divide(Complex)
- */
- public Complex divide(double divisor) {
- if (isNaN || Double.isNaN(divisor)) {
- return NaN;
- }
- if (divisor == 0d) {
- return NaN;
- }
- if (Double.isInfinite(divisor)) {
- return !isInfinite() ? ZERO : NaN;
- }
- return createComplex(real / divisor,
- imaginary / divisor);
- }
-
- /** {@inheritDoc} */
- @Override
- public Complex reciprocal() {
- if (isNaN) {
- return NaN;
- }
-
- if (real == 0.0 && imaginary == 0.0) {
- return INF;
- }
-
- if (isInfinite) {
- return ZERO;
- }
-
- if (FastMath.abs(real) < FastMath.abs(imaginary)) {
- double q = real / imaginary;
- double scale = 1. / (real * q + imaginary);
- return createComplex(scale * q, -scale);
- } else {
- double q = imaginary / real;
- double scale = 1. / (imaginary * q + real);
- return createComplex(scale, -scale * q);
- }
- }
-
- /**
- * Test for equality with another object.
- * If both the real and imaginary parts of two complex numbers
- * are exactly the same, and neither is {@code Double.NaN}, the two
- * Complex objects are considered to be equal.
- * The behavior is the same as for JDK's {@link Double#equals(Object)
- * Double}:
- * <ul>
- * <li>All {@code NaN} values are considered to be equal,
- * i.e, if either (or both) real and imaginary parts of the complex
- * number are equal to {@code Double.NaN}, the complex number is equal
- * to {@code NaN}.
- * </li>
- * <li>
- * Instances constructed with different representations of zero (i.e.
- * either "0" or "-0") are <em>not</em> considered to be equal.
- * </li>
- * </ul>
- *
- * @param other Object to test for equality with this instance.
- * @return {@code true} if the objects are equal, {@code false} if object
- * is {@code null}, not an instance of {@code Complex}, or not equal to
- * this instance.
- */
- @Override
- public boolean equals(Object other) {
- if (this == other) {
- return true;
- }
- if (other instanceof Complex){
- Complex c = (Complex) other;
- if (c.isNaN) {
- return isNaN;
- } else {
- return MathUtils.equals(real, c.real) &&
- MathUtils.equals(imaginary, c.imaginary);
- }
- }
- return false;
- }
-
- /**
- * Test for the floating-point equality between Complex objects.
- * It returns {@code true} if both arguments are equal or within the
- * range of allowed error (inclusive).
- *
- * @param x First value (cannot be {@code null}).
- * @param y Second value (cannot be {@code null}).
- * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
- * values between the real (resp. imaginary) parts of {@code x} and
- * {@code y}.
- * @return {@code true} if there are fewer than {@code maxUlps} floating
- * point values between the real (resp. imaginary) parts of {@code x}
- * and {@code y}.
- *
- * @see Precision#equals(double,double,int)
- * @since 3.3
- */
- public static boolean equals(Complex x, Complex y, int maxUlps) {
- return Precision.equals(x.real, y.real, maxUlps) &&
- Precision.equals(x.imaginary, y.imaginary, maxUlps);
- }
-
- /**
- * Returns {@code true} iff the values are equal as defined by
- * {@link #equals(Complex,Complex,int) equals(x, y, 1)}.
- *
- * @param x First value (cannot be {@code null}).
- * @param y Second value (cannot be {@code null}).
- * @return {@code true} if the values are equal.
- *
- * @since 3.3
- */
- public static boolean equals(Complex x, Complex y) {
- return equals(x, y, 1);
- }
-
- /**
- * Returns {@code true} if, both for the real part and for the imaginary
- * part, there is no double value strictly between the arguments or the
- * difference between them is within the range of allowed error
- * (inclusive). Returns {@code false} if either of the arguments is NaN.
- *
- * @param x First value (cannot be {@code null}).
- * @param y Second value (cannot be {@code null}).
- * @param eps Amount of allowed absolute error.
- * @return {@code true} if the values are two adjacent floating point
- * numbers or they are within range of each other.
- *
- * @see Precision#equals(double,double,double)
- * @since 3.3
- */
- public static boolean equals(Complex x, Complex y, double eps) {
- return Precision.equals(x.real, y.real, eps) &&
- Precision.equals(x.imaginary, y.imaginary, eps);
- }
-
- /**
- * Returns {@code true} if, both for the real part and for the imaginary
- * part, there is no double value strictly between the arguments or the
- * relative difference between them is smaller or equal to the given
- * tolerance. Returns {@code false} if either of the arguments is NaN.
- *
- * @param x First value (cannot be {@code null}).
- * @param y Second value (cannot be {@code null}).
- * @param eps Amount of allowed relative error.
- * @return {@code true} if the values are two adjacent floating point
- * numbers or they are within range of each other.
- *
- * @see Precision#equalsWithRelativeTolerance(double,double,double)
- * @since 3.3
- */
- public static boolean equalsWithRelativeTolerance(Complex x, Complex y,
- double eps) {
- return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) &&
- Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps);
- }
-
- /**
- * Get a hashCode for the complex number.
- * Any {@code Double.NaN} value in real or imaginary part produces
- * the same hash code {@code 7}.
- *
- * @return a hash code value for this object.
- */
- @Override
- public int hashCode() {
- if (isNaN) {
- return 7;
- }
- return 37 * (17 * MathUtils.hash(imaginary) +
- MathUtils.hash(real));
- }
-
- /**
- * Access the imaginary part.
- *
- * @return the imaginary part.
- */
- public double getImaginary() {
- return imaginary;
- }
-
- /**
- * Access the real part.
- *
- * @return the real part.
- */
- public double getReal() {
- return real;
- }
-
- /**
- * 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
- * {@code NaN}; false otherwise.
- */
- public boolean isNaN() {
- return isNaN;
- }
-
- /**
- * Checks whether either the real or imaginary part of this complex number
- * takes an infinite value (either {@code Double.POSITIVE_INFINITY} or
- * {@code Double.NEGATIVE_INFINITY}) and neither part
- * is {@code NaN}.
- *
- * @return true if one or both parts of this complex number are infinite
- * and neither part is {@code NaN}.
- */
- public boolean isInfinite() {
- return isInfinite;
- }
-
- /**
- * Returns a {@code Complex} whose value is {@code this * factor}.
- * Implements preliminary checks for {@code NaN} and infinity followed by
- * the definitional formula:
- * <p>
- * {@code (a + bi)(c + di) = (ac - bd) + (ad + bc)i}
- * </p>
- * Returns {@link #NaN} if either {@code this} or {@code factor} has one or
- * more {@code NaN} parts.
- * <p>
- * Returns {@link #INF} if neither {@code this} nor {@code factor} has one
- * or more {@code NaN} parts and if either {@code this} or {@code factor}
- * has one or more infinite parts (same result is returned regardless of
- * the sign of the components).
- * </p><p>
- * Returns finite values in components of the result per the definitional
- * formula in all remaining cases.</p>
- *
- * @param factor value to be multiplied by this {@code Complex}.
- * @return {@code this * factor}.
- * @throws NullArgumentException if {@code factor} is {@code null}.
- */
- @Override
- public Complex multiply(Complex factor)
- throws NullArgumentException {
- MathUtils.checkNotNull(factor);
- if (isNaN || factor.isNaN) {
- return NaN;
- }
- if (Double.isInfinite(real) ||
- Double.isInfinite(imaginary) ||
- Double.isInfinite(factor.real) ||
- Double.isInfinite(factor.imaginary)) {
- // we don't use isInfinite() to avoid testing for NaN again
- return INF;
- }
- return createComplex(real * factor.real - imaginary * factor.imaginary,
- real * factor.imaginary + imaginary * factor.real);
- }
-
- /**
- * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
- * interpreted as a integer number.
- *
- * @param factor value to be multiplied by this {@code Complex}.
- * @return {@code this * factor}.
- * @see #multiply(Complex)
- */
- @Override
- public Complex multiply(final int factor) {
- if (isNaN) {
- return NaN;
- }
- if (Double.isInfinite(real) ||
- Double.isInfinite(imaginary)) {
- return INF;
- }
- return createComplex(real * factor, imaginary * factor);
- }
-
- /**
- * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
- * interpreted as a real number.
- *
- * @param factor value to be multiplied by this {@code Complex}.
- * @return {@code this * factor}.
- * @see #multiply(Complex)
- */
- public Complex multiply(double factor) {
- if (isNaN || Double.isNaN(factor)) {
- return NaN;
- }
- if (Double.isInfinite(real) ||
- Double.isInfinite(imaginary) ||
- Double.isInfinite(factor)) {
- // we don't use isInfinite() to avoid testing for NaN again
- return INF;
- }
- return createComplex(real * factor, imaginary * factor);
- }
-
- /**
- * 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}.
- *
- * @return {@code -this}.
- */
- @Override
- public Complex negate() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(-real, -imaginary);
- }
-
- /**
- * Returns a {@code Complex} whose value is
- * {@code (this - subtrahend)}.
- * Uses the definitional formula
- * <p>
- * {@code (a + bi) - (c + di) = (a-c) + (b-d)i}
- * </p>
- * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part,
- * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are
- * returned in the parts of the result according to the rules for
- * {@link java.lang.Double} arithmetic.
- *
- * @param subtrahend value to be subtracted from this {@code Complex}.
- * @return {@code this - subtrahend}.
- * @throws NullArgumentException if {@code subtrahend} is {@code null}.
- */
- @Override
- public Complex subtract(Complex subtrahend)
- throws NullArgumentException {
- MathUtils.checkNotNull(subtrahend);
- if (isNaN || subtrahend.isNaN) {
- return NaN;
- }
-
- return createComplex(real - subtrahend.getReal(),
- imaginary - subtrahend.getImaginary());
- }
-
- /**
- * Returns a {@code Complex} whose value is
- * {@code (this - subtrahend)}.
- *
- * @param subtrahend value to be subtracted from this {@code Complex}.
- * @return {@code this - subtrahend}.
- * @see #subtract(Complex)
- */
- public Complex subtract(double subtrahend) {
- if (isNaN || Double.isNaN(subtrahend)) {
- return NaN;
- }
- return createComplex(real - subtrahend, imaginary);
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/InverseCosine.html" TARGET="_top">
- * inverse cosine</a> of this complex number.
- * Implements the formula:
- * <p>
- * {@code acos(z) = -i (log(z + i (sqrt(1 - z<sup>2</sup>))))}
- * </p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN} or infinite.
- *
- * @return the inverse cosine of this complex number.
- * @since 1.2
- */
- public Complex acos() {
- if (isNaN) {
- return NaN;
- }
-
- return this.add(this.sqrt1z().multiply(I)).log().multiply(I.negate());
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/InverseSine.html" TARGET="_top">
- * inverse sine</a> of this complex number.
- * Implements the formula:
- * <p>
- * {@code asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))}
- * </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 sine of this complex number.
- * @since 1.2
- */
- public Complex asin() {
- if (isNaN) {
- return NaN;
- }
-
- return sqrt1z().add(this.multiply(I)).log().multiply(I.negate());
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top">
- * inverse tangent</a> of this complex number.
- * Implements the formula:
- * <p>
- * {@code atan(z) = (i/2) log((i + z)/(i - z))}
- * </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 tangent of this complex number
- * @since 1.2
- */
- public Complex atan() {
- if (isNaN) {
- return NaN;
- }
-
- return this.add(I).divide(I.subtract(this)).log()
- .multiply(I.divide(createComplex(2.0, 0.0)));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/Cosine.html" TARGET="_top">
- * cosine</a> of this complex number.
- * Implements the formula:
- * <p>
- * {@code cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)i}
- * </p><p>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos},
- * {@link FastMath#cosh} and {@link FastMath#sinh}.
- * </p><p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p><p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.</p>
- * <pre>
- * Examples:
- * <code>
- * cos(1 ± INFINITY i) = 1 \u2213 INFINITY i
- * cos(±INFINITY + i) = NaN + NaN i
- * cos(±INFINITY ± INFINITY i) = NaN + NaN i
- * </code>
- * </pre>
- *
- * @return the cosine of this complex number.
- * @since 1.2
- */
- public Complex cos() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(FastMath.cos(real) * FastMath.cosh(imaginary),
- -FastMath.sin(real) * FastMath.sinh(imaginary));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/HyperbolicCosine.html" TARGET="_top">
- * hyperbolic cosine</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * cosh(a + bi) = cosh(a)cos(b) + sinh(a)sin(b)i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos},
- * {@link FastMath#cosh} and {@link FastMath#sinh}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * cosh(1 ± INFINITY i) = NaN + NaN i
- * cosh(±INFINITY + i) = INFINITY ± INFINITY i
- * cosh(±INFINITY ± INFINITY i) = NaN + NaN i
- * </code>
- * </pre>
- *
- * @return the hyperbolic cosine of this complex number.
- * @since 1.2
- */
- public Complex cosh() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(FastMath.cosh(real) * FastMath.cos(imaginary),
- FastMath.sinh(real) * FastMath.sin(imaginary));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/ExponentialFunction.html" TARGET="_top">
- * exponential function</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * exp(a + bi) = exp(a)cos(b) + exp(a)sin(b)i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#exp}, {@link FastMath#cos}, and
- * {@link FastMath#sin}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * exp(1 ± INFINITY i) = NaN + NaN i
- * exp(INFINITY + i) = INFINITY + INFINITY i
- * exp(-INFINITY + i) = 0 + 0i
- * exp(±INFINITY ± INFINITY i) = NaN + NaN i
- * </code>
- * </pre>
- *
- * @return <code><i>e</i><sup>this</sup></code>.
- * @since 1.2
- */
- public Complex exp() {
- if (isNaN) {
- return NaN;
- }
-
- double expReal = FastMath.exp(real);
- return createComplex(expReal * FastMath.cos(imaginary),
- expReal * FastMath.sin(imaginary));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/NaturalLogarithm.html" TARGET="_top">
- * natural logarithm</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * log(a + bi) = ln(|a + bi|) + arg(a + bi)i
- * </code>
- * </pre>
- * where ln on the right hand side is {@link FastMath#log},
- * {@code |a + bi|} is the modulus, {@link Complex#abs}, and
- * {@code arg(a + bi) = }{@link FastMath#atan2}(b, a).
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite (or critical) values in real or imaginary parts of the input may
- * result in infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * log(1 ± INFINITY i) = INFINITY ± (π/2)i
- * log(INFINITY + i) = INFINITY + 0i
- * log(-INFINITY + i) = INFINITY + πi
- * log(INFINITY ± INFINITY i) = INFINITY ± (π/4)i
- * log(-INFINITY ± INFINITY i) = INFINITY ± (3π/4)i
- * log(0 + 0i) = -INFINITY + 0i
- * </code>
- * </pre>
- *
- * @return the value <code>ln this</code>, the natural logarithm
- * of {@code this}.
- * @since 1.2
- */
- public Complex log() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(FastMath.log(abs()),
- FastMath.atan2(imaginary, real));
- }
-
- /**
- * Returns of value of this complex number raised to the power of {@code x}.
- * Implements the formula:
- * <pre>
- * <code>
- * y<sup>x</sup> = exp(x·log(y))
- * </code>
- * </pre>
- * where {@code exp} and {@code log} are {@link #exp} and
- * {@link #log}, respectively.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN} or infinite, or if {@code y}
- * equals {@link Complex#ZERO}.</p>
- *
- * @param x exponent to which this {@code Complex} is to be raised.
- * @return <code> this<sup>x</sup></code>.
- * @throws NullArgumentException if x is {@code null}.
- * @since 1.2
- */
- public Complex pow(Complex x)
- throws NullArgumentException {
- MathUtils.checkNotNull(x);
- return this.log().multiply(x).exp();
- }
-
- /**
- * Returns of value of this complex number raised to the power of {@code x}.
- *
- * @param x exponent to which this {@code Complex} is to be raised.
- * @return <code>this<sup>x</sup></code>.
- * @see #pow(Complex)
- */
- public Complex pow(double x) {
- return this.log().multiply(x).exp();
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/Sine.html" TARGET="_top">
- * sine</a>
- * of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * sin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos},
- * {@link FastMath#cosh} and {@link FastMath#sinh}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p><p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or {@code NaN} values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * sin(1 ± INFINITY i) = 1 ± INFINITY i
- * sin(±INFINITY + i) = NaN + NaN i
- * sin(±INFINITY ± INFINITY i) = NaN + NaN i
- * </code>
- * </pre>
- *
- * @return the sine of this complex number.
- * @since 1.2
- */
- public Complex sin() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(FastMath.sin(real) * FastMath.cosh(imaginary),
- FastMath.cos(real) * FastMath.sinh(imaginary));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/HyperbolicSine.html" TARGET="_top">
- * hyperbolic sine</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * sinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos},
- * {@link FastMath#cosh} and {@link FastMath#sinh}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p><p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * sinh(1 ± INFINITY i) = NaN + NaN i
- * sinh(±INFINITY + i) = ± INFINITY + INFINITY i
- * sinh(±INFINITY ± INFINITY i) = NaN + NaN i
- * </code>
- * </pre>
- *
- * @return the hyperbolic sine of {@code this}.
- * @since 1.2
- */
- public Complex sinh() {
- if (isNaN) {
- return NaN;
- }
-
- return createComplex(FastMath.sinh(real) * FastMath.cos(imaginary),
- FastMath.cosh(real) * FastMath.sin(imaginary));
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top">
- * square root</a> of this complex number.
- * Implements the following algorithm to compute {@code sqrt(a + bi)}:
- * <ol><li>Let {@code t = sqrt((|a| + |a + bi|) / 2)}</li>
- * <li><pre>if {@code a ≥ 0} return {@code t + (b/2t)i}
- * else return {@code |b|/2t + sign(b)t i }</pre></li>
- * </ol>
- * where <ul>
- * <li>{@code |a| = }{@link FastMath#abs}(a)</li>
- * <li>{@code |a + bi| = }{@link Complex#abs}(a + bi)</li>
- * <li>{@code sign(b) = }{@link FastMath#copySign(double,double) copySign(1d, b)}
- * </ul>
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * sqrt(1 ± INFINITY i) = INFINITY + NaN i
- * sqrt(INFINITY + i) = INFINITY + 0i
- * sqrt(-INFINITY + i) = 0 + INFINITY i
- * sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i
- * sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i
- * </code>
- * </pre>
- *
- * @return the square root of {@code this}.
- * @since 1.2
- */
- public Complex sqrt() {
- if (isNaN) {
- return NaN;
- }
-
- if (real == 0.0 && imaginary == 0.0) {
- return createComplex(0.0, 0.0);
- }
-
- double t = FastMath.sqrt((FastMath.abs(real) + abs()) / 2.0);
- if (real >= 0.0) {
- return createComplex(t, imaginary / (2.0 * t));
- } else {
- return createComplex(FastMath.abs(imaginary) / (2.0 * t),
- FastMath.copySign(1d, imaginary) * t);
- }
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top">
- * square root</a> of <code>1 - this<sup>2</sup></code> for this complex
- * number.
- * Computes the result directly as
- * {@code sqrt(ONE.subtract(z.multiply(z)))}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- *
- * @return the square root of <code>1 - this<sup>2</sup></code>.
- * @since 1.2
- */
- public Complex sqrt1z() {
- return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt();
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/Tangent.html" TARGET="_top">
- * tangent</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and
- * {@link FastMath#sinh}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite (or critical) values in real or imaginary parts of the input may
- * result in infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * tan(a ± INFINITY i) = 0 ± i
- * tan(±INFINITY + bi) = NaN + NaN i
- * tan(±INFINITY ± INFINITY i) = NaN + NaN i
- * tan(±π/2 + 0 i) = ±INFINITY + NaN i
- * </code>
- * </pre>
- *
- * @return the tangent of {@code this}.
- * @since 1.2
- */
- public Complex tan() {
- if (isNaN || Double.isInfinite(real)) {
- return NaN;
- }
- if (imaginary > 20.0) {
- return createComplex(0.0, 1.0);
- }
- if (imaginary < -20.0) {
- return createComplex(0.0, -1.0);
- }
-
- double real2 = 2.0 * real;
- double imaginary2 = 2.0 * imaginary;
- double d = FastMath.cos(real2) + FastMath.cosh(imaginary2);
-
- return createComplex(FastMath.sin(real2) / d,
- FastMath.sinh(imaginary2) / d);
- }
-
- /**
- * Compute the
- * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html" TARGET="_top">
- * hyperbolic tangent</a> of this complex number.
- * Implements the formula:
- * <pre>
- * <code>
- * tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
- * </code>
- * </pre>
- * where the (real) functions on the right-hand side are
- * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and
- * {@link FastMath#sinh}.
- * <p>
- * Returns {@link Complex#NaN} if either real or imaginary part of the
- * input argument is {@code NaN}.
- * </p>
- * Infinite values in real or imaginary parts of the input may result in
- * infinite or NaN values returned in parts of the result.
- * <pre>
- * Examples:
- * <code>
- * tanh(a ± INFINITY i) = NaN + NaN i
- * tanh(±INFINITY + bi) = ±1 + 0 i
- * tanh(±INFINITY ± INFINITY i) = NaN + NaN i
- * tanh(0 + (π/2)i) = NaN + INFINITY i
- * </code>
- * </pre>
- *
- * @return the hyperbolic tangent of {@code this}.
- * @since 1.2
- */
- public Complex tanh() {
- if (isNaN || Double.isInfinite(imaginary)) {
- return NaN;
- }
- if (real > 20.0) {
- return createComplex(1.0, 0.0);
- }
- if (real < -20.0) {
- return createComplex(-1.0, 0.0);
- }
- double real2 = 2.0 * real;
- double imaginary2 = 2.0 * imaginary;
- double d = FastMath.cosh(real2) + FastMath.cos(imaginary2);
-
- return createComplex(FastMath.sinh(real2) / d,
- FastMath.sin(imaginary2) / d);
- }
-
-
-
- /**
- * 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.
- *
- * @return the argument of {@code this}.
- */
- public double getArgument() {
- return FastMath.atan2(getImaginary(), getReal());
- }
-
- /**
- * Computes the n-th roots of this complex number.
- * The nth roots are defined by the formula:
- * <pre>
- * <code>
- * z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))
- * </code>
- * </pre>
- * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
- * are respectively the {@link #abs() modulus} and
- * {@link #getArgument() argument} of this complex number.
- * <p>
- * If one or both parts of this complex number is NaN, a list with just
- * one element, {@link #NaN} is returned.
- * if neither part is NaN, but at least one part is infinite, the result
- * is a one-element list containing {@link #INF}.
- *
- * @param n Degree of root.
- * @return a List of all {@code n}-th roots of {@code this}.
- * @throws NotPositiveException if {@code n <= 0}.
- * @since 2.0
- */
- public List<Complex> nthRoot(int n) throws NotPositiveException {
-
- if (n <= 0) {
- throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
- n);
- }
-
- final List<Complex> result = new ArrayList<>();
-
- if (isNaN) {
- result.add(NaN);
- return result;
- }
- if (isInfinite()) {
- result.add(INF);
- return result;
- }
-
- // nth root of abs -- faster / more accurate to use a solver here?
- final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);
-
- // Compute nth roots of complex number with k = 0, 1, ... n-1
- final double nthPhi = getArgument() / n;
- final double slice = 2 * FastMath.PI / n;
- double innerPart = nthPhi;
- for (int k = 0; k < n ; k++) {
- // inner part
- final double realPart = nthRootOfAbs * FastMath.cos(innerPart);
- final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart);
- result.add(createComplex(realPart, imaginaryPart));
- innerPart += slice;
- }
-
- return result;
- }
-
- /**
- * Create a complex number given the real and imaginary parts.
- *
- * @param realPart Real part.
- * @param imaginaryPart Imaginary part.
- * @return a new complex number instance.
- * @since 1.2
- * @see #valueOf(double, double)
- */
- protected Complex createComplex(double realPart,
- double imaginaryPart) {
- return new Complex(realPart, imaginaryPart);
- }
-
- /**
- * Create a complex number given the real and imaginary parts.
- *
- * @param realPart Real part.
- * @param imaginaryPart Imaginary part.
- * @return a Complex instance.
- */
- public static Complex valueOf(double realPart,
- double imaginaryPart) {
- if (Double.isNaN(realPart) ||
- Double.isNaN(imaginaryPart)) {
- return NaN;
- }
- return new Complex(realPart, imaginaryPart);
- }
-
- /**
- * Create a complex number given only the real part.
- *
- * @param realPart Real part.
- * @return a Complex instance.
- */
- public static Complex valueOf(double realPart) {
- if (Double.isNaN(realPart)) {
- return NaN;
- }
- return new Complex(realPart);
- }
-
- /**
- * Resolve the transient fields in a deserialized Complex Object.
- * Subclasses will need to override {@link #createComplex} to
- * deserialize properly.
- *
- * @return A Complex instance with all fields resolved.
- * @since 2.0
- */
- protected final Object readResolve() {
- return createComplex(real, imaginary);
- }
-
- /** {@inheritDoc} */
- @Override
- public ComplexField getField() {
- return ComplexField.getInstance();
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return "(" + real + ", " + imaginary + ")";
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/583d9ec8/src/main/java/org/apache/commons/math4/complex/ComplexField.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/complex/ComplexField.java b/src/main/java/org/apache/commons/math4/complex/ComplexField.java
deleted file mode 100644
index c761941..0000000
--- a/src/main/java/org/apache/commons/math4/complex/ComplexField.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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.math4.complex;
-
-import java.io.Serializable;
-
-import org.apache.commons.math4.Field;
-import org.apache.commons.math4.FieldElement;
-
-/**
- * Representation of the complex numbers field.
- * <p>
- * This class is a singleton.
- * </p>
- * @see Complex
- * @since 2.0
- */
-public class ComplexField implements Field<Complex>, Serializable {
-
- /** Serializable version identifier. */
- private static final long serialVersionUID = -6130362688700788798L;
-
- /** Private constructor for the singleton.
- */
- private ComplexField() {
- }
-
- /** Get the unique instance.
- * @return the unique instance
- */
- public static ComplexField getInstance() {
- return LazyHolder.INSTANCE;
- }
-
- /** {@inheritDoc} */
- @Override
- public Complex getOne() {
- return Complex.ONE;
- }
-
- /** {@inheritDoc} */
- @Override
- public Complex getZero() {
- return Complex.ZERO;
- }
-
- /** {@inheritDoc} */
- @Override
- public Class<? extends FieldElement<Complex>> getRuntimeClass() {
- return Complex.class;
- }
-
- // CHECKSTYLE: stop HideUtilityClassConstructor
- /** Holder for the instance.
- * <p>We use here the Initialization On Demand Holder Idiom.</p>
- */
- private static class LazyHolder {
- /** Cached field instance. */
- private static final ComplexField INSTANCE = new ComplexField();
- }
- // CHECKSTYLE: resume HideUtilityClassConstructor
-
- /** Handle deserialization of the singleton.
- * @return the singleton instance
- */
- private Object readResolve() {
- // return the singleton instance
- return LazyHolder.INSTANCE;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/583d9ec8/src/test/java/org/apache/commons/math4/complex/ComplexFieldTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/complex/ComplexFieldTest.java b/src/test/java/org/apache/commons/math4/complex/ComplexFieldTest.java
deleted file mode 100644
index 50462fb..0000000
--- a/src/test/java/org/apache/commons/math4/complex/ComplexFieldTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.math4.complex;
-
-import org.apache.commons.math4.TestUtils;
-import org.apache.commons.math4.complex.Complex;
-import org.apache.commons.math4.complex.ComplexField;
-import org.junit.Assert;
-import org.junit.Test;
-
-public class ComplexFieldTest {
-
- @Test
- public void testZero() {
- Assert.assertEquals(Complex.ZERO, ComplexField.getInstance().getZero());
- }
-
- @Test
- public void testOne() {
- Assert.assertEquals(Complex.ONE, ComplexField.getInstance().getOne());
- }
-
- @Test
- public void testSerial() {
- // deserializing the singleton should give the singleton itself back
- ComplexField field = ComplexField.getInstance();
- Assert.assertTrue(field == TestUtils.serializeAndRecover(field));
- }
-
-}