public class Complex_polar { private double r; private double theta; private Complex_polar(double r, double theta) { if(r < 0) { r = -r; theta = theta - Math.PI; } theta -= 2 * Math.PI * (theta / (2 * Math.PI)); // theta = theta % 2*PI if(theta > Math.PI) theta -= Math.PI * 2; this.r = r; this.theta = theta; } public static Complex_polar Complex_from_cartesian(double a, double b) { return new Complex_polar(Math.sqrt(a*a + b*b), Math.atan2(a, b)); } public static Complex_polar Complex_from_polar(double r, double theta) { return new Complex_polar(r, theta); } public double getR() { return r; } public double getTheta() { return theta; } public Complex_polar add(Complex_polar c) { double new_a = r * Math.cos(theta) + c.getR() * Math.cos(c.getTheta()); double new_b = r * Math.sin(theta) + c.getR() * Math.sin(c.getTheta()); return new Complex_polar(r + c.getR(), Math.atan2(new_a, new_b)); } public Complex_polar multiply(Complex_polar c) { return new Complex_polar(r * c.getR(), theta + c.getTheta()); } }