package jetbrains.mps.baseLanguage.math.runtime; /*Generated by MPS */ public class Complex extends Number { public static final Complex I = new Complex(0, 1); public static final Complex ONE = new Complex(1, 0); public static final Complex ZERO = new Complex(0, 0); private double myRealPart; private double myImagPart; public Complex(double re, double im) { myRealPart = re; myImagPart = im; } @Override public boolean equals(Object obj) { if ((obj == null) || !((obj instanceof Complex))) { return false; } Complex Cobj = (Complex) obj; return (Cobj.myRealPart == myRealPart) && (Cobj.myImagPart == myImagPart); } @Override public String toString() { if (myImagPart == 0) { return "" + myRealPart; } if (myRealPart == 0) { return myImagPart + "*I"; } return "(" + myRealPart + ((Math.signum(myImagPart) > 0 ? "+" : "-")) + Math.abs(myImagPart) + "*I)"; } public double re() { return myRealPart; } public double im() { return myImagPart; } public double abs() { return Math.hypot(myRealPart, myImagPart); } public double arg() { return Math.atan2(myImagPart, myRealPart); } public Complex add(Complex b) { return new Complex(myRealPart + b.myRealPart, myImagPart + b.myImagPart); } public Complex sub(Complex b) { return new Complex(myRealPart - b.myRealPart, myImagPart - b.myImagPart); } public Complex mul(Complex b) { return new Complex(myRealPart * b.myRealPart - myImagPart * b.myImagPart, myRealPart * b.myImagPart + b.myRealPart * myImagPart); } public Complex inv() { double s = myRealPart * myRealPart + myImagPart * myImagPart; return new Complex(myRealPart / s, -myImagPart / s); } public Complex div(Complex b) { return mul(b.inv()); } public Complex conj() { return new Complex(myRealPart, -myImagPart); } public Complex exp() { double r = Math.exp(myRealPart); return new Complex(r * Math.cos(myImagPart), r * Math.sin(myImagPart)); } public Complex sin() { return new Complex(Math.sin(myRealPart) * Math.cosh(myImagPart), Math.cos(myRealPart) * Math.sinh(myImagPart)); } public Complex cos() { return new Complex(Math.cos(myRealPart) * Math.cosh(myImagPart), Math.sin(myRealPart) * Math.sinh(myImagPart)); } public Complex tan() { return sin().div(cos()); } public Complex atan() { Complex ix = this.mul(I); return new Complex(0, 0.5).mul(ONE.sub(ix).ln().sub(ONE.add(ix).ln())); } public Complex ln() { return new Complex(abs(), arg()); } public Complex pow(int k) { double r = Math.pow(abs(), k); double theta = arg() * k; return new Complex(r * Math.cos(theta), r * Math.sin(theta)); } @Override public int intValue() { return (int) this.myRealPart; } @Override public long longValue() { return (long) this.myRealPart; } @Override public float floatValue() { return (short) this.myRealPart; } @Override public double doubleValue() { return this.myRealPart; } }