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