Module: CMath
- Includes:
- Math
- Defined in:
- lib/cmath.rb
Overview
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
Class Method Summary collapse
-
.acos(z) ⇒ Object
returns the arc cosine of
z
. - .acos! ⇒ Object
-
.acosh(z) ⇒ Object
returns the inverse hyperbolic cosine of
z
. - .acosh! ⇒ Object
-
.asin(z) ⇒ Object
returns the arc sine of
z
. - .asin! ⇒ Object
-
.asinh(z) ⇒ Object
returns the inverse hyperbolic sine of
z
. - .asinh! ⇒ Object
-
.atan(z) ⇒ Object
returns the arc tangent of
z
. - .atan! ⇒ Object
-
.atan2(y, x) ⇒ Object
returns the arc tangent of
y
divided byx
using the signs ofy
andx
to determine the quadrant. - .atan2! ⇒ Object
-
.atanh(z) ⇒ Object
returns the inverse hyperbolic tangent of
z
. - .atanh! ⇒ Object
-
.cbrt(z) ⇒ Object
returns the principal value of the cube root of
z
. - .cbrt! ⇒ Object
-
.cos(z) ⇒ Object
returns the cosine of
z
, wherez
is given in radians. - .cos! ⇒ Object
-
.cosh(z) ⇒ Object
returns the hyperbolic cosine of
z
, wherez
is given in radians. - .cosh! ⇒ Object
- .erf ⇒ Object
- .erfc ⇒ Object
-
.exp(z) ⇒ Object
Math::E raised to the
z
power. - .exp! ⇒ Object
- .frexp ⇒ Object
- .gamma ⇒ Object
-
.handle_no_method_error ⇒ Object
:nodoc:.
- .hypot ⇒ Object
- .ldexp ⇒ Object
- .lgamma ⇒ Object
-
.log(*args) ⇒ Object
Returns the natural logarithm of Complex.
- .log! ⇒ Object
-
.log10(z) ⇒ Object
returns the base 10 logarithm of
z
. - .log10! ⇒ Object
-
.log2(z) ⇒ Object
returns the base 2 logarithm of
z
. - .log2! ⇒ Object
-
.sin(z) ⇒ Object
returns the sine of
z
, wherez
is given in radians. - .sin! ⇒ Object
-
.sinh(z) ⇒ Object
returns the hyperbolic sine of
z
, wherez
is given in radians. - .sinh! ⇒ Object
-
.sqrt(z) ⇒ Object
Returns the non-negative square root of Complex.
- .sqrt! ⇒ Object
-
.tan(z) ⇒ Object
returns the tangent of
z
, wherez
is given in radians. - .tan! ⇒ Object
-
.tanh(z) ⇒ Object
returns the hyperbolic tangent of
z
, wherez
is given in radians. - .tanh! ⇒ Object
Class Method Details
.acos(z) ⇒ Object
returns the arc cosine of z
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cmath.rb', line 257 def acos(z) begin if z.real? and z >= -1 and z <= 1 acos!(z) else (-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z)) end rescue NoMethodError handle_no_method_error end end |
.acos! ⇒ Object
38 |
# File 'lib/cmath.rb', line 38 alias acos! acos |
.acosh(z) ⇒ Object
returns the inverse hyperbolic cosine of z
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/cmath.rb', line 314 def acosh(z) begin if z.real? and z >= 1 acosh!(z) else log(z + sqrt(z * z - 1.0)) end rescue NoMethodError handle_no_method_error end end |
.acosh! ⇒ Object
43 |
# File 'lib/cmath.rb', line 43 alias acosh! acosh |
.asin(z) ⇒ Object
returns the arc sine of z
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/cmath.rb', line 243 def asin(z) begin if z.real? and z >= -1 and z <= 1 asin!(z) else (-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z)) end rescue NoMethodError handle_no_method_error end end |
.asin! ⇒ Object
37 |
# File 'lib/cmath.rb', line 37 alias asin! asin |
.asinh(z) ⇒ Object
returns the inverse hyperbolic sine of z
300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/cmath.rb', line 300 def asinh(z) begin if z.real? asinh!(z) else log(z + sqrt(1.0 + z * z)) end rescue NoMethodError handle_no_method_error end end |
.asinh! ⇒ Object
42 |
# File 'lib/cmath.rb', line 42 alias asinh! asinh |
.atan(z) ⇒ Object
returns the arc tangent of z
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/cmath.rb', line 271 def atan(z) begin if z.real? atan!(z) else 1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0 end rescue NoMethodError handle_no_method_error end end |
.atan! ⇒ Object
39 |
# File 'lib/cmath.rb', line 39 alias atan! atan |
.atan2(y, x) ⇒ Object
returns the arc tangent of y
divided by x
using the signs of y
and x
to determine the quadrant
286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/cmath.rb', line 286 def atan2(y,x) begin if y.real? and x.real? atan2!(y,x) else (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y)) end rescue NoMethodError handle_no_method_error end end |
.atan2! ⇒ Object
40 |
# File 'lib/cmath.rb', line 40 alias atan2! atan2 |
.atanh(z) ⇒ Object
returns the inverse hyperbolic tangent of z
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/cmath.rb', line 328 def atanh(z) begin if z.real? and z >= -1 and z <= 1 atanh!(z) else log((1.0 + z) / (1.0 - z)) / 2.0 end rescue NoMethodError handle_no_method_error end end |
.atanh! ⇒ Object
44 |
# File 'lib/cmath.rb', line 44 alias atanh! atanh |
.cbrt(z) ⇒ Object
returns the principal value of the cube root of z
149 150 151 |
# File 'lib/cmath.rb', line 149 def cbrt(z) z ** (1.0/3) end |
.cbrt! ⇒ Object
27 |
# File 'lib/cmath.rb', line 27 alias cbrt! cbrt |
.cos(z) ⇒ Object
returns the cosine of z
, where z
is given in radians
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/cmath.rb', line 170 def cos(z) begin if z.real? cos!(z) else Complex(cos!(z.real) * cosh!(z.imag), -sin!(z.real) * sinh!(z.imag)) end rescue NoMethodError handle_no_method_error end end |
.cos! ⇒ Object
30 |
# File 'lib/cmath.rb', line 30 alias cos! cos |
.cosh(z) ⇒ Object
returns the hyperbolic cosine of z
, where z
is given in radians
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/cmath.rb', line 214 def cosh(z) begin if z.real? cosh!(z) else Complex(cosh!(z.real) * cos!(z.imag), sinh!(z.real) * sin!(z.imag)) end rescue NoMethodError handle_no_method_error end end |
.cosh! ⇒ Object
34 |
# File 'lib/cmath.rb', line 34 alias cosh! cosh |
.erf ⇒ Object
.erfc ⇒ Object
.exp(z) ⇒ Object
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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cmath.rb', line 52 def exp(z) begin if z.real? exp!(z) else ere = exp!(z.real) Complex(ere * cos!(z.imag), ere * sin!(z.imag)) end rescue NoMethodError handle_no_method_error end end |
.exp! ⇒ Object
22 |
# File 'lib/cmath.rb', line 22 alias exp! exp |
.frexp ⇒ Object
.gamma ⇒ Object
.handle_no_method_error ⇒ Object
:nodoc:
392 393 394 395 396 397 398 |
# File 'lib/cmath.rb', line 392 def handle_no_method_error # :nodoc: if $!.name == :real? raise TypeError, "Numeric Number required" else raise end end |
.hypot ⇒ Object
.ldexp ⇒ Object
.lgamma ⇒ Object
.log(*args) ⇒ Object
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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 |
.log! ⇒ Object
23 |
# File 'lib/cmath.rb', line 23 alias log! log |
.log10(z) ⇒ Object
returns the base 10 logarithm of z
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cmath.rb', line 107 def log10(z) begin if z.real? and z >= 0 log10!(z) else log(z) / log!(10) end rescue NoMethodError handle_no_method_error end end |
.log10! ⇒ Object
25 |
# File 'lib/cmath.rb', line 25 alias log10! log10 |
.log2(z) ⇒ Object
returns the base 2 logarithm of z
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cmath.rb', line 93 def log2(z) begin if z.real? and z >= 0 log2!(z) else log(z) / log!(2) end rescue NoMethodError handle_no_method_error end end |
.log2! ⇒ Object
24 |
# File 'lib/cmath.rb', line 24 alias log2! log2 |
.sin(z) ⇒ Object
returns the sine of z
, where z
is given in radians
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cmath.rb', line 155 def sin(z) begin if z.real? sin!(z) else Complex(sin!(z.real) * cosh!(z.imag), cos!(z.real) * sinh!(z.imag)) end rescue NoMethodError handle_no_method_error end end |
.sin! ⇒ Object
29 |
# File 'lib/cmath.rb', line 29 alias sin! sin |
.sinh(z) ⇒ Object
returns the hyperbolic sine of z
, where z
is given in radians
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/cmath.rb', line 199 def sinh(z) begin if z.real? sinh!(z) else Complex(sinh!(z.real) * cos!(z.imag), cosh!(z.real) * sin!(z.imag)) end rescue NoMethodError handle_no_method_error end end |
.sinh! ⇒ Object
33 |
# File 'lib/cmath.rb', line 33 alias sinh! sinh |
.sqrt(z) ⇒ Object
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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# 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 |
.sqrt! ⇒ Object
26 |
# File 'lib/cmath.rb', line 26 alias sqrt! sqrt |
.tan(z) ⇒ Object
returns the tangent of z
, where z
is given in radians
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/cmath.rb', line 185 def tan(z) begin if z.real? tan!(z) else sin(z) / cos(z) end rescue NoMethodError handle_no_method_error end end |
.tan! ⇒ Object
31 |
# File 'lib/cmath.rb', line 31 alias tan! tan |
.tanh(z) ⇒ Object
returns the hyperbolic tangent of z
, where z
is given in radians
229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/cmath.rb', line 229 def tanh(z) begin if z.real? tanh!(z) else sinh(z) / cosh(z) end rescue NoMethodError handle_no_method_error end end |
.tanh! ⇒ Object
35 |
# File 'lib/cmath.rb', line 35 alias tanh! tanh |