public ComplexNumber scalarProduct(ComplexVector2D v1) {
   ComplexNumber c;
   ComplexNumber d;
   c = this.x.mult(v1.x);
   d = this.y.mult(v1.y);
   return (c.add(d));
 }
 public ComplexNumber pow(int n) {
   ComplexNumber powResult = new ComplexNumber(a, b);
   ComplexNumber staticComplexNum = new ComplexNumber(a, b);
   for (int i = 1; i < n; i++) {
     powResult = powResult.multiply(staticComplexNum);
   }
   return powResult;
 }
 @Test
 public void shouldAddTwoComplexNumbers() {
   ComplexNumber number1 = new ComplexNumber(4, 7);
   ComplexNumber number2 = new ComplexNumber(5, 3);
   ComplexNumber result = number1.add(number2);
   assertEquals(9, result.getReal(), 0);
   assertEquals(10, result.getImaginary(), 0);
 }
 @Test
 public void multShouldWorkCorrect() {
   ComplexMatrix2x2 matrix = (ComplexMatrix2x2) context.getBean("zeroMatrix");
   ComplexNumber cN = ComplexMatrix2x2Util.getComplexNumber(0, 0);
   when(cN.add(any(ComplexNumber.class))).thenReturn(cN);
   when(cN.mult(any(ComplexNumber.class))).thenReturn(cN);
   ComplexNumber[][] complexNumbers = new ComplexNumber[][] {{cN, cN}, {cN, cN}};
   assertTrue(Arrays.deepEquals(matrix.mult(matrix).getMatrix(), complexNumbers));
 }
  @Test
  public void shouldCalculateSquare() {
    ComplexNumber number1 = new ComplexNumber(2, 3);
    assertEquals(-5, number1.getSquare().getReal(), 0);
    assertEquals(12, number1.getSquare().getImaginary(), 0);

    ComplexNumber number2 = new ComplexNumber(4, 2);
    assertEquals(12, number2.getSquare().getReal(), 0);
    assertEquals(16, number2.getSquare().getImaginary(), 0);
  }
 public void calcZeros(ArrayAdapter<String> zerosArr, int nFns) {
   int n;
   String title;
   for (int i = 0; i < nFns; i += 1) {
     if (!graphCalcs[i].empty()) {
       title = "Fn" + Integer.toString(i + 1) + "(x):";
       n =
           graphCalcs[i].calcZeros(
               zeros[i],
               graph.getXLeft(),
               graph.getXRight(),
               graph.getYBot(),
               graph.getYTop(),
               graph.getXMin(),
               graph.getXMax(),
               graph.getYMin(),
               graph.getYMax(),
               graph.getXUnitLen());
       // Log.v ("calcZeros",Integer.toString(n));
       if (n == 0) title += " None";
       zerosArr.add(title);
       // Convert x values of zeros to strings
       for (int k = 0; k < n; k += 1) {
         float num = zeros[i][k];
         if ((num > -0.001 && num < 0) || (num < 0.001 && num > 0)) num = 0;
         String numStr = ComplexNumber.roundStr(num, 3);
         zerosArr.add("   x = " + numStr);
       }
     }
   }
 }
  public static void main(String[] args) {
    ComplexNumber A = new ComplexNumber(2, 3);
    ComplexNumber B = new ComplexNumber(1, 4);

    System.out.println("Add  result: " + A.addComplex(B).toString());
    System.out.println("Sub  result: " + A.subComplex(B).toString());
    System.out.println("Mult result: " + A.multComplex(B).toString());
    System.out.println("Div  result: " + A.divComplex(B).toString());
    System.out.println("A Mag: " + A.magComplex());
    System.out.println("B Mag: " + B.magComplex());
  }
 @Test
 public void canConvertToString() {
   ComplexNumber z = new ComplexNumber(1, 2);
   assertEquals("1.0 + 2.0i", z.toString());
 }
Exemplo n.º 9
0
 /**
  * Returns the complex plane currently being used.
  *
  * @return complex plan currently being used.
  */
 private String complexPlane() {
   ComplexNumber C1 = new ComplexNumber(new RealNumber(currWXMin), new RealNumber(currWYMin));
   ComplexNumber C2 = new ComplexNumber(new RealNumber(currWXMax), new RealNumber(currWYMax));
   return "< " + C1.toString() + ", " + C2.toString() + " >";
 }
 @Test
 public void shouldCalculateSquareWithNegativeRealAndImaginary() {
   ComplexNumber number = new ComplexNumber(-5, -3);
   assertEquals(16, number.getSquare().getReal(), 0);
   assertEquals(30, number.getSquare().getImaginary(), 0);
 }
 @Test
 public void canConvertNegativeRealPartToString() {
   ComplexNumber z = new ComplexNumber(-1, 1);
   assertEquals("-1.0 + 1.0i", z.toString());
 }
 public ComplexNumber specialDivide(ComplexNumber a, double b) {
   double aNew = a.getA() / b;
   double bNew = a.getB() / b;
   ComplexNumber newComplexNum = new ComplexNumber(aNew, bNew);
   return newComplexNum;
 }
 public ComplexNumber subtract(ComplexNumber other) {
   ComplexNumber newComplexNum = new ComplexNumber(-other.getA(), -other.getB());
   newComplexNum = add(newComplexNum);
   return newComplexNum;
 }
 public ComplexNumber add(ComplexNumber other) {
   double aNew = other.getA() + a;
   double bNew = other.getB() + b;
   ComplexNumber newComplexNum = new ComplexNumber(aNew, bNew);
   return newComplexNum;
 }
 @Test
 public void canConvertFloatingComplexNumberToString() {
   ComplexNumber z = new ComplexNumber(3.14, 2);
   assertEquals("3.14 + 2.0i", z.toString());
 }
 public ComplexNumber multiply(ComplexNumber other) {
   double aNew = other.getA() * a - other.getB() * b;
   double bNew = other.getB() * a + other.getA() * b;
   ComplexNumber newComplexNum = new ComplexNumber(aNew, bNew);
   return newComplexNum;
 }
 @Test
 public void canConvertScientificFormatToString() {
   ComplexNumber z = new ComplexNumber(1, 1.2456e-2);
   assertEquals("1.0 + 0.01i", z.toString());
 }
 public ComplexNumber divide(ComplexNumber other) {
   double tempNumber = other.getA() * other.getA() + other.getB() * other.getB();
   ComplexNumber tempComplexNum = new ComplexNumber(other.getA(), -other.getB());
   tempComplexNum = multiply(tempComplexNum);
   return specialDivide(tempComplexNum, tempNumber);
 }
 @Test
 public void canConvertNegativeImaginaryPartToString() {
   ComplexNumber z = new ComplexNumber(1, -1);
   assertEquals("1.0 - 1.0i", z.toString());
 }
 @Test
 public void shouldCalculateSquareWithNegativeImaginary() {
   ComplexNumber number = new ComplexNumber(3, -2);
   assertEquals(5, number.getSquare().getReal(), 0);
   assertEquals(-12, number.getSquare().getImaginary(), 0);
 }