Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: mathn.rb
#
[0] Fix | Delete
# mathn.rb -
[1] Fix | Delete
# $Release Version: 0.5 $
[2] Fix | Delete
# $Revision: 1.1.1.1.4.1 $
[3] Fix | Delete
# $Date: 1998/01/16 12:36:05 $
[4] Fix | Delete
# by Keiju ISHITSUKA(SHL Japan Inc.)
[5] Fix | Delete
#
[6] Fix | Delete
# --
[7] Fix | Delete
#
[8] Fix | Delete
#
[9] Fix | Delete
#
[10] Fix | Delete
[11] Fix | Delete
require "complex.rb"
[12] Fix | Delete
require "rational.rb"
[13] Fix | Delete
require "matrix.rb"
[14] Fix | Delete
[15] Fix | Delete
class Integer
[16] Fix | Delete
[17] Fix | Delete
def gcd2(int)
[18] Fix | Delete
a = self.abs
[19] Fix | Delete
b = int.abs
[20] Fix | Delete
a, b = b, a if a < b
[21] Fix | Delete
[22] Fix | Delete
pd_a = a.prime_division
[23] Fix | Delete
pd_b = b.prime_division
[24] Fix | Delete
[25] Fix | Delete
gcd = 1
[26] Fix | Delete
for pair in pd_a
[27] Fix | Delete
as = pd_b.assoc(pair[0])
[28] Fix | Delete
if as
[29] Fix | Delete
gcd *= as[0] ** [as[1], pair[1]].min
[30] Fix | Delete
end
[31] Fix | Delete
end
[32] Fix | Delete
return gcd
[33] Fix | Delete
end
[34] Fix | Delete
[35] Fix | Delete
def Integer.from_prime_division(pd)
[36] Fix | Delete
value = 1
[37] Fix | Delete
for prime, index in pd
[38] Fix | Delete
value *= prime**index
[39] Fix | Delete
end
[40] Fix | Delete
value
[41] Fix | Delete
end
[42] Fix | Delete
[43] Fix | Delete
def prime_division
[44] Fix | Delete
raise ZeroDivisionError if self == 0
[45] Fix | Delete
ps = Prime.new
[46] Fix | Delete
value = self
[47] Fix | Delete
pv = []
[48] Fix | Delete
for prime in ps
[49] Fix | Delete
count = 0
[50] Fix | Delete
while (value1, mod = value.divmod(prime)
[51] Fix | Delete
mod) == 0
[52] Fix | Delete
value = value1
[53] Fix | Delete
count += 1
[54] Fix | Delete
end
[55] Fix | Delete
if count != 0
[56] Fix | Delete
pv.push [prime, count]
[57] Fix | Delete
end
[58] Fix | Delete
break if prime * prime >= value
[59] Fix | Delete
end
[60] Fix | Delete
if value > 1
[61] Fix | Delete
pv.push [value, 1]
[62] Fix | Delete
end
[63] Fix | Delete
return pv
[64] Fix | Delete
end
[65] Fix | Delete
end
[66] Fix | Delete
[67] Fix | Delete
class Prime
[68] Fix | Delete
include Enumerable
[69] Fix | Delete
[70] Fix | Delete
def initialize
[71] Fix | Delete
@seed = 1
[72] Fix | Delete
@primes = []
[73] Fix | Delete
@counts = []
[74] Fix | Delete
end
[75] Fix | Delete
[76] Fix | Delete
def succ
[77] Fix | Delete
i = -1
[78] Fix | Delete
size = @primes.size
[79] Fix | Delete
while i < size
[80] Fix | Delete
if i == -1
[81] Fix | Delete
@seed += 1
[82] Fix | Delete
i += 1
[83] Fix | Delete
else
[84] Fix | Delete
while @seed > @counts[i]
[85] Fix | Delete
@counts[i] += @primes[i]
[86] Fix | Delete
end
[87] Fix | Delete
if @seed != @counts[i]
[88] Fix | Delete
i += 1
[89] Fix | Delete
else
[90] Fix | Delete
i = -1
[91] Fix | Delete
end
[92] Fix | Delete
end
[93] Fix | Delete
end
[94] Fix | Delete
@primes.push @seed
[95] Fix | Delete
@counts.push @seed + @seed
[96] Fix | Delete
return @seed
[97] Fix | Delete
end
[98] Fix | Delete
alias next succ
[99] Fix | Delete
[100] Fix | Delete
def each
[101] Fix | Delete
loop do
[102] Fix | Delete
yield succ
[103] Fix | Delete
end
[104] Fix | Delete
end
[105] Fix | Delete
end
[106] Fix | Delete
[107] Fix | Delete
class Fixnum
[108] Fix | Delete
alias / quo
[109] Fix | Delete
end
[110] Fix | Delete
[111] Fix | Delete
class Bignum
[112] Fix | Delete
alias / quo
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
class Rational
[116] Fix | Delete
Unify = true
[117] Fix | Delete
[118] Fix | Delete
def inspect
[119] Fix | Delete
format "%s/%s", numerator.inspect, denominator.inspect
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
alias power! **
[123] Fix | Delete
[124] Fix | Delete
def ** (other)
[125] Fix | Delete
if other.kind_of?(Rational)
[126] Fix | Delete
other2 = other
[127] Fix | Delete
if self < 0
[128] Fix | Delete
return Complex.new!(self, 0) ** other
[129] Fix | Delete
elsif other == 0
[130] Fix | Delete
return Rational(1,1)
[131] Fix | Delete
elsif self == 0
[132] Fix | Delete
return Rational(0,1)
[133] Fix | Delete
elsif self == 1
[134] Fix | Delete
return Rational(1,1)
[135] Fix | Delete
end
[136] Fix | Delete
[137] Fix | Delete
npd = numerator.prime_division
[138] Fix | Delete
dpd = denominator.prime_division
[139] Fix | Delete
if other < 0
[140] Fix | Delete
other = -other
[141] Fix | Delete
npd, dpd = dpd, npd
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
for elm in npd
[145] Fix | Delete
elm[1] = elm[1] * other
[146] Fix | Delete
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
[147] Fix | Delete
return Float(self) ** other2
[148] Fix | Delete
end
[149] Fix | Delete
elm[1] = elm[1].to_i
[150] Fix | Delete
end
[151] Fix | Delete
[152] Fix | Delete
for elm in dpd
[153] Fix | Delete
elm[1] = elm[1] * other
[154] Fix | Delete
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
[155] Fix | Delete
return Float(self) ** other2
[156] Fix | Delete
end
[157] Fix | Delete
elm[1] = elm[1].to_i
[158] Fix | Delete
end
[159] Fix | Delete
[160] Fix | Delete
num = Integer.from_prime_division(npd)
[161] Fix | Delete
den = Integer.from_prime_division(dpd)
[162] Fix | Delete
[163] Fix | Delete
Rational(num,den)
[164] Fix | Delete
[165] Fix | Delete
elsif other.kind_of?(Integer)
[166] Fix | Delete
if other > 0
[167] Fix | Delete
num = numerator ** other
[168] Fix | Delete
den = denominator ** other
[169] Fix | Delete
elsif other < 0
[170] Fix | Delete
num = denominator ** -other
[171] Fix | Delete
den = numerator ** -other
[172] Fix | Delete
elsif other == 0
[173] Fix | Delete
num = 1
[174] Fix | Delete
den = 1
[175] Fix | Delete
end
[176] Fix | Delete
Rational.new!(num, den)
[177] Fix | Delete
elsif other.kind_of?(Float)
[178] Fix | Delete
Float(self) ** other
[179] Fix | Delete
else
[180] Fix | Delete
x , y = other.coerce(self)
[181] Fix | Delete
x ** y
[182] Fix | Delete
end
[183] Fix | Delete
end
[184] Fix | Delete
[185] Fix | Delete
def power2(other)
[186] Fix | Delete
if other.kind_of?(Rational)
[187] Fix | Delete
if self < 0
[188] Fix | Delete
return Complex(self, 0) ** other
[189] Fix | Delete
elsif other == 0
[190] Fix | Delete
return Rational(1,1)
[191] Fix | Delete
elsif self == 0
[192] Fix | Delete
return Rational(0,1)
[193] Fix | Delete
elsif self == 1
[194] Fix | Delete
return Rational(1,1)
[195] Fix | Delete
end
[196] Fix | Delete
[197] Fix | Delete
dem = nil
[198] Fix | Delete
x = self.denominator.to_f.to_i
[199] Fix | Delete
neard = self.denominator.to_f ** (1.0/other.denominator.to_f)
[200] Fix | Delete
loop do
[201] Fix | Delete
if (neard**other.denominator == self.denominator)
[202] Fix | Delete
dem = neaed
[203] Fix | Delete
break
[204] Fix | Delete
end
[205] Fix | Delete
end
[206] Fix | Delete
nearn = self.numerator.to_f ** (1.0/other.denominator.to_f)
[207] Fix | Delete
Rational(num,den)
[208] Fix | Delete
[209] Fix | Delete
elsif other.kind_of?(Integer)
[210] Fix | Delete
if other > 0
[211] Fix | Delete
num = numerator ** other
[212] Fix | Delete
den = denominator ** other
[213] Fix | Delete
elsif other < 0
[214] Fix | Delete
num = denominator ** -other
[215] Fix | Delete
den = numerator ** -other
[216] Fix | Delete
elsif other == 0
[217] Fix | Delete
num = 1
[218] Fix | Delete
den = 1
[219] Fix | Delete
end
[220] Fix | Delete
Rational.new!(num, den)
[221] Fix | Delete
elsif other.kind_of?(Float)
[222] Fix | Delete
Float(self) ** other
[223] Fix | Delete
else
[224] Fix | Delete
x , y = other.coerce(self)
[225] Fix | Delete
x ** y
[226] Fix | Delete
end
[227] Fix | Delete
end
[228] Fix | Delete
end
[229] Fix | Delete
[230] Fix | Delete
module Math
[231] Fix | Delete
def sqrt(a)
[232] Fix | Delete
if a.kind_of?(Complex)
[233] Fix | Delete
abs = sqrt(a.real*a.real + a.image*a.image)
[234] Fix | Delete
# if not abs.kind_of?(Rational)
[235] Fix | Delete
# return a**Rational(1,2)
[236] Fix | Delete
# end
[237] Fix | Delete
x = sqrt((a.real + abs)/Rational(2))
[238] Fix | Delete
y = sqrt((-a.real + abs)/Rational(2))
[239] Fix | Delete
# if !(x.kind_of?(Rational) and y.kind_of?(Rational))
[240] Fix | Delete
# return a**Rational(1,2)
[241] Fix | Delete
# end
[242] Fix | Delete
if a.image >= 0
[243] Fix | Delete
Complex(x, y)
[244] Fix | Delete
else
[245] Fix | Delete
Complex(x, -y)
[246] Fix | Delete
end
[247] Fix | Delete
elsif a >= 0
[248] Fix | Delete
rsqrt(a)
[249] Fix | Delete
else
[250] Fix | Delete
Complex(0,rsqrt(-a))
[251] Fix | Delete
end
[252] Fix | Delete
end
[253] Fix | Delete
[254] Fix | Delete
def rsqrt(a)
[255] Fix | Delete
if a.kind_of?(Float)
[256] Fix | Delete
sqrt!(a)
[257] Fix | Delete
elsif a.kind_of?(Rational)
[258] Fix | Delete
rsqrt(a.numerator)/rsqrt(a.denominator)
[259] Fix | Delete
else
[260] Fix | Delete
src = a
[261] Fix | Delete
max = 2 ** 32
[262] Fix | Delete
byte_a = [src & 0xffffffff]
[263] Fix | Delete
# ruby's bug
[264] Fix | Delete
while (src >= max) and (src >>= 32)
[265] Fix | Delete
byte_a.unshift src & 0xffffffff
[266] Fix | Delete
end
[267] Fix | Delete
[268] Fix | Delete
answer = 0
[269] Fix | Delete
main = 0
[270] Fix | Delete
side = 0
[271] Fix | Delete
for elm in byte_a
[272] Fix | Delete
main = (main << 32) + elm
[273] Fix | Delete
side <<= 16
[274] Fix | Delete
if answer != 0
[275] Fix | Delete
if main * 4 < side * side
[276] Fix | Delete
applo = main.div(side)
[277] Fix | Delete
else
[278] Fix | Delete
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
[279] Fix | Delete
end
[280] Fix | Delete
else
[281] Fix | Delete
applo = sqrt!(main).to_i + 1
[282] Fix | Delete
end
[283] Fix | Delete
[284] Fix | Delete
while (x = (side + applo) * applo) > main
[285] Fix | Delete
applo -= 1
[286] Fix | Delete
end
[287] Fix | Delete
main -= x
[288] Fix | Delete
answer = (answer << 16) + applo
[289] Fix | Delete
side += applo * 2
[290] Fix | Delete
end
[291] Fix | Delete
if main == 0
[292] Fix | Delete
answer
[293] Fix | Delete
else
[294] Fix | Delete
sqrt!(a)
[295] Fix | Delete
end
[296] Fix | Delete
end
[297] Fix | Delete
end
[298] Fix | Delete
[299] Fix | Delete
module_function :sqrt
[300] Fix | Delete
module_function :rsqrt
[301] Fix | Delete
end
[302] Fix | Delete
[303] Fix | Delete
class Complex
[304] Fix | Delete
Unify = true
[305] Fix | Delete
end
[306] Fix | Delete
[307] Fix | Delete
[308] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function