본문 바로가기
programming language/JAVA

자바에서 복소수(Complex Number) 사용하기 commons.apache.math

by __observer__ 2014. 3. 19.
반응형

자바에는 정말 많은 클래스들이 존재하고~ 정말 헤아릴 수 없을 정도로 많더군요.

 

자바 코딩하는 분들이 많이 사용하는 Eclipse 역시도 너무 좋아서 Ctrl + Space 를 누르면~ 긴 자바 구문들도 외울 필요가 없어서 좋더군요.

 

자바에서 수학과 관련한 작업을 할 때 원주율, 지수, sin, cos등과 같은 기본적인 수학 함수들은 Math 클래스를 사용하곤 하는데~~ 복소수를 사용하려고 찾아보니 복소수 관련한 클래스는 제공하지 않더군요.

 

그래서 찾아보니 아래 주소의 Apache Commons Math 라이브러리에서 수학과 관련한 훨씬 다양한 기능들을 제공해 주더군요.

 

http://commons.apache.org/proper/commons-math/

 

일단 아래 주소에서 아래 그림과 같이 binaries 를 다운로드 받습니다.

http://commons.apache.org/proper/commons-math/download_math.cgi

 

현재 최신 버전은 3.2 버전이더군요. 저는 현재 윈도우 운영체제라 zip 파일을 다운로드 받았습니다.

 

다운도드 받은후에 commons-math3-3.2-bin.zip 파일의 압축을 풀어줍니다.

 

저는 자바 프로젝트에서 lib 폴더를 하나 만들고 commons-math3-3.2.jar 파일을 넣어놓고 사용합니다.

 

Eclipse에서 아래 그림과 같이 Add JARs를 눌러서 commons-math3-3.2.jar 파일을 추가 합니다.

 

이제 자바에서 복소수를 맘껏 사용해 보세요~

 

저는 다음과 같이 복소수를 사용해 봤습니다.

 

package test;

 

import org.apache.commons.math3.complex.Complex;

 

public class ComplexTest {

 

    public static void main(String[] args) {

        Complex c1 = new Complex(4, 3);

        Complex c2 = new Complex(5, 2);

        Complex c3 = null;

 

        c3 = c1.multiply(c2); // 곱하기

        double realV = c1.getReal(); // Real part

        double ImagV = c1.getImaginary(); // Imaginary part

 

        System.out.println("복소수 곱하기 : " + c3);

        System.out.println("Real Part :" + realV);

        System.out.println("Image Part: " + ImagV);

    }

}

 

이렇게 결과가 나오더군요~

 

복소수 곱하기 : (14.0, 23.0)

Real Part : 4.0

Image Part: 3.0

 

Apache Commons Math 라이브러리에는 복소수 뿐만 아니라 아래 내용과 같이 다양한 수학 라이브러리를 제공해 줍니다. FFT, DCT, DST, Fast Hadamard Transform 등과 같은 transform 뿐만 아니라 Machine Learning 까지도 제공하더군요.

 

http://commons.apache.org/proper/commons-math/userguide/overview.html

 

org.apache.commons.math3.stat - statistics, statistical tests

org.apache.commons.math3.analysis - rootfinding, integration, interpolation, polynomials

org.apache.commons.math3.random - random numbers, strings and data generation

org.apache.commons.math3.special - special functions (Gamma, Beta)

org.apache.commons.math3.linear - matrices, solving linear systems

org.apache.commons.math3.util - common math/stat functions extending java.lang.Math

org.apache.commons.math3.complex - complex numbers

org.apache.commons.math3.distribution - probability distributions

org.apache.commons.math3.fraction - rational numbers

org.apache.commons.math3.transform - transform methods (Fast Fourier)

org.apache.commons.math3.geometry - geometry (Euclidean spaces and Binary Space Partitioning)

org.apache.commons.math3.optim - function maximization or minimization

org.apache.commons.math3.ode - Ordinary Differential Equations integration

org.apache.commons.math3.genetics - Genetic Algorithms

org.apache.commons.math3.fitting - Curve Fitting

org.apache.commons.math3.ml - Machine Learning

 

간단하게 Fast Fourier Transform 을 한번 해 봤습니다. FFT 는 당연히 2의 자승으로 넣어주셔야 합니다.

 

package test;

 

import org.apache.commons.math3.complex.Complex;

import org.apache.commons.math3.transform.DftNormalization;

import org.apache.commons.math3.transform.FastFourierTransformer;

import org.apache.commons.math3.transform.TransformType;

 

public class ComplexTest {

 

    public static void main(String[] args) {

 

        FastFourierTransformer v = new FastFourierTransformer(

                DftNormalization.STANDARD); // FFT

 

        Complex[] vv = new Complex[16]; // 16 size FFT

        for (int j = 0; j < vv.length; j++) {

            vv[j] = new Complex(3, 0);

        }

        vv = v.transform(vv, TransformType.FORWARD); //

        for (Complex c : vv) {

            System.out.println(c);

        }

    }

}

 

FastFourierTransformer 의 옵션인 DftNormalization.STANDARD 는 FFT 의 스케일을 정규화 하지 않겠다는 것이고~ DftNormalization.UNITARY 는 정규화 하겠다는 뜻입니다.

 

Transform() 의 TransformType.FORWARD 은 FFT 를 TransformType.INVERSE 는 Inverse FFT 를 의미 합니다.

 

다음과 같이 정상적인 결과가 나오더군요.

 

(48.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

(0.0, 0.0)

 

이번에는 그냥 간단하게만 사용해 봤는데~ 시간을 가지고 깊이 있게 공부해 보고 싶더군요.


반응형

'programming language > JAVA' 카테고리의 다른 글

자바 Maven 간단 사용기  (0) 2014.04.06
자바 공부 시작하기 좋은 사이트들  (0) 2014.03.25

댓글