Submitted by:	Brent Worden


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141007 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-10-30 19:42:43 +00:00
parent 091eaf2a3a
commit fe5d7c1693
4 changed files with 701 additions and 1 deletions

View File

@ -0,0 +1,255 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.commons.math.complex;
/**
* Reference:
* http://myweb.lmu.edu/dmsmith/ZMLIB.pdf
*
* @version $Revision: 1.1 $ $Date: 2003/10/30 19:42:43 $
*/
public class Complex {
/** The square root of -1. */
public static final Complex I = new Complex(0.0, 1.0);
/** */
public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
/** 1. */
public static final Complex ONE = new Complex(1.0, 0.0);
/** The imaginary part. */
protected double imaginary;
/** The real part. */
protected double real;
/**
* Create a complex number given the real and imaginary parts.
* @param real the real part.
* @param imaginary the imaginary part.
*/
public Complex(double real, double imaginary) {
super();
this.real = real;
this.imaginary = imaginary;
}
/**
* Return the absolute value of this complex number.
* @return the absolute value.
*/
public double abs() {
if (isNaN()) {
return Double.NaN;
}
return Math.sqrt(squareSum());
}
/**
* Return the sum of this complex number and the given complex number.
* @param rhs the other complex number.
* @return the complex number sum.
*/
public Complex add(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
return new Complex(real + rhs.getReal(),
imaginary + rhs.getImaginary());
}
/**
* Return the conjugate of this complex number.
* @return the conjugate.
*/
public Complex conjugate() {
if (isNaN()) {
return NaN;
}
return new Complex(real, -imaginary);
}
/**
* Return the quotient of this complex number and the given complex number.
* @param rhs the other complex number.
* @return the complex number quotient.
*/
public Complex divide(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
if (Math.abs(rhs.getReal()) < Math.abs(rhs.getImaginary())) {
double q = rhs.getReal() / rhs.getImaginary();
double d = (rhs.getReal() * q) + rhs.getImaginary();
return new Complex(((real * q) + imaginary) / d,
((imaginary * q) - real) / d);
} else {
double q = rhs.getImaginary() / rhs.getReal();
double d = (rhs.getImaginary() * q) + rhs.getReal();
return new Complex(((imaginary * q) + real) / d,
(imaginary - (real * q)) / d);
}
}
/**
*
*/
public boolean equals(Object other) {
boolean ret;
if (this == other) {
ret = true;
} else if (other == null) {
ret = false;
} else {
try {
Complex rhs = (Complex)other;
ret = (Double.doubleToRawLongBits(real) ==
Double.doubleToRawLongBits(rhs.getReal())) &&
(Double.doubleToRawLongBits(imaginary) ==
Double.doubleToRawLongBits(rhs.getImaginary()));
} catch (ClassCastException ex) {
// ignore exception
ret = false;
}
}
return ret;
}
/**
* 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;
}
/**
* Returns true if this complex number is the special Not-a-Number (NaN)
* value.
* @return true if the value represented by this object is NaN; false
* otherwise.
*/
public boolean isNaN() {
return Double.isNaN(real) || Double.isNaN(imaginary);
}
/**
* Return the product of this complex number and the given complex number.
* @param rhs the other complex number.
* @return the complex number product.
*/
public Complex multiply(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
double p = (real + imaginary) * (rhs.getReal() + rhs.getImaginary());
double ac = real * rhs.getReal();
double bd = imaginary * rhs.getImaginary();
return new Complex(ac - bd, p - ac - bd);
}
/**
* Return the additive inverse of this complex number.
* @return the negation of this complex number.
*/
public Complex negate() {
if (isNaN()) {
return NaN;
}
return new Complex(-real, -imaginary);
}
/**
* Return the sum of the squared terms.
* @return the square sum.
*/
private double squareSum() {
return real * real + imaginary * imaginary;
}
/**
* Return the difference between this complex number and the given complex
* number.
* @param rhs the other complex number.
* @return the complex number difference.
*/
public Complex subtract(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
return new Complex(real - rhs.getReal(),
imaginary - rhs.getImaginary());
}
}

View File

@ -0,0 +1,264 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.commons.math.complex;
import org.apache.commons.math.util.MathUtils;
/**
* Reference:
* http://myweb.lmu.edu/dmsmith/ZMLIB.pdf
*
* @version $Revision: 1.1 $ $Date: 2003/10/30 19:42:43 $
*/
public class ComplexMath {
/**
*
*/
private ComplexMath() {
super();
}
/**
*
*/
public static Complex acos(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
return Complex.I.negate().multiply(log(z.add(
Complex.I.multiply(sqrt1z(z)))));
}
/**
*
*/
public static Complex asin(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
return Complex.I.negate().multiply(log(sqrt1z(z).add(
Complex.I.multiply(z))));
}
/**
*
*/
public static Complex atan(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
return Complex.I.multiply(
log(Complex.I.add(z).divide(Complex.I.subtract(z))))
.multiply(new Complex(2.0, 0.0));
}
/**
*
*/
public static Complex cos(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a = z.getReal();
double b = z.getImaginary();
return new Complex(Math.cos(a) * MathUtils.cosh(b),
-Math.sin(a) * MathUtils.sinh(b));
}
/**
*
*/
public static Complex cosh(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a = z.getReal();
double b = z.getImaginary();
return new Complex(MathUtils.cosh(a) * Math.cos(b),
MathUtils.sinh(a) * Math.sin(b));
}
/**
*
*/
public static Complex exp(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double b = z.getImaginary();
double expA = Math.exp(z.getReal());
double sinB = Math.sin(b);
double cosB = Math.cos(b);
return new Complex(expA * cosB, expA * sinB);
}
/**
*
*/
public static Complex log(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
return new Complex(Math.log(z.abs()),
Math.atan2(z.getImaginary(), z.getReal()));
}
/**
*
*/
public static Complex pow(Complex y, Complex x) {
return exp(x.multiply(log(y)));
}
/**
*
*/
public static Complex sin(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a = z.getReal();
double b = z.getImaginary();
return new Complex(Math.sin(a) * MathUtils.cosh(b),
Math.cos(a) * MathUtils.sinh(b));
}
/**
*
*/
public static Complex sinh(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a = z.getReal();
double b = z.getImaginary();
return new Complex(MathUtils.sinh(a) * Math.cos(b),
MathUtils.cosh(a) * Math.sin(b));
}
/**
*
*/
public static Complex sqrt(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a = z.getReal();
double b = z.getImaginary();
double t = Math.sqrt((Math.abs(a) + z.abs()) / 2.0);
if (a >= 0.0) {
return new Complex(t, b / (2.0 * t));
} else {
double s = (b > 0.0 ? 1.0 : (b < 0.0 ? -1.0 : 0.0));
return new Complex(Math.abs(z.getImaginary()) / (2.0 * t), s * t);
}
}
/**
* Returns the square root of 1 - z^2.
* @return the square root of 1 - z^2.
*/
public static Complex sqrt1z(Complex z) {
return sqrt(Complex.ONE.subtract(z.multiply(z)));
}
/**
*
*/
public static Complex tan(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a2 = 2.0 * z.getReal();
double b2 = 2.0 * z.getImaginary();
double d = Math.cos(a2) + MathUtils.cosh(b2);
return new Complex(Math.sin(a2) / d, MathUtils.sinh(b2) / 2);
}
/**
*
*/
public static Complex tanh(Complex z) {
if (z.isNaN()) {
return Complex.NaN;
}
double a2 = 2.0 * z.getReal();
double b2 = 2.0 * z.getImaginary();
double d = MathUtils.cosh(a2) + Math.cos(b2);
return new Complex(MathUtils.sinh(a2) / d, Math.sin(b2) / 2);
}
}

View File

@ -57,7 +57,7 @@ package org.apache.commons.math.util;
/** /**
* Some useful additions to the built-in functions in {@link Math}. * Some useful additions to the built-in functions in {@link Math}.
* *
* @version $Revision: 1.6 $ $Date: 2003/10/16 15:24:30 $ * @version $Revision: 1.7 $ $Date: 2003/10/30 19:42:43 $
*/ */
public final class MathUtils { public final class MathUtils {
@ -350,4 +350,18 @@ public final class MathUtils {
} }
return logSum; return logSum;
} }
/**
*
*/
public static double cosh(double x) {
return (Math.exp(x) + Math.exp(-x)) / 2.0;
}
/**
*
*/
public static double sinh(double x) {
return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
} }

View File

@ -0,0 +1,167 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.commons.math.complex;
import junit.framework.TestCase;
/**
* @version $Revision: 1.1 $ $Date: 2003/10/30 19:42:43 $
*/
public class ComplexTest extends TestCase {
public void testConstructor() {
Complex z = new Complex(3.0, 4.0);
assertEquals(3.0, z.getReal(), 1.0e-5);
assertEquals(4.0, z.getImaginary(), 1.0e-5);
}
public void testConstructorNaN() {
Complex z = new Complex(3.0, Double.NaN);
assertTrue(z.isNaN());
z = new Complex(Double.NaN, 4.0);
assertTrue(z.isNaN());
z = new Complex(3.0, 4.0);
assertFalse(z.isNaN());
}
public void testAbs() {
Complex z = new Complex(3.0, 4.0);
assertEquals(5.0, z.abs(), 1.0e-5);
}
public void testAdd() {
Complex x = new Complex(3.0, 4.0);
Complex y = new Complex(5.0, 6.0);
Complex z = x.add(y);
assertEquals(8.0, z.getReal(), 1.0e-5);
assertEquals(10.0, z.getImaginary(), 1.0e-5);
}
public void testAddNaN() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.add(Complex.NaN);
assertTrue(z.isNaN());
}
public void testConjugate() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.conjugate();
assertEquals(3.0, z.getReal(), 1.0e-5);
assertEquals(-4.0, z.getImaginary(), 1.0e-5);
}
public void testConjugateNaN() {
Complex z = Complex.NaN.conjugate();
assertTrue(z.isNaN());
}
public void testDivide() {
Complex x = new Complex(3.0, 4.0);
Complex y = new Complex(5.0, 6.0);
Complex z = x.divide(y);
assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
}
public void testDivideNaN() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.divide(Complex.NaN);
assertTrue(z.isNaN());
}
public void testMultiply() {
Complex x = new Complex(3.0, 4.0);
Complex y = new Complex(5.0, 6.0);
Complex z = x.multiply(y);
assertEquals(-9.0, z.getReal(), 1.0e-5);
assertEquals(38.0, z.getImaginary(), 1.0e-5);
}
public void testMultiplyNaN() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.multiply(Complex.NaN);
assertTrue(z.isNaN());
}
public void testNegate() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.negate();
assertEquals(-3.0, z.getReal(), 1.0e-5);
assertEquals(-4.0, z.getImaginary(), 1.0e-5);
}
public void testNegateNaN() {
Complex z = Complex.NaN.negate();
assertTrue(z.isNaN());
}
public void testSubtract() {
Complex x = new Complex(3.0, 4.0);
Complex y = new Complex(5.0, 6.0);
Complex z = x.subtract(y);
assertEquals(-2.0, z.getReal(), 1.0e-5);
assertEquals(-2.0, z.getImaginary(), 1.0e-5);
}
public void testSubtractNaN() {
Complex x = new Complex(3.0, 4.0);
Complex z = x.subtract(Complex.NaN);
assertTrue(z.isNaN());
}
}