Complex 클래스
Complex 클래스
Complex 객체는 두 값의 쌍을 담아요. 객체를 만들 때 직교 좌표(rectangular coordinates)나 극 좌표(polar coordinates) 중 하나로 주어져요.
직교 좌표 (Rectangular Coordinates)
복소수의 직교 좌표는 실수부(real part)와 허수부(imaginary part)라고 불러요. Complex number 정의를 참고하세요.
직교 좌표로 Complex 객체를 만들 수 있는 방법은 다음과 같아요.
- 복소수 리터럴.
Complex.rect메서드.Kernel#Complex메서드(숫자 인자 또는 특정 문자열 인자).String#to_c메서드(특정 문자열).
저장된 각 부분은 Complex, Float, Integer, Rational 클래스 중 하나의 인스턴스일 수 있어요. 이들을 가져오는 방법은.
- 각각,
Complex#real과Complex#imaginary메서드. - 함께,
Complex#rect메서드.
대응하는 (계산된) 극 좌표 값은.
- 각각,
Complex#abs와Complex#arg메서드. - 함께,
Complex#polar메서드.
극 좌표 (Polar Coordinates)
복소수의 극 좌표는 절댓값(absolute part)과 편각(argument part)이라고 불러요. Complex polar plane을 참고하세요.
이 클래스에서 편각은 도(degree)가 아니라 라디안(radian)으로 표현돼요.
극 좌표로 Complex 객체를 만들 수 있는 방법은 다음과 같아요.
Complex.polar메서드.Kernel#Complex메서드(특정 문자열 인자).String#to_c메서드(특정 문자열).
저장된 각 부분은 Complex, Float, Integer, Rational 클래스 중 하나의 인스턴스일 수 있어요. 이들을 가져오는 방법은.
- 각각,
Complex#abs와Complex#arg메서드. - 함께,
Complex#polar메서드.
대응하는 (계산된) 직교 좌표 값은.
- 각각,
Complex#real과Complex#imag메서드. - 함께,
Complex#rect메서드.
출처: Ruby 3.3 API
본문
상수
I—Complex(0, 1)과 동등해요:Complex::I # => (0+1i)
클래스 메서드
json_create(object)—as_json참고. JSON으로 직렬화된 복소수 해시를 받아Complex객체로 되돌려줘요.polar(abs, arg = 0) → complex— 인자들로 새Complex객체를 만들어요. 각 인자는Numeric또는 그 서브클래스(Complex,Float,Integer,Rational)의 인스턴스여야 해요.arg는 라디안으로 주어져요.
Complex.polar(3) # => (3+0i)
Complex.polar(3, 2.0) # => (-1.2484405096414273+2.727892280477045i)
Complex.polar(-3, -2.0) # => (1.2484405096414273+2.727892280477045i)
rect(real, imag = 0) → complex— 인자들로 새Complex객체를 만들어요.Complex.rectangular는Complex.rect의 별칭이에요.
Complex.rect(3) # => (3+0i)
Complex.rect(3, Math::PI) # => (3+3.141592653589793i)
Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
인스턴스 메서드
complex * numeric → new_complex—self와numeric의 곱을 돌려줘요.
Complex(2, 3) * Complex(2, 3) # => (-5+12i)
Complex(900) * Complex(1) # => (900+0i)
Complex(-2, 9) * Complex(-9, 2) # => (0-85i)
Complex(9, 8) * 4 # => (36+32i)
Complex(20, 9) * 9.8 # => (196.0+88.2i)
complex ** numeric → new_complex—self를numeric거듭제곱한 결과를 돌려줘요.
Complex('i') ** 2 # => (-1+0i)
Complex(-8) ** Rational(1, 3) # => (1.0000000000000002+1.7320508075688772i)
complex + numeric → new_complex—self와numeric의 합을 돌려줘요.
Complex(2, 3) + Complex(2, 3) # => (4+6i)
Complex(900) + Complex(1) # => (901+0i)
Complex(-2, 9) + Complex(-9, 2) # => (-11+11i)
Complex(9, 8) + 4 # => (13+8i)
Complex(20, 9) + 9.8 # => (29.8+9i)
complex - numeric → new_complex—self와numeric의 차를 돌려줘요.
Complex(2, 3) - Complex(2, 3) # => (0+0i)
Complex(900) - Complex(1) # => (899+0i)
Complex(-2, 9) - Complex(-9, 2) # => (7+7i)
Complex(9, 8) - 4 # => (5+8i)
Complex(20, 9) - 9.8 # => (10.2+9i)
-complex → new_complex—self의 부정, 즉 각 부분의 부정을 돌려줘요.
-Complex(1, 2) # => (-1-2i)
-Complex(-1, -2) # => (1+2i)
complex / numeric → new_complex—self와numeric의 몫을 돌려줘요.
Complex(2, 3) / Complex(2, 3) # => ((1/1)+(0/1)*i)
Complex(900) / Complex(1) # => ((900/1)+(0/1)*i)
Complex(-2, 9) / Complex(-9, 2) # => ((36/85)-(77/85)*i)
Complex(9, 8) / 4 # => ((9/4)+(2/1)*i)
Complex(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
complex <=> object → -1, 0, 1, or nil— 다음 두 조건이 모두 참이면self.real <=> object.real을 돌려줘요:self.imag == 0, 그리고object.imag == 0. (object가 숫자지만 complex가 아니면 항상 참이에요.) 그 외에는nil을 돌려줘요.
Complex(2) <=> 3 # => -1
Complex(2) <=> 2 # => 0
Complex(2) <=> 1 # => 1
Complex(2, 1) <=> 1 # => nil # self.imag not zero.
Complex(1) <=> Complex(1, 1) # => nil # object.imag not zero.
Complex(1) <=> 'Foo' # => nil # object.imag not defined.
complex == object → true or false—self.real == object.real이고self.imag == object.imag이면 true를 돌려줘요.
Complex(2, 3) == Complex(2.0, 3.0) # => true
abs → float(magnitude의 별칭) —self의 절댓값(크기)을 돌려줘요. 직교 좌표로 만든 값이면 계산된 값이라 부정확할 수 있어요.
Complex.polar(-1, 0).abs # => 1.0
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # 2의 제곱근
abs2 → float—self의 절댓값(크기)의 제곱을 돌려줘요.
Complex.polar(2, 2).abs2 # => 4.0
Complex.rectangular(1.0/3, 1.0/3).abs2 # => 0.2222222222222222
arg → float(angle,phase의 별칭) —self의 편각(각도)을 라디안으로 돌려줘요.
Complex.polar(3, Math::PI/2).arg # => 1.5707963267948966
as_json(*)—Complex#as_json과Complex.json_create메서드로Complex객체를 직렬화/역직렬화할 수 있어요.Complex#as_json은self를 나타내는 2-요소 해시를 돌려줘요.
require 'json/add/complex'
x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
Complex.json_create(x) # => (2+0i)
Complex.json_create(y) # => (2.0+4i)
conj → complex(conjugate의 별칭) —self의 켤레복소수를 돌려줘요:Complex.rect(1, 2).conj # => (1-2i)denominator → integer—self의 분모, 즉self.real.denominator와self.imag.denominator의 최소공배수를 돌려줘요:Complex.rect(Rational(1, 2), Rational(2, 3)).denominator # => 6. 비유리수 숫자의n.denominator는 1이에요. 관련:Complex#numerator.fdiv(numeric) → new_complex—Complex(self.real/numeric, self.imag/numeric)을 돌려줘요:Complex(11, 22).fdiv(3) # => (3.6666666666666665+7.333333333333333i)finite? → true or false—self.real.finite?와self.imag.finite?가 모두 true이면 true를, 그 외에는 false를 돌려줘요:Complex(1, 1).finite? # => true,Complex(Float::INFINITY, 0).finite? # => falsehash → integer—self의 정수 해시 값을 돌려줘요. 같은 값으로 만든 두Complex객체는 같은 해시 값을 가져요:Complex(1, 2).hash == Complex(1, 2).hash # => trueimag → numeric(imaginary의 별칭) —self의 허수 값을 돌려줘요.
Complex(7).imaginary #=> 0
Complex(9, -4).imaginary #=> -4
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476
infinite? → 1 or nil—self.real.infinite?나self.imag.infinite?중 하나라도 true이면 1을, 그 외에는 nil을 돌려줘요:Complex(Float::INFINITY, 0).infinite? # => 1,Complex(1, 1).infinite? # => nilinspect → string—self의 문자열 표현을 돌려줘요.
Complex(2).inspect # => "(2+0i)"
Complex('-8/6').inspect # => "((-4/3)+0i)"
Complex('1/2i').inspect # => "(0+(1/2)*i)"
Complex(0, Float::INFINITY).inspect # => "(0+Infinity*i)"
Complex(Float::NAN, Float::NAN).inspect # => "(NaN+NaN*i)"
numerator → new_complex—self의 두 부분을 공통 분모(최소공배수)로 맞춘 후 각 분자로 만든Complex객체를 돌려줘요.
c = Complex(Rational(2, 3), Rational(3, 4)) # => ((2/3)+(3/4)*i)
c.numerator # => (8+9i)
polar → array— 배열[self.abs, self.arg]를 돌려줘요:Complex.polar(1, 2).polar # => [1.0, 2.0],Complex.rect(1, 1).polar # => [1.4142135623730951, 0.7853981633974483]rationalize(epsilon = nil) → rational—self.real과 정확히 또는 대략적으로 같은 값의Rational객체를 돌려줘요.self.imag가 정확히 0이 아니면RangeError가 발생해요.
Complex(1, 0).rationalize # => (1/1)
Complex(3.14159, 0).rationalize # => (314159/100000)
Complex(3.14159, 0).rationalize(0.1) # => (16/5)
Complex(3.14159, 0).rationalize(0.00001) # => (355/113)
real → numeric—self의 실수 값을 돌려줘요:Complex(7).real #=> 7,Complex(9, -4).real #=> 9real? → false—Numeric#real?와의 호환을 위해 false를 돌려줘요.rect → array(rectangular의 별칭) —Complex.rect참고.to_c → self—self를 돌려줘요.to_d → bigdecimal,to_d(precision) → bigdecimal— 값을BigDecimal로 돌려줘요.precision매개변수는 유리수 복소수에 필요해요.
require 'bigdecimal'
require 'bigdecimal/util'
Complex(0.1234567, 0).to_d(4) # => 0.1235e0
Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
to_f → float— 가능하면self.real의 값을Float로 돌려줘요.self.imag가 정확히 0이 아니면RangeError를 발생시켜요.
Complex(1, 0).to_f # => 1.0
Complex(1, Rational(0, 1)).to_f # => 1.0
to_i → integer— 가능하면self.real의 값을Integer로 돌려줘요.self.imag가 정확히 0이 아니면RangeError를 발생시켜요.
Complex(1, 0).to_i # => 1
Complex(1, Rational(0, 1)).to_i # => 1
to_json(*args)—self를 나타내는JSON문자열을 돌려줘요.
require 'json/add/complex'
puts Complex(2).to_json
puts Complex(2.0, 4).to_json
# {"json_class":"Complex","r":2,"i":0}
# {"json_class":"Complex","r":2.0,"i":4}
to_r → rational— 가능하면self.real의 값을Rational로 돌려줘요.self.imag가 정확히 0이 아니면RangeError를 발생시켜요. 관련:Complex#rationalize.
Complex(1, 0).to_r # => (1/1)
Complex(1, Rational(0, 1)).to_r # => (1/1)
to_s → string—self의 문자열 표현을 돌려줘요.
Complex(2).to_s # => "2+0i"
Complex('-8/6').to_s # => "-4/3+0i"
Complex('1/2i').to_s # => "0+1/2i"
Complex(0, Float::INFINITY).to_s # => "0+Infinity*i"
Complex(Float::NAN, Float::NAN).to_s # => "NaN+NaN*i"