Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: cmath.rb
##
[0] Fix | Delete
# = Trigonometric and transcendental functions for complex numbers.
[1] Fix | Delete
#
[2] Fix | Delete
# CMath is a library that provides trigonometric and transcendental
[3] Fix | Delete
# functions for complex numbers. The functions in this module accept
[4] Fix | Delete
# integers, floating-point numbers or complex numbers as arguments.
[5] Fix | Delete
#
[6] Fix | Delete
# Note that the selection of functions is similar, but not identical,
[7] Fix | Delete
# to that in module math. The reason for having two modules is that
[8] Fix | Delete
# some users aren't interested in complex numbers, and perhaps don't
[9] Fix | Delete
# even know what they are. They would rather have Math.sqrt(-1) raise
[10] Fix | Delete
# an exception than return a complex number.
[11] Fix | Delete
#
[12] Fix | Delete
# == Usage
[13] Fix | Delete
#
[14] Fix | Delete
# To start using this library, simply require cmath library:
[15] Fix | Delete
#
[16] Fix | Delete
# require "cmath"
[17] Fix | Delete
#
[18] Fix | Delete
# And after call any CMath function. For example:
[19] Fix | Delete
#
[20] Fix | Delete
# CMath.sqrt(-9) #=> 0+3.0i
[21] Fix | Delete
# CMath.exp(0 + 0i) #=> 1.0+0.0i
[22] Fix | Delete
# CMath.log10(-5.to_c) #=> (0.6989700043360187+1.3643763538418412i)
[23] Fix | Delete
#
[24] Fix | Delete
#
[25] Fix | Delete
# For more information you can see Complec class.
[26] Fix | Delete
[27] Fix | Delete
module CMath
[28] Fix | Delete
[29] Fix | Delete
include Math
[30] Fix | Delete
[31] Fix | Delete
alias exp! exp
[32] Fix | Delete
alias log! log
[33] Fix | Delete
alias log2! log2
[34] Fix | Delete
alias log10! log10
[35] Fix | Delete
alias sqrt! sqrt
[36] Fix | Delete
alias cbrt! cbrt
[37] Fix | Delete
[38] Fix | Delete
alias sin! sin
[39] Fix | Delete
alias cos! cos
[40] Fix | Delete
alias tan! tan
[41] Fix | Delete
[42] Fix | Delete
alias sinh! sinh
[43] Fix | Delete
alias cosh! cosh
[44] Fix | Delete
alias tanh! tanh
[45] Fix | Delete
[46] Fix | Delete
alias asin! asin
[47] Fix | Delete
alias acos! acos
[48] Fix | Delete
alias atan! atan
[49] Fix | Delete
alias atan2! atan2
[50] Fix | Delete
[51] Fix | Delete
alias asinh! asinh
[52] Fix | Delete
alias acosh! acosh
[53] Fix | Delete
alias atanh! atanh
[54] Fix | Delete
[55] Fix | Delete
##
[56] Fix | Delete
# Math::E raised to the +z+ power
[57] Fix | Delete
#
[58] Fix | Delete
# CMath.exp(2i) #=> (-0.4161468365471424+0.9092974268256817i)
[59] Fix | Delete
def exp(z)
[60] Fix | Delete
begin
[61] Fix | Delete
if z.real?
[62] Fix | Delete
exp!(z)
[63] Fix | Delete
else
[64] Fix | Delete
ere = exp!(z.real)
[65] Fix | Delete
Complex(ere * cos!(z.imag),
[66] Fix | Delete
ere * sin!(z.imag))
[67] Fix | Delete
end
[68] Fix | Delete
rescue NoMethodError
[69] Fix | Delete
handle_no_method_error
[70] Fix | Delete
end
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
##
[74] Fix | Delete
# Returns the natural logarithm of Complex. If a second argument is given,
[75] Fix | Delete
# it will be the base of logarithm.
[76] Fix | Delete
#
[77] Fix | Delete
# CMath.log(1 + 4i) #=> (1.416606672028108+1.3258176636680326i)
[78] Fix | Delete
# CMath.log(1 + 4i, 10) #=> (0.6152244606891369+0.5757952953408879i)
[79] Fix | Delete
def log(*args)
[80] Fix | Delete
begin
[81] Fix | Delete
z, b = args
[82] Fix | Delete
unless b.nil? || b.kind_of?(Numeric)
[83] Fix | Delete
raise TypeError, "Numeric Number required"
[84] Fix | Delete
end
[85] Fix | Delete
if z.real? and z >= 0 and (b.nil? or b >= 0)
[86] Fix | Delete
log!(*args)
[87] Fix | Delete
else
[88] Fix | Delete
a = Complex(log!(z.abs), z.arg)
[89] Fix | Delete
if b
[90] Fix | Delete
a /= log(b)
[91] Fix | Delete
end
[92] Fix | Delete
a
[93] Fix | Delete
end
[94] Fix | Delete
rescue NoMethodError
[95] Fix | Delete
handle_no_method_error
[96] Fix | Delete
end
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
##
[100] Fix | Delete
# returns the base 2 logarithm of +z+
[101] Fix | Delete
#
[102] Fix | Delete
# CMath.log2(-1) => (0.0+4.532360141827194i)
[103] Fix | Delete
def log2(z)
[104] Fix | Delete
begin
[105] Fix | Delete
if z.real? and z >= 0
[106] Fix | Delete
log2!(z)
[107] Fix | Delete
else
[108] Fix | Delete
log(z) / log!(2)
[109] Fix | Delete
end
[110] Fix | Delete
rescue NoMethodError
[111] Fix | Delete
handle_no_method_error
[112] Fix | Delete
end
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
##
[116] Fix | Delete
# returns the base 10 logarithm of +z+
[117] Fix | Delete
#
[118] Fix | Delete
# CMath.log10(-1) #=> (0.0+1.3643763538418412i)
[119] Fix | Delete
def log10(z)
[120] Fix | Delete
begin
[121] Fix | Delete
if z.real? and z >= 0
[122] Fix | Delete
log10!(z)
[123] Fix | Delete
else
[124] Fix | Delete
log(z) / log!(10)
[125] Fix | Delete
end
[126] Fix | Delete
rescue NoMethodError
[127] Fix | Delete
handle_no_method_error
[128] Fix | Delete
end
[129] Fix | Delete
end
[130] Fix | Delete
[131] Fix | Delete
##
[132] Fix | Delete
# Returns the non-negative square root of Complex.
[133] Fix | Delete
#
[134] Fix | Delete
# CMath.sqrt(-1 + 0i) #=> 0.0+1.0i
[135] Fix | Delete
def sqrt(z)
[136] Fix | Delete
begin
[137] Fix | Delete
if z.real?
[138] Fix | Delete
if z < 0
[139] Fix | Delete
Complex(0, sqrt!(-z))
[140] Fix | Delete
else
[141] Fix | Delete
sqrt!(z)
[142] Fix | Delete
end
[143] Fix | Delete
else
[144] Fix | Delete
if z.imag < 0 ||
[145] Fix | Delete
(z.imag == 0 && z.imag.to_s[0] == '-')
[146] Fix | Delete
sqrt(z.conjugate).conjugate
[147] Fix | Delete
else
[148] Fix | Delete
r = z.abs
[149] Fix | Delete
x = z.real
[150] Fix | Delete
Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0))
[151] Fix | Delete
end
[152] Fix | Delete
end
[153] Fix | Delete
rescue NoMethodError
[154] Fix | Delete
handle_no_method_error
[155] Fix | Delete
end
[156] Fix | Delete
end
[157] Fix | Delete
[158] Fix | Delete
##
[159] Fix | Delete
# returns the principal value of the cube root of +z+
[160] Fix | Delete
#
[161] Fix | Delete
# CMath.cbrt(1 + 4i) #=> (1.449461632813119+0.6858152562177092i)
[162] Fix | Delete
def cbrt(z)
[163] Fix | Delete
z ** (1.0/3)
[164] Fix | Delete
end
[165] Fix | Delete
[166] Fix | Delete
##
[167] Fix | Delete
# returns the sine of +z+, where +z+ is given in radians
[168] Fix | Delete
#
[169] Fix | Delete
# CMath.sin(1 + 1i) #=> (1.2984575814159773+0.6349639147847361i)
[170] Fix | Delete
def sin(z)
[171] Fix | Delete
begin
[172] Fix | Delete
if z.real?
[173] Fix | Delete
sin!(z)
[174] Fix | Delete
else
[175] Fix | Delete
Complex(sin!(z.real) * cosh!(z.imag),
[176] Fix | Delete
cos!(z.real) * sinh!(z.imag))
[177] Fix | Delete
end
[178] Fix | Delete
rescue NoMethodError
[179] Fix | Delete
handle_no_method_error
[180] Fix | Delete
end
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
##
[184] Fix | Delete
# returns the cosine of +z+, where +z+ is given in radians
[185] Fix | Delete
#
[186] Fix | Delete
# CMath.cos(1 + 1i) #=> (0.8337300251311491-0.9888977057628651i)
[187] Fix | Delete
def cos(z)
[188] Fix | Delete
begin
[189] Fix | Delete
if z.real?
[190] Fix | Delete
cos!(z)
[191] Fix | Delete
else
[192] Fix | Delete
Complex(cos!(z.real) * cosh!(z.imag),
[193] Fix | Delete
-sin!(z.real) * sinh!(z.imag))
[194] Fix | Delete
end
[195] Fix | Delete
rescue NoMethodError
[196] Fix | Delete
handle_no_method_error
[197] Fix | Delete
end
[198] Fix | Delete
end
[199] Fix | Delete
[200] Fix | Delete
##
[201] Fix | Delete
# returns the tangent of +z+, where +z+ is given in radians
[202] Fix | Delete
#
[203] Fix | Delete
# CMath.tan(1 + 1i) #=> (0.27175258531951174+1.0839233273386943i)
[204] Fix | Delete
def tan(z)
[205] Fix | Delete
begin
[206] Fix | Delete
if z.real?
[207] Fix | Delete
tan!(z)
[208] Fix | Delete
else
[209] Fix | Delete
sin(z) / cos(z)
[210] Fix | Delete
end
[211] Fix | Delete
rescue NoMethodError
[212] Fix | Delete
handle_no_method_error
[213] Fix | Delete
end
[214] Fix | Delete
end
[215] Fix | Delete
[216] Fix | Delete
##
[217] Fix | Delete
# returns the hyperbolic sine of +z+, where +z+ is given in radians
[218] Fix | Delete
#
[219] Fix | Delete
# CMath.sinh(1 + 1i) #=> (0.6349639147847361+1.2984575814159773i)
[220] Fix | Delete
def sinh(z)
[221] Fix | Delete
begin
[222] Fix | Delete
if z.real?
[223] Fix | Delete
sinh!(z)
[224] Fix | Delete
else
[225] Fix | Delete
Complex(sinh!(z.real) * cos!(z.imag),
[226] Fix | Delete
cosh!(z.real) * sin!(z.imag))
[227] Fix | Delete
end
[228] Fix | Delete
rescue NoMethodError
[229] Fix | Delete
handle_no_method_error
[230] Fix | Delete
end
[231] Fix | Delete
end
[232] Fix | Delete
[233] Fix | Delete
##
[234] Fix | Delete
# returns the hyperbolic cosine of +z+, where +z+ is given in radians
[235] Fix | Delete
#
[236] Fix | Delete
# CMath.cosh(1 + 1i) #=> (0.8337300251311491+0.9888977057628651i)
[237] Fix | Delete
def cosh(z)
[238] Fix | Delete
begin
[239] Fix | Delete
if z.real?
[240] Fix | Delete
cosh!(z)
[241] Fix | Delete
else
[242] Fix | Delete
Complex(cosh!(z.real) * cos!(z.imag),
[243] Fix | Delete
sinh!(z.real) * sin!(z.imag))
[244] Fix | Delete
end
[245] Fix | Delete
rescue NoMethodError
[246] Fix | Delete
handle_no_method_error
[247] Fix | Delete
end
[248] Fix | Delete
end
[249] Fix | Delete
[250] Fix | Delete
##
[251] Fix | Delete
# returns the hyperbolic tangent of +z+, where +z+ is given in radians
[252] Fix | Delete
#
[253] Fix | Delete
# CMath.tanh(1 + 1i) #=> (1.0839233273386943+0.27175258531951174i)
[254] Fix | Delete
def tanh(z)
[255] Fix | Delete
begin
[256] Fix | Delete
if z.real?
[257] Fix | Delete
tanh!(z)
[258] Fix | Delete
else
[259] Fix | Delete
sinh(z) / cosh(z)
[260] Fix | Delete
end
[261] Fix | Delete
rescue NoMethodError
[262] Fix | Delete
handle_no_method_error
[263] Fix | Delete
end
[264] Fix | Delete
end
[265] Fix | Delete
[266] Fix | Delete
##
[267] Fix | Delete
# returns the arc sine of +z+
[268] Fix | Delete
#
[269] Fix | Delete
# CMath.asin(1 + 1i) #=> (0.6662394324925153+1.0612750619050355i)
[270] Fix | Delete
def asin(z)
[271] Fix | Delete
begin
[272] Fix | Delete
if z.real? and z >= -1 and z <= 1
[273] Fix | Delete
asin!(z)
[274] Fix | Delete
else
[275] Fix | Delete
(-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
[276] Fix | Delete
end
[277] Fix | Delete
rescue NoMethodError
[278] Fix | Delete
handle_no_method_error
[279] Fix | Delete
end
[280] Fix | Delete
end
[281] Fix | Delete
[282] Fix | Delete
##
[283] Fix | Delete
# returns the arc cosine of +z+
[284] Fix | Delete
#
[285] Fix | Delete
# CMath.acos(1 + 1i) #=> (0.9045568943023813-1.0612750619050357i)
[286] Fix | Delete
def acos(z)
[287] Fix | Delete
begin
[288] Fix | Delete
if z.real? and z >= -1 and z <= 1
[289] Fix | Delete
acos!(z)
[290] Fix | Delete
else
[291] Fix | Delete
(-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
[292] Fix | Delete
end
[293] Fix | Delete
rescue NoMethodError
[294] Fix | Delete
handle_no_method_error
[295] Fix | Delete
end
[296] Fix | Delete
end
[297] Fix | Delete
[298] Fix | Delete
##
[299] Fix | Delete
# returns the arc tangent of +z+
[300] Fix | Delete
#
[301] Fix | Delete
# CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
[302] Fix | Delete
def atan(z)
[303] Fix | Delete
begin
[304] Fix | Delete
if z.real?
[305] Fix | Delete
atan!(z)
[306] Fix | Delete
else
[307] Fix | Delete
1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
[308] Fix | Delete
end
[309] Fix | Delete
rescue NoMethodError
[310] Fix | Delete
handle_no_method_error
[311] Fix | Delete
end
[312] Fix | Delete
end
[313] Fix | Delete
[314] Fix | Delete
##
[315] Fix | Delete
# returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
[316] Fix | Delete
# +x+ to determine the quadrant
[317] Fix | Delete
#
[318] Fix | Delete
# CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
[319] Fix | Delete
def atan2(y,x)
[320] Fix | Delete
begin
[321] Fix | Delete
if y.real? and x.real?
[322] Fix | Delete
atan2!(y,x)
[323] Fix | Delete
else
[324] Fix | Delete
(-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
[325] Fix | Delete
end
[326] Fix | Delete
rescue NoMethodError
[327] Fix | Delete
handle_no_method_error
[328] Fix | Delete
end
[329] Fix | Delete
end
[330] Fix | Delete
[331] Fix | Delete
##
[332] Fix | Delete
# returns the inverse hyperbolic sine of +z+
[333] Fix | Delete
#
[334] Fix | Delete
# CMath.asinh(1 + 1i) #=> (1.0612750619050357+0.6662394324925153i)
[335] Fix | Delete
def asinh(z)
[336] Fix | Delete
begin
[337] Fix | Delete
if z.real?
[338] Fix | Delete
asinh!(z)
[339] Fix | Delete
else
[340] Fix | Delete
log(z + sqrt(1.0 + z * z))
[341] Fix | Delete
end
[342] Fix | Delete
rescue NoMethodError
[343] Fix | Delete
handle_no_method_error
[344] Fix | Delete
end
[345] Fix | Delete
end
[346] Fix | Delete
[347] Fix | Delete
##
[348] Fix | Delete
# returns the inverse hyperbolic cosine of +z+
[349] Fix | Delete
#
[350] Fix | Delete
# CMath.acosh(1 + 1i) #=> (1.0612750619050357+0.9045568943023813i)
[351] Fix | Delete
def acosh(z)
[352] Fix | Delete
begin
[353] Fix | Delete
if z.real? and z >= 1
[354] Fix | Delete
acosh!(z)
[355] Fix | Delete
else
[356] Fix | Delete
log(z + sqrt(z * z - 1.0))
[357] Fix | Delete
end
[358] Fix | Delete
rescue NoMethodError
[359] Fix | Delete
handle_no_method_error
[360] Fix | Delete
end
[361] Fix | Delete
end
[362] Fix | Delete
[363] Fix | Delete
##
[364] Fix | Delete
# returns the inverse hyperbolic tangent of +z+
[365] Fix | Delete
#
[366] Fix | Delete
# CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
[367] Fix | Delete
def atanh(z)
[368] Fix | Delete
begin
[369] Fix | Delete
if z.real? and z >= -1 and z <= 1
[370] Fix | Delete
atanh!(z)
[371] Fix | Delete
else
[372] Fix | Delete
log((1.0 + z) / (1.0 - z)) / 2.0
[373] Fix | Delete
end
[374] Fix | Delete
rescue NoMethodError
[375] Fix | Delete
handle_no_method_error
[376] Fix | Delete
end
[377] Fix | Delete
end
[378] Fix | Delete
[379] Fix | Delete
module_function :exp!
[380] Fix | Delete
module_function :exp
[381] Fix | Delete
module_function :log!
[382] Fix | Delete
module_function :log
[383] Fix | Delete
module_function :log2!
[384] Fix | Delete
module_function :log2
[385] Fix | Delete
module_function :log10!
[386] Fix | Delete
module_function :log10
[387] Fix | Delete
module_function :sqrt!
[388] Fix | Delete
module_function :sqrt
[389] Fix | Delete
module_function :cbrt!
[390] Fix | Delete
module_function :cbrt
[391] Fix | Delete
[392] Fix | Delete
module_function :sin!
[393] Fix | Delete
module_function :sin
[394] Fix | Delete
module_function :cos!
[395] Fix | Delete
module_function :cos
[396] Fix | Delete
module_function :tan!
[397] Fix | Delete
module_function :tan
[398] Fix | Delete
[399] Fix | Delete
module_function :sinh!
[400] Fix | Delete
module_function :sinh
[401] Fix | Delete
module_function :cosh!
[402] Fix | Delete
module_function :cosh
[403] Fix | Delete
module_function :tanh!
[404] Fix | Delete
module_function :tanh
[405] Fix | Delete
[406] Fix | Delete
module_function :asin!
[407] Fix | Delete
module_function :asin
[408] Fix | Delete
module_function :acos!
[409] Fix | Delete
module_function :acos
[410] Fix | Delete
module_function :atan!
[411] Fix | Delete
module_function :atan
[412] Fix | Delete
module_function :atan2!
[413] Fix | Delete
module_function :atan2
[414] Fix | Delete
[415] Fix | Delete
module_function :asinh!
[416] Fix | Delete
module_function :asinh
[417] Fix | Delete
module_function :acosh!
[418] Fix | Delete
module_function :acosh
[419] Fix | Delete
module_function :atanh!
[420] Fix | Delete
module_function :atanh
[421] Fix | Delete
[422] Fix | Delete
module_function :frexp
[423] Fix | Delete
module_function :ldexp
[424] Fix | Delete
module_function :hypot
[425] Fix | Delete
module_function :erf
[426] Fix | Delete
module_function :erfc
[427] Fix | Delete
module_function :gamma
[428] Fix | Delete
module_function :lgamma
[429] Fix | Delete
[430] Fix | Delete
private
[431] Fix | Delete
def handle_no_method_error # :nodoc:
[432] Fix | Delete
if $!.name == :real?
[433] Fix | Delete
raise TypeError, "Numeric Number required"
[434] Fix | Delete
else
[435] Fix | Delete
raise
[436] Fix | Delete
end
[437] Fix | Delete
end
[438] Fix | Delete
module_function :handle_no_method_error
[439] Fix | Delete
[440] Fix | Delete
end
[441] Fix | Delete
[442] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function