import java.util.*;
import java.lang.*;
import java.io.*;
class ComplexNumber {
private double re;
private double im;
public ComplexNumber(double re, double im) {
this.re = re;
this.im = im;
}
public double getRe() {
return re;
}
public double getIm() {
return im;
}
private double getModule() {
return Math.
sqrt(this.
re * this.
re + this.
im * this.
im); }
public static ComplexNumber sum(ComplexNumber cn1, ComplexNumber cn2) {
return new ComplexNumber(cn1.getRe() + cn2.getRe(), cn1.getIm() + cn2.getIm());
}
public static ComplexNumber multiply(ComplexNumber cn1, ComplexNumber cn2) {
return new ComplexNumber(cn1.getRe() * cn2.getRe() - cn1.getIm() * cn2.getIm(), cn1.getRe() * cn2.getIm() + cn1.getIm() * cn2.getRe());
}
public static ComplexNumber subtract(ComplexNumber cn1, ComplexNumber cn2) {
return new ComplexNumber(cn1.getRe() - cn2.getRe(), cn1.getIm() - cn2.getIm());
}
public static ComplexNumber divide(ComplexNumber cn1, ComplexNumber cn2) {
ComplexNumber temp = new ComplexNumber(cn2.getRe(), (-1)* cn2.getIm());
temp = ComplexNumber.multiply(cn1, temp);
double denominator = cn2.getRe()*cn2.getRe() + cn2.getIm() * cn2.getIm();
return new ComplexNumber(temp.getRe()/denominator, temp.getIm()/denominator);
}
private double GetArg() {
if (this.re > 0) {
return Math.
atan(im
/ re
); } else {
if (re < 0 && im > 0) {
} else {
}
}
}
public static ComplexNumber pow(ComplexNumber cn, int power) {
double factor
= Math.
pow(cn.
getModule(), power
); return new ComplexNumber
(factor
*Math.
cos(power
* cn.
GetArg()), factor
*Math.
sin(power
* cn.
GetArg())); }
public static ComplexNumber[] sqrt(ComplexNumber cn) {
double a = cn.getModule() / 2;
ComplexNumber pos
= new ComplexNumber
(Math.
sqrt(a
+ cn.
getRe() / 2),
Math.
signum(cn.
getIm()) * Math.
sqrt(a
- cn.
getRe() / 2)); ComplexNumber neg = new ComplexNumber((-1) * pos.getRe(), (-1) * pos.getIm());
ComplexNumber[] answer = {pos, neg};
return answer;
}
if (im > 0) return " + ";
else return " - ";
}
@Override
if (im == 1 || im == -1) {
if (re == 0) {
string = sign() + "i";
} else {
string
= Double.
toString(re
) + sign
() + "i"; }
} else {
}
return string;
}
@Override
public boolean equals
(Object obj
) { if (this.getClass() != obj.getClass() || obj == null)
return false;
return true;
}
public static void main
(String[] args
) { ComplexNumber x = new ComplexNumber(2, 3);
ComplexNumber y = new ComplexNumber(-1, 2);
System.
out.
println("z1 = " + x
+ ", z2 = " + y
);
ComplexNumber z;
z = ComplexNumber.sum(x, y);
System.
out.
println("+ : " + z
);
z = ComplexNumber.subtract(x, y);
System.
out.
println("- : " + z
);
z = ComplexNumber.divide(x, y);
System.
out.
println("/ : " + z
);
z = ComplexNumber.multiply(x, y);
System.
out.
println(" * :" + z
);
z = ComplexNumber.pow(y, 2);
System.
out.
println("Pow 2 of z2 : " + z
);
ComplexNumber b = new ComplexNumber(3, 4);
ComplexNumber[] ans = ComplexNumber.sqrt(b);
System.
out.
println("Sqrt of " + b
+ " = " + ans
[0]+ ", " + ans
[1]);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBDb21wbGV4TnVtYmVyIHsKCiAgICBwcml2YXRlIGRvdWJsZSByZTsKICAgIHByaXZhdGUgZG91YmxlIGltOwoKICAgIHB1YmxpYyBDb21wbGV4TnVtYmVyKGRvdWJsZSByZSwgZG91YmxlIGltKSB7CiAgICAgICAgdGhpcy5yZSA9IHJlOwogICAgICAgIHRoaXMuaW0gPSBpbTsKICAgIH0KCiAgICBwdWJsaWMgZG91YmxlIGdldFJlKCkgewogICAgICAgIHJldHVybiByZTsKICAgIH0KCiAgICBwdWJsaWMgZG91YmxlIGdldEltKCkgewogICAgICAgIHJldHVybiBpbTsKICAgIH0KCiAgICBwcml2YXRlIGRvdWJsZSBnZXRNb2R1bGUoKSB7CiAgICAgICAgcmV0dXJuIE1hdGguc3FydCh0aGlzLnJlICogdGhpcy5yZSArIHRoaXMuaW0gKiB0aGl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F0b3IgPSBjbjIuZ2V0UmUoKSpjbjIuZ2V0UmUoKSArIGNuMi5nZXRJbSgpICogY24yLmdldEltKCk7CiAgICAgICAgcmV0dXJuIG5ldyBDb21wbGV4TnVtYmVyKHRlbXAuZ2V0UmUoKS9kZW5vbWluYXRvciwgdGVtcC5nZXRJbSgpL2Rlbm9taW5hdG9yKTsKICAgIH0KCiAgICBwcml2YXRlIGRvdWJsZSBHZXRBcmcoKSB7CiAgICAgICAgaWYgKHRoaXMucmUgPiAwKSB7CiAgICAgICAgICAgIHJldHVybiBNYXRoLmF0YW4oaW0gLyByZSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaWYgKHJlIDwgMCAmJiBpbSA+IDApIHsKICAgICAgICAgICAgICAgIHJldHVybiBNYXRoLlBJICsgTWF0aC5hdGFuKGltIC8gcmUpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmV0dXJuIC1NYXRoLlBJICsgTWF0aC5hdGFuKGltIC8gcmUpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgQ29tcGxleE51bWJlciBwb3coQ29tcGxleE51bWJlciBjbiwgaW50IHBvd2VyKSB7CiAgICAgICAgZG91YmxlIGZhY3RvciA9IE1hdGgucG93KGNuLmdldE1vZHVsZSgpLCBwb3dlcik7CiAgICAgICAgcmV0dXJuIG5ldyBDb21wbGV4TnVtYmVyKGZhY3RvcipNYXRoLmNvcyhwb3dlciAqIGNuLkdldEFyZygpKSwgZmFjdG9yKk1hdGguc2luKHBvd2VyICogY24uR2V0QXJnKCkpKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIENvbXBsZXhOdW1iZXJbXSBzcXJ0KENvbXBsZXhOdW1iZXIgY24pIHsKICAgICAgICBkb3VibGUgYSA9IGNuLmdldE1vZHVsZSgpIC8gMjsKICAgICAgICBDb21wbGV4TnVtYmVyIHBvcyA9IG5ldyBDb21wbGV4TnVtYmVyKE1hdGguc3FydChhICsgY24uZ2V0UmUoKSAvIDIpLCBNYXRoLnNpZ251bShjbi5nZXRJbSgpKSAqIE1hdGguc3FydChhIC0gY24uZ2V0UmUoKSAvIDIpKTsKICAgICAgICBDb21wbGV4TnVtYmVyIG5lZyA9IG5ldyBDb21wbGV4TnVtYmVyKCgtMSkgKiBwb3MuZ2V0UmUoKSwgKC0xKSAqIHBvcy5nZXRJbSgpKTsKICAgICAgICBDb21wbGV4TnVtYmVyW10gYW5zd2VyID0ge3BvcywgbmVnfTsKICAgICAgICByZXR1cm4gYW5zd2VyOwogICAgfQoKCiAgICBwcml2YXRlIFN0cmluZyBzaWduKCkgewogICAgICAgIGlmIChpbSA+IDApIHJldHVybiAiICsgIjsKICAgICAgICBlbHNlIHJldHVybiAiIC0gIjsKICAgIH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7CiAgICAgICAgU3RyaW5nIHN0cmluZzsKICAgICAgICBpZiAoaW0gPT0gMSB8fCBpbSA9PSAtMSkgewogICAgICAgICAgICBpZiAocmUgPT0gMCkgewogICAgICAgICAgICAgICAgc3RyaW5nID0gc2lnbigpICsgImkiOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgc3RyaW5nID0gRG91YmxlLnRvU3RyaW5nKHJlKSArIHNpZ24oKSArICJpIjsKICAgICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHN0cmluZyA9IERvdWJsZS50b1N0cmluZyhyZSkgKyBzaWduKCkgKyBEb3VibGUudG9TdHJpbmcoTWF0aC5hYnMoaW0pKSArICJpIjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN0cmluZzsKICAgIH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBib29sZWFuIGVxdWFscyhPYmplY3Qgb2JqKSB7CiAgICAgICAgaWYgKHRoaXMuZ2V0Q2xhc3MoKSAhPSBvYmouZ2V0Q2xhc3MoKSB8fCBvYmogPT0gbnVsbCkKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBDb21wbGV4TnVtYmVyIHggPSBuZXcgQ29tcGxleE51bWJlcigyLCAzKTsKICAgICAgICBDb21wbGV4TnVtYmVyIHkgPSBuZXcgQ29tcGxleE51bWJlcigtMSwgMik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ6MSA9ICIgKyB4ICsgIiwgICAgIHoyID0gIiArIHkpOwoKICAgICAgICBDb21wbGV4TnVtYmVyIHo7CiAgICAgICAgeiA9IENvbXBsZXhOdW1iZXIuc3VtKHgsIHkpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiKyA6ICIgKyB6KTsKCiAgICAgICAgeiA9IENvbXBsZXhOdW1iZXIuc3VidHJhY3QoeCwgeSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCItIDogIiArIHopOwoKICAgICAgICB6ID0gQ29tcGxleE51bWJlci5kaXZpZGUoeCwgeSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCIvIDogIiArIHopOwoKICAgICAgICB6ID0gQ29tcGxleE51bWJlci5tdWx0aXBseSh4LCB5KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIiAqIDoiICsgeik7CgogICAgICAgIHogPSBDb21wbGV4TnVtYmVyLnBvdyh5LCAyKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlBvdyAyIG9mIHoyIDogIiArIHopOwoKICAgICAgICBDb21wbGV4TnVtYmVyIGIgPSBuZXcgQ29tcGxleE51bWJlcigzLCA0KTsKICAgICAgICBDb21wbGV4TnVtYmVyW10gYW5zID0gQ29tcGxleE51bWJlci5zcXJ0KGIpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU3FydCBvZiAiICsgYiArICIgPSAiICsgYW5zWzBdKyAiLCAgIiArIGFuc1sxXSk7CgogICAgfQp9CgoK