/****************************************************************************** * This program demonstrates the Complex Number class. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class ComplexNumberClass { static class Complex { // Internal representation for a complex number is two doubles private double real = 0.0; private double imaginary = 0.0; // Constructor public Complex(double r, double i) { real = r; imaginary = i; } public Complex add(final Complex x) { return new Complex(real + x.real, imaginary + x.imaginary); } public Complex sub(final Complex x) { return new Complex(real - x.real, imaginary - x.imaginary); } public Complex mul(final Complex x) { return new Complex(real * x.real - imaginary * x.imaginary, real * x.imaginary + imaginary * x.real); } public Complex recip() { final double MAG_SQUARED = real * real + imaginary * imaginary; return new Complex(real / MAG_SQUARED, -imaginary / MAG_SQUARED); } public Complex div(final Complex x) { return this.mul(x.recip()); } public double abs() { final double MAG_SQUARED = real * real + imaginary * imaginary; return Math.sqrt(MAG_SQUARED); } public double phase() { final double MAGNITUDE = this.abs(); if (real < 0. && imaginary == 0.) { return Math.PI; } return 2. * Math.atan(imaginary / (MAGNITUDE + real)); } public Complex csqrt() { final double MAG = this.abs(); final double r = Math.sqrt((real + MAG) / 2.); double i = Math.sqrt((-real + MAG) / 2.); i *= ((imaginary > 0) ? 1 : ((imaginary < 0) ? -1 : 0)); return new Complex(r, i); } public Complex cexp() { final double MAG = Math.exp(real); return new Complex(MAG * Math.cos(imaginary), MAG * Math.sin(imaginary)); } public Complex clog() { final double MAG = this.abs(); return new Complex(Math.log(MAG), phase()); } public String toString() { if (real == 0.0) { return Utils.join("", String.valueOf(imaginary), "i"); } else if (imaginary == 0.0) { return String.valueOf(real); } else if (imaginary < 0) { return Utils.join("", String.valueOf(real), String.valueOf(imaginary), "i"); } else { return Utils.join("", String.valueOf(real), "+", String.valueOf(imaginary), "i"); } } } public static void main(String[] args) { Complex a = new Complex(1., 1.); Complex b = new Complex(3., 4.); System.out.println(Utils.join("", "a: ", a.toString())); System.out.println(Utils.join("", "b: ", b.toString())); System.out.println(Utils.join("", "a + b: ", a.add(b).toString())); System.out.println(Utils.join("", "a - b: ", a.sub(b).toString())); System.out.println(Utils.join("", "a * b: ", a.mul(b).toString())); System.out.println(Utils.join("", "1 / a: ", a.recip().toString())); System.out.println(Utils.join("", "1 / b: ", b.recip().toString())); System.out.println(Utils.join("", "a / b: ", a.div(b).toString())); System.out.println(Utils.join("", "|a|: ", a.abs())); System.out.println(Utils.join("", "|b|: ", b.abs())); System.out.println(Utils.join("", "φ(a): ", a.phase())); System.out.println(Utils.join("", "φ(b): ", b.phase())); System.out.println(Utils.join("", "sqrt(a): ", a.csqrt().toString())); System.out.println(Utils.join("", "sqrt(b): ", b.csqrt().toString())); System.out.println(Utils.join("", "exp(a): ", a.cexp().toString())); System.out.println(Utils.join("", "exp(b): ", b.cexp().toString())); System.out.println(Utils.join("", "ln(a): ", a.clog().toString())); System.out.println(Utils.join("", "ln(b): ", b.clog().toString())); } }