Package org.jamesii.core.math.complex

Examples of org.jamesii.core.math.complex.Complex


    Complex[] reference, x;

    // Fifth root of a positive real number
    reference =
        new Complex[] {
            new Complex(-1.11622474376531575, -0.81098474715738870),
            new Complex(-1.11622474376531575, 0.81098474715738870),
            new Complex(0.42635991303470834, -1.31220088525839459),
            new Complex(0.42635991303470834, 1.31220088525839459),
            new Complex(1.3797296614612148) };
    x = ComplexMath.root(5, new Complex(5));
    assertTrue(x.length == 5);
    for (int i = 0; i < x.length; i++) {
      boolean temp = false;
      for (int j = 0; j < reference.length; j++) {
        temp |= approximatelyEqual(x[i], reference[j]);
      }
      assertTrue(temp);
    }

    // Eighth root of a negative real number
    reference =
        new Complex[] {
            new Complex(-1.84775906502257351, -0.76536686473017954),
            new Complex(-1.84775906502257351, 0.76536686473017954),
            new Complex(-0.76536686473017954, -1.84775906502257351),
            new Complex(-0.76536686473017954, 1.84775906502257351),
            new Complex(0.76536686473017954, -1.84775906502257351),
            new Complex(0.76536686473017954, 1.84775906502257351),
            new Complex(1.84775906502257351, -0.76536686473017954),
            new Complex(1.84775906502257351, 0.76536686473017954) };
    x = ComplexMath.root(8, new Complex(-256));
    assertTrue(x.length == 8);
    for (int i = 0; i < x.length; i++) {
      boolean temp = false;
      for (int j = 0; j < reference.length; j++) {
        temp |= approximatelyEqual(x[i], reference[j]);
      }
      assertTrue(temp);
    }

    // Fifth root of a complex number
    reference =
        new Complex[] { new Complex(-1.49595075945776575, 0.12140512225626765),
            new Complex(-0.57773734005399185, -1.38521747185763709),
            new Complex(-0.34681107478712560, 1.46024996382034682),
            new Complex(1.13888944673444278, -0.97751660167448831),
            new Complex(1.28160972756444042, 0.78107898745551094) };
    x = ComplexMath.root(5, new Complex(-7, 3));
    assertTrue(x.length == 5);
    for (int i = 0; i < x.length; i++) {
      boolean temp = false;
      for (int j = 0; j < reference.length; j++) {
        temp |= approximatelyEqual(x[i], reference[j]);
      }
      assertTrue(temp);
    }

    // negative square root of a complex number
    reference =
        new Complex[] { new Complex(-0.07285869092739988, 0.35496203179504731),
            new Complex(0.07285869092739988, -0.35496203179504731) };
    x = ComplexMath.root(-2, new Complex(-7, 3));
    assertTrue(x.length == 2);
    for (int i = 0; i < x.length; i++) {
      boolean temp = false;
      for (int j = 0; j < reference.length; j++) {
        temp |= approximatelyEqual(x[i], reference[j]);
      }
      assertTrue(temp);
    }

    /* Edge cases */

    // root(0, Complex) -- should yield no results.
    x = ComplexMath.root(0, new Complex(23, 42));
    assertTrue(x.length == 0);

    // root(0, 0) -- should yield no results.
    x = ComplexMath.root(0, new Complex(0, 0));
    assertTrue(x.length == 0);

    // root(3, 0) -- should yield zero, three times
    x = ComplexMath.root(3, new Complex(0, 0));
    assertTrue(x.length == 3);
    for (int i = 0; i < x.length; i++) {
      assertTrue(x[i].equals(new Complex(0, 0)));
    }

    // root(-2, 0) -- should yield no results.
    x = ComplexMath.root(-2, new Complex(0, 0));
    assertTrue(x.length == 0);
  }
View Full Code Here


  /**
   * Test divide exceptions.
   */
  public void testDivideExceptions() {
    ComplexArray1D a = new ComplexArray1D(new Complex[] { new Complex(1, 1) });

    // testing divide(int, double, double, double, double)
    try {
      a.divide(-1, 2d, 3d, 1d, 2d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(5, 5d, 4d, 2d, 1d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }

    // testing divide(int, int, double, double)
    try {
      a.divide(-1, 0, 1d, 2d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(5, 0, 1d, 1d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, -2, 2d, 3d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, 10, 3d, 2d);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }

    // testing divide(int, int, int)
    try {
      a.divide(-1, 0, 0);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(5, 0, 0);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, -2, 0);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, 10, 0);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, 0, -1);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.divide(0, 0, 10);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }

    // check for side-effects. Nothing should have changed!
    assertTrue(a.getComplex(0).equals(new Complex(1, 1)));
  }
View Full Code Here

  /**
   * Test divide.
   */
  public void testDivide() {
    ComplexArray1D a =
        new ComplexArray1D(new Complex[] { new Complex(1, 2),
            new Complex(3, 4), new Complex(5, 6) });

    // initial tests ... we can't have too many of them :)
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(5, 6)));

    // testing divide(int, int, int)
    a.divide(0, 1, 2);
    assertTrue(a.getComplex(0).equals(new Complex(39.0 / 61, 2.0 / 61)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(5, 6)));

    // testing divide(int, int, double, double)
    a.divide(0, 1, 1.5, -4.75);
    assertTrue(a.getComplex(0).equals(new Complex(-232d / 397, 324d / 397)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(5, 6)));

    // testing divide(int, double, double, double, double)
    a.divide(2, 4.0, 2.0, -1.0, -8.0);
    assertTrue(a.getComplex(0).equals(new Complex(-232d / 397, 324d / 397)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(-4d / 13, 6d / 13)));

    // testing divide(int, double, double, int)
    a.divide(1, 1.0, 1.0, 2);
    assertTrue(a.getComplex(0).equals(new Complex(-232d / 397, 324d / 397)));
    System.out.println(a.getComplex(1));
    assertTrue(a.getComplex(1).equals(new Complex(1d / 2, -5d / 2)));
    assertTrue(a.getComplex(2).equals(new Complex(-4d / 13, 6d / 13)));
  }
View Full Code Here

  /**
   * Test to complex array.
   */
  public void testToComplexArray() {
    Complex[] a =
        new Complex[] { new Complex(1, 2), new Complex(3, 4), new Complex(5, 6) };
    ComplexArray1D b = new ComplexArray1D(a);
    Complex[] c = b.toComplexArray();

    for (int i = 0; i < a.length; i++) {
      assertTrue(a[i].equals(c[i]));
View Full Code Here

    assertTrue(a.getLength() == 5);

    // check for numbers being zero
    for (int i = 0; i < a.getLength(); i++) {
      assertTrue(a.getComplex(i).equals(new Complex(0, 0)));
    }
  }
View Full Code Here

  /**
   * Test complex array constructor.
   */
  public void testComplexArrayConstructor() {
    Complex[] x =
        new Complex[] { new Complex(1, 2), new Complex(3, 4),
            new Complex(8, 7), new Complex(1.5, 5.75), new Complex(-8, -5.4) };
    ComplexArray1D a = new ComplexArray1D(x);

    assertTrue(x.length == a.getLength());

    for (int i = 0; i < a.getLength(); i++) {
View Full Code Here

  public void testPutGetComplex() {
    ComplexArray1D a = new ComplexArray1D(5);

    // test put and get
    a.putComplex(2, -24, 42);
    a.putComplex(0, new Complex(1, 2));
    a.putComplex(3, 10, 12);
    a.putComplex(1, new Complex(5, -7));

    // test exceptions
    try {
      a.putComplex(-1, new Complex(8, 18));
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.putComplex(34436, new Complex(8, 18));
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.getComplex(-4);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.getComplex(123);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }

    // if nothing interfered in between all values should be correct
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(5, -7)));
    assertTrue(a.getComplex(2).equals(new Complex(-24, 42)));
    assertTrue(a.getComplex(3).equals(new Complex(10, 12)));
    assertTrue(a.getComplex(4).equals(new Complex(0, 0)));
  }
View Full Code Here

  /**
   * Test set length.
   */
  public void testSetLength() {
    ComplexArray1D a =
        new ComplexArray1D(new Complex[] { new Complex(1, 2),
            new Complex(3, 4), new Complex(5, 6) });

    assertTrue(a.getLength() == 3);
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(5, 6)));

    a.setLength(3);

    assertTrue(a.getLength() == 3);
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(5, 6)));

    a.setLength(2);

    assertTrue(a.getLength() == 2);
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));

    a.setLength(3);

    assertTrue(a.getLength() == 3);
    assertTrue(a.getComplex(0).equals(new Complex(1, 2)));
    assertTrue(a.getComplex(1).equals(new Complex(3, 4)));
    assertTrue(a.getComplex(2).equals(new Complex(0, 0)));
  }
View Full Code Here

  /**
   * Test set length exceptions.
   */
  public void testSetLengthExceptions() {
    ComplexArray1D a =
        new ComplexArray1D(new Complex[] { new Complex(1, 2),
            new Complex(3, 4), new Complex(5, 6) });

    try {
      a.setLength(-1);
      fail();
    } catch (IllegalArgumentException e) {
View Full Code Here

  /**
   * Test negate exceptions.
   */
  public void testNegateExceptions() {
    ComplexArray1D a = new ComplexArray1D(new Complex[] { new Complex(1, 1) });

    try {
      a.negate(-1);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }
    try {
      a.negate(5);
      fail();
    } catch (IndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Exception e) {
      fail();
    }

    // check for side-effects. Nothing should have changed!
    assertTrue(a.getComplex(0).equals(new Complex(1, 1)));
  }
View Full Code Here

TOP

Related Classes of org.jamesii.core.math.complex.Complex

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.