1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
# complex.rb -
|
|
|
|
# $Release Version: 0.5 $
|
1999-01-19 23:59:39 -05:00
|
|
|
# $Revision: 1.3 $
|
|
|
|
# $Date: 1998/07/08 10:05:28 $
|
1998-01-16 07:13:05 -05:00
|
|
|
# by Keiju ISHITSUKA(SHL Japan Inc.)
|
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
# ----
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
# complex.rb implements the Complex class for complex numbers. Additionally,
|
|
|
|
# some methods in other Numeric classes are redefined or added to allow greater
|
|
|
|
# interoperability with Complex numbers.
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
# Complex numbers can be created in the following manner:
|
|
|
|
# - <tt>Complex(a, b)</tt>
|
|
|
|
# - <tt>Complex.polar(radius, theta)</tt>
|
|
|
|
#
|
|
|
|
# Additionally, note the following:
|
|
|
|
# - <tt>Complex::I</tt> (the mathematical constant <i>i</i>)
|
|
|
|
# - <tt>Numeric#im</tt> (e.g. <tt>5.im -> 0+5i</tt>)
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
# The following +Math+ module methods are redefined to handle Complex arguments.
|
|
|
|
# They will work as normal with non-Complex arguments.
|
2003-07-26 11:03:16 -04:00
|
|
|
# sqrt exp cos sin tan log log10
|
|
|
|
# cosh sinh tanh acos asin atan atan2 acosh asinh atanh
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
|
|
|
|
|
2003-07-31 04:42:44 -04:00
|
|
|
#
|
|
|
|
# Numeric is a built-in class on which Fixnum, Bignum, etc., are based. Here
|
|
|
|
# some methods are added so that all number types can be treated to some extent
|
|
|
|
# as Complex numbers.
|
|
|
|
#
|
|
|
|
class Numeric
|
|
|
|
#
|
|
|
|
# Returns a Complex number <tt>(0,<i>self</i>)</tt>.
|
|
|
|
#
|
|
|
|
def im
|
|
|
|
Complex(0, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The real part of a complex number, i.e. <i>self</i>.
|
|
|
|
#
|
|
|
|
def real
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The imaginary part of a complex number, i.e. 0.
|
|
|
|
#
|
|
|
|
def image
|
|
|
|
0
|
|
|
|
end
|
|
|
|
alias imag image
|
|
|
|
|
|
|
|
#
|
|
|
|
# See Complex#arg.
|
|
|
|
#
|
|
|
|
def arg
|
|
|
|
if self >= 0
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return Math::PI
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias angle arg
|
|
|
|
|
|
|
|
#
|
|
|
|
# See Complex#polar.
|
|
|
|
#
|
|
|
|
def polar
|
|
|
|
return abs, arg
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# See Complex#conjugate (short answer: returns <i>self</i>).
|
|
|
|
#
|
|
|
|
def conjugate
|
|
|
|
self
|
|
|
|
end
|
|
|
|
alias conj conjugate
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2003-02-03 22:47:42 -05:00
|
|
|
# Creates a Complex number. +a+ and +b+ should be Numeric. The result will be
|
|
|
|
# <tt>a+bi</tt>.
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
def Complex(a, b = 0)
|
2003-07-31 04:42:44 -04:00
|
|
|
if b == 0 and (a.kind_of?(Complex) or defined? Complex::Unify)
|
1998-01-16 07:13:05 -05:00
|
|
|
a
|
|
|
|
else
|
2003-07-31 04:42:44 -04:00
|
|
|
Complex.new( a.real-b.imag, a.imag+b.real )
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# The complex number class. See complex.rb for an overview.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
class Complex < Numeric
|
1999-01-19 23:59:39 -05:00
|
|
|
@RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
|
2002-04-25 09:57:01 -04:00
|
|
|
|
|
|
|
undef step
|
|
|
|
|
2005-12-07 03:41:59 -05:00
|
|
|
def scalar?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
def Complex.generic?(other) # :nodoc:
|
1998-01-16 07:13:05 -05:00
|
|
|
other.kind_of?(Integer) or
|
|
|
|
other.kind_of?(Float) or
|
|
|
|
(defined?(Rational) and other.kind_of?(Rational))
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle).
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def Complex.polar(r, theta)
|
|
|
|
Complex(r*Math.cos(theta), r*Math.sin(theta))
|
|
|
|
end
|
2003-04-23 02:31:29 -04:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Creates a +Complex+ number <tt>a</tt>+<tt>b</tt><i>i</i>.
|
|
|
|
#
|
2003-04-24 16:25:45 -04:00
|
|
|
def Complex.new!(a, b=0)
|
|
|
|
new(a,b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(a, b)
|
2003-07-29 03:52:55 -04:00
|
|
|
raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
|
|
|
|
raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
|
|
|
|
raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
|
|
|
|
raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
|
1998-01-16 07:13:05 -05:00
|
|
|
@real = a
|
|
|
|
@image = b
|
|
|
|
end
|
2003-07-29 03:52:55 -04:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Addition with real or complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def + (other)
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
re = @real + other.real
|
|
|
|
im = @image + other.image
|
|
|
|
Complex(re, im)
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
Complex(@real + other, @image)
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
x , y = other.coerce(self)
|
1998-01-16 07:13:05 -05:00
|
|
|
x + y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Subtraction with real or complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def - (other)
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
re = @real - other.real
|
|
|
|
im = @image - other.image
|
|
|
|
Complex(re, im)
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
Complex(@real - other, @image)
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
x , y = other.coerce(self)
|
1998-01-16 07:13:05 -05:00
|
|
|
x - y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Multiplication with real or complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def * (other)
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
re = @real*other.real - @image*other.image
|
|
|
|
im = @real*other.image + @image*other.real
|
|
|
|
Complex(re, im)
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
Complex(@real * other, @image * other)
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
x , y = other.coerce(self)
|
1998-01-16 07:13:05 -05:00
|
|
|
x * y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Division by real or complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def / (other)
|
|
|
|
if other.kind_of?(Complex)
|
2000-08-07 01:05:04 -04:00
|
|
|
self*other.conjugate/other.abs2
|
1998-01-16 07:13:05 -05:00
|
|
|
elsif Complex.generic?(other)
|
2000-08-07 01:05:04 -04:00
|
|
|
Complex(@real/other, @image/other)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2000-08-07 01:05:04 -04:00
|
|
|
x, y = other.coerce(self)
|
|
|
|
x/y
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Raise this complex number to the given (real or complex) power.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def ** (other)
|
|
|
|
if other == 0
|
|
|
|
return Complex(1)
|
|
|
|
end
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
r, theta = polar
|
|
|
|
ore = other.real
|
|
|
|
oim = other.image
|
|
|
|
nr = Math.exp!(ore*Math.log!(r) - oim * theta)
|
|
|
|
ntheta = theta*ore + oim*Math.log!(r)
|
|
|
|
Complex.polar(nr, ntheta)
|
|
|
|
elsif other.kind_of?(Integer)
|
|
|
|
if other > 0
|
|
|
|
x = self
|
|
|
|
z = x
|
|
|
|
n = other - 1
|
|
|
|
while n != 0
|
|
|
|
while (div, mod = n.divmod(2)
|
|
|
|
mod == 0)
|
|
|
|
x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
|
|
|
|
n = div
|
|
|
|
end
|
|
|
|
z *= x
|
|
|
|
n -= 1
|
|
|
|
end
|
|
|
|
z
|
|
|
|
else
|
|
|
|
if defined? Rational
|
|
|
|
(Rational(1) / self) ** -other
|
|
|
|
else
|
|
|
|
self ** Float(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
r, theta = polar
|
2003-07-26 11:03:16 -04:00
|
|
|
Complex.polar(r**other, theta*other)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2000-08-07 01:05:04 -04:00
|
|
|
x, y = other.coerce(self)
|
2003-07-22 04:42:47 -04:00
|
|
|
x**y
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Remainder after division by a real or complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def % (other)
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
Complex(@real % other.real, @image % other.image)
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
Complex(@real % other, @image % other)
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
x , y = other.coerce(self)
|
1998-01-16 07:13:05 -05:00
|
|
|
x % y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#--
|
2000-01-17 03:37:53 -05:00
|
|
|
# def divmod(other)
|
|
|
|
# if other.kind_of?(Complex)
|
|
|
|
# rdiv, rmod = @real.divmod(other.real)
|
|
|
|
# idiv, imod = @image.divmod(other.image)
|
|
|
|
# return Complex(rdiv, idiv), Complex(rmod, rmod)
|
|
|
|
# elsif Complex.generic?(other)
|
|
|
|
# Complex(@real.divmod(other), @image.divmod(other))
|
|
|
|
# else
|
|
|
|
# x , y = other.coerce(self)
|
|
|
|
# x.divmod(y)
|
|
|
|
# end
|
|
|
|
# end
|
2003-02-03 22:47:42 -05:00
|
|
|
#++
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Absolute value (aka modulus): distance from the zero point on the complex
|
|
|
|
# plane.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def abs
|
2003-07-22 04:42:47 -04:00
|
|
|
Math.hypot(@real, @image)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Square of the absolute value.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def abs2
|
|
|
|
@real*@real + @image*@image
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Argument (angle from (1,0) on the complex plane).
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def arg
|
2003-07-26 11:03:16 -04:00
|
|
|
Math.atan2!(@image, @real)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
alias angle arg
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Returns the absolute value _and_ the argument.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def polar
|
|
|
|
return abs, arg
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Complex conjugate (<tt>z + z.conjugate = 2 * z.real</tt>).
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def conjugate
|
|
|
|
Complex(@real, -@image)
|
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
alias conj conjugate
|
2005-11-29 23:50:04 -05:00
|
|
|
|
|
|
|
undef <=>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def == (other)
|
|
|
|
if other.kind_of?(Complex)
|
|
|
|
@real == other.real and @image == other.image
|
|
|
|
elsif Complex.generic?(other)
|
|
|
|
@real == other and @image == 0
|
|
|
|
else
|
2003-02-06 04:49:12 -05:00
|
|
|
other == self
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Attempts to coerce +other+ to a Complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def coerce(other)
|
|
|
|
if Complex.generic?(other)
|
2003-04-24 16:25:45 -04:00
|
|
|
return Complex.new!(other), self
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# FIXME
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def denominator
|
|
|
|
@real.denominator.lcm(@image.denominator)
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# FIXME
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def numerator
|
|
|
|
cd = denominator
|
|
|
|
Complex(@real.numerator*(cd/@real.denominator),
|
|
|
|
@image.numerator*(cd/@image.denominator))
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Standard string representation of the complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def to_s
|
|
|
|
if @real != 0
|
|
|
|
if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
|
|
|
|
if @image >= 0
|
|
|
|
@real.to_s+"+("+@image.to_s+")i"
|
|
|
|
else
|
|
|
|
@real.to_s+"-("+(-@image).to_s+")i"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if @image >= 0
|
|
|
|
@real.to_s+"+"+@image.to_s+"i"
|
|
|
|
else
|
|
|
|
@real.to_s+"-"+(-@image).to_s+"i"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
|
|
|
|
"("+@image.to_s+")i"
|
|
|
|
else
|
|
|
|
@image.to_s+"i"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Returns a hash code for the complex number.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def hash
|
2001-10-15 23:27:23 -04:00
|
|
|
@real.hash ^ @image.hash
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# Returns "<tt>Complex(<i>real</i>, <i>image</i>)</tt>".
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def inspect
|
|
|
|
sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
#
|
|
|
|
# +I+ is the imaginary number. It exists at point (0,1) on the complex plane.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
I = Complex(0,1)
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# The real part of a complex number.
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_reader :real
|
2003-02-03 22:47:42 -05:00
|
|
|
|
|
|
|
# The imaginary part of a complex number.
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_reader :image
|
2003-07-22 04:42:47 -04:00
|
|
|
alias imag image
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
module Math
|
|
|
|
alias sqrt! sqrt
|
|
|
|
alias exp! exp
|
2003-07-26 11:03:16 -04:00
|
|
|
alias log! log
|
|
|
|
alias log10! log10
|
1998-01-16 07:13:05 -05:00
|
|
|
alias cos! cos
|
|
|
|
alias sin! sin
|
|
|
|
alias tan! tan
|
2003-07-26 11:03:16 -04:00
|
|
|
alias cosh! cosh
|
|
|
|
alias sinh! sinh
|
|
|
|
alias tanh! tanh
|
|
|
|
alias acos! acos
|
|
|
|
alias asin! asin
|
|
|
|
alias atan! atan
|
1998-01-16 07:13:05 -05:00
|
|
|
alias atan2! atan2
|
2003-07-26 11:03:16 -04:00
|
|
|
alias acosh! acosh
|
|
|
|
alias asinh! asinh
|
|
|
|
alias atanh! atanh
|
2002-05-30 02:12:29 -04:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def sqrt(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
if z >= 0
|
|
|
|
sqrt!(z)
|
|
|
|
else
|
|
|
|
Complex(0,sqrt!(-z))
|
|
|
|
end
|
|
|
|
else
|
2003-07-22 04:42:47 -04:00
|
|
|
if z.image < 0
|
|
|
|
sqrt(z.conjugate).conjugate
|
2003-03-23 10:38:44 -05:00
|
|
|
else
|
2003-07-22 04:42:47 -04:00
|
|
|
r = z.abs
|
|
|
|
x = z.real
|
|
|
|
Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) )
|
2003-03-23 10:38:44 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def exp(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
exp!(z)
|
|
|
|
else
|
|
|
|
Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def cos(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
cos!(z)
|
|
|
|
else
|
|
|
|
Complex(cos!(z.real)*cosh!(z.image),
|
1999-01-19 23:59:39 -05:00
|
|
|
-sin!(z.real)*sinh!(z.image))
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def sin(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
sin!(z)
|
|
|
|
else
|
|
|
|
Complex(sin!(z.real)*cosh!(z.image),
|
1999-01-19 23:59:39 -05:00
|
|
|
cos!(z.real)*sinh!(z.image))
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def tan(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
tan!(z)
|
|
|
|
else
|
|
|
|
sin(z)/cos(z)
|
|
|
|
end
|
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
|
|
|
|
def sinh(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
sinh!(z)
|
|
|
|
else
|
|
|
|
Complex( sinh!(z.real)*cos!(z.image), cosh!(z.real)*sin!(z.image) )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cosh(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
cosh!(z)
|
|
|
|
else
|
|
|
|
Complex( cosh!(z.real)*cos!(z.image), sinh!(z.real)*sin!(z.image) )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tanh(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
tanh!(z)
|
|
|
|
else
|
|
|
|
sinh(z)/cosh(z)
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def log(z)
|
|
|
|
if Complex.generic?(z) and z >= 0
|
|
|
|
log!(z)
|
|
|
|
else
|
|
|
|
r, theta = z.polar
|
|
|
|
Complex(log!(r.abs), theta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-03 22:47:42 -05:00
|
|
|
# Redefined to handle a Complex argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
def log10(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
log10!(z)
|
|
|
|
else
|
|
|
|
log(z)/log!(10)
|
|
|
|
end
|
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
|
|
|
|
def acos(z)
|
2003-07-31 04:42:44 -04:00
|
|
|
if Complex.generic?(z) and z >= -1 and z <= 1
|
2003-07-26 11:03:16 -04:00
|
|
|
acos!(z)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2003-07-26 11:03:16 -04:00
|
|
|
-1.0.im * log( z + 1.0.im * sqrt(1.0-z*z) )
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
|
|
|
|
def asin(z)
|
2003-07-31 04:42:44 -04:00
|
|
|
if Complex.generic?(z) and z >= -1 and z <= 1
|
2003-07-26 11:03:16 -04:00
|
|
|
asin!(z)
|
|
|
|
else
|
|
|
|
-1.0.im * log( 1.0.im * z + sqrt(1.0-z*z) )
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def atan(z)
|
|
|
|
if Complex.generic?(z)
|
2003-07-26 11:03:16 -04:00
|
|
|
atan!(z)
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2003-07-26 11:03:16 -04:00
|
|
|
1.0.im * log( (1.0.im+z) / (1.0.im-z) ) / 2.0
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-07-26 11:03:16 -04:00
|
|
|
def atan2(y,x)
|
|
|
|
if Complex.generic?(y) and Complex.generic?(x)
|
|
|
|
atan2!(y,x)
|
|
|
|
else
|
|
|
|
-1.0.im * log( (x+1.0.im*y) / sqrt(x*x+y*y) )
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2003-07-26 11:03:16 -04:00
|
|
|
|
|
|
|
def acosh(z)
|
2003-07-31 04:42:44 -04:00
|
|
|
if Complex.generic?(z) and z >= 1
|
2003-07-26 11:03:16 -04:00
|
|
|
acosh!(z)
|
|
|
|
else
|
|
|
|
log( z + sqrt(z*z-1.0) )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def asinh(z)
|
|
|
|
if Complex.generic?(z)
|
|
|
|
asinh!(z)
|
|
|
|
else
|
|
|
|
log( z + sqrt(1.0+z*z) )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def atanh(z)
|
2003-07-31 04:42:44 -04:00
|
|
|
if Complex.generic?(z) and z >= -1 and z <= 1
|
2003-07-26 11:03:16 -04:00
|
|
|
atanh!(z)
|
|
|
|
else
|
|
|
|
log( (1.0+z) / (1.0-z) ) / 2.0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :sqrt!
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :sqrt
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :exp!
|
|
|
|
module_function :exp
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :log!
|
|
|
|
module_function :log
|
|
|
|
module_function :log10!
|
|
|
|
module_function :log10
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :cosh!
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :cosh
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :cos!
|
|
|
|
module_function :cos
|
|
|
|
module_function :sinh!
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :sinh
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :sin!
|
|
|
|
module_function :sin
|
|
|
|
module_function :tan!
|
|
|
|
module_function :tan
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :tanh!
|
|
|
|
module_function :tanh
|
|
|
|
module_function :acos!
|
|
|
|
module_function :acos
|
|
|
|
module_function :asin!
|
|
|
|
module_function :asin
|
|
|
|
module_function :atan!
|
|
|
|
module_function :atan
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :atan2!
|
|
|
|
module_function :atan2
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :acosh!
|
|
|
|
module_function :acosh
|
|
|
|
module_function :asinh!
|
|
|
|
module_function :asinh
|
1998-01-16 07:13:05 -05:00
|
|
|
module_function :atanh!
|
2003-07-26 11:03:16 -04:00
|
|
|
module_function :atanh
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
end
|
2003-02-03 22:47:42 -05:00
|
|
|
|
|
|
|
# Documentation comments:
|
|
|
|
# - source: original (researched from pickaxe)
|
|
|
|
# - a couple of fixme's
|
|
|
|
# - RDoc output for Bignum etc. is a bit short, with nothing but an
|
|
|
|
# (undocumented) alias. No big deal.
|