Beispiel #1
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()));
 }
Beispiel #2
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);
 }
Beispiel #3
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);
 }
Beispiel #4
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);
 }
Beispiel #5
0
  /**
   * Evaluates and returns the value of the expression as a double number.
   *
   * @return The calculated value of the expression as a double number. If the type of the value
   *     does not implement the Number interface (e.g. Complex), NaN is returned. If an error occurs
   *     during evaluation, NaN is returned and hasError() will return true.
   * @see #getComplexValue()
   */
  public double getValue() {
    Object value = getValueAsObject();

    if (value == null) return Double.NaN;

    if (value instanceof Complex) {
      Complex c = (Complex) value;
      if (c.im() != 0.0) return Double.NaN;
      return c.re();
    }
    if (value != null && value instanceof Number) {
      return ((Number) value).doubleValue();
    }

    return Double.NaN;
  }
Beispiel #6
0
 public static DComplex power(double x_re, double x_im, double y_re, double y_im) {
   double h;
   /* #ifdef JAVA5 */
   // h = Math.hypot(x_re, x_im);
   /* #else */
   h = DComplex.hypot(x_re, x_im);
   /* #endif */
   double logr = Math.log(h);
   double t = Math.atan2(x_im, x_re);
   double r = Math.exp(logr * y_re - y_im * t);
   t = y_im * logr + y_re * t;
   return Complex.polar(r, t);
 }