Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: complex.rb
#
[0] Fix | Delete
# complex.rb -
[1] Fix | Delete
# $Release Version: 0.5 $
[2] Fix | Delete
# $Revision: 1.3 $
[3] Fix | Delete
# $Date: 1998/07/08 10:05:28 $
[4] Fix | Delete
# by Keiju ISHITSUKA(SHL Japan Inc.)
[5] Fix | Delete
#
[6] Fix | Delete
# ----
[7] Fix | Delete
#
[8] Fix | Delete
# complex.rb implements the Complex class for complex numbers. Additionally,
[9] Fix | Delete
# some methods in other Numeric classes are redefined or added to allow greater
[10] Fix | Delete
# interoperability with Complex numbers.
[11] Fix | Delete
#
[12] Fix | Delete
# Complex numbers can be created in the following manner:
[13] Fix | Delete
# - <tt>Complex(a, b)</tt>
[14] Fix | Delete
# - <tt>Complex.polar(radius, theta)</tt>
[15] Fix | Delete
#
[16] Fix | Delete
# Additionally, note the following:
[17] Fix | Delete
# - <tt>Complex::I</tt> (the mathematical constant <i>i</i>)
[18] Fix | Delete
# - <tt>Numeric#im</tt> (e.g. <tt>5.im -> 0+5i</tt>)
[19] Fix | Delete
#
[20] Fix | Delete
# The following +Math+ module methods are redefined to handle Complex arguments.
[21] Fix | Delete
# They will work as normal with non-Complex arguments.
[22] Fix | Delete
# sqrt exp cos sin tan log log10
[23] Fix | Delete
# cosh sinh tanh acos asin atan atan2 acosh asinh atanh
[24] Fix | Delete
#
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
#
[28] Fix | Delete
# Numeric is a built-in class on which Fixnum, Bignum, etc., are based. Here
[29] Fix | Delete
# some methods are added so that all number types can be treated to some extent
[30] Fix | Delete
# as Complex numbers.
[31] Fix | Delete
#
[32] Fix | Delete
class Numeric
[33] Fix | Delete
#
[34] Fix | Delete
# Returns a Complex number <tt>(0,<i>self</i>)</tt>.
[35] Fix | Delete
#
[36] Fix | Delete
def im
[37] Fix | Delete
Complex(0, self)
[38] Fix | Delete
end
[39] Fix | Delete
[40] Fix | Delete
#
[41] Fix | Delete
# The real part of a complex number, i.e. <i>self</i>.
[42] Fix | Delete
#
[43] Fix | Delete
def real
[44] Fix | Delete
self
[45] Fix | Delete
end
[46] Fix | Delete
[47] Fix | Delete
#
[48] Fix | Delete
# The imaginary part of a complex number, i.e. 0.
[49] Fix | Delete
#
[50] Fix | Delete
def image
[51] Fix | Delete
0
[52] Fix | Delete
end
[53] Fix | Delete
alias imag image
[54] Fix | Delete
[55] Fix | Delete
#
[56] Fix | Delete
# See Complex#arg.
[57] Fix | Delete
#
[58] Fix | Delete
def arg
[59] Fix | Delete
Math.atan2!(0, self)
[60] Fix | Delete
end
[61] Fix | Delete
alias angle arg
[62] Fix | Delete
[63] Fix | Delete
#
[64] Fix | Delete
# See Complex#polar.
[65] Fix | Delete
#
[66] Fix | Delete
def polar
[67] Fix | Delete
return abs, arg
[68] Fix | Delete
end
[69] Fix | Delete
[70] Fix | Delete
#
[71] Fix | Delete
# See Complex#conjugate (short answer: returns <i>self</i>).
[72] Fix | Delete
#
[73] Fix | Delete
def conjugate
[74] Fix | Delete
self
[75] Fix | Delete
end
[76] Fix | Delete
alias conj conjugate
[77] Fix | Delete
end
[78] Fix | Delete
[79] Fix | Delete
[80] Fix | Delete
#
[81] Fix | Delete
# Creates a Complex number. +a+ and +b+ should be Numeric. The result will be
[82] Fix | Delete
# <tt>a+bi</tt>.
[83] Fix | Delete
#
[84] Fix | Delete
def Complex(a, b = 0)
[85] Fix | Delete
if b == 0 and (a.kind_of?(Complex) or defined? Complex::Unify)
[86] Fix | Delete
a
[87] Fix | Delete
else
[88] Fix | Delete
Complex.new( a.real-b.imag, a.imag+b.real )
[89] Fix | Delete
end
[90] Fix | Delete
end
[91] Fix | Delete
[92] Fix | Delete
#
[93] Fix | Delete
# The complex number class. See complex.rb for an overview.
[94] Fix | Delete
#
[95] Fix | Delete
class Complex < Numeric
[96] Fix | Delete
@RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
[97] Fix | Delete
[98] Fix | Delete
undef step
[99] Fix | Delete
undef div, divmod
[100] Fix | Delete
undef floor, truncate, ceil, round
[101] Fix | Delete
[102] Fix | Delete
def Complex.generic?(other) # :nodoc:
[103] Fix | Delete
other.kind_of?(Integer) or
[104] Fix | Delete
other.kind_of?(Float) or
[105] Fix | Delete
(defined?(Rational) and other.kind_of?(Rational))
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
#
[109] Fix | Delete
# Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle).
[110] Fix | Delete
#
[111] Fix | Delete
def Complex.polar(r, theta)
[112] Fix | Delete
Complex(r*Math.cos(theta), r*Math.sin(theta))
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
#
[116] Fix | Delete
# Creates a +Complex+ number <tt>a</tt>+<tt>b</tt><i>i</i>.
[117] Fix | Delete
#
[118] Fix | Delete
def Complex.new!(a, b=0)
[119] Fix | Delete
new(a,b)
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
def initialize(a, b)
[123] Fix | Delete
raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
[124] Fix | Delete
raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
[125] Fix | Delete
raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
[126] Fix | Delete
raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
[127] Fix | Delete
@real = a
[128] Fix | Delete
@image = b
[129] Fix | Delete
end
[130] Fix | Delete
[131] Fix | Delete
#
[132] Fix | Delete
# Addition with real or complex number.
[133] Fix | Delete
#
[134] Fix | Delete
def + (other)
[135] Fix | Delete
if other.kind_of?(Complex)
[136] Fix | Delete
re = @real + other.real
[137] Fix | Delete
im = @image + other.image
[138] Fix | Delete
Complex(re, im)
[139] Fix | Delete
elsif Complex.generic?(other)
[140] Fix | Delete
Complex(@real + other, @image)
[141] Fix | Delete
else
[142] Fix | Delete
x , y = other.coerce(self)
[143] Fix | Delete
x + y
[144] Fix | Delete
end
[145] Fix | Delete
end
[146] Fix | Delete
[147] Fix | Delete
#
[148] Fix | Delete
# Subtraction with real or complex number.
[149] Fix | Delete
#
[150] Fix | Delete
def - (other)
[151] Fix | Delete
if other.kind_of?(Complex)
[152] Fix | Delete
re = @real - other.real
[153] Fix | Delete
im = @image - other.image
[154] Fix | Delete
Complex(re, im)
[155] Fix | Delete
elsif Complex.generic?(other)
[156] Fix | Delete
Complex(@real - other, @image)
[157] Fix | Delete
else
[158] Fix | Delete
x , y = other.coerce(self)
[159] Fix | Delete
x - y
[160] Fix | Delete
end
[161] Fix | Delete
end
[162] Fix | Delete
[163] Fix | Delete
#
[164] Fix | Delete
# Multiplication with real or complex number.
[165] Fix | Delete
#
[166] Fix | Delete
def * (other)
[167] Fix | Delete
if other.kind_of?(Complex)
[168] Fix | Delete
re = @real*other.real - @image*other.image
[169] Fix | Delete
im = @real*other.image + @image*other.real
[170] Fix | Delete
Complex(re, im)
[171] Fix | Delete
elsif Complex.generic?(other)
[172] Fix | Delete
Complex(@real * other, @image * other)
[173] Fix | Delete
else
[174] Fix | Delete
x , y = other.coerce(self)
[175] Fix | Delete
x * y
[176] Fix | Delete
end
[177] Fix | Delete
end
[178] Fix | Delete
[179] Fix | Delete
#
[180] Fix | Delete
# Division by real or complex number.
[181] Fix | Delete
#
[182] Fix | Delete
def / (other)
[183] Fix | Delete
if other.kind_of?(Complex)
[184] Fix | Delete
self*other.conjugate/other.abs2
[185] Fix | Delete
elsif Complex.generic?(other)
[186] Fix | Delete
Complex(@real/other, @image/other)
[187] Fix | Delete
else
[188] Fix | Delete
x, y = other.coerce(self)
[189] Fix | Delete
x/y
[190] Fix | Delete
end
[191] Fix | Delete
end
[192] Fix | Delete
[193] Fix | Delete
def quo(other)
[194] Fix | Delete
Complex(@real.quo(1), @image.quo(1)) / other
[195] Fix | Delete
end
[196] Fix | Delete
[197] Fix | Delete
#
[198] Fix | Delete
# Raise this complex number to the given (real or complex) power.
[199] Fix | Delete
#
[200] Fix | Delete
def ** (other)
[201] Fix | Delete
if other == 0
[202] Fix | Delete
return Complex(1)
[203] Fix | Delete
end
[204] Fix | Delete
if other.kind_of?(Complex)
[205] Fix | Delete
r, theta = polar
[206] Fix | Delete
ore = other.real
[207] Fix | Delete
oim = other.image
[208] Fix | Delete
nr = Math.exp!(ore*Math.log!(r) - oim * theta)
[209] Fix | Delete
ntheta = theta*ore + oim*Math.log!(r)
[210] Fix | Delete
Complex.polar(nr, ntheta)
[211] Fix | Delete
elsif other.kind_of?(Integer)
[212] Fix | Delete
if other > 0
[213] Fix | Delete
x = self
[214] Fix | Delete
z = x
[215] Fix | Delete
n = other - 1
[216] Fix | Delete
while n != 0
[217] Fix | Delete
while (div, mod = n.divmod(2)
[218] Fix | Delete
mod == 0)
[219] Fix | Delete
x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
[220] Fix | Delete
n = div
[221] Fix | Delete
end
[222] Fix | Delete
z *= x
[223] Fix | Delete
n -= 1
[224] Fix | Delete
end
[225] Fix | Delete
z
[226] Fix | Delete
else
[227] Fix | Delete
if defined? Rational
[228] Fix | Delete
(Rational(1) / self) ** -other
[229] Fix | Delete
else
[230] Fix | Delete
self ** Float(other)
[231] Fix | Delete
end
[232] Fix | Delete
end
[233] Fix | Delete
elsif Complex.generic?(other)
[234] Fix | Delete
r, theta = polar
[235] Fix | Delete
Complex.polar(r**other, theta*other)
[236] Fix | Delete
else
[237] Fix | Delete
x, y = other.coerce(self)
[238] Fix | Delete
x**y
[239] Fix | Delete
end
[240] Fix | Delete
end
[241] Fix | Delete
[242] Fix | Delete
#
[243] Fix | Delete
# Remainder after division by a real or complex number.
[244] Fix | Delete
#
[245] Fix | Delete
def % (other)
[246] Fix | Delete
if other.kind_of?(Complex)
[247] Fix | Delete
Complex(@real % other.real, @image % other.image)
[248] Fix | Delete
elsif Complex.generic?(other)
[249] Fix | Delete
Complex(@real % other, @image % other)
[250] Fix | Delete
else
[251] Fix | Delete
x , y = other.coerce(self)
[252] Fix | Delete
x % y
[253] Fix | Delete
end
[254] Fix | Delete
end
[255] Fix | Delete
[256] Fix | Delete
#--
[257] Fix | Delete
# def divmod(other)
[258] Fix | Delete
# if other.kind_of?(Complex)
[259] Fix | Delete
# rdiv, rmod = @real.divmod(other.real)
[260] Fix | Delete
# idiv, imod = @image.divmod(other.image)
[261] Fix | Delete
# return Complex(rdiv, idiv), Complex(rmod, rmod)
[262] Fix | Delete
# elsif Complex.generic?(other)
[263] Fix | Delete
# Complex(@real.divmod(other), @image.divmod(other))
[264] Fix | Delete
# else
[265] Fix | Delete
# x , y = other.coerce(self)
[266] Fix | Delete
# x.divmod(y)
[267] Fix | Delete
# end
[268] Fix | Delete
# end
[269] Fix | Delete
#++
[270] Fix | Delete
[271] Fix | Delete
#
[272] Fix | Delete
# Absolute value (aka modulus): distance from the zero point on the complex
[273] Fix | Delete
# plane.
[274] Fix | Delete
#
[275] Fix | Delete
def abs
[276] Fix | Delete
Math.hypot(@real, @image)
[277] Fix | Delete
end
[278] Fix | Delete
[279] Fix | Delete
#
[280] Fix | Delete
# Square of the absolute value.
[281] Fix | Delete
#
[282] Fix | Delete
def abs2
[283] Fix | Delete
@real*@real + @image*@image
[284] Fix | Delete
end
[285] Fix | Delete
[286] Fix | Delete
#
[287] Fix | Delete
# Argument (angle from (1,0) on the complex plane).
[288] Fix | Delete
#
[289] Fix | Delete
def arg
[290] Fix | Delete
Math.atan2!(@image, @real)
[291] Fix | Delete
end
[292] Fix | Delete
alias angle arg
[293] Fix | Delete
[294] Fix | Delete
#
[295] Fix | Delete
# Returns the absolute value _and_ the argument.
[296] Fix | Delete
#
[297] Fix | Delete
def polar
[298] Fix | Delete
return abs, arg
[299] Fix | Delete
end
[300] Fix | Delete
[301] Fix | Delete
#
[302] Fix | Delete
# Complex conjugate (<tt>z + z.conjugate = 2 * z.real</tt>).
[303] Fix | Delete
#
[304] Fix | Delete
def conjugate
[305] Fix | Delete
Complex(@real, -@image)
[306] Fix | Delete
end
[307] Fix | Delete
alias conj conjugate
[308] Fix | Delete
[309] Fix | Delete
#
[310] Fix | Delete
# Compares the absolute values of the two numbers.
[311] Fix | Delete
#
[312] Fix | Delete
def <=> (other)
[313] Fix | Delete
self.abs <=> other.abs
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
#
[317] Fix | Delete
# Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
[318] Fix | Delete
#
[319] Fix | Delete
def == (other)
[320] Fix | Delete
if other.kind_of?(Complex)
[321] Fix | Delete
@real == other.real and @image == other.image
[322] Fix | Delete
elsif Complex.generic?(other)
[323] Fix | Delete
@real == other and @image == 0
[324] Fix | Delete
else
[325] Fix | Delete
other == self
[326] Fix | Delete
end
[327] Fix | Delete
end
[328] Fix | Delete
[329] Fix | Delete
#
[330] Fix | Delete
# Attempts to coerce +other+ to a Complex number.
[331] Fix | Delete
#
[332] Fix | Delete
def coerce(other)
[333] Fix | Delete
if Complex.generic?(other)
[334] Fix | Delete
return Complex.new!(other), self
[335] Fix | Delete
else
[336] Fix | Delete
super
[337] Fix | Delete
end
[338] Fix | Delete
end
[339] Fix | Delete
[340] Fix | Delete
#
[341] Fix | Delete
# FIXME
[342] Fix | Delete
#
[343] Fix | Delete
def denominator
[344] Fix | Delete
@real.denominator.lcm(@image.denominator)
[345] Fix | Delete
end
[346] Fix | Delete
[347] Fix | Delete
#
[348] Fix | Delete
# FIXME
[349] Fix | Delete
#
[350] Fix | Delete
def numerator
[351] Fix | Delete
cd = denominator
[352] Fix | Delete
Complex(@real.numerator*(cd/@real.denominator),
[353] Fix | Delete
@image.numerator*(cd/@image.denominator))
[354] Fix | Delete
end
[355] Fix | Delete
[356] Fix | Delete
#
[357] Fix | Delete
# Standard string representation of the complex number.
[358] Fix | Delete
#
[359] Fix | Delete
def to_s
[360] Fix | Delete
if @real != 0
[361] Fix | Delete
if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
[362] Fix | Delete
if @image >= 0
[363] Fix | Delete
@real.to_s+"+("+@image.to_s+")i"
[364] Fix | Delete
else
[365] Fix | Delete
@real.to_s+"-("+(-@image).to_s+")i"
[366] Fix | Delete
end
[367] Fix | Delete
else
[368] Fix | Delete
if @image >= 0
[369] Fix | Delete
@real.to_s+"+"+@image.to_s+"i"
[370] Fix | Delete
else
[371] Fix | Delete
@real.to_s+"-"+(-@image).to_s+"i"
[372] Fix | Delete
end
[373] Fix | Delete
end
[374] Fix | Delete
else
[375] Fix | Delete
if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
[376] Fix | Delete
"("+@image.to_s+")i"
[377] Fix | Delete
else
[378] Fix | Delete
@image.to_s+"i"
[379] Fix | Delete
end
[380] Fix | Delete
end
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
#
[384] Fix | Delete
# Returns a hash code for the complex number.
[385] Fix | Delete
#
[386] Fix | Delete
def hash
[387] Fix | Delete
@real.hash ^ @image.hash
[388] Fix | Delete
end
[389] Fix | Delete
[390] Fix | Delete
#
[391] Fix | Delete
# Returns "<tt>Complex(<i>real</i>, <i>image</i>)</tt>".
[392] Fix | Delete
#
[393] Fix | Delete
def inspect
[394] Fix | Delete
sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
[395] Fix | Delete
end
[396] Fix | Delete
[397] Fix | Delete
[398] Fix | Delete
#
[399] Fix | Delete
# +I+ is the imaginary number. It exists at point (0,1) on the complex plane.
[400] Fix | Delete
#
[401] Fix | Delete
I = Complex(0,1)
[402] Fix | Delete
[403] Fix | Delete
# The real part of a complex number.
[404] Fix | Delete
attr :real
[405] Fix | Delete
[406] Fix | Delete
# The imaginary part of a complex number.
[407] Fix | Delete
attr :image
[408] Fix | Delete
alias imag image
[409] Fix | Delete
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
class Integer
[413] Fix | Delete
[414] Fix | Delete
unless defined?(1.numerator)
[415] Fix | Delete
def numerator() self end
[416] Fix | Delete
def denominator() 1 end
[417] Fix | Delete
[418] Fix | Delete
def gcd(other)
[419] Fix | Delete
min = self.abs
[420] Fix | Delete
max = other.abs
[421] Fix | Delete
while min > 0
[422] Fix | Delete
tmp = min
[423] Fix | Delete
min = max % min
[424] Fix | Delete
max = tmp
[425] Fix | Delete
end
[426] Fix | Delete
max
[427] Fix | Delete
end
[428] Fix | Delete
[429] Fix | Delete
def lcm(other)
[430] Fix | Delete
if self.zero? or other.zero?
[431] Fix | Delete
0
[432] Fix | Delete
else
[433] Fix | Delete
(self.div(self.gcd(other)) * other).abs
[434] Fix | Delete
end
[435] Fix | Delete
end
[436] Fix | Delete
[437] Fix | Delete
end
[438] Fix | Delete
[439] Fix | Delete
end
[440] Fix | Delete
[441] Fix | Delete
module Math
[442] Fix | Delete
alias sqrt! sqrt
[443] Fix | Delete
alias exp! exp
[444] Fix | Delete
alias log! log
[445] Fix | Delete
alias log10! log10
[446] Fix | Delete
alias cos! cos
[447] Fix | Delete
alias sin! sin
[448] Fix | Delete
alias tan! tan
[449] Fix | Delete
alias cosh! cosh
[450] Fix | Delete
alias sinh! sinh
[451] Fix | Delete
alias tanh! tanh
[452] Fix | Delete
alias acos! acos
[453] Fix | Delete
alias asin! asin
[454] Fix | Delete
alias atan! atan
[455] Fix | Delete
alias atan2! atan2
[456] Fix | Delete
alias acosh! acosh
[457] Fix | Delete
alias asinh! asinh
[458] Fix | Delete
alias atanh! atanh
[459] Fix | Delete
[460] Fix | Delete
# Redefined to handle a Complex argument.
[461] Fix | Delete
def sqrt(z)
[462] Fix | Delete
if Complex.generic?(z)
[463] Fix | Delete
if z >= 0
[464] Fix | Delete
sqrt!(z)
[465] Fix | Delete
else
[466] Fix | Delete
Complex(0,sqrt!(-z))
[467] Fix | Delete
end
[468] Fix | Delete
else
[469] Fix | Delete
if z.image < 0
[470] Fix | Delete
sqrt(z.conjugate).conjugate
[471] Fix | Delete
else
[472] Fix | Delete
r = z.abs
[473] Fix | Delete
x = z.real
[474] Fix | Delete
Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) )
[475] Fix | Delete
end
[476] Fix | Delete
end
[477] Fix | Delete
end
[478] Fix | Delete
[479] Fix | Delete
# Redefined to handle a Complex argument.
[480] Fix | Delete
def exp(z)
[481] Fix | Delete
if Complex.generic?(z)
[482] Fix | Delete
exp!(z)
[483] Fix | Delete
else
[484] Fix | Delete
Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image))
[485] Fix | Delete
end
[486] Fix | Delete
end
[487] Fix | Delete
[488] Fix | Delete
# Redefined to handle a Complex argument.
[489] Fix | Delete
def cos(z)
[490] Fix | Delete
if Complex.generic?(z)
[491] Fix | Delete
cos!(z)
[492] Fix | Delete
else
[493] Fix | Delete
Complex(cos!(z.real)*cosh!(z.image),
[494] Fix | Delete
-sin!(z.real)*sinh!(z.image))
[495] Fix | Delete
end
[496] Fix | Delete
end
[497] Fix | Delete
[498] Fix | Delete
# Redefined to handle a Complex argument.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function