1 

# This module provides access to mathematical functions for complex

2 

# numbers.

3 

#

4 

# Example

5 

# # Square root of a negative number is a complex number.


1 
##


2 
# = CMath


3 
#


4 
# CMath is a library that provides trigonometric and transcendental


5 
# functions for complex numbers.


6 
#


7 
# == Usage


8 
#


9 
# To start using this library, simply:


10 
#


11 
# require "cmath"


12 
#


13 
# Square root of a negative number is a complex number.


14 
#

6 
15 
# CMath.sqrt(9) #=> 0+3.0i

7 
16 
#

8 
17 
module CMath

...  ...  
33 
42 
alias acosh! acosh

34 
43 
alias atanh! atanh

35 
44 


45 
# Returns e**z.


46 
# exp(Complex(0,0)) #=> 1.0+0.0i


47 
# exp(Complex(0,PI)) #=> 1.0+1.2246467991473532e16i


48 
# exp(Complex(0,PI/2.0)) #=> 6.123233995736766e17+1.0i

36 
49 
def exp(z)

37 
50 
if z.real?

38 
51 
exp!(z)

...  ...  
43 
56 
end

44 
57 
end

45 
58 


59 
# Returns the natural logarithm of Complex. If additional second


60 
# argument is given, it will be the base of logarithm.


61 
# log(Complex(0,0)) #=> Infinity+0.0i

46 
62 
def log(*args)

47 
63 
z, b = args

48 
64 
if z.real? and z >= 0 and (b.nil? or b >= 0)

...  ...  
56 
72 
end

57 
73 
end

58 
74 


75 
# Returns the base 2 logarithm of Complex.

59 
76 
def log2(z)

60 
77 
if z.real? and z >= 0

61 
78 
log2!(z)

...  ...  
64 
81 
end

65 
82 
end

66 
83 


84 
# Returns the base 10 logarithm of Complex.

67 
85 
def log10(z)

68 
86 
if z.real? and z >= 0

69 
87 
log10!(z)

...  ...  
72 
90 
end

73 
91 
end

74 
92 


93 
# Returns the nonnegative square root of Complex.


94 
# sqrt(1) #=> 0+1.0i


95 
# sqrt(Complex(1,0)) #=> 0.0+1.0i


96 
# sqrt(Complex(0,8)) #=> 2.0+2.0i

75 
97 
def sqrt(z)

76 
98 
if z.real?

77 
99 
if z < 0

...  ...  
91 
113 
end

92 
114 
end

93 
115 


116 
# Returns the cube root of a Complex.

94 
117 
def cbrt(z)

95 
118 
if z.real?

96 
119 
cbrt!(z)

...  ...  
99 
122 
end

100 
123 
end

101 
124 


125 
# Computes the sine of z (expressed in radians).

102 
126 
def sin(z)

103 
127 
if z.real?

104 
128 
sin!(z)

...  ...  
108 
132 
end

109 
133 
end

110 
134 


135 
# Computes the cosine of z (expressed in radians).

111 
136 
def cos(z)

112 
137 
if z.real?

113 
138 
cos!(z)

...  ...  
117 
142 
end

118 
143 
end

119 
144 


145 
# Returns the tangent of z (expressed in radians).

120 
146 
def tan(z)

121 
147 
if z.real?

122 
148 
tan!(z)

...  ...  
125 
151 
end

126 
152 
end

127 
153 


154 
# Computes the hyperbolic sine of z (expressed in radians).

128 
155 
def sinh(z)

129 
156 
if z.real?

130 
157 
sinh!(z)

...  ...  
134 
161 
end

135 
162 
end

136 
163 


164 
# Computes the hyperbolic cosine of z (expressed in radians).

137 
165 
def cosh(z)

138 
166 
if z.real?

139 
167 
cosh!(z)

...  ...  
143 
171 
end

144 
172 
end

145 
173 


174 
# Computes the hyperbolic tangent of z (expressed in radians).

146 
175 
def tanh(z)

147 
176 
if z.real?

148 
177 
tanh!(z)

...  ...  
151 
180 
end

152 
181 
end

153 
182 


183 
# Computes the arc sine of z.

154 
184 
def asin(z)

155 
185 
if z.real? and z >= 1 and z <= 1

156 
186 
asin!(z)

...  ...  
159 
189 
end

160 
190 
end

161 
191 


192 
# Computes the arc cosine of z.

162 
193 
def acos(z)

163 
194 
if z.real? and z >= 1 and z <= 1

164 
195 
acos!(z)

...  ...  
167 
198 
end

168 
199 
end

169 
200 


201 
# Computes the arc tangent of z.

170 
202 
def atan(z)

171 
203 
if z.real?

172 
204 
atan!(z)

...  ...  
175 
207 
end

176 
208 
end

177 
209 


210 
# Computes the arc tangent given y and x.

178 
211 
def atan2(y,x)

179 
212 
if y.real? and x.real?

180 
213 
atan2!(y,x)

...  ...  
183 
216 
end

184 
217 
end

185 
218 


219 
# Computes the inverse hyperbolic sine of z.

186 
220 
def asinh(z)

187 
221 
if z.real?

188 
222 
asinh!(z)

...  ...  
191 
225 
end

192 
226 
end

193 
227 


228 
# Computes the inverse hyperbolic cosine of z.

194 
229 
def acosh(z)

195 
230 
if z.real? and z >= 1

196 
231 
acosh!(z)

...  ...  
199 
234 
end

200 
235 
end

201 
236 


237 
# Computes the inverse hyperbolic tangent of z.

202 
238 
def atanh(z)

203 
239 
if z.real? and z >= 1 and z <= 1

204 
240 
atanh!(z)

205 


