merge of complex-constructor branch
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/5166d4be Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/5166d4be Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/5166d4be Branch: refs/heads/complex-constructors Commit: 5166d4beb42c1a71314312197d247ede015c2050 Parents: abec5ad Author: Eric Barnhill <[email protected]> Authored: Thu Mar 1 14:01:48 2018 +0100 Committer: Eric Barnhill <[email protected]> Committed: Thu Mar 1 14:01:48 2018 +0100 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 77 ++++++-------------- .../commons/numbers/complex/ComplexUtils.java | 62 ++++++++-------- .../commons/numbers/complex/RootsOfUnity.java | 6 +- 3 files changed, 57 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/5166d4be/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java index cfc9625..c36bea1 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java @@ -63,24 +63,28 @@ public class Complex implements Serializable { /** The real part. */ private final double real; + private Complex(double real, double imaginary) { + this.real = real; + this.imaginary = imaginary; + } + /** - * Create a complex number given only the real part. - * - * @param real Real part. - */ - public Complex(double real) { - this(real, 0); + * Create a complex number given the real and imaginary parts. + * + * @param real Real part. + * @param imaginary Imaginary part. + */ + public static Complex ofCartesian(double real, double imaginary) { + return new Complex(real, imaginary); } - /** - * 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; + /** + * Create a complex number given the real part. + * + * @param real Real part. + */ + public static Complex ofCartesian(double real) { + return new Complex(real, 0); } /** @@ -103,7 +107,7 @@ public class Complex implements Serializable { * @param theta the argument of the complex number to create * @return {@code Complex} */ - public Complex polar(double r, double theta) { + public static Complex ofPolar(double r, double theta) { checkNotNegative(r); return new Complex(r * Math.cos(theta), r * Math.sin(theta)); } @@ -115,7 +119,7 @@ public class Complex implements Serializable { * @param x {@code double} to build the cis number * @return {@code Complex} */ - public Complex cis(double x) { + public static Complex ofCis(double x) { return new Complex(Math.cos(x), Math.sin(x)); } @@ -163,7 +167,7 @@ public class Complex implements Serializable { public Complex proj() { if (Double.isInfinite(real) || Double.isInfinite(imaginary)) { - return new Complex(Double.POSITIVE_INFINITY); + return new Complex(Double.POSITIVE_INFINITY, 0); } else { return this; } @@ -1233,7 +1237,7 @@ public class Complex implements Serializable { // inner part final double realPart = nthRootOfAbs * Math.cos(innerPart); final double imaginaryPart = nthRootOfAbs * Math.sin(innerPart); - result.add(createComplex(realPart, imaginaryPart)); + result.add(new Complex(realPart, imaginaryPart)); innerPart += slice; } @@ -1241,41 +1245,6 @@ public class Complex implements Serializable { } /** - * Create a complex number given the real and imaginary parts. - * - * @param realPart Real part. - * @param imaginaryPart Imaginary part. - * @return a new complex number instance. - * @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) { - 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) { - return new Complex(realPart); - } - - /** * Resolve the transient fields in a deserialized Complex Object. * Subclasses will need to override {@link #createComplex} to * deserialize properly. http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/5166d4be/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java index b9f758a..684d406 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java @@ -55,7 +55,7 @@ public class ComplexUtils { if (r < 0) { throw new NegativeModulusException(r); } - return new Complex(r * Math.cos(theta), r * Math.sin(theta)); + return Complex.ofCartesian(r * Math.cos(theta), r * Math.sin(theta)); } /** @@ -74,7 +74,7 @@ public class ComplexUtils { if (r[x] < 0) { throw new NegativeModulusException(r[x]); } - c[x] = new Complex(r[x] * Math.cos(theta[x]), r[x] * Math.sin(theta[x])); + c[x] = Complex.ofCartesian(r[x] * Math.cos(theta[x]), r[x] * Math.sin(theta[x])); } return c; } @@ -126,7 +126,7 @@ public class ComplexUtils { * @since 1.0 */ public static Complex extractComplexFromRealArray(double[] real, int index) { - return new Complex(real[index]); + return Complex.ofCartesian(real[index]); } /** @@ -140,7 +140,7 @@ public class ComplexUtils { * @since 1.0 */ public static Complex extractComplexFromRealArray(float[] real, int index) { - return new Complex(real[index]); + return Complex.ofCartesian(real[index]); } /** @@ -154,7 +154,7 @@ public class ComplexUtils { * @since 1.0 */ public static Complex extractComplexFromImaginaryArray(double[] imaginary, int index) { - return new Complex(0, imaginary[index]); + return Complex.ofCartesian(0, imaginary[index]); } /** @@ -168,7 +168,7 @@ public class ComplexUtils { * @since 1.0 */ public static Complex extractComplexFromImaginaryArray(float[] imaginary, int index) { - return new Complex(0, imaginary[index]); + return Complex.ofCartesian(0, imaginary[index]); } /** @@ -232,13 +232,13 @@ public class ComplexUtils { * {@code index}. * * @param d array of interleaved complex numbers alternating real and imaginary values - * @param index location in the array This is the location by complex number, e.g. index number 5 in the array will return {@code new Complex(d[10], d[11])} + * @param index location in the array This is the location by complex number, e.g. index number 5 in the array will return {@code Complex.ofCartesian(d[10], d[11])} * @return {@code Complex}. * * @since 1.0 */ public static Complex extractComplexFromInterleavedArray(double[] d, int index) { - return new Complex(d[index * 2], d[index * 2 + 1]); + return Complex.ofCartesian(d[index * 2], d[index * 2 + 1]); } /** @@ -252,7 +252,7 @@ public class ComplexUtils { * @since 1.0 */ public static Complex extractComplexFromInterleavedArray(float[] f, int index) { - return new Complex(f[index * 2], f[index * 2 + 1]); + return Complex.ofCartesian(f[index * 2], f[index * 2 + 1]); } /** @@ -295,7 +295,7 @@ public class ComplexUtils { int index = 0; final Complex c[] = new Complex[real.length]; for (double d : real) { - c[index] = new Complex(d); + c[index] = Complex.ofCartesian(d); index++; } return c; @@ -313,7 +313,7 @@ public class ComplexUtils { int index = 0; final Complex c[] = new Complex[real.length]; for (float d : real) { - c[index] = new Complex(d); + c[index] = Complex.ofCartesian(d); index++; } return c; @@ -564,7 +564,7 @@ public class ComplexUtils { int index = 0; final Complex c[] = new Complex[imaginary.length]; for (double d : imaginary) { - c[index] = new Complex(0, d); + c[index] = Complex.ofCartesian(0, d); index++; } return c; @@ -582,7 +582,7 @@ public class ComplexUtils { int index = 0; final Complex c[] = new Complex[imaginary.length]; for (float d : imaginary) { - c[index] = new Complex(0, d); + c[index] = Complex.ofCartesian(0, d); index++; } return c; @@ -799,7 +799,7 @@ public class ComplexUtils { final int length = interleaved.length / 2; final Complex c[] = new Complex[length]; for (int n = 0; n < length; n++) { - c[n] = new Complex(interleaved[n * 2], interleaved[n * 2 + 1]); + c[n] = Complex.ofCartesian(interleaved[n * 2], interleaved[n * 2 + 1]); } return c; } @@ -817,7 +817,7 @@ public class ComplexUtils { final int length = interleaved.length / 2; final Complex c[] = new Complex[length]; for (int n = 0; n < length; n++) { - c[n] = new Complex(interleaved[n * 2], interleaved[n * 2 + 1]); + c[n] = Complex.ofCartesian(interleaved[n * 2], interleaved[n * 2 + 1]); } return c; } @@ -1220,14 +1220,14 @@ public class ComplexUtils { c = new Complex[w / 2][h]; for (int x = 0; x < w / 2; x++) { for (int y = 0; y < h; y++) { - c[x][y] = new Complex(i[x * 2][y], i[x * 2 + 1][y]); + c[x][y] = Complex.ofCartesian(i[x * 2][y], i[x * 2 + 1][y]); } } } else { c = new Complex[w][h / 2]; for (int x = 0; x < w; x++) { for (int y = 0; y < h / 2; y++) { - c[x][y] = new Complex(i[x][y * 2], i[x][y * 2 + 1]); + c[x][y] = Complex.ofCartesian(i[x][y * 2], i[x][y * 2 + 1]); } } } @@ -1271,7 +1271,7 @@ public class ComplexUtils { for (int x = 0; x < w / 2; x++) { for (int y = 0; y < h; y++) { for (int z = 0; z < d; z++) { - c[x][y][z] = new Complex(i[x * 2][y][z], i[x * 2 + 1][y][z]); + c[x][y][z] = Complex.ofCartesian(i[x * 2][y][z], i[x * 2 + 1][y][z]); } } } @@ -1280,7 +1280,7 @@ public class ComplexUtils { for (int x = 0; x < w; x++) { for (int y = 0; y < h / 2; y++) { for (int z = 0; z < d; z++) { - c[x][y][z] = new Complex(i[x][y * 2][z], i[x][y * 2 + 1][z]); + c[x][y][z] = Complex.ofCartesian(i[x][y * 2][z], i[x][y * 2 + 1][z]); } } } @@ -1289,7 +1289,7 @@ public class ComplexUtils { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { for (int z = 0; z < d / 2; z++) { - c[x][y][z] = new Complex(i[x][y][z * 2], i[x][y][z * 2 + 1]); + c[x][y][z] = Complex.ofCartesian(i[x][y][z * 2], i[x][y][z * 2 + 1]); } } } @@ -1322,7 +1322,7 @@ public class ComplexUtils { for (int y = 0; y < h; y++) { for (int z = 0; z < d; z++) { for (int t = 0; t < v; t++) { - c[x][y][z][t] = new Complex(i[x * 2][y][z][t], i[x * 2 + 1][y][z][t]); + c[x][y][z][t] = Complex.ofCartesian(i[x * 2][y][z][t], i[x * 2 + 1][y][z][t]); } } } @@ -1333,7 +1333,7 @@ public class ComplexUtils { for (int y = 0; y < h / 2; y++) { for (int z = 0; z < d; z++) { for (int t = 0; t < v; t++) { - c[x][y][z][t] = new Complex(i[x][y * 2][z][t], i[x][y * 2 + 1][z][t]); + c[x][y][z][t] = Complex.ofCartesian(i[x][y * 2][z][t], i[x][y * 2 + 1][z][t]); } } } @@ -1344,7 +1344,7 @@ public class ComplexUtils { for (int y = 0; y < h; y++) { for (int z = 0; z < d / 2; z++) { for (int t = 0; t < v; t++) { - c[x][y][z][t] = new Complex(i[x][y][z * 2][t], i[x][y][z * 2 + 1][t]); + c[x][y][z][t] = Complex.ofCartesian(i[x][y][z * 2][t], i[x][y][z * 2 + 1][t]); } } } @@ -1355,7 +1355,7 @@ public class ComplexUtils { for (int y = 0; y < h; y++) { for (int z = 0; z < d; z++) { for (int t = 0; t < v / 2; t++) { - c[x][y][z][t] = new Complex(i[x][y][z][t * 2], i[x][y][z][t * 2 + 1]); + c[x][y][z][t] = Complex.ofCartesian(i[x][y][z][t * 2], i[x][y][z][t * 2 + 1]); } } } @@ -1399,14 +1399,14 @@ public class ComplexUtils { c = new Complex[w / 2][h]; for (int x = 0; x < w / 2; x++) { for (int y = 0; y < h; y++) { - c[x][y] = new Complex(i[x * 2][y], i[x * 2 + 1][y]); + c[x][y] = Complex.ofCartesian(i[x * 2][y], i[x * 2 + 1][y]); } } } else { c = new Complex[w][h / 2]; for (int x = 0; x < w; x++) { for (int y = 0; y < h / 2; y++) { - c[x][y] = new Complex(i[x][y * 2], i[x][y * 2 + 1]); + c[x][y] = Complex.ofCartesian(i[x][y * 2], i[x][y * 2 + 1]); } } } @@ -1450,7 +1450,7 @@ public class ComplexUtils { for (int x = 0; x < w/2; x ++) { for (int y = 0; y < h; y++) { for (int z = 0; z < d; z++) { - c[x][y][z] = new Complex(i[x * 2][y][z], i[x * 2 + 1][y][z]); + c[x][y][z] = Complex.ofCartesian(i[x * 2][y][z], i[x * 2 + 1][y][z]); } } } @@ -1459,7 +1459,7 @@ public class ComplexUtils { for (int x = 0; x < w; x++) { for (int y = 0; y < h/2; y ++) { for (int z = 0; z < d; z++) { - c[x][y][z] = new Complex(i[x][y * 2][z], i[x][y * 2 + 1][z]); + c[x][y][z] = Complex.ofCartesian(i[x][y * 2][z], i[x][y * 2 + 1][z]); } } } @@ -1468,7 +1468,7 @@ public class ComplexUtils { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { for (int z = 0; z < d/2; z++) { - c[x][y][z] = new Complex(i[x][y][z * 2], i[x][y][z * 2 + 1]); + c[x][y][z] = Complex.ofCartesian(i[x][y][z * 2], i[x][y][z * 2 + 1]); } } } @@ -1506,7 +1506,7 @@ public class ComplexUtils { final int length = real.length; final Complex[] c = new Complex[length]; for (int n = 0; n < length; n++) { - c[n] = new Complex(real[n], imag[n]); + c[n] = Complex.ofCartesian(real[n], imag[n]); } return c; } @@ -1582,7 +1582,7 @@ public class ComplexUtils { final int length = real.length; final Complex[] c = new Complex[length]; for (int n = 0; n < length; n++) { - c[n] = new Complex(real[n], imag[n]); + c[n] = Complex.ofCartesian(real[n], imag[n]); } return c; } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/5166d4be/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/RootsOfUnity.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/RootsOfUnity.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/RootsOfUnity.java index 89bf8d3..c3035ce 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/RootsOfUnity.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/RootsOfUnity.java @@ -65,14 +65,14 @@ public class RootsOfUnity { double previousReal = 1; double previousImag = 0; - omega[0] = new Complex(previousReal, previousImag); + omega[0] = Complex.ofCartesian(previousReal, previousImag); for (int i = 1; i < omegaCount; i++) { final double real = previousReal * cosT - previousImag * sinT; final double imag = previousReal * sinT + previousImag * cosT; omega[i] = isCounterClockwise ? - new Complex(real, imag) : - new Complex(real, -imag); + Complex.ofCartesian(real, imag) : + Complex.ofCartesian(real, -imag); previousReal = real; previousImag = imag;
