컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Friday, February 16, 2007

Java/자바] 복소수 사칙연산 클래스; 덧셈, 뺄셈, 곱셈, 나눗셈; Complex Number Class


자바는 복소수를 지원하지 않기에, 복소수 클래스를 만들어 주어야 합니다. 파이썬은 물론이고, 펄 같은 언어도 다 복소수 계산을 지원하는데, 왜 자바에서는 지원이 안되는지 모르겠군요. (참고: ▶▶ Perl/펄] 복소수 사칙연산; 덧셈, 뺄셈, 곱셈, 나눗셈; Complex Number)

복소수 더하기, 빼기, 곱하기, 나누기 예제


소스 파일명: Example.java
public class Example {
  public static void main(String[] args) {

    Complex a = new Complex(8, 3); // 복소수 8+3i
    Complex b = new Complex(5, 2); // 복소수 5+2i


    // 복소수의 덧셈
    System.out.println(Complex.add(a, b));
    // 출력 결과: 13.0+5.0i


    // 복소수의 뺄셈
    System.out.println(Complex.subtract(a, b));
    // 출력 결과: 3.0+1.0i


    // 복소수의 곱셈
    System.out.println(Complex.multiply(a, b));
    // 출력 결과: 34.0+31.0i


    // 복소수의 나눗셈
    System.out.println(Complex.divide(a, b));
    // 출력 결과: 1.5862068965517242-0.034482758620689655i




    // 복소수 a의 실수부 구하기
    System.out.println(a.getReal());
    // 출력 결과: 8.0


    // 복소수 a의 허수부 구하기
    System.out.println(a.getImaginary());
    // 출력 결과: 3.0


    // 복소수 a의 절대값/크기값(magnitude) 구하기
    System.out.println(a.magnitude());
    // 출력 결과: 8.54400374531753
    // 8+3i의 경우, 8^2 + 3^2 의 제곱근입니다.


  }
}


다음의 클래스 파일이 있어야 합니다. 그렇지 않으면 Example.java:4: cannot find symbol ... symbol : class Complex 라는 에러가 납니다.

복소수 계산 클래스 파일


클래스 파일명: Complex.java
"Java Cookbook, 2nd Edition (O'Reilly 2004)" 이라는 책의 "Recipe 5.18 Using Complex Numbers" 라는 장에 나와 있는 클래스입니다.
/** A class to represent Complex Numbers. A Complex object is
 * immutable once created; the add, subtract and multiply routines
 * return newly created Complex objects containing the results.
 *
 */

public class Complex {

  /** The real part */
  private double r;


  /** The imaginary part */
  private double i;


  /** Construct a Complex */
  Complex(double rr, double ii) {
    r = rr;
    i = ii;
  }


  /** Display the current Complex as a String, for use in
   * println( ) and elsewhere.
   */
  public String toString() {
    StringBuffer sb = new StringBuffer().append(r);

    if (i > 0)
      sb.append('+');    // else append(i) appends - sign

    return sb.append(i).append('i').toString();
  }


  /** Return just the Real part */
  public double getReal() {
    return r;
  }


  /** Return just the Imaginary part */
  public double getImaginary() {
    return i;
  }


  /** Return the magnitude of a complex number */
  public double magnitude() {
    return Math.sqrt(r*r + i*i);
  }


  /** Add another Complex to this one */
  public Complex add(Complex other) {
    return add(this, other);
  }


  /** Add two Complexes */
  public static Complex add(Complex c1, Complex c2) {
    return new Complex(c1.r+c2.r, c1.i+c2.i);
  }


  /** Subtract another Complex from this one */
  public Complex subtract(Complex other) {
    return subtract(this, other);
  }


  /** Subtract two Complexes */
  public static Complex subtract(Complex c1, Complex c2) {
    return new Complex(c1.r-c2.r, c1.i-c2.i);
  }


  /** Multiply this Complex times another one */
  public Complex multiply(Complex other) {
    return multiply(this, other);
  }


  /** Multiply two Complexes */
  public static Complex multiply(Complex c1, Complex c2) {
    return new Complex(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r);
  }


  /** Divide c1 by  c2.
    * @author Gisbert Selke.
   */
  public static Complex divide(Complex c1, Complex c2) {
    return new Complex(
    (c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i),
    (c1.i*c2.r-c1.r*c2.i)/(c2.r*c2.r+c2.i*c2.i));
  }


}


위의, 복소수 취급 클래스인 Complex.java 파일을 같은 디렉토리에 넣어주고, Example.java 라는 첫번째 파일을 컴파일해야 합니다.


컴파일 및 실행 결과 화면:
D:\Z>javac Example.java && java Example
13.0+5.0i
3.0+1.0i
34.0+31.0i
1.5862068965517242-0.034482758620689655i
8.0
3.0
8.54400374531753

D:\Z>



복소수의 사칙연산 공식: ▶▶ Python/파이썬] 복소수 사칙연산; 덧셈, 뺄셈, 곱셈, 나눗셈; Complex Number Formula
(파이썬에서는 i 를 j 로 표현합니다. 그 외에는 같습니다.)

실수의 사칙연산 기호: ▶▶ 엑셀] 곱하기/나누기(곱셈/나눗셈) 방법, 윈도우 계산기와 엑셀(Excel)에서 사칙연산, Multiply / Divide




0 Comments:

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top