Module: CMath

Includes:
Math
Defined in:
lib/cmath.rb

Overview

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

Class Method Details

.acos(z) ⇒ Object

returns the arc cosine of z


255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cmath.rb', line 255

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


36
# File 'lib/cmath.rb', line 36

alias acos! acos

.acosh(z) ⇒ Object

returns the inverse hyperbolic cosine of z


312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cmath.rb', line 312

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


41
# File 'lib/cmath.rb', line 41

alias acosh! acosh

.asin(z) ⇒ Object

returns the arc sine of z


241
242
243
244
245
246
247
248
249
250
251
# File 'lib/cmath.rb', line 241

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


35
# File 'lib/cmath.rb', line 35

alias asin! asin

.asinh(z) ⇒ Object

returns the inverse hyperbolic sine of z


298
299
300
301
302
303
304
305
306
307
308
# File 'lib/cmath.rb', line 298

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


40
# File 'lib/cmath.rb', line 40

alias asinh! asinh

.atan(z) ⇒ Object

returns the arc tangent of z


269
270
271
272
273
274
275
276
277
278
279
# File 'lib/cmath.rb', line 269

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


37
# File 'lib/cmath.rb', line 37

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


284
285
286
287
288
289
290
291
292
293
294
# File 'lib/cmath.rb', line 284

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


38
# File 'lib/cmath.rb', line 38

alias atan2! atan2

.atanh(z) ⇒ Object

returns the inverse hyperbolic tangent of z


326
327
328
329
330
331
332
333
334
335
336
# File 'lib/cmath.rb', line 326

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


42
# File 'lib/cmath.rb', line 42

alias atanh! atanh

.cbrt(z) ⇒ Object

returns the principal value of the cube root of z


147
148
149
# File 'lib/cmath.rb', line 147

def cbrt(z)
  z ** (1.0/3)
end

.cbrt!Object


25
# File 'lib/cmath.rb', line 25

alias cbrt! cbrt

.cos(z) ⇒ Object

returns the cosine of z, where z is given in radians


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cmath.rb', line 168

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


28
# File 'lib/cmath.rb', line 28

alias cos! cos

.cosh(z) ⇒ Object

returns the hyperbolic cosine of z, where z is given in radians


212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cmath.rb', line 212

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


32
# File 'lib/cmath.rb', line 32

alias cosh! cosh

.erfObject

.erfcObject

.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

50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cmath.rb', line 50

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


20
# File 'lib/cmath.rb', line 20

alias exp! exp

.frexpObject

.gammaObject

.handle_no_method_errorObject

:nodoc:


390
391
392
393
394
395
396
# File 'lib/cmath.rb', line 390

def handle_no_method_error # :nodoc:
  if $!.name == :real?
    raise TypeError, "Numeric Number required"
  else
    raise
  end
end

.hypotObject

.ldexpObject

.lgammaObject

.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

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cmath.rb', line 69

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


21
# File 'lib/cmath.rb', line 21

alias log! log

.log10(z) ⇒ Object

returns the base 10 logarithm of z


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cmath.rb', line 105

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


23
# File 'lib/cmath.rb', line 23

alias log10! log10

.log2(z) ⇒ Object

returns the base 2 logarithm of z


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cmath.rb', line 91

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


22
# File 'lib/cmath.rb', line 22

alias log2! log2

.sin(z) ⇒ Object

returns the sine of z, where z is given in radians


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cmath.rb', line 153

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


27
# File 'lib/cmath.rb', line 27

alias sin! sin

.sinh(z) ⇒ Object

returns the hyperbolic sine of z, where z is given in radians


197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cmath.rb', line 197

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


31
# File 'lib/cmath.rb', line 31

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

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cmath.rb', line 122

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


24
# File 'lib/cmath.rb', line 24

alias sqrt! sqrt

.tan(z) ⇒ Object

returns the tangent of z, where z is given in radians


183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cmath.rb', line 183

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


29
# File 'lib/cmath.rb', line 29

alias tan! tan

.tanh(z) ⇒ Object

returns the hyperbolic tangent of z, where z is given in radians


227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cmath.rb', line 227

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


33
# File 'lib/cmath.rb', line 33

alias tanh! tanh