1
1
##
2
+ # = Trigonometric and transcendental functions for complex numbers.
3
+ #
2
4
# CMath is a library that provides trigonometric and transcendental
3
- # functions for complex numbers.
5
+ # functions for complex numbers. The functions in this module accept
6
+ # integers, floating-point numbers or complex numbers as arguments.
7
+ #
8
+ # Note that the selection of functions is similar, but not identical,
9
+ # to that in module math. The reason for having two modules is that
10
+ # some users aren’t interested in complex numbers, and perhaps don’t
11
+ # even know what they are. They would rather have Math.sqrt(-1) raise
12
+ # an exception than return a complex number.
4
13
#
5
14
# == Usage
6
15
#
7
- # To start using this library, simply:
16
+ # To start using this library, simply require cmath library :
8
17
#
9
18
# require "cmath"
10
19
#
11
- # Square root of a negative number is a complex number.
20
+ # And after call any CMath function. For example:
21
+ #
22
+ # CMath.sqrt(-9) #=> 0+3.0i
23
+ # CMath.exp(0 + 0i) #=> 1.0+0.0i
24
+ # CMath.log10(-5.to_c) #=> (0.6989700043360187+1.3643763538418412i)
12
25
#
13
- # CMath.sqrt(-9) #=> 0+3.0i
14
26
#
27
+ # For more information you can see Complec class.
15
28
16
29
module CMath
17
30
@@ -44,9 +57,7 @@ module CMath
44
57
##
45
58
# Math::E raised to the +z+ power
46
59
#
47
- # exp(Complex(0,0)) #=> 1.0+0.0i
48
- # exp(Complex(0,PI)) #=> -1.0+1.2246467991473532e-16i
49
- # exp(Complex(0,PI/2.0)) #=> 6.123233995736766e-17+1.0i
60
+ # CMath.exp(2i) #=> (-0.4161468365471424+0.9092974268256817i)
50
61
def exp ( z )
51
62
begin
52
63
if z . real?
@@ -62,10 +73,11 @@ def exp(z)
62
73
end
63
74
64
75
##
65
- # Returns the natural logarithm of Complex. If a second argument is given,
76
+ # Returns the natural logarithm of Complex. If a second argument is given,
66
77
# it will be the base of logarithm.
67
78
#
68
- # log(Complex(0,0)) #=> -Infinity+0.0i
79
+ # CMath.log(1 + 4i) #=> (1.416606672028108+1.3258176636680326i)
80
+ # CMath.log(1 + 4i, 10) #=> (0.6152244606891369+0.5757952953408879i)
69
81
def log ( *args )
70
82
begin
71
83
z , b = args
@@ -88,6 +100,8 @@ def log(*args)
88
100
89
101
##
90
102
# returns the base 2 logarithm of +z+
103
+ #
104
+ # CMath.log2(-1) => (0.0+4.532360141827194i)
91
105
def log2 ( z )
92
106
begin
93
107
if z . real? and z >= 0
@@ -102,6 +116,8 @@ def log2(z)
102
116
103
117
##
104
118
# returns the base 10 logarithm of +z+
119
+ #
120
+ # CMath.log10(-1) #=> (0.0+1.3643763538418412i)
105
121
def log10 ( z )
106
122
begin
107
123
if z . real? and z >= 0
@@ -116,9 +132,8 @@ def log10(z)
116
132
117
133
##
118
134
# Returns the non-negative square root of Complex.
119
- # sqrt(-1) #=> 0+1.0i
120
- # sqrt(Complex(-1,0)) #=> 0.0+1.0i
121
- # sqrt(Complex(0,8)) #=> 2.0+2.0i
135
+ #
136
+ # CMath.sqrt(-1 + 0i) #=> 0.0+1.0i
122
137
def sqrt ( z )
123
138
begin
124
139
if z . real?
@@ -144,12 +159,16 @@ def sqrt(z)
144
159
145
160
##
146
161
# returns the principal value of the cube root of +z+
162
+ #
163
+ # CMath.cbrt(1 + 4i) #=> (1.449461632813119+0.6858152562177092i)
147
164
def cbrt ( z )
148
165
z ** ( 1.0 /3 )
149
166
end
150
167
151
168
##
152
169
# returns the sine of +z+, where +z+ is given in radians
170
+ #
171
+ # CMath.sin(1 + 1i) #=> (1.2984575814159773+0.6349639147847361i)
153
172
def sin ( z )
154
173
begin
155
174
if z . real?
@@ -165,6 +184,8 @@ def sin(z)
165
184
166
185
##
167
186
# returns the cosine of +z+, where +z+ is given in radians
187
+ #
188
+ # CMath.cos(1 + 1i) #=> (0.8337300251311491-0.9888977057628651i)
168
189
def cos ( z )
169
190
begin
170
191
if z . real?
@@ -180,6 +201,8 @@ def cos(z)
180
201
181
202
##
182
203
# returns the tangent of +z+, where +z+ is given in radians
204
+ #
205
+ # CMath.tan(1 + 1i) #=> (0.27175258531951174+1.0839233273386943i)
183
206
def tan ( z )
184
207
begin
185
208
if z . real?
@@ -194,6 +217,8 @@ def tan(z)
194
217
195
218
##
196
219
# returns the hyperbolic sine of +z+, where +z+ is given in radians
220
+ #
221
+ # CMath.sinh(1 + 1i) #=> (0.6349639147847361+1.2984575814159773i)
197
222
def sinh ( z )
198
223
begin
199
224
if z . real?
@@ -209,6 +234,8 @@ def sinh(z)
209
234
210
235
##
211
236
# returns the hyperbolic cosine of +z+, where +z+ is given in radians
237
+ #
238
+ # CMath.cosh(1 + 1i) #=> (0.8337300251311491+0.9888977057628651i)
212
239
def cosh ( z )
213
240
begin
214
241
if z . real?
@@ -224,6 +251,8 @@ def cosh(z)
224
251
225
252
##
226
253
# returns the hyperbolic tangent of +z+, where +z+ is given in radians
254
+ #
255
+ # CMath.tanh(1 + 1i) #=> (1.0839233273386943+0.27175258531951174i)
227
256
def tanh ( z )
228
257
begin
229
258
if z . real?
@@ -238,6 +267,8 @@ def tanh(z)
238
267
239
268
##
240
269
# returns the arc sine of +z+
270
+ #
271
+ # CMath.asin(1 + 1i) #=> (0.6662394324925153+1.0612750619050355i)
241
272
def asin ( z )
242
273
begin
243
274
if z . real? and z >= -1 and z <= 1
@@ -252,6 +283,8 @@ def asin(z)
252
283
253
284
##
254
285
# returns the arc cosine of +z+
286
+ #
287
+ # CMath.acos(1 + 1i) #=> (0.9045568943023813-1.0612750619050357i)
255
288
def acos ( z )
256
289
begin
257
290
if z . real? and z >= -1 and z <= 1
@@ -266,6 +299,8 @@ def acos(z)
266
299
267
300
##
268
301
# returns the arc tangent of +z+
302
+ #
303
+ # CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
269
304
def atan ( z )
270
305
begin
271
306
if z . real?
@@ -281,6 +316,8 @@ def atan(z)
281
316
##
282
317
# returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
283
318
# +x+ to determine the quadrant
319
+ #
320
+ # CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
284
321
def atan2 ( y , x )
285
322
begin
286
323
if y . real? and x . real?
@@ -295,6 +332,8 @@ def atan2(y,x)
295
332
296
333
##
297
334
# returns the inverse hyperbolic sine of +z+
335
+ #
336
+ # CMath.asinh(1 + 1i) #=> (1.0612750619050357+0.6662394324925153i)
298
337
def asinh ( z )
299
338
begin
300
339
if z . real?
@@ -309,6 +348,8 @@ def asinh(z)
309
348
310
349
##
311
350
# returns the inverse hyperbolic cosine of +z+
351
+ #
352
+ # CMath.acosh(1 + 1i) #=> (1.0612750619050357+0.9045568943023813i)
312
353
def acosh ( z )
313
354
begin
314
355
if z . real? and z >= 1
@@ -323,6 +364,8 @@ def acosh(z)
323
364
324
365
##
325
366
# returns the inverse hyperbolic tangent of +z+
367
+ #
368
+ # CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
326
369
def atanh ( z )
327
370
begin
328
371
if z . real? and z >= -1 and z <= 1
@@ -397,4 +440,3 @@ def handle_no_method_error # :nodoc:
397
440
module_function :handle_no_method_error
398
441
399
442
end
400
-
0 commit comments