Class | Complex |
In: |
lib/complex.rb
lib/mathn.rb |
Parent: | Numeric |
The complex number class. See complex.rb for an overview.
I | = | Complex(0,1) | I is the imaginary number. It exists at point (0,1) on the complex plane. | |
Unify | = | true |
image | -> | imag |
image | [R] | The imaginary part of a complex number. |
real | [R] | The real part of a complex number. |
# File lib/complex.rb, line 128 128: def initialize(a, b) 129: raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric 130: raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex 131: raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric 132: raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex 133: @real = a 134: @image = b 135: end
Remainder after division by a real or complex number.
# File lib/complex.rb, line 251 251: def % (other) 252: if other.kind_of?(Complex) 253: Complex(@real % other.real, @image % other.image) 254: elsif Complex.generic?(other) 255: Complex(@real % other, @image % other) 256: else 257: x , y = other.coerce(self) 258: x % y 259: end 260: end
Multiplication with real or complex number.
# File lib/complex.rb, line 172 172: def * (other) 173: if other.kind_of?(Complex) 174: re = @real*other.real - @image*other.image 175: im = @real*other.image + @image*other.real 176: Complex(re, im) 177: elsif Complex.generic?(other) 178: Complex(@real * other, @image * other) 179: else 180: x , y = other.coerce(self) 181: x * y 182: end 183: end
Raise this complex number to the given (real or complex) power.
# File lib/complex.rb, line 206 206: def ** (other) 207: if other == 0 208: return Complex(1) 209: end 210: if other.kind_of?(Complex) 211: r, theta = polar 212: ore = other.real 213: oim = other.image 214: nr = Math.exp!(ore*Math.log!(r) - oim * theta) 215: ntheta = theta*ore + oim*Math.log!(r) 216: Complex.polar(nr, ntheta) 217: elsif other.kind_of?(Integer) 218: if other > 0 219: x = self 220: z = x 221: n = other - 1 222: while n != 0 223: while (div, mod = n.divmod(2) 224: mod == 0) 225: x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image) 226: n = div 227: end 228: z *= x 229: n -= 1 230: end 231: z 232: else 233: if defined? Rational 234: (Rational(1) / self) ** -other 235: else 236: self ** Float(other) 237: end 238: end 239: elsif Complex.generic?(other) 240: r, theta = polar 241: Complex.polar(r**other, theta*other) 242: else 243: x, y = other.coerce(self) 244: x**y 245: end 246: end
Addition with real or complex number.
# File lib/complex.rb, line 140 140: def + (other) 141: if other.kind_of?(Complex) 142: re = @real + other.real 143: im = @image + other.image 144: Complex(re, im) 145: elsif Complex.generic?(other) 146: Complex(@real + other, @image) 147: else 148: x , y = other.coerce(self) 149: x + y 150: end 151: end
Subtraction with real or complex number.
# File lib/complex.rb, line 156 156: def - (other) 157: if other.kind_of?(Complex) 158: re = @real - other.real 159: im = @image - other.image 160: Complex(re, im) 161: elsif Complex.generic?(other) 162: Complex(@real - other, @image) 163: else 164: x , y = other.coerce(self) 165: x - y 166: end 167: end
Division by real or complex number.
# File lib/complex.rb, line 188 188: def / (other) 189: if other.kind_of?(Complex) 190: self*other.conjugate/other.abs2 191: elsif Complex.generic?(other) 192: Complex(@real/other, @image/other) 193: else 194: x, y = other.coerce(self) 195: x/y 196: end 197: end
Compares the absolute values of the two numbers.
# File lib/complex.rb, line 318 318: def <=> (other) 319: self.abs <=> other.abs 320: end
Test for numerical equality (a == a + 0i).
# File lib/complex.rb, line 325 325: def == (other) 326: if other.kind_of?(Complex) 327: @real == other.real and @image == other.image 328: elsif Complex.generic?(other) 329: @real == other and @image == 0 330: else 331: other == self 332: end 333: end
Absolute value (aka modulus): distance from the zero point on the complex plane.
# File lib/complex.rb, line 281 281: def abs 282: Math.hypot(@real, @image) 283: end
Square of the absolute value.
# File lib/complex.rb, line 288 288: def abs2 289: @real*@real + @image*@image 290: end
FIXME
# File lib/complex.rb, line 349 349: def denominator 350: @real.denominator.lcm(@image.denominator) 351: end
FIXME
# File lib/complex.rb, line 356 356: def numerator 357: cd = denominator 358: Complex(@real.numerator*(cd/@real.denominator), 359: @image.numerator*(cd/@image.denominator)) 360: end
Returns the absolute value and the argument.
# File lib/complex.rb, line 303 303: def polar 304: return abs, arg 305: end
# File lib/complex.rb, line 199 199: def quo(other) 200: Complex(@real.quo(1), @image.quo(1)) / other 201: end
Standard string representation of the complex number.
# File lib/complex.rb, line 365 365: def to_s 366: if @real != 0 367: if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1 368: if @image >= 0 369: @real.to_s+"+("+@image.to_s+")i" 370: else 371: @real.to_s+"-("+(-@image).to_s+")i" 372: end 373: else 374: if @image >= 0 375: @real.to_s+"+"+@image.to_s+"i" 376: else 377: @real.to_s+"-"+(-@image).to_s+"i" 378: end 379: end 380: else 381: if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1 382: "("+@image.to_s+")i" 383: else 384: @image.to_s+"i" 385: end 386: end 387: end