CMath
CMath is a library that provides trigonometric and transcendental functions for complex numbers.
Usage
To start using this library, simply:
require "cmath"
Square root of a negative number is a complex number.
CMath.sqrt(-9) #=> 0+3.0i
Methods
- A
- C
- E
- L
- S
- T
Included Modules
Class Public methods
atan2(y,x)
Link
returns the arc tangent of y
divided by x
using
the signs of y
and x
to determine the quadrant
Also aliased as: atan2!
exp(z)
Link
Math::E raised to the z
power
exp(Complex(0,0)) #=> 1.0+0.0i
exp(Complex(0,PI)) #=> -1.0+1.2246467991473532e-16i
exp(Complex(0,PI/2.0)) #=> 6.123233995736766e-17+1.0i
Also aliased as: exp!
log(*args)
Link
Returns the natural logarithm of Complex. If a second argument is given, it will be the base of logarithm.
log(Complex(0,0)) #=> -Infinity+0.0i
Also aliased as: log!
# File lib/cmath.rb, line 71 def log(*args) begin z, b = args unless b.nil? || b.kind_of?(Numeric) raise TypeError, "Numeric Number required" end if z.real? and z >= 0 and (b.nil? or b >= 0) log!(*args) else a = Complex(log!(z.abs), z.arg) if b a /= log(b) end a end rescue NoMethodError handle_no_method_error end end
sqrt(z)
Link
Returns the non-negative square root of Complex.
sqrt(-1) #=> 0+1.0i
sqrt(Complex(-1,0)) #=> 0.0+1.0i
sqrt(Complex(0,8)) #=> 2.0+2.0i
Also aliased as: sqrt!
# File lib/cmath.rb, line 124 def sqrt(z) begin if z.real? if z < 0 Complex(0, sqrt!(-z)) else sqrt!(z) end else if z.imag < 0 || (z.imag == 0 && z.imag.to_s[0] == '-') sqrt(z.conjugate).conjugate else r = z.abs x = z.real Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0)) end end rescue NoMethodError handle_no_method_error end end