* Mathematica 로 연습해본 복소수 계산 (PDF 파일로 저장한 것)



* Octave 로 연습해본 복소수 계산 (허수 단위는 기호 j 로 표현)
octave-3.4.0:1> abs(1+2j)
ans =  2.2361
octave-3.4.0:2> log(1+2j)
ans =  0.80472 + 1.10715i
octave-3.4.0:3> log(-1+2j)
ans =  0.80472 + 2.03444i
octave-3.4.0:4> log(-1-2j)
ans =  0.80472 - 2.03444i
octave-3.4.0:5> arg(1+2j)                           
ans =  1.1071
octave-3.4.0:6> arg(1+-2j)
ans = -1.1071
octave-3.4.0:7> arg(-1+-2j)
ans = -2.0344
octave-3.4.0:8> arg(-1-2j) 
ans = -2.0344
octave-3.4.0:9> arg(-1+2j)
ans =  2.0344
octave-3.4.0:10> (1+2J)*exp(pi j/2)
parse error:

  syntax error

>>> (1+2J)*exp(pi j/2)
                  ^

octave-3.4.0:10> pi
ans =  3.1416
octave-3.4.0:11> j
ans =  0 + 1i
octave-3.4.0:12> pi j
error: pi: invalid data type specified
octave-3.4.0:12> (1+2J)*exp(pi* j/2)
ans = -2.0000 + 1.0000i
octave-3.4.0:13> (1+2J)^2           
ans = -3 + 4i
octave-3.4.0:14> (1+2J)^3
ans = -11 -  2i
octave-3.4.0:15> (1+2J)**2
ans = -3 + 4i
octave-3.4.0:16> (1+2J)**3
ans = -11 -  2i
octave-3.4.0:17>                                                               




* 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)
*/




* Groovy 언어로 작성된 Complex 클래스를 테스트하는 Java 예제 소스
/**********************************************************************************
 * Filename: TestComplex.java
 *
 *  Purpose:
 *           Testing the Complex class made by Groovy,
 *
 *  Setting Environment Variables:
 *           set JAVA_HOME=c:\Java7
 *           set GROOVY_HOME=c:\groovy182
 *           set PATH=c:%GROOVY_HOME%\bin;%PATH%
 *
 *  Compile: javac -d . TestComplex.java
 *  Execute: java -cp .;%GROOVY_HOME%\embeddable\groovy-all-1.8.2.jar
 *                kr.pe.scripts.numeric.complex.TestComplex
 *
 *    Author: Copyright (c) 2011. 10. 7 (Fri)  PH Kim  ( pkim (AT) scripts (DOT) pe (DOT) kr )
 ***********************************************************************************/

package kr.pe.scripts.numeric.complex;

import kr.pe.scripts.numeric.complex.Complex;

public class TestComplex {

    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.plus(w));
        println("    z - w = " + z.minus(w));
        println("    z * w = " + z.multiply(w));
        println("    z / w = " + z.div(w));
        println("    z**2 = " + z.power(2));
        println("    z**3 = " + z.power(3));
        println("    z.inverse() = " + z.inverse());
        println("    z**-1 = " + z.power(-1));
        println("    z**-2 = " + z.power(-2));
        println("    z**-3 = " + z.power(-3));
        println("    -z = " + z.negative());
        println("    conjugate(z) = conjugate(" + z + ") = " + Complex.conjugate(z));
        println("    z.conjugate() = (" + z + ").conjuate() = " + z.conjugate());
        println("    abs(z) = abs(" + z + ") = " + Complex.abs(z));
        println("    norm(z) = norm(" + z + ") = " + Complex.norm(z));
        println("    z.abs() = (" + z + ").abs() = " + z.abs());
        println("    z.norm() = (" + z + ").norm() = " + z.norm());
        println("    distance(z, w) = distance(" + z + ", " + w + ") = " + Complex.distance(z, w));
        println("    z.distance(w) = (" + z + ").distance("+ w + ") = " + z.distance(w));
        println("    exp(z) = exp(" + z + ") = " + Complex.exp(z));
        println("    log(z) = log(" + z + ") = " + Complex.log(z));
        println("    arg(z) = arg(" + z + ") = " + Complex.arg(z));
        println("    abs(" + new Complex(-1, 2)  + ") = " + Complex.abs(new Complex(-1, 2)));
        println("    log(" + new Complex(-1, 2)  + ") = " + Complex.log(new Complex(-1, 2)));
        println("    abs(" + new Complex(-1, -2)  + ") = " + Complex.abs(new Complex(-1, -2)));
        println("    log(" + new Complex(-1, -2)  + ") = " + Complex.log(new Complex(-1, -2)));
        println("    arg(" + new Complex(1, -2)  + ") = " + Complex.arg(new Complex(1, -2)));
        println("    arg(" + new Complex(-1, 2)  + ") = " + Complex.arg(new Complex(-1, 2)));
        println("    arg(" + new Complex(-1, -2)  + ") = " + Complex.arg(new Complex(-1, -2)));
        println("    sin(z) = sin(" + z + ") = " + Complex.sin(z));
        println("    cos(z) = cos(" + z + ") = " + Complex.cos(z));
        println("    tan(z) = tan(" + z + ") = " + Complex.tan(z));
        println("    sec(z) = sec(" + z + ") = " + Complex.sec(z));
        println("    csc(z) = csc(" + z + ") = " + Complex.csc(z));
        println("    cot(z) = cot(" + z + ") = " + Complex.cot(z));
        println("    sinh(z) = sinh(" + z + ") = " + Complex.sinh(z));
        println("    cosh(z) = cosh(" + z + ") = " + Complex.cosh(z));
        println("    tanh(z) = tanh(" + z + ") = " + Complex.tanh(z));
        println("    sech(z) = sech(" + z + ") = " + Complex.sech(z));
        println("    csch(z) = csch(" + z + ") = " + Complex.csch(z));
        println("    coth(z) = coth(" + z + ") = " + Complex.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 == " + new Complex(1, 2) + " ? " + (z.equals(new Complex(1,2))));
        println("    z - z = " + z.minus(z));
        println("    (z - z).isZero() ? " + z.minus(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) = " + Complex.innerProduct(z, w));
        println("    z.innerProduct(w) = " + z.innerProduct(w));
        println("    isOrthogonal(z, w) = " + Complex.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))= " + Complex.isOrthogonal(z, z.rotate(90)));
        println("    z.isOrthogonal(z.rotate(90)) = " + z.isOrthogonal(z.rotate(90)));
        println("    angleCosine(z, w) = " + Complex.angleCosine(z, w));
        println("    z.angleCosine(w) = " + z.angleCosine(w));
        println("    angle(z, w) = " + Complex.angle(z, w));
        println("    z.angle(w) = " + z.angle(w));
        println("    angle(z, z.rotate(90))= " + Complex.angle(z, z.rotate(90)));
        println("    z.angle(z.rotate(90)) = " + z.angle(z.rotate(90)));
        println("    angle(z, z.rotate(-90))= " + Complex.angle(z, z.rotate(-90)));
        println("    z.angle(z.rotate(-90)) = " + z.angle(z.rotate(-90)));
        println("    I**I = " + Complex.I.power(Complex.I));
        println("    TWO**I = " + Complex.TWO.power(Complex.I));
        println("    THREE**I = " + Complex.THREE.power(Complex.I));
        println("    TEN**I = " + Complex.TEN.power(Complex.I));
        println("    I**TWO = " + Complex.I.power(Complex.TWO));
        println("    I**THREE = " + Complex.I.power(Complex.THREE));
        println("    I**TEN = " + Complex.I.power(Complex.TEN));
        Complex u = Complex.I.power(Complex.TEN);
        println("    u = I**TEN = " + u);
        Complex.adjust(u);
        println("    Atfer Complex.adjust(u)");
        println("    u = " + u);
        println("    sqrt(z) = " + Complex.sqrt(z));
        println("    cbrt(z) = " + Complex.cbrt(z));
        println("    square(z) = " + Complex.square(z));
        println("    cube(z) = " + Complex.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));
    }
}
/*
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)
*/




* 파이썬 언어로 해보는 복소수 계산(파이썬 언어에서 허수 단위는 j로 표기)
>>> z = 1 + 2j
>>> w = 5 + 4j
>>> z + w
(6+6j)
>>> z - w
(-4-2j)
>>> z * w
(-3+14j)
>>> z / w
(0.3170731707317074+0.14634146341463417j)
>>> z**2
(-3+4j)
>>> z**3
(-11-2j)
>>> 1/z
(0.2-0.4j)
>>> z**-1
(0.2-0.4j)
>>> z**-2
(-0.12-0.16j)
>>> z**-3
(-0.08800000000000001+0.016j)
>>> -z
(-1-2j)
>>> z.conjugate()
(1-2j)
>>> abs(z)
2.23606797749979
>>> abs(z - w)
4.47213595499958
>>> import math
>>> def exp(z):
...     return math.e**z
...
>>> exp(z)
(-1.1312043837568135+2.4717266720048188j)
>>> def sin(z):
...     return (math.e**(1j*z) - math.e**(-1j*z))/(2j)
...
>>> sin(z)
(3.165778513216168+1.9596010414216058j)
>>> def cos(z):
...     return (math.e**(1j*z) + math.e**(-1j*z))/2
...
>>> cos(z)
(2.0327230070196656-3.0518977991517997j)
>>> def tan(z):
...     return sin(z)/cos(z)
...
>>> tan(z)
(0.033812826079896816+1.0147936161466338j)
>>> def sec(z):
...     return 1/cos(z)
...
>>> sec(z)
(0.15117629826557727+0.22697367539372162j)
>>> def csc(z):
...     return 1/sin(z)
...
>>> csc(z)
(0.2283750655996866-0.1413630216124078j)
>>> def cot(z):
...     return cos(z)/sin(z)
...
>>> cot(z)
(0.03279775553375272-0.9843292264581911j)
>>> z == 1 + 2j
True
>>> z - z == 0
True
>>> 1j
1j
>>> z.conjugate()*w
(13-6j)



* Python 에서 math 대신 cmath 를 임포트(import, 수입)하여 초월함수 값 계산하기
>>> z = 1 + 2j
>>> w = 5 + 4j
>>> z + w
(6+6j)
>>> z - w
(-4-2j)
>>> z * w
(-3+14j)
>>> z / w
(0.3170731707317074+0.14634146341463417j)
>>> z**2
(-3+4j)
>>> z**3
(-11-2j)
>>> 1/z
(0.2-0.4j)
>>> z**-1
(0.2-0.4j)
>>> z**-2
(-0.12-0.16j)
>>> z**-3
(-0.08800000000000001+0.016j)
>>> -z
(-1-2j)
>>> z.conjugate()
(1-2j)
>>> abs(z)
2.23606797749979
>>> exp(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'exp' is not defined
>>> import cmath
>>> cmath.exp(z)
(-1.1312043837568135+2.4717266720048188j)
>>> cmath.sin(z)
(3.165778513216168+1.959601041421606j)
>>> cmath.cos(z)
(2.0327230070196656-3.0518977991518j)
>>> cmath.sec(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sec'
>>> cmath.sinh(z)
(-0.4890562590412937+1.4031192506220407j)
>>> cmath.cosh(z)
(-0.64214812471552+1.0686074213827783j)
>>> cmath.tanh(z)
(1.16673625724092-0.24345820118572525j)
>>> cmath.sech(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sech'
>>> cmath.log(z)
(0.8047189562170503+1.1071487177940904j)




* irb(인터렉티브 ruby)를 실행하여 연습해본 복소수 계산
irb(main):001:0> z = Complex(1, 2)
=> (1+2i)
irb(main):002:0> w = Complex(5,1)
=> (5+1i)
irb(main):003:0> z + w
=> (6+3i)
irb(main):004:0> z - w
=> (-4+1i)
irb(main):005:0> z * w
=> (3+11i)
irb(main):006:0> z / w
=> ((7/26)+(9/26)*i)
irb(main):007:0> z**2
=> (-3+4i)
irb(main):008:0> z**3
=> (-11-2i)
irb(main):009:0> 1/z
=> ((1/5)-(2/5)*i)
irb(main):010:0> z**-1
=> ((1/5)-(2/5)*i)
irb(main):011:0> z**-2
=> ((-3/25)-(4/25)*i)
irb(main):012:0> z**-3
=> ((-11/125)+(2/125)*i)
irb(main):013:0> -z
=> (-1-2i)
irb(main):014:0> z.conjugate
=> (1-2i)
irb(main):015:0> z.abs
=> 2.23606797749979
irb(main):016:0> (z - w).abs
=> 4.12310562561766
irb(main):017:0> require 'complex'
=> true
irb(main):018:0> Math::exp(z)
=> (-1.13120438375681+2.47172667200482i)
irb(main):019:0> Math::sin(z)
=> (3.16577851321617+1.95960104142161i)
irb(main):020:0> Math::cos(z)
=> (2.03272300701967-3.0518977991518i)
irb(main):021:0> Math::tan(z)
=> (0.0338128260798967+1.01479361614663i)
irb(main):022:0> Math::sinh(z)
=> (-0.489056259041294+1.40311925062204i)
irb(main):023:0> Math::cosh(z)
=> (-0.64214812471552+1.06860742138278i)
irb(main):024:0> Math::tanh(z)
=> (1.16673625724092-0.243458201185725i)
irb(main):025:0> Math::log(z)
=> (0.80471895621705+1.10714871779409i)
irb(main):026:0> z - z == 0
=> true
irb(main):027:0> z == Complex(1, 2)
=> true
irb(main):028:0> Math::sqrt(z)
=> (1.27201964951407+0.786151377757423i)
irb(main):029:0> Math::E
=> 2.71828182845905
irb(main):030:0> Math::PI
=> 3.14159265358979
irb(main):031:0> Math::E**z
=> (-1.13120438375681+2.47172667200482i)
irb(main):032:0> Math::exp(z)
=> (-1.13120438375681+2.47172667200482i)



* Lua 언어로 연습해본 복소수 계산
  (참고로 아래의 예제는 http://lua-users.org/wiki/ComplexNumbers 에 있는
             complex 패키지를 이용한 것이다. 이는 Lua 5.1.4 에서도 잘 동작한다. )
--  ------------ Testcode: --------------
-- Filename: TestComplex-01.lua
--           Using the complex class on http://lua-users.org/wiki/ComplexNumbers
--  Execute:  lua TestComplex-01.lua

local complex = require "complex"

function complex.sin( cx )
   local tmp = complex.new(0, 1) * cx;
   return (tmp:exp() - (-tmp):exp())/(complex {0, 2})
end

function complex.cos( cx )
   local tmp = complex.new(0, 1) * cx;
   return (tmp:exp() + (-tmp):exp())/2
end

function complex.tan( cx )
   local tmp = complex.new(0, 2) * cx;
   return (tmp:exp() - 1)/((tmp:exp() + 1)*(complex "i"))
end

function complex.sec( cx )
   local tmp = complex.new(0, 1) * cx;
   return 2/(tmp:exp() + (-tmp):exp())
end

function complex.csc( cx )
   local tmp = complex.new(0, 1) * cx;
   return (complex "2i")/(tmp:exp() - (-tmp):exp())
end

function complex.cot( cx )
   local tmp = complex.new(0, 2) * cx;
   return ((tmp:exp() + 1)*(complex "i"))/(tmp:exp() - 1)
end

function complex.sinh( cx )
   return (cx:exp() - (-cx):exp())/2
end

function complex.cosh( cx )
   return (cx:exp() + (-cx):exp())/2
end

function complex.tanh( cx )
   local tmp = 2 * cx;
   return (tmp:exp() - 1)/(tmp:exp() + 1)
end

function complex.sech( cx )
   return 2/(cx:exp() + (-cx):exp())
end

function complex.csch( cx )
   return 2/(cx:exp() - (-cx):exp())
end

function complex.coth( cx )
   local tmp = 2 * cx;
   return (tmp:exp() + 1)/(tmp:exp() - 1)
end

function complex.cbrt( cx )
   local r, phi = complex.polar( cx )
   local tmp = r^(1/3)
   local tmp2 = phi/3
   return complex.new(tmp*math.cos(tmp2), tmp*math.sin(tmp2))
end

function square( cx )
    return cx*cx
end

function cube( cx )
    return cx*cx*cx
end

-- instantiate of complex
z = complex { 1, 2 }
w = complex.new( 5, 4 )
print( "z = " .. z )
print( "w = " .. w )
print( "    complex.type( z ) = " .. complex.type( z ) )
print( "    complex.type( w ) = " .. complex.type( w ) )
print( "    z - w = " .. z - w )
print( "    z * w = " .. z * w )
print( "    z / w = " .. z / w )
print( "    z^2 = " .. z^2 )
print( "    z^3 = " .. z^3 )
print( "    z^0 = " .. z^0 )
print( "    1/z = " .. 1/z )
print( "    z^-1 = " .. z^-1 )
print( "    z^-2 = " .. z^-2 )
print( "    z^-3 = " .. z^-3 )
print( "    z:conjugate() = " .. z:conjugate() )
print( "    z:abs() = " .. z:abs() )
print( "    |z - w| = " .. (z - w):abs() )
print( "    z:exp() = " .. z:exp() )
print( "    z:ln() = " .. z:ln() )
print( "    complex.sin(z) = " .. complex.sin(z) )
print( "    complex.cos(z) = " .. complex.cos(z) )
print( "    complex.tan(z) = " .. complex.tan(z) )
print( "    complex.sec(z) = " .. complex.sec(z) )
print( "    complex.csc(z) = " .. complex.csc(z) )
print( "    complex.cot(z) = " .. complex.cot(z) )
print( "    complex.sinh(z) = " .. complex.sinh(z) )
print( "    complex.cosh(z) = " .. complex.cosh(z) )
print( "    complex.tanh(z) = " .. complex.tanh(z) )
print( "    complex.sech(z) = " .. complex.sech(z) )
print( "    complex.csch(z) = " .. complex.csch(z) )
print( "    complex.coth(z) = " .. complex.coth(z) )
print( "    z - z == 0 ? ", (z - z == (complex "0")) )
print( "    z == complex \"1+2i\" ? ", z == (complex "1+2i") )
print( "    complex.sqrt(z) = " .. complex.sqrt(z) )
print( "    complex.cbrt(z) = " .. complex.cbrt(z) )
print( "    square(z) = " .. square(z) )
print( "    cube(z) = " .. cube(z) )
print( "    complex.convpolar(1, math.pi/2) = " .. complex.convpolar(1, math.pi/2) )
print( "    complex.convpolar(1, math.pi) = " .. complex.convpolar(1, math.pi) )
print( "    complex.convpolar(1, math.pi/2):round(15) = " .. complex.convpolar(1, math.pi/2):round(15) )
print( "    complex.convpolardeg(1, math.pi):round(15) = " .. complex.convpolar(1, math.pi):round(15) )
print( "    complex.convpolardeg(1, 90) = " .. complex.convpolardeg(1, 90) )
print( "    complex.convpolardeg(1, 180) = " .. complex.convpolardeg(1, 180) )
print( "    complex.convpolardeg(1, 90):round(15) = " .. complex.convpolardeg(1, 90):round(15) )
print( "    complex.convpolardeg(1, 180):round(15) = " .. complex.convpolardeg(1, 180):round(15) )
print( "    complex.tostring( z, \"%.2f\" ) = " .. complex.tostring( z, "%.2f" ) )
local r, phi = complex.polar( {0,3} )
print( "    complex.polar( {3,0} ) = " .. r .. ",  " .. phi )
r, phi = complex.polar( z )
print( "    complex.polar( z ) = " .. r .. ",  " .. phi )
r, phi = complex.polardeg( z )
print( "    complex.polardeg( z ) = " .. r .. ",  " .. phi )
local re, im = complex.get( z )
print( "    complex.get( z ) = " .. re .. ",  " .. im .. "        // the real and imaginary parts of z" )
re, im = complex.get( w )
print( "    complex.get( w ) = " .. re .. ",  " .. im .. "        // the real and imaginary parts of z" )
print( "    complex.mulconjugate( z ) = " .. complex.mulconjugate( z ) .. "   // the square of abs(z)" )
print( "    complex.mulconjugate( w ) = " .. complex.mulconjugate( w ) .. "  // the square of abs(w)" )
--[[
Execution Result:
z = 1+2i
w = 5+4i
    complex.type( z ) = complex
    complex.type( w ) = complex
    z - w = -4-2i
    z * w = -3+14i
    z / w = 0.31707317073171+0.14634146341463i
    z^2 = -3+4i
    z^3 = -11-2i
    z^0 = 1+2i
    1/z = 0.2-0.4i
    z^-1 = 0.2-0.4i
    z^-2 = -0.12-0.16i
    z^-3 = -0.088+0.016i
    z:conjugate() = 1-2i
    z:abs() = 2.2360679774998
    |z - w| = 4.4721359549996
    z:exp() = -1.1312043837568+2.4717266720048i
    z:ln() = 0.80471895621705+1.1071487177941i
    complex.sin(z) = 3.1657785132162+1.9596010414216i
    complex.cos(z) = 2.0327230070197-3.0518977991518i
    complex.tan(z) = 0.033812826079897+1.0147936161466i
    complex.sec(z) = 0.15117629826558+0.22697367539372i
    complex.csc(z) = 0.22837506559969-0.14136302161241i
    complex.cot(z) = 0.032797755533753-0.98432922645819i
    complex.sinh(z) = -0.48905625904129+1.403119250622i
    complex.cosh(z) = -0.64214812471552+1.0686074213828i
    complex.tanh(z) = 1.1667362572409-0.24345820118573i
    complex.sech(z) = -0.41314934426694-0.68752743865548i
    complex.csch(z) = -0.22150093085051-0.6354937992539i
    complex.coth(z) = 0.82132979749385+0.17138361290918i
    z - z == 0 ?        true
    z == complex "1+2i" ?       true
    complex.sqrt(z) = 1.2720196495141+0.78615137775742i
    complex.cbrt(z) = 1.2196165079718+0.47171126778939i
    square(z) = -3+4i
    cube(z) = -11-2i
    complex.convpolar(1, math.pi/2) = 6.1232339957368e-017+i
    complex.convpolar(1, math.pi) = -1+1.2246467991474e-016i
    complex.convpolar(1, math.pi/2):round(15) = i
    complex.convpolardeg(1, math.pi):round(15) = -1
    complex.convpolardeg(1, 90) = 6.1232339957368e-017+i
    complex.convpolardeg(1, 180) = -1+1.2246467991474e-016i
    complex.convpolardeg(1, 90):round(15) = i
    complex.convpolardeg(1, 180):round(15) = -1
    complex.tostring( z, "%.2f" ) = 1.00+2.00i
    complex.polar( {3,0} ) = 3,  1.5707963267949
    complex.polar( z ) = 2.2360679774998,  1.1071487177941
    complex.polardeg( z ) = 2.2360679774998,  63.434948822922
    complex.get( z ) = 1,  2       // the real and imaginary parts of z
    complex.get( w ) = 5,  4       // the real and imaginary parts of z
    complex.mulconjugate( z ) = 5  // the square of abs(z)
    complex.mulconjugate( w ) = 41  // the square of abs(w)
--]]


Posted by Scripter
,