Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 public void testScalarMultiply() {
   Complex x = new Complex(3.0, 4.0);
   double y = 2.0;
   Complex z = x.multiply(y);
   assertEquals(6.0, z.getReal(), 1.0e-5);
   assertEquals(8.0, z.getImaginary(), 1.0e-5);
 }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
  // compute the FFT of x[], assuming its length is a power of 2
  public static Complex[] fft(Complex[] x) {
    int n = x.length;

    // base case
    if (n == 1) return new Complex[] {x[0]};

    // radix 2 Cooley-Tukey FFT
    if (n % 2 != 0) {
      throw new RuntimeException("n is not a power of 2");
    }

    // fft of even terms
    Complex[] even = new Complex[n / 2];
    for (int k = 0; k < n / 2; k++) {
      even[k] = x[2 * k];
    }
    Complex[] q = fft(even);

    // fft of odd terms
    Complex[] odd = even; // reuse the array
    for (int k = 0; k < n / 2; k++) {
      odd[k] = x[2 * k + 1];
    }
    Complex[] r = fft(odd);

    // combine
    Complex[] y = new Complex[n];
    for (int k = 0; k < n / 2; k++) {
      double kth = -2 * k * Math.PI / n;
      Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
      y[k] = q[k].plus(wk.times(r[k]));
      y[k + n / 2] = q[k].minus(wk.times(r[k]));
    }
    return y;
  }
 public int[] toCoordinate(Complex punkt) {
   int[] koordinater = {
     (int) (punkt.getRe() * pixelPerRe + (bredd / 2)),
     (int) ((höjd / 2) + (-1 * punkt.getIm() * pixelPerIm))
   };
   return koordinater;
 }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
  /** Test cornercases with NaN and Infinity. */
  public void testNthRoot_cornercase_NAN_Inf() {
    // NaN + finite -> NaN
    List<Complex> roots = oneNaN.nthRoot(3);
    assertEquals(1, roots.size());
    assertEquals(Complex.NaN, roots.get(0));

    roots = nanZero.nthRoot(3);
    assertEquals(1, roots.size());
    assertEquals(Complex.NaN, roots.get(0));

    // NaN + infinite -> NaN
    roots = nanInf.nthRoot(3);
    assertEquals(1, roots.size());
    assertEquals(Complex.NaN, roots.get(0));

    // finite + infinite -> Inf
    roots = oneInf.nthRoot(3);
    assertEquals(1, roots.size());
    assertEquals(Complex.INF, roots.get(0));

    // infinite + infinite -> Inf
    roots = negInfInf.nthRoot(3);
    assertEquals(1, roots.size());
    assertEquals(Complex.INF, roots.get(0));
  }
Exemplo n.º 8
0
  public Complex tanh() {
    double scalar;
    double temp1Re, temp1Im;
    double temp2Re, temp2Im;
    Complex sinRes, cosRes;
    //  tanh(z)  =  sinh(z) / cosh(z)

    scalar = Math.exp(re);
    temp1Re = scalar * Math.cos(im);
    temp1Im = scalar * Math.sin(im);

    scalar = Math.exp(-re);
    temp2Re = scalar * Math.cos(-im);
    temp2Im = scalar * Math.sin(-im);

    temp1Re -= temp2Re;
    temp1Im -= temp2Im;

    sinRes = new Complex(0.5 * temp1Re, 0.5 * temp1Im);

    scalar = Math.exp(re);
    temp1Re = scalar * Math.cos(im);
    temp1Im = scalar * Math.sin(im);

    scalar = Math.exp(-re);
    temp2Re = scalar * Math.cos(-im);
    temp2Im = scalar * Math.sin(-im);

    temp1Re += temp2Re;
    temp1Im += temp2Im;

    cosRes = new Complex(0.5 * temp1Re, 0.5 * temp1Im);

    return sinRes.div(cosRes);
  }
Exemplo n.º 9
0
 public Numeric div(Object y) {
   if (y instanceof Complex) {
     Complex yc = (Complex) y;
     return div(real, imag, yc.doubleValue(), yc.doubleImagValue());
   }
   return ((Numeric) y).divReversed(this);
 }
Exemplo n.º 10
0
 /**
  * add:(复数的乘法)
  *
  * @param 设定文件
  * @return void 返回复数
  * @throws
  * @since CodingExample Ver 1.1
  */
 public Complex mul(Complex b) {
   //		(a+bi) * (c + di) = ac + adi + bci + bd(i*i) = (ac-bd) + * (ad+bc)i
   Complex temp = new Complex();
   temp.real = this.real * b.real - this.im * b.im;
   temp.im = this.real * b.im + this.im + b.real;
   return temp;
 }
Exemplo n.º 11
0
 public void testComplex() {
   Real x = new Real(2.4);
   Real y = new Real(-0.3);
   Complex complex = new Complex(x, y);
   assertSame(x, complex.getX());
   assertSame(y, complex.getY());
 }
Exemplo n.º 12
0
 Complex chargeat(Complex z) {
   Complex sum = Complex.zero;
   for (LocCharge lc : this) {
     sum = sum.add(z.sub(lc.pos).ln().scale(-1.0 * lc.q));
   }
   return sum;
 }
Exemplo n.º 13
0
 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);
 }
Exemplo n.º 14
0
 public static Complex QuoteOf(Complex X, Complex Y) {
   double e = Math.pow(Y.real, 2) + Math.pow(Y.imag, 2);
   Complex Z = new Complex();
   Z.setReal((X.real * Y.real + X.imag * Y.imag) / e);
   Z.setImag((X.imag * Y.real - X.real * Y.imag) / e);
   return Z;
 }
Exemplo n.º 15
0
 public boolean equals(Object obj) {
   if (obj == null || !(obj instanceof Complex)) return false;
   Complex y = (Complex) obj;
   return y.unit() == Unit.Empty
       && (Double.doubleToLongBits(real) == Double.doubleToLongBits(y.reValue()))
       && (Double.doubleToLongBits(imag) == Double.doubleToLongBits(y.imValue()));
 }
Exemplo n.º 16
0
 public Abs toAbs(String real, String immaginary) {
   Abs abs = new Abs();
   Complex complex = new Complex();
   complex.setImmaginary(Float.parseFloat(immaginary));
   complex.setReal(Float.parseFloat(real));
   abs.setComplex(complex);
   return abs;
 }
Exemplo n.º 17
0
 public Abs toAbs(float real, float immaginary) {
   Abs abs = new Abs();
   Complex complex = new Complex();
   complex.setImmaginary(immaginary);
   complex.setReal(real);
   abs.setComplex(complex);
   return abs;
 }
Exemplo n.º 18
0
 public void testExp() {
   Complex z = new Complex(3, 4);
   Complex expected = new Complex(-13.12878, -15.20078);
   TestUtils.assertEquals(expected, z.exp(), 1.0e-5);
   TestUtils.assertEquals(Complex.ONE, Complex.ZERO.exp(), 10e-12);
   Complex iPi = Complex.I.multiply(new Complex(pi, 0));
   TestUtils.assertEquals(Complex.ONE.negate(), iPi.exp(), 10e-12);
 }
 private void checkRoots(double a, double b, double c, double d) {
   List<Complex> roots = roots(a, b, c, d);
   assertThat(roots.size(), is(3));
   for (Complex root : roots) {
     Complex polyValue = polynomial(root, a, b, c, d);
     assertThat(polyValue.modulusSquare(), is(closeTo(0, 0.00000001)));
   }
 }
Exemplo n.º 20
0
 public void testAbsInfinite() {
   Complex z = new Complex(inf, 0);
   assertEquals(inf, z.abs(), 0);
   z = new Complex(0, neginf);
   assertEquals(inf, z.abs(), 0);
   z = new Complex(inf, neginf);
   assertEquals(inf, z.abs(), 0);
 }
Exemplo n.º 21
0
 public Numeric add(Object y, int k) {
   if (y instanceof Complex) {
     Complex yc = (Complex) y;
     if (yc.dimensions() != Dimensions.Empty) throw new ArithmeticException("units mis-match");
     return new DComplex(real + k * yc.reValue(), imag + k * yc.imValue());
   }
   return ((Numeric) y).addReversed(this, k);
 }
Exemplo n.º 22
0
 public void testEqualsNaN() {
   Complex realNaN = new Complex(Double.NaN, 0.0);
   Complex imaginaryNaN = new Complex(0.0, Double.NaN);
   Complex complexNaN = Complex.NaN;
   assertTrue(realNaN.equals(imaginaryNaN));
   assertTrue(imaginaryNaN.equals(complexNaN));
   assertTrue(realNaN.equals(complexNaN));
 }
Exemplo n.º 23
0
 public static void main(String[] args) {
   new Complex(2, 4).printNum();
   new Complex(6, 7).addNum(new Complex(4, 6)).printNum();
   new Complex(6, 7).multiplyNum(new Complex(4, 6)).printNum();
   Complex c = new Complex(4, 7);
   c.setImag(6);
   c.setReal(3);
   c.printNum();
 }
Exemplo n.º 24
0
 @Override
 public Complex execute() {
   Complex c = new Complex();
   c.setName("Test");
   Inner i = new Inner();
   i.setType("type");
   c.setInner(i);
   return c;
 }
Exemplo n.º 25
0
  public void testConstructorNaN() {
    Complex z = new Complex(3.0, Double.NaN);
    assertTrue(z.isNaN());

    z = new Complex(nan, 4.0);
    assertTrue(z.isNaN());

    z = new Complex(3.0, 4.0);
    assertFalse(z.isNaN());
  }
Exemplo n.º 26
0
  public Complex division(Complex n) throws ArithmeticException {
    Complex div = new Complex();
    if (n.module() == 0.0) {
      throw new ArithmeticException("Divide by zero");
    } else {
      div = this.modToComp(this.module() / n.module(), this.phase() - n.phase());
    }

    return div;
  }
Exemplo n.º 27
0
  /** Test standard values */
  public void testGetArgument() {
    Complex z = new Complex(1, 0);
    assertEquals(0.0, z.getArgument(), 1.0e-12);

    z = new Complex(1, 1);
    assertEquals(Math.PI / 4, z.getArgument(), 1.0e-12);

    z = new Complex(0, 1);
    assertEquals(Math.PI / 2, z.getArgument(), 1.0e-12);

    z = new Complex(-1, 1);
    assertEquals(3 * Math.PI / 4, z.getArgument(), 1.0e-12);

    z = new Complex(-1, 0);
    assertEquals(Math.PI, z.getArgument(), 1.0e-12);

    z = new Complex(-1, -1);
    assertEquals(-3 * Math.PI / 4, z.getArgument(), 1.0e-12);

    z = new Complex(0, -1);
    assertEquals(-Math.PI / 2, z.getArgument(), 1.0e-12);

    z = new Complex(1, -1);
    assertEquals(-Math.PI / 4, z.getArgument(), 1.0e-12);
  }
Exemplo n.º 28
0
  /**
   * <u>Funktion zur Berechnung mit dem Exponenten 'expon' >= 1</u><br>
   *
   * @param comp Komplexe Zahl
   * @param expon Exponenten, mit dem die komplexe Zahl exponiert ist.
   * @return R&uuml;ckgabe von neuem Objekt mit Ergebnis aus Exponierung
   */
  public static Complex pow(Complex comp, int expon) {
    double abs, arg; // interne Variablen fuer Betrag und Phase
    Complex swap = null;
    if (expon >= 1) {
      abs = Math.pow(comp.getAbs(), (double) expon); // Betrag exponieren
      arg = comp.getArg() * (double) expon; // Winkel mit Exponenten multiplizieren
      swap = new Complex(abs, arg, true);
    } else System.out.println("! Exponent kleiner 1 !");

    return swap;
  }
Exemplo n.º 29
0
  public void draw() {
    fill(0);
    noStroke();
    rect(0, höjd, bredd, höjd + 21);

    stroke(220);
    fill(230);
    mouse = toComplex(mouseX, mouseY);
    text("      X: " + Math.round(mouse.getRe() * 1000000) / 1000000.0, 5, höjd + 16);
    text("Y: " + Math.round(mouse.getIm() * 1000000) / 1000000.0 + "i", bredd / 2, höjd + 16);
  }
Exemplo n.º 30
0
 public Numeric mul(Object y) {
   if (y instanceof Complex) {
     Complex yc = (Complex) y;
     if (yc.unit() == Unit.Empty) {
       double y_re = yc.reValue();
       double y_im = yc.imValue();
       return new DComplex(real * y_re - imag * y_im, real * y_im + imag * y_re);
     }
     return Complex.times(this, yc);
   }
   return ((Numeric) y).mulReversed(this);
 }