commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/complex ComplexUtilsTest.java ComplexMathTest.java
Date Mon, 12 Jul 2004 00:27:09 GMT
psteitz     2004/07/11 17:27:09

  Added:       math/src/java/org/apache/commons/math/complex
                        ComplexUtils.java
               math/src/test/org/apache/commons/math/complex
                        ComplexUtilsTest.java
  Removed:     math/src/java/org/apache/commons/math/complex
                        ComplexMath.java
               math/src/test/org/apache/commons/math/complex
                        ComplexMathTest.java
  Log:
  Renamed ComplexMath to ComplexUtils.
  
  Revision  Changes    Path
  1.1                  jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexUtils.java
  
  Index: ComplexUtils.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   *
   * Licensed 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.math.complex;
  
  import org.apache.commons.math.util.MathUtils;
  
  /**
   * Implementations of various transcendental functions for
   * {@link org.apache.commons.math.complex.Complex} arguments.
   *
   * Reference:
   * <ul>
   * <li><a href="http://myweb.lmu.edu/dmsmith/ZMLIB.pdf">
   * Multiple Precision Complex Arithmetic and Functions</a></li>
   * </ul>
   *
   * @version $Revision: 1.1 $ $Date: 2004/07/12 00:27:09 $
   */
  public class ComplexUtils {
      
      /**
       * Default constructor.
       */
      private ComplexUtils() {
          super();
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/InverseCosine.html">
       * inverse cosine</a> for the given complex argument.
       * @param z the value whose inverse cosine is to be returned.
       * @return the inverse cosine of <code>z</code>.
       */
      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)))));       
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/InverseSine.html">
       * inverse sine</a> for the given complex argument.
       * @param z the value whose inverse sine is to be returned.
       * @return the inverse sine of <code>z</code>.
       */
      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))));       
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/InverseTangent.html">
       * inverse tangent</a> for the given complex argument.
       * @param z the value whose inverse tangent is to be returned.
       * @return the inverse tangent of <code>z</code>.
       */
      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))))
              .divide(new Complex(2.0, 0.0));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/Cosine.html">cosine</a>
       * for the given complex argument.
       * @param z the value whose cosine is to be returned.
       * @return the cosine of <code>z</code>.
       */
      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));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html">
       * hyperbolic cosine</a> for the given complex argument.
       * @param z the value whose hyperbolic cosine is to be returned.
       * @return the hyperbolic cosine of <code>z</code>.
       */
      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));
      }
      
      /**
       * Compute the
       * <a href="http://mathworld.wolfram.com/ExponentialFunction.html">
       * exponential function</a> for the given complex argument.
       * @param z the value.
       * @return <i>e</i><sup><code>z</code></sup>.
       */
      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);
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/NaturalLogarithm.html">
       * natural logarithm</a> for the given complex argument.
       * @param z the value.
       * @return ln <code>z</code>.
       */
      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()));        
      }
      
      
      /**
       * Returns of value of <code>y</code> raised to the power of <code>x</code>.
       * @param y the base.
       * @param x the exponent.
       * @return <code>y</code><sup><code>z</code></sup>.
       */
      public static Complex pow(Complex y, Complex x) {
          return exp(x.multiply(log(y)));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/Sine.html">sine</a>
       * for the given complex argument.
       * @param z the value whose sine is to be returned.
       * @return the sine of <code>z</code>.
       */
      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));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/HyperbolicSine.html">
       * hyperbolic sine</a> for the given complex argument.
       * @param z the value whose hyperbolic sine is to be returned.
       * @return the hyperbolic sine of <code>z</code>.
       */
      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));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/SquareRoot.html">squre
       * root</a> for the given complex argument.
       * @param z the value whose square root is to be returned.
       * @return the square root of <code>z</code>.
       */
      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 {
              return new Complex(Math.abs(z.getImaginary()) / (2.0 * t),
                  MathUtils.indicator(b) * t);
          }
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/SquareRoot.html">squre
       * root of 1 - <code>z</code><sup>2</sup> for the given complex
argument.
       * @param z the value.
       * @return the square root of 1 - <code>z</code><sup>2</sup>.
       */
      public static Complex sqrt1z(Complex z) {
          return sqrt(Complex.ONE.subtract(z.multiply(z)));
      }
      
      /**
       * Compute the <a href="http://mathworld.wolfram.com/Tangent.html">
       * tangent</a> for the given complex argument.
       * @param z the value whose tangent is to be returned.
       * @return the tangent of <code>z</code>.
       */
      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) / d);
      }
      
      /**
       * Compute the
       * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html">
       * hyperbolic tangent</a> for the given complex argument.
       * @param z the value whose hyperbolic tangent is to be returned.
       * @return the hyperbolic tangent of <code>z</code>.
       */
      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) / d);
      }
  }
  
  
  
  1.1                  jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java
  
  Index: ComplexUtilsTest.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   * 
   * Licensed 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.math.complex;
  
  import org.apache.commons.math.TestUtils;
  
  import junit.framework.TestCase;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/07/12 00:27:09 $
   */
  public class ComplexUtilsTest extends TestCase {
      
      public void testAcos() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(0.936812, -2.30551);
          TestUtils.assertEquals(expected, ComplexUtils.acos(z), 1.0e-5);
      }
      
      public void testAcosNaN() {
          assertTrue(ComplexUtils.acos(Complex.NaN).isNaN());
      }
      
      public void testAsin() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(0.633984, 2.30551);
          TestUtils.assertEquals(expected, ComplexUtils.asin(z), 1.0e-5);
      }
      
      public void testAsinNaN() {
          assertTrue(ComplexUtils.asin(Complex.NaN).isNaN());
      }
      
      public void testAtan() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.44831, 0.158997);
          TestUtils.assertEquals(expected, ComplexUtils.atan(z), 1.0e-5);
      }
      
      public void testAtanNaN() {
          assertTrue(ComplexUtils.atan(Complex.NaN).isNaN());
      }
      
      public void testCos() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-27.03495, -3.851153);
          TestUtils.assertEquals(expected, ComplexUtils.cos(z), 1.0e-5);
      }
      
      public void testCosh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-6.58066, -7.58155);
          TestUtils.assertEquals(expected, ComplexUtils.cosh(z), 1.0e-5);
      }
      
      public void testCoshNaN() {
          assertTrue(ComplexUtils.cosh(Complex.NaN).isNaN());
      }
      
      public void testCosNaN() {
          assertTrue(ComplexUtils.cos(Complex.NaN).isNaN());
      }
      
      public void testExp() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-13.12878, -15.20078);
          TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5);
      }
      
      public void testExpNaN() {
          assertTrue(ComplexUtils.exp(Complex.NaN).isNaN());
      }
      
      public void testLog() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.60944, 0.927295);
          TestUtils.assertEquals(expected, ComplexUtils.log(z), 1.0e-5);
      }
      
      public void testLogNaN() {
          assertTrue(ComplexUtils.log(Complex.NaN).isNaN());
      }
      
      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, ComplexUtils.pow(x, y), 1.0e-5);
      }
      
      public void testPowNaNBase() {
          Complex x = new Complex(3, 4);
          assertTrue(ComplexUtils.pow(Complex.NaN, x).isNaN());
      }
      
      public void testPowNaNExponent() {
          Complex x = new Complex(3, 4);
          assertTrue(ComplexUtils.pow(x, Complex.NaN).isNaN());
      }
      
      public void testSin() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(3.853738, -27.01681);
          TestUtils.assertEquals(expected, ComplexUtils.sin(z), 1.0e-5);
      }
      
      public void testSinh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-6.54812, -7.61923);
          TestUtils.assertEquals(expected, ComplexUtils.sinh(z), 1.0e-5);
      }
      
      public void testSinhNaN() {
          assertTrue(ComplexUtils.sinh(Complex.NaN).isNaN());
      }
      
      public void testSinNaN() {
          assertTrue(ComplexUtils.sin(Complex.NaN).isNaN());
      }
      
      public void testSqrtRealPositive() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(2, 1);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtRealZero() {
          Complex z = new Complex(0.0, 4);
          Complex expected = new Complex(1.41421, 1.41421);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtRealNegative() {
          Complex z = new Complex(-3.0, 4);
          Complex expected = new Complex(1, 2);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtImaginaryZero() {
          Complex z = new Complex(-3.0, 0.0);
          Complex expected = new Complex(0.0, 1.73205);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtImaginaryNegative() {
          Complex z = new Complex(-3.0, -4.0);
          Complex expected = new Complex(1.0, -2.0);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
      }
  
      public void testSqrt1z() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(4.08033, -2.94094);
          TestUtils.assertEquals(expected, ComplexUtils.sqrt1z(z), 1.0e-5);
      }
      
      public void testSqrt1zNaN() {
          assertTrue(ComplexUtils.sqrt1z(Complex.NaN).isNaN());
      }
      
      public void testSqrtNaN() {
          assertTrue(ComplexUtils.sqrt(Complex.NaN).isNaN());
      }
      
      public void testTan() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-0.000187346, 0.999356);
          TestUtils.assertEquals(expected, ComplexUtils.tan(z), 1.0e-5);
      }
      
      public void testTanh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.00071, 0.00490826);
          TestUtils.assertEquals(expected, ComplexUtils.tanh(z), 1.0e-5);
      }
      
      public void testTanhNaN() {
          assertTrue(ComplexUtils.tanh(Complex.NaN).isNaN());
      }
      
      public void testTanNaN() {
          assertTrue(ComplexUtils.tan(Complex.NaN).isNaN());
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message