Project

General

Profile

Feature #10974 » remove_methods_from_cmath.patch

gogotanaka (Kazuki Tanaka), 03/15/2015 08:27 AM

View differences:

lib/cmath.rb
include Math
alias exp! exp
alias log! log
alias log2! log2
alias log10! log10
alias sqrt! sqrt
alias cbrt! cbrt
alias sin! sin
alias cos! cos
alias tan! tan
alias sinh! sinh
alias cosh! cosh
alias tanh! tanh
alias asin! asin
alias acos! acos
alias atan! atan
alias atan2! atan2
alias asinh! asinh
alias acosh! acosh
alias atanh! atanh
##
# Math::E raised to the +z+ power
#
......
def exp(z)
begin
if z.real?
exp!(z)
Math.exp(z)
else
ere = exp!(z.real)
Complex(ere * cos!(z.imag),
ere * sin!(z.imag))
ere = Math.exp(z.real)
Complex(ere * Math.cos(z.imag),
ere * Math.sin(z.imag))
end
rescue NoMethodError
handle_no_method_error
......
def log(z, b=::Math::E)
begin
if z.real? && z >= 0 && b >= 0
log!(z, b)
Math.log(z, b)
else
Complex(log!(z.abs), z.arg) / log(b)
Complex(Math.log(z.abs), z.arg) / log(b)
end
rescue NoMethodError
handle_no_method_error
......
def log2(z)
begin
if z.real? and z >= 0
log2!(z)
Math.log2(z)
else
log(z) / log!(2)
log(z) / Math.log(2)
end
rescue NoMethodError
handle_no_method_error
......
def log10(z)
begin
if z.real? and z >= 0
log10!(z)
Math.log10(z)
else
log(z) / log!(10)
log(z) / Math.log(10)
end
rescue NoMethodError
handle_no_method_error
......
begin
if z.real?
if z < 0
Complex(0, sqrt!(-z))
Complex(0, Math.sqrt(-z))
else
sqrt!(z)
Math.sqrt(z)
end
else
if z.imag < 0 ||
......
else
r = z.abs
x = z.real
Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0))
Complex(Math.sqrt((r + x) / 2.0), Math.sqrt((r - x) / 2.0))
end
end
rescue NoMethodError
......
def sin(z)
begin
if z.real?
sin!(z)
Math.sin(z)
else
Complex(sin!(z.real) * cosh!(z.imag),
cos!(z.real) * sinh!(z.imag))
Complex(Math.sin(z.real) * Math.cosh(z.imag),
Math.cos(z.real) * Math.sinh(z.imag))
end
rescue NoMethodError
handle_no_method_error
......
def cos(z)
begin
if z.real?
cos!(z)
Math.cos(z)
else
Complex(cos!(z.real) * cosh!(z.imag),
-sin!(z.real) * sinh!(z.imag))
Complex(Math.cos(z.real) * Math.cosh(z.imag),
-Math.sin(z.real) * Math.sinh(z.imag))
end
rescue NoMethodError
handle_no_method_error
......
def tan(z)
begin
if z.real?
tan!(z)
Math.tan(z)
else
sin(z) / cos(z)
end
......
def sinh(z)
begin
if z.real?
sinh!(z)
Math.sinh(z)
else
Complex(sinh!(z.real) * cos!(z.imag),
cosh!(z.real) * sin!(z.imag))
Complex(Math.sinh(z.real) * Math.cos(z.imag),
Math.cosh(z.real) * Math.sin(z.imag))
end
rescue NoMethodError
handle_no_method_error
......
def cosh(z)
begin
if z.real?
cosh!(z)
Math.cosh(z)
else
Complex(cosh!(z.real) * cos!(z.imag),
sinh!(z.real) * sin!(z.imag))
Complex(Math.cosh(z.real) * Math.cos(z.imag),
Math.sinh(z.real) * Math.sin(z.imag))
end
rescue NoMethodError
handle_no_method_error
......
def tanh(z)
begin
if z.real?
tanh!(z)
Math.tanh(z)
else
sinh(z) / cosh(z)
end
......
def asin(z)
begin
if z.real? and z >= -1 and z <= 1
asin!(z)
Math.asin(z)
else
(-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
end
......
def acos(z)
begin
if z.real? and z >= -1 and z <= 1
acos!(z)
Math.acos(z)
else
(-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
end
......
def atan(z)
begin
if z.real?
atan!(z)
Math.atan(z)
else
1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
end
......
def atan2(y,x)
begin
if y.real? and x.real?
atan2!(y,x)
Math.atan2(y,x)
else
(-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
end
......
def asinh(z)
begin
if z.real?
asinh!(z)
Math.asinh(z)
else
log(z + sqrt(1.0 + z * z))
end
......
def acosh(z)
begin
if z.real? and z >= 1
acosh!(z)
Math.acosh(z)
else
log(z + sqrt(z * z - 1.0))
end
......
def atanh(z)
begin
if z.real? and z >= -1 and z <= 1
atanh!(z)
Math.atanh(z)
else
log((1.0 + z) / (1.0 - z)) / 2.0
end
......
end
end
module_function :exp!
module_function :exp
module_function :log!
module_function :log
module_function :log2!
module_function :log2
module_function :log10!
module_function :log10
module_function :sqrt!
module_function :sqrt
module_function :cbrt!
module_function :cbrt
module_function :sin!
module_function :sin
module_function :cos!
module_function :cos
module_function :tan!
module_function :tan
module_function :sinh!
module_function :sinh
module_function :cosh!
module_function :cosh
module_function :tanh!
module_function :tanh
module_function :asin!
module_function :asin
module_function :acos!
module_function :acos
module_function :atan!
module_function :atan
module_function :atan2!
module_function :atan2
module_function :asinh!
module_function :asinh
module_function :acosh!
module_function :acosh
module_function :atanh!
module_function :atanh
module_function :frexp
-
test/ruby/test_complex.rb
assert_in_delta(1.092, c.real, 0.001)
assert_in_delta(-0.420, c.imag, 0.001)
c = CMath.log2(Complex(1, 2))
assert_in_delta(1.160, c.real, 0.001)
assert_in_delta(1.597, c.imag, 0.001)
c = CMath.log10(Complex(1, 2))
assert_in_delta(0.349, c.real, 0.001)
assert_in_delta(0.480, c.imag, 0.001)
-
(1-1/2)