11##
2+ # = Trigonometric and transcendental functions for complex numbers.
3+ #
24# 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.
413#
514# == Usage
615#
7- # To start using this library, simply:
16+ # To start using this library, simply require cmath library :
817#
918# require "cmath"
1019#
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)
1225#
13- # CMath.sqrt(-9) #=> 0+3.0i
1426#
27+ # For more information you can see Complec class.
1528
1629module CMath
1730
@@ -44,9 +57,7 @@ module CMath
4457 ##
4558 # Math::E raised to the +z+ power
4659 #
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)
5061 def exp ( z )
5162 begin
5263 if z . real?
@@ -62,10 +73,11 @@ def exp(z)
6273 end
6374
6475 ##
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,
6677 # it will be the base of logarithm.
6778 #
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)
6981 def log ( *args )
7082 begin
7183 z , b = args
@@ -88,6 +100,8 @@ def log(*args)
88100
89101 ##
90102 # returns the base 2 logarithm of +z+
103+ #
104+ # CMath.log2(-1) => (0.0+4.532360141827194i)
91105 def log2 ( z )
92106 begin
93107 if z . real? and z >= 0
@@ -102,6 +116,8 @@ def log2(z)
102116
103117 ##
104118 # returns the base 10 logarithm of +z+
119+ #
120+ # CMath.log10(-1) #=> (0.0+1.3643763538418412i)
105121 def log10 ( z )
106122 begin
107123 if z . real? and z >= 0
@@ -116,9 +132,8 @@ def log10(z)
116132
117133 ##
118134 # 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
122137 def sqrt ( z )
123138 begin
124139 if z . real?
@@ -144,12 +159,16 @@ def sqrt(z)
144159
145160 ##
146161 # returns the principal value of the cube root of +z+
162+ #
163+ # CMath.cbrt(1 + 4i) #=> (1.449461632813119+0.6858152562177092i)
147164 def cbrt ( z )
148165 z ** ( 1.0 /3 )
149166 end
150167
151168 ##
152169 # returns the sine of +z+, where +z+ is given in radians
170+ #
171+ # CMath.sin(1 + 1i) #=> (1.2984575814159773+0.6349639147847361i)
153172 def sin ( z )
154173 begin
155174 if z . real?
@@ -165,6 +184,8 @@ def sin(z)
165184
166185 ##
167186 # returns the cosine of +z+, where +z+ is given in radians
187+ #
188+ # CMath.cos(1 + 1i) #=> (0.8337300251311491-0.9888977057628651i)
168189 def cos ( z )
169190 begin
170191 if z . real?
@@ -180,6 +201,8 @@ def cos(z)
180201
181202 ##
182203 # returns the tangent of +z+, where +z+ is given in radians
204+ #
205+ # CMath.tan(1 + 1i) #=> (0.27175258531951174+1.0839233273386943i)
183206 def tan ( z )
184207 begin
185208 if z . real?
@@ -194,6 +217,8 @@ def tan(z)
194217
195218 ##
196219 # returns the hyperbolic sine of +z+, where +z+ is given in radians
220+ #
221+ # CMath.sinh(1 + 1i) #=> (0.6349639147847361+1.2984575814159773i)
197222 def sinh ( z )
198223 begin
199224 if z . real?
@@ -209,6 +234,8 @@ def sinh(z)
209234
210235 ##
211236 # returns the hyperbolic cosine of +z+, where +z+ is given in radians
237+ #
238+ # CMath.cosh(1 + 1i) #=> (0.8337300251311491+0.9888977057628651i)
212239 def cosh ( z )
213240 begin
214241 if z . real?
@@ -224,6 +251,8 @@ def cosh(z)
224251
225252 ##
226253 # returns the hyperbolic tangent of +z+, where +z+ is given in radians
254+ #
255+ # CMath.tanh(1 + 1i) #=> (1.0839233273386943+0.27175258531951174i)
227256 def tanh ( z )
228257 begin
229258 if z . real?
@@ -238,6 +267,8 @@ def tanh(z)
238267
239268 ##
240269 # returns the arc sine of +z+
270+ #
271+ # CMath.asin(1 + 1i) #=> (0.6662394324925153+1.0612750619050355i)
241272 def asin ( z )
242273 begin
243274 if z . real? and z >= -1 and z <= 1
@@ -252,6 +283,8 @@ def asin(z)
252283
253284 ##
254285 # returns the arc cosine of +z+
286+ #
287+ # CMath.acos(1 + 1i) #=> (0.9045568943023813-1.0612750619050357i)
255288 def acos ( z )
256289 begin
257290 if z . real? and z >= -1 and z <= 1
@@ -266,6 +299,8 @@ def acos(z)
266299
267300 ##
268301 # returns the arc tangent of +z+
302+ #
303+ # CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
269304 def atan ( z )
270305 begin
271306 if z . real?
@@ -281,6 +316,8 @@ def atan(z)
281316 ##
282317 # returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
283318 # +x+ to determine the quadrant
319+ #
320+ # CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
284321 def atan2 ( y , x )
285322 begin
286323 if y . real? and x . real?
@@ -295,6 +332,8 @@ def atan2(y,x)
295332
296333 ##
297334 # returns the inverse hyperbolic sine of +z+
335+ #
336+ # CMath.asinh(1 + 1i) #=> (1.0612750619050357+0.6662394324925153i)
298337 def asinh ( z )
299338 begin
300339 if z . real?
@@ -309,6 +348,8 @@ def asinh(z)
309348
310349 ##
311350 # returns the inverse hyperbolic cosine of +z+
351+ #
352+ # CMath.acosh(1 + 1i) #=> (1.0612750619050357+0.9045568943023813i)
312353 def acosh ( z )
313354 begin
314355 if z . real? and z >= 1
@@ -323,6 +364,8 @@ def acosh(z)
323364
324365 ##
325366 # returns the inverse hyperbolic tangent of +z+
367+ #
368+ # CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
326369 def atanh ( z )
327370 begin
328371 if z . real? and z >= -1 and z <= 1
@@ -397,4 +440,3 @@ def handle_no_method_error # :nodoc:
397440 module_function :handle_no_method_error
398441
399442end
400-
0 commit comments