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 ( z , b = ::Math ::E )
70
82
begin
71
83
if z . real? && z >= 0 && b >= 0
@@ -80,6 +92,8 @@ def log(z, b=::Math::E)
80
92
81
93
##
82
94
# returns the base 2 logarithm of +z+
95
+ #
96
+ # CMath.log2(-1) => (0.0+4.532360141827194i)
83
97
def log2 ( z )
84
98
begin
85
99
if z . real? and z >= 0
@@ -94,6 +108,8 @@ def log2(z)
94
108
95
109
##
96
110
# returns the base 10 logarithm of +z+
111
+ #
112
+ # CMath.log10(-1) #=> (0.0+1.3643763538418412i)
97
113
def log10 ( z )
98
114
begin
99
115
if z . real? and z >= 0
@@ -108,9 +124,8 @@ def log10(z)
108
124
109
125
##
110
126
# Returns the non-negative square root of Complex.
111
- # sqrt(-1) #=> 0+1.0i
112
- # sqrt(Complex(-1,0)) #=> 0.0+1.0i
113
- # sqrt(Complex(0,8)) #=> 2.0+2.0i
127
+ #
128
+ # CMath.sqrt(-1 + 0i) #=> 0.0+1.0i
114
129
def sqrt ( z )
115
130
begin
116
131
if z . real?
@@ -136,12 +151,16 @@ def sqrt(z)
136
151
137
152
##
138
153
# returns the principal value of the cube root of +z+
154
+ #
155
+ # CMath.cbrt(1 + 4i) #=> (1.449461632813119+0.6858152562177092i)
139
156
def cbrt ( z )
140
157
z ** ( 1.0 /3 )
141
158
end
142
159
143
160
##
144
161
# returns the sine of +z+, where +z+ is given in radians
162
+ #
163
+ # CMath.sin(1 + 1i) #=> (1.2984575814159773+0.6349639147847361i)
145
164
def sin ( z )
146
165
begin
147
166
if z . real?
@@ -157,6 +176,8 @@ def sin(z)
157
176
158
177
##
159
178
# returns the cosine of +z+, where +z+ is given in radians
179
+ #
180
+ # CMath.cos(1 + 1i) #=> (0.8337300251311491-0.9888977057628651i)
160
181
def cos ( z )
161
182
begin
162
183
if z . real?
@@ -172,6 +193,8 @@ def cos(z)
172
193
173
194
##
174
195
# returns the tangent of +z+, where +z+ is given in radians
196
+ #
197
+ # CMath.tan(1 + 1i) #=> (0.27175258531951174+1.0839233273386943i)
175
198
def tan ( z )
176
199
begin
177
200
if z . real?
@@ -186,6 +209,8 @@ def tan(z)
186
209
187
210
##
188
211
# returns the hyperbolic sine of +z+, where +z+ is given in radians
212
+ #
213
+ # CMath.sinh(1 + 1i) #=> (0.6349639147847361+1.2984575814159773i)
189
214
def sinh ( z )
190
215
begin
191
216
if z . real?
@@ -201,6 +226,8 @@ def sinh(z)
201
226
202
227
##
203
228
# returns the hyperbolic cosine of +z+, where +z+ is given in radians
229
+ #
230
+ # CMath.cosh(1 + 1i) #=> (0.8337300251311491+0.9888977057628651i)
204
231
def cosh ( z )
205
232
begin
206
233
if z . real?
@@ -216,6 +243,8 @@ def cosh(z)
216
243
217
244
##
218
245
# returns the hyperbolic tangent of +z+, where +z+ is given in radians
246
+ #
247
+ # CMath.tanh(1 + 1i) #=> (1.0839233273386943+0.27175258531951174i)
219
248
def tanh ( z )
220
249
begin
221
250
if z . real?
@@ -230,6 +259,8 @@ def tanh(z)
230
259
231
260
##
232
261
# returns the arc sine of +z+
262
+ #
263
+ # CMath.asin(1 + 1i) #=> (0.6662394324925153+1.0612750619050355i)
233
264
def asin ( z )
234
265
begin
235
266
if z . real? and z >= -1 and z <= 1
@@ -244,6 +275,8 @@ def asin(z)
244
275
245
276
##
246
277
# returns the arc cosine of +z+
278
+ #
279
+ # CMath.acos(1 + 1i) #=> (0.9045568943023813-1.0612750619050357i)
247
280
def acos ( z )
248
281
begin
249
282
if z . real? and z >= -1 and z <= 1
@@ -258,6 +291,8 @@ def acos(z)
258
291
259
292
##
260
293
# returns the arc tangent of +z+
294
+ #
295
+ # CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
261
296
def atan ( z )
262
297
begin
263
298
if z . real?
@@ -273,6 +308,8 @@ def atan(z)
273
308
##
274
309
# returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
275
310
# +x+ to determine the quadrant
311
+ #
312
+ # CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
276
313
def atan2 ( y , x )
277
314
begin
278
315
if y . real? and x . real?
@@ -287,6 +324,8 @@ def atan2(y,x)
287
324
288
325
##
289
326
# returns the inverse hyperbolic sine of +z+
327
+ #
328
+ # CMath.asinh(1 + 1i) #=> (1.0612750619050357+0.6662394324925153i)
290
329
def asinh ( z )
291
330
begin
292
331
if z . real?
@@ -301,6 +340,8 @@ def asinh(z)
301
340
302
341
##
303
342
# returns the inverse hyperbolic cosine of +z+
343
+ #
344
+ # CMath.acosh(1 + 1i) #=> (1.0612750619050357+0.9045568943023813i)
304
345
def acosh ( z )
305
346
begin
306
347
if z . real? and z >= 1
@@ -315,6 +356,8 @@ def acosh(z)
315
356
316
357
##
317
358
# returns the inverse hyperbolic tangent of +z+
359
+ #
360
+ # CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
318
361
def atanh ( z )
319
362
begin
320
363
if z . real? and z >= -1 and z <= 1
@@ -388,4 +431,4 @@ def handle_no_method_error # :nodoc:
388
431
end
389
432
module_function :handle_no_method_error
390
433
391
- end
434
+ end
0 commit comments