* C++ 언어로 작성된 복소수 계산 예제
/**
* Filename TestComplex.cpp
*
* Purpose: Calculate complex numbers.
*
* With VC++
* Compile: cl TestComplex.cpp /EHsc
* Execute: TestComplex
*
* With g++
* Compile: g++ TestComplex.cpp -o TestComplex
* Execute: ./TestComplex
*
* Date: 2011. 10. 8 (Sat)
*/
#include <iostream>
#include <cstring>
#include <cmath>
#include <complex>
using namespace std;
void printComplex(const char *msg, const complex<long double> &cx) {
if (msg != NULL && strlen(msg) > 0) {
cout << msg;
}
if (cx.imag() == 0)
cout << cx.real() << endl;
else
cout << "(" << cx.real() << " + " << cx.imag() << "j)" << endl;
}
complex<long double> operator*(long double n, const complex<long double> &z) {
double x = n*z.real();
double y = n*z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator*(const complex<long double> &z, long double n) {
double x = n*z.real();
double y = n*z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator/(long double n, const complex<long double> &z) {
double sq = z.real()*z.real() + z.imag()*z.imag();
double x = n*z.real()/sq;
double y = -n*z.imag()/sq;
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator/(const complex<long double> &z, long double n) {
double x = z.real()/n;
double y = z.imag()/n;
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator+(const complex<long double> &z, long double n) {
double x = z.real() + n;
double y = z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator+(long double n, const complex<long double> &z) {
double x = z.real() + n;
double y = z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator-(const complex<long double> &z, long double n) {
double x = z.real() - n;
double y = z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator-(long double n, const complex<long double> &z) {
double x = n - z.real();
double y = -z.imag();
complex<long double> tmp(x, y);
return tmp;
}
complex<long double> operator^(const complex<long double> &z, long double n) {
return exp(n*log(z));
}
complex<long double> operator^(long double n, const complex<long double> &z) {
return exp(z*log(n));
}
bool operator==(const complex<long double> &z, long n) {
return z.real() == n && z.imag() == 0;
}
bool operator==(long n, const complex<long double> &z) {
return z.real() == n && z.imag() == 0;
}
complex<long double> sec(const complex<long double> &z) {
return 1/cos(z);
}
complex<long double> csc(const complex<long double> &z) {
return 1/sin(z);
}
complex<long double> cot(const complex<long double> &z) {
return cos(z)/sin(z);
}
complex<long double> sech(const complex<long double> &z) {
return 1/cosh(z);
}
complex<long double> csch(const complex<long double> &z) {
return 1/sinh(z);
}
complex<long double> coth(const complex<long double> &z) {
return cosh(z)/sinh(z);
}
complex<long double> cbrt(const complex<long double> &z) {
/*
double x = z.real();
double y = z.imag();
double r = pow(x*x + y*y, 1.0/(2*3));
double ang = atan(y/x) / 3.0;
complex<long double> tmp(r*cos(ang), r*sin(ang));
return tmp;
*/
return z^(1.0/3);
}
complex<long double> square(const complex<long double> &z) {
return z*z;
}
complex<long double> cube(const complex<long double> &z) {
return z*z*z;
}
int main() {
complex<long double> a(1, 2);
printComplex("a = ", a);
complex<long double> b(5, 4);
printComplex("b = ", b);
complex<long double> c = a + b;
printComplex("c = a + b = ", c);
c = a - b;
printComplex("c = a - b = ", c);
c = a * b;
printComplex("c = a * b = ", c);
c = a / b;
printComplex("c = a / b = ", c);
c = a^2;
printComplex("c = a^2 = ", c);
printComplex("a^3 = ", a^3);
printComplex("a^-3 = ", a^-3);
printComplex("b = ", b);
printComplex("1/(-b) = ", 1/(-b));
printComplex("-b^-1 = ", -b^-1);
printComplex("-b^-3 = ", -b^-3);
printComplex("b/2 = ", b/2);
printComplex("a = ", a);
printComplex("a + 1 = ", a + 1);
printComplex("b - 2 = ", b - 2);
printComplex("2 - b = ", 2 - b);
printComplex("b - b = ", b - b);
cout << "b - b == 0 ? " << (b - b == 0) << endl;
complex<long double> t(b.real(), b.imag());
printComplex("b = ", b);
printComplex("t = ", t);
cout << "b == t ? " << (b == t) << endl;
printComplex("a = ", a);
printComplex("sin(a) = ", sin(a));
printComplex("cos(a) = ", cos(a));
printComplex("tan(a) = ", tan(a));
printComplex("sec(a) = 1/cos(a) = ", sec(a));
printComplex("csc(a) = 1/sin(a) = ", csc(a));
printComplex("cot(a) = 1/tan(a) = ", cot(a));
printComplex("sinh(a) = ", sinh(a));
printComplex("cosh(a) = ", cosh(a));
printComplex("tanh(a) = ", tanh(a));
printComplex("sech(a) = 1/cosh(a) = ", sech(a));
printComplex("csch(a) = 1/sinh(a) = ", csch(a));
printComplex("coth(a) = 1/tanh(a) = ", coth(a));
printComplex("exp(a) = ", exp(a));
printComplex("log(a) = ", log(a));
printComplex("norm(a) = ", norm(a));
printComplex("abs(a) = ", abs(a));
printComplex("2^a = ", 2^a);
printComplex("3^a = ", 3^a);
printComplex("sqrt(a) = ", sqrt(a));
printComplex("cbrt(a) = ", cbrt(a));
printComplex("square(a) = ", square(a));
printComplex("cube(a) = ", cube(a));
return 0;
}
/*
a = (1 + 2j)
b = (5 + 4j)
c = a + b = (6 + 6j)
c = a - b = (-4 + -2j)
c = a * b = (-3 + 14j)
c = a / b = (0.317073 + 0.146341j)
c = a^2 = (-3 + 4j)
a^3 = (-11 + -2j)
a^-3 = (-0.088 + 0.016j)
b = (5 + 4j)
1/(-b) = (-0.121951 + 0.097561j)
-b^-1 = (-0.121951 + 0.097561j)
-b^-3 = (0.00166858 + 0.00342421j)
b/2 = (2.5 + 2j)
a = (1 + 2j)
a + 1 = (2 + 2j)
b - 2 = (3 + 4j)
2 - b = (-3 + -4j)
b - b = 0
b - b == 0 ? 1
b = (5 + 4j)
t = (5 + 4j)
b == t ? 1
a = (1 + 2j)
sin(a) = (3.16578 + 1.9596j)
cos(a) = (2.03272 + -3.0519j)
tan(a) = (0.0338128 + 1.01479j)
sec(a) = 1/cos(a) = (0.151176 + 0.226974j)
csc(a) = 1/sin(a) = (0.228375 + -0.141363j)
cot(a) = 1/tan(a) = (0.0327978 + -0.984329j)
sinh(a) = (-0.489056 + 1.40312j)
cosh(a) = (-0.642148 + 1.06861j)
tanh(a) = (1.16674 + -0.243458j)
sech(a) = 1/cosh(a) = (-0.413149 + -0.687527j)
csch(a) = 1/sinh(a) = (-0.221501 + -0.635494j)
coth(a) = 1/tanh(a) = (0.82133 + 0.171384j)
exp(a) = (-1.1312 + 2.47173j)
log(a) = (0.804719 + 1.10715j)
norm(a) = 5
abs(a) = 2.23607
2^a = (0.366914 + 1.96606j)
3^a = (-1.75876 + 2.43038j)
sqrt(a) = (1.27202 + 0.786151j)
cbrt(a) = (1.21962 + 0.471711j)
square(a) = (-3 + 4j)
cube(a) = (-11 + -2j)
*/
* Groovy 언어로 작성된 Complex 클래스의 소스 (Java 에서도 사용 가능한 클래스)
(참고: Groovy 1.8.0 과 Java 7 에서 테스트되었음)
/**********************************************************************************
* Filename: Complex.groovy
*
* Purpose:
* A class, supporting complex calculations,
* which depends on the double types of Java.
*
* Setting Environment Variables:
* set JAVA_HOME=c:\Java7
* set GROOVY_HOME=c:\groovy182
* set PATH=c:%GROOVY_HOME%\bin;%PATH%
*
* Execute Only: groovy Complex.groovy
* Compile for Java: groovyc -d . Complex.groovy
*
* Author: Copyright (c) 2011. 10. 7 (Fri) PH Kim ( pkim (AT) scripts (DOT) pe (DOT) kr )
***********************************************************************************/
package kr.pe.scripts.numeric.complex
public class Complex {
public double re;
public double im;
final public static double EPS = 9.0E-16;
final public static Complex ZERO = new Complex(0, 0);
final public static Complex ONE = new Complex(1, 0);
final public static Complex TWO = new Complex(2, 0);
final public static Complex THREE = new Complex(3, 0);
final public static Complex TEN = new Complex(10, 0);
final public static Complex I = new Complex(0, 1);
final public static Complex E = new Complex(Math.E, 0);
final public static Complex PI = new Complex(Math.PI, 0);
public Complex(double x, double y) {
this.re = x;
this.im = y;
}
public static Complex polarForm(double r, double theta) {
double x = r*Math.cos(theta);
double y = r*Math.sin(theta);
Complex z = new Complex(x, y);
adjust(z);
return z;
}
public boolean isZero() {
// return this.re == 0.0 && this.im == 0.0;
return Math.abs(this.re*this.re + this.im*this.im) < EPS
}
public Complex plus(Complex other) {
return new Complex(this.re + other.re, this.im + other.im);
}
public Complex minus(Complex other) {
return new Complex(this.re - other.re, this.im - other.im);
}
public Complex multiply(Complex other) {
double x = this.re*other.re - this.im*other.im;
double y = this.re*other.im + this.im*other.re;
return new Complex(x, y);
}
public Complex div(Complex other) {
if (other.isZero()) {
throw new RuntimeException("Cannot divide by " + other + ", since it is zero.");
}
double x = this.re*other.re + this.im*other.im;
double y = - this.re*other.im + this.im*other.re;
double tmp = other.re*other.re + other.im*other.im;
return new Complex(x/tmp, y/tmp);
}
/*
public Complex power(long n) {
long mn = n;
if (mn < 0.0)
mn = -mn;
Complex tmp = new Complex(1, 0);
for (int i = 0; i < mn; i++) {
tmp = tmp*this;
}
if (n < 0) {
tmp = new Complex(1, 0) / tmp;
}
return tmp;
}
*/
public Complex power(long n) {
if (n == 0)
return new Complex(1, 0);
long mn = n;
if (mn < 0L)
mn = -mn;
Complex tmp = this.power((mn/2) as long);
if (mn % 2 == 1) {
tmp = tmp*tmp*this;
}
else {
tmp = tmp*tmp;
}
if (n < 0) {
tmp = Complex.ONE / tmp;
}
return tmp;
}
public Complex power(double x) {
if (x == 0.0)
return new Complex(1, 0);
return exp(new Complex(x, 0)*log(this));
}
public Complex power(Complex other) {
if (other.isZero())
return new Complex(1, 0);
return exp(other*log(this));
}
public static Complex sqrt(Complex z) {
if (z.isZero())
return new Complex(0, 0);
double r = Math.sqrt(abs(z));
double angle = arg(z) / 2.0;
double x = r*Math.cos(angle);
double y = r*Math.sin(angle);
return new Complex(x, y);
}
public static Complex cbrt(Complex z) {
if (z.isZero())
return new Complex(0, 0);
double r = Math.cbrt(abs(z));
double angle = arg(z)/3.0;
double x = r*Math.cos(angle);
double y = r*Math.sin(angle);
return new Complex(x, y);
}
public static Complex square(Complex z) {
return z*z;
}
public static Complex cube(Complex z) {
return z*z*z;
}
public Complex inverse() {
return new Complex(1, 0) / this;
}
public double abs() {
double x = this.re;
double y = this.im;
return Math.sqrt(x*x + y*y);
}
public double norm() {
double x = this.re;
double y = this.im;
return Math.sqrt(x*x + y*y);
}
public double distance(Complex other) {
double x = this.re - other.re;
double y = this.im - other.im;
return Math.sqrt(x*x + y*y);
}
public static double abs(Complex z) {
double x = z.re;
double y = z.im;
return Math.sqrt(x*x + y*y);
}
public static double norm(Complex z) {
double x = z.re;
double y = z.im;
return Math.sqrt(x*x + y*y);
}
public static double distance(Complex z, Complex w) {
double x = z.re - w.re;
double y = z.im - w.im;
return Math.sqrt(x*x + y*y);
}
public static Complex innerProduct(Complex z, Complex w) {
return conjugate(z)*w;
}
public Complex innerProduct(Complex other) {
return this.conjugate()*other;
}
public static boolean isOrthogonal(Complex z, Complex w) {
// return z.re*w.re + z.im*w.im == 0.0;
return Math.abs(z.re*w.re + z.im*w.im) < EPS;
}
public boolean isOrthogonal(Complex other) {
// return this.re*other.re + this.im*other.im == 0.0;
return Math.abs(this.re*other.re + this.im*other.im) < EPS;
}
public static double angleCosine(Complex z, Complex w) {
return (z.re*w.re + z.im*w.im)/(Math.sqrt(z.re*z.re + z.im*z.im) * Math.sqrt(w.re*w.re + w.im*w.im));
}
public angleCosine(Complex other) {
return (this.re*other.re + this.im*other.im)/(Math.sqrt(this.re*this.re + this.im*this.im) * Math.sqrt(other.re*other.re + other.im*other.im));
}
public static double angle(Complex z, Complex w) {
return 180.0 / Math.PI * Math.acos((z.re*w.re + z.im*w.im)/(Math.sqrt(z.re*z.re + z.im*z.im) * Math.sqrt(w.re*w.re + w.im*w.im)));
}
public double angle(Complex other) {
return 180.0 / Math.PI * Math.acos((this.re*other.re + this.im*other.im)/(Math.sqrt(this.re*this.re + this.im*this.im) * Math.sqrt(other.re*other.re + other.im*other.im)));
}
public Complex negative() {
return new Complex(-this.re, -this.im);
}
public Complex conjugate() {
return new Complex(this.re, -this.im);
}
public static Complex conjugate(Complex z) {
return new Complex(z.re, -z.im);
}
public static Complex exp(Complex z) {
double x, y;
x = Math.exp(z.re)*Math.cos(z.im);
y = Math.exp(z.re)*Math.sin(z.im);
return new Complex(x, y);
}
public static Complex log(Complex z) {
double x, y;
x = Math.log(abs(z));
if (z.re == 0.0) {
if (z.im > 0.0)
y = Math.PI/2;
else if (z.im < 0.0)
y = -Math.PI/2;
else
throw new RuntimeException("log(" + z + ") cannot be evaulated.");
}
else {
y = Math.atan(z.im/z.re);
if (z.re < 0) {
if (z.im >= 0)
y = y + Math.PI;
else
y = y - Math.PI;
}
}
return new Complex(x, y);
}
public double arg() {
double y;
if (this.re == 0.0) {
if (this.im > 0.0)
y = Math.PI/2;
else if (this.im < 0.0)
y = -Math.PI/2;
else
throw new RuntimeException("arg(" + this + ") cannot be evaulated.");
}
else {
y = Math.atan(this.im/this.re);
if (this.re < 0) {
if (this.im >= 0)
y = y + Math.PI;
else
y = y - Math.PI;
}
}
return y;
}
public static double arg(Complex z) {
double y;
if (z.re == 0.0) {
if (z.im > 0.0)
y = Math.PI/2;
else if (z.im < 0.0)
y = -Math.PI/2;
else
throw new RuntimeException("arg(" + z + ") cannot be evaulated.");
}
else {
y = Math.atan(z.im/z.re);
if (z.re < 0) {
if (z.im >= 0)
y = y + Math.PI;
else
y = y - Math.PI;
}
}
return y;
}
public static Complex sin(Complex z) {
double x, y;
x = Math.sin(z.re)*Math.cosh(z.im);
y = Math.cos(z.re)*Math.sinh(z.im);
return new Complex(x, y);
}
public static Complex cos(Complex z) {
double x, y;
x = Math.cos(z.re)*Math.cosh(z.im);
y = -Math.sin(z.re)*Math.sinh(z.im);
return new Complex(x, y);
}
public static Complex tan(Complex z) {
Complex a = cos(z);
if (a.isZero()) {
throw new RuntimeException("tan(" + z + ") cannot be evaulated, since cos(" + z + ") = 0.");
}
Complex b = sin(z);
return b/a;
}
public static Complex sec(Complex z) {
Complex a = cos(z);
if (a.isZero()) {
throw new RuntimeException("sec(" + z + ") cannot be evaulated, since cos(" + z + ") = 0.");
}
return ONE/a;
}
public static Complex csc(Complex z) {
Complex a = sin(z);
if (a.isZero()) {
throw new RuntimeException("csc(" + z + ") cannot be evaulated, since sin(" + z + ") = 0.");
}
return ONE/a;
}
public static Complex cot(Complex z) {
Complex a = sin(z);
if (a.isZero()) {
throw new RuntimeException("tan(" + z + ") cannot be evaulated, since sin(" + z + ") = 0.");
}
Complex b = cos(z);
return b/a;
}
public static Complex sinh(Complex z) {
Complex a = exp(z);
return (a - a.inverse())/TWO;
}
public static Complex cosh(Complex z) {
Complex a = exp(z);
return (a + a.inverse())/TWO;
}
public static Complex tanh(Complex z) {
Complex a = cosh(z);
if (a.isZero()) {
throw new RuntimeException("tanh(" + z + ") cannot be evaulated, since cosh(" + z + ") = 0.");
}
Complex b = sinh(z);
return b/a;
}
public static Complex sech(Complex z) {
Complex a = cosh(z);
if (a.isZero()) {
throw new RuntimeException("sech(" + z + ") cannot be evaulated, since cosh(" + z + ") = 0.");
}
return ONE/a;
}
public static Complex csch(Complex z) {
Complex a = sinh(z);
if (a.isZero()) {
throw new RuntimeException("csch(" + z + ") cannot be evaulated, since sinh(" + z + ") = 0.");
}
return ONE/a;
}
public static Complex coth(Complex z) {
Complex a = sinh(z);
if (a.isZero()) {
throw new RuntimeException("coth(" + z + ") cannot be evaulated, since sinh(" + z + ") = 0.");
}
Complex b = cosh(z);
return b/a;
}
public boolean equals(Complex other) {
// return this.re == other.re && this.im == other.im;
double x = this.re - other.re;
double y = this.re - other.re;
return Math.sqrt(x*x + y*y) < EPS;
}
public Complex rotate(double deg) {
return this*exp(new Complex(0, deg*Math.PI/180.0));
}
public static void adjust(Complex z) {
if (Math.abs(z.re) < EPS)
z.re = 0.0;
if (Math.abs(z.im) < EPS)
z.im = 0.0;
}
public void adjust() {
if (Math.abs(this.re) < EPS)
this.re = 0.0;
if (Math.abs(this.im) < EPS)
this.im = 0.0;
}
public String toString() {
return "(" + this.re + " + " + this.im + "j)";
}
public static void println(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Complex z = new Complex(1, 2);
Complex w = new Complex(5, 4);
println("z = " + z);
println("w = " + w);
println(" z + w = " + (z + w));
println(" z - w = " + (z - w));
println(" z * w = " + (z * w));
println(" z / w = " + (z / w));
println(" z**2 = " + (z**2));
println(" z**3 = " + (z**3));
println(" z.inverse() = " + z.inverse());
println(" z**-1 = " + (z**-1));
println(" z**-2 = " + (z**-2));
println(" z**-3 = " + (z**-3));
println(" -z = " + (-z));
println(" conjugate(z) = conjugate(" + z + ") = " + conjugate(z));
println(" z.conjugate() = (" + z + ").conjuate() = " + z.conjugate());
println(" abs(z) = abs(" + z + ") = " + abs(z));
println(" norm(z) = norm(" + z + ") = " + norm(z));
println(" z.abs() = (" + z + ").abs() = " + z.abs());
println(" z.norm() = (" + z + ").norm() = " + z.norm());
println(" distance(z, w) = distance(" + z + ", " + w + ") = " + distance(z, w));
println(" z.distance(w) = (" + z + ").distance("+ w + ") = " + z.distance(w));
println(" exp(z) = exp(" + z + ") = " + exp(z));
println(" log(z) = log(" + z + ") = " + log(z));
println(" arg(z) = arg(" + z + ") = " + arg(z));
println(" abs(" + new Complex(-1, 2) + ") = " + abs(new Complex(-1, 2)));
println(" log(" + new Complex(-1, 2) + ") = " + log(new Complex(-1, 2)));
println(" abs(" + new Complex(-1, -2) + ") = " + abs(new Complex(-1, -2)));
println(" log(" + new Complex(-1, -2) + ") = " + log(new Complex(-1, -2)));
println(" arg(" + new Complex(1, -2) + ") = " + arg(new Complex(1, -2)));
println(" arg(" + new Complex(-1, 2) + ") = " + arg(new Complex(-1, 2)));
println(" arg(" + new Complex(-1, -2) + ") = " + arg(new Complex(-1, -2)));
println(" sin(z) = sin(" + z + ") = " + sin(z));
println(" cos(z) = cos(" + z + ") = " + cos(z));
println(" tan(z) = tan(" + z + ") = " + tan(z));
println(" sec(z) = sec(" + z + ") = " + sec(z));
println(" csc(z) = csc(" + z + ") = " + csc(z));
println(" cot(z) = cot(" + z + ") = " + cot(z));
println(" sinh(z) = sinh(" + z + ") = " + sinh(z));
println(" cosh(z) = cosh(" + z + ") = " + cosh(z));
println(" tanh(z) = tanh(" + z + ") = " + tanh(z));
println(" sech(z) = sech(" + z + ") = " + sech(z));
println(" csch(z) = csch(" + z + ") = " + csch(z));
println(" coth(z) = coth(" + z + ") = " + coth(z));
println(" z.rotate(90) = " + z.rotate(90));
println(" z.rotate(-90) = " + z.rotate(-90));
println(" z == " + new Complex(1, 2) + " ? " + (z == new Complex(1,2)));
println(" z - z = " + (z - z));
println(" (z - z).isZero() ? " + (z - z).isZero());
println(" Complex.ZERO = " + Complex.ZERO);
println(" Complex.ONE = " + Complex.ONE);
println(" Complex.TWO = " + Complex.TWO);
println(" Complex.THREE = " + Complex.THREE);
println(" Complex.TEN = " + Complex.TEN);
println(" Complex.I = " + Complex.I);
println(" Complex.E = " + Complex.E);
println(" Complex.PI = " + Complex.PI);
println(" z = " + z);
println(" w = " + w);
println(" innerProduct(z, w) = " + innerProduct(z, w));
println(" z.innerProduct(w) = " + z.innerProduct(w));
println(" isOrthogonal(z, w) = " + isOrthogonal(z, w));
println(" z.isOrthogonal(w) = " + z.isOrthogonal(w));
println(" z.innerProduct(z.rotate(90)) = " + z.innerProduct(z.rotate(90)));
println(" isOrthogonal(z, z.rotate(90))= " + isOrthogonal(z, z.rotate(90)));
println(" z.isOrthogonal(z.rotate(90)) = " + z.isOrthogonal(z.rotate(90)));
println(" angleCosine(z, w) = " + angleCosine(z, w));
println(" z.angleCosine(w) = " + z.angleCosine(w));
println(" angle(z, w) = " + angle(z, w));
println(" z.angle(w) = " + z.angle(w));
println(" angle(z, z.rotate(90))= " + angle(z, z.rotate(90)));
println(" z.angle(z.rotate(90)) = " + z.angle(z.rotate(90)));
println(" angle(z, z.rotate(-90))= " + angle(z, z.rotate(-90)));
println(" z.angle(z.rotate(-90)) = " + z.angle(z.rotate(-90)));
println(" I**I = " + (I**I));
println(" TWO**I = " + (TWO**I));
println(" THREE**I = " + (THREE**I));
println(" TEN**I = " + (TEN**I));
println(" I**TWO = " + (I**TWO));
println(" I**THREE = " + (I**THREE));
println(" I**TEN = " + (I**TEN));
Complex u = I**TEN;
println(" u = I**TEN = " + u);
Complex.adjust(u);
println(" Atfer Complex.adjust(u)");
println(" u = " + u);
println(" sqrt(z) = " + sqrt(z));
println(" cbrt(z) = " + cbrt(z));
println(" square(z) = " + square(z));
println(" cube(z) = " + cube(z));
println(" Complex.polarForm(1, Math.PI/2) = " + Complex.polarForm(1, Math.PI/2));
println(" Complex.polarForm(1, Math.PI) = " + Complex.polarForm(1, Math.PI));
}
}
/*
Execution Result:
z = (1.0 + 2.0j)
w = (5.0 + 4.0j)
z + w = (6.0 + 6.0j)
z - w = (-4.0 + -2.0j)
z * w = (-3.0 + 14.0j)
z / w = (0.3170731707317073 + 0.14634146341463414j)
z**2 = (-3.0 + 4.0j)
z**3 = (-11.0 + -2.0j)
z.inverse() = (0.2 + -0.4j)
z**-1 = (0.2 + -0.4j)
z**-2 = (-0.12 + -0.16j)
z**-3 = (-0.088 + 0.016j)
-z = (-1.0 + -2.0j)
conjugate(z) = conjugate((1.0 + 2.0j)) = (1.0 + -2.0j)
z.conjugate() = ((1.0 + 2.0j)).conjuate() = (1.0 + -2.0j)
abs(z) = abs((1.0 + 2.0j)) = 2.23606797749979
norm(z) = norm((1.0 + 2.0j)) = 2.23606797749979
z.abs() = ((1.0 + 2.0j)).abs() = 2.23606797749979
z.norm() = ((1.0 + 2.0j)).norm() = 2.23606797749979
distance(z, w) = distance((1.0 + 2.0j), (5.0 + 4.0j)) = 4.47213595499958
z.distance(w) = ((1.0 + 2.0j)).distance((5.0 + 4.0j)) = 4.47213595499958
exp(z) = exp((1.0 + 2.0j)) = (-1.1312043837568138 + 2.471726672004819j)
log(z) = log((1.0 + 2.0j)) = (0.8047189562170503 + 1.1071487177940904j)
arg(z) = arg((1.0 + 2.0j)) = 1.1071487177940904
abs((-1.0 + 2.0j)) = 2.23606797749979
log((-1.0 + 2.0j)) = (0.8047189562170503 + 2.0344439357957027j)
abs((-1.0 + -2.0j)) = 2.23606797749979
log((-1.0 + -2.0j)) = (0.8047189562170503 + -2.0344439357957027j)
arg((1.0 + -2.0j)) = -1.1071487177940904
arg((-1.0 + 2.0j)) = 2.0344439357957027
arg((-1.0 + -2.0j)) = -2.0344439357957027
sin(z) = sin((1.0 + 2.0j)) = (3.165778513216168 + 1.9596010414216063j)
cos(z) = cos((1.0 + 2.0j)) = (2.0327230070196656 + -3.0518977991518j)
tan(z) = tan((1.0 + 2.0j)) = (0.0338128260798966 + 1.0147936161466335j)
sec(z) = sec((1.0 + 2.0j)) = (0.15117629826557724 + 0.2269736753937216j)
csc(z) = csc((1.0 + 2.0j)) = (0.22837506559968654 + -0.1413630216124078j)
cot(z) = cot((1.0 + 2.0j)) = (0.0327977555337525 + -0.9843292264581908j)
sinh(z) = sinh((1.0 + 2.0j)) = (-0.48905625904129374 + 1.4031192506220407j)
cosh(z) = cosh((1.0 + 2.0j)) = (-0.6421481247155201 + 1.0686074213827785j)
tanh(z) = tanh((1.0 + 2.0j)) = (1.16673625724092 + -0.24345820118572523j)
sech(z) = sech((1.0 + 2.0j)) = (-0.41314934426693994 + -0.6875274386554789j)
csch(z) = csch((1.0 + 2.0j)) = (-0.2215009308505094 + -0.6354937992538999j)
coth(z) = coth((1.0 + 2.0j)) = (0.8213297974938518 + 0.17138361290918502j)
z.rotate(90) = (-2.0 + 1.0000000000000002j)
z.rotate(-90) = (2.0 + -0.9999999999999999j)
z == (1.0 + 2.0j) ? true
z - z = (0.0 + 0.0j)
(z - z).isZero() ? true
Complex.ZERO = (0.0 + 0.0j)
Complex.ONE = (1.0 + 0.0j)
Complex.TWO = (2.0 + 0.0j)
Complex.THREE = (3.0 + 0.0j)
Complex.TEN = (10.0 + 0.0j)
Complex.I = (0.0 + 1.0j)
Complex.E = (2.718281828459045 + 0.0j)
Complex.PI = (3.141592653589793 + 0.0j)
z = (1.0 + 2.0j)
w = (5.0 + 4.0j)
innerProduct(z, w) = (13.0 + -6.0j)
z.innerProduct(w) = (13.0 + -6.0j)
isOrthogonal(z, w) = false
z.isOrthogonal(w) = false
z.innerProduct(z.rotate(90)) = (4.440892098500626E-16 + 5.0j)
isOrthogonal(z, z.rotate(90))= true
z.isOrthogonal(z.rotate(90)) = true
angleCosine(z, w) = 0.9079593845004517
z.angleCosine(w) = 0.9079593845004517
angle(z, w) = 24.77514056883192
z.angle(w) = 24.77514056883192
angle(z, z.rotate(90))= 90.0
z.angle(z.rotate(90)) = 90.0
angle(z, z.rotate(-90))= 90.0
z.angle(z.rotate(-90)) = 90.0
I**I = (0.20787957635076193 + 0.0j)
TWO**I = (0.7692389013639721 + 0.6389612763136348j)
THREE**I = (0.4548324228266097 + 0.8905770416677471j)
TEN**I = (-0.6682015101903132 + 0.7439803369574931j)
I**TWO = (-1.0 + 1.2246467991473532E-16j)
I**THREE = (-1.8369701987210297E-16 + -1.0j)
I**TEN = (-1.0 + 6.123233995736766E-16j)
u = I**TEN = (-1.0 + 6.123233995736766E-16j)
Atfer Complex.adjust(u)
u = (-1.0 + 0.0j)
sqrt(z) = (1.272019649514069 + 0.7861513777574233j)
cbrt(z) = (1.2196165079717578 + 0.47171126778938893j)
square(z) = (-3.0 + 4.0j)
cube(z) = (-11.0 + -2.0j)
Complex.polarForm(1, Math.PI/2) = (0.0 + 1.0j)
Complex.polarForm(1, Math.PI) = (-1.0 + 0.0j)
*/