
1 
##


2 
# Math functions for the complex numbers


3 

1 
4 
module CMath

2 
5 

3 
6 
include Math

...  ...  
26 
29 
alias acosh! acosh

27 
30 
alias atanh! atanh

28 
31 


32 
##


33 
# returns the value of e raised to the +z+ power

29 
34 
def exp(z)

30 
35 
if z.real?

31 
36 
exp!(z)

...  ...  
36 
41 
end

37 
42 
end

38 
43 


44 
##


45 
# returns the log of the first argument with the base


46 
# optionally specified as the second argument

39 
47 
def log(*args)

40 
48 
z, b = args

41 
49 
if z.real? and z >= 0 and (b.nil? or b >= 0)

...  ...  
49 
57 
end

50 
58 
end

51 
59 


60 
##


61 
# returns the log base 2 of +z+

52 
62 
def log2(z)

53 
63 
if z.real? and z >= 0

54 
64 
log2!(z)

...  ...  
57 
67 
end

58 
68 
end

59 
69 


70 
##


71 
# returns the log base 10 of +z+

60 
72 
def log10(z)

61 
73 
if z.real? and z >= 0

62 
74 
log10!(z)

...  ...  
65 
77 
end

66 
78 
end

67 
79 


80 
##


81 
# returns the square root of +z+

68 
82 
def sqrt(z)

69 
83 
if z.real?

70 
84 
if z < 0

...  ...  
84 
98 
end

85 
99 
end

86 
100 


101 
##


102 
# returns the cube root of +z+

87 
103 
def cbrt(z)

88 
104 
if z.real?

89 
105 
cbrt!(z)

...  ...  
92 
108 
end

93 
109 
end

94 
110 


111 
##


112 
# returns the sine of +z+, where +z+ is given in radians

95 
113 
def sin(z)

96 
114 
if z.real?

97 
115 
sin!(z)

...  ...  
101 
119 
end

102 
120 
end

103 
121 


122 
##


123 
# returns the cosine of +z+, where +z+ is given in radians

104 
124 
def cos(z)

105 
125 
if z.real?

106 
126 
cos!(z)

...  ...  
110 
130 
end

111 
131 
end

112 
132 


133 
##


134 
# returns the tangent of +z+, where +z+ is given in radians

113 
135 
def tan(z)

114 
136 
if z.real?

115 
137 
tan!(z)

...  ...  
118 
140 
end

119 
141 
end

120 
142 


143 
##


144 
# returns the hyperbolic sine of +z+

121 
145 
def sinh(z)

122 
146 
if z.real?

123 
147 
sinh!(z)

...  ...  
127 
151 
end

128 
152 
end

129 
153 


154 
##


155 
# returns the hyperbolic cosine of +z+

130 
156 
def cosh(z)

131 
157 
if z.real?

132 
158 
cosh!(z)

...  ...  
136 
162 
end

137 
163 
end

138 
164 


165 
##


166 
# returns the hyperbolic tangent of +z+

139 
167 
def tanh(z)

140 
168 
if z.real?

141 
169 
tanh!(z)

...  ...  
144 
172 
end

145 
173 
end

146 
174 


175 
##


176 
# returns the arc sine of +z+

147 
177 
def asin(z)

148 
178 
if z.real? and z >= 1 and z <= 1

149 
179 
asin!(z)

...  ...  
152 
182 
end

153 
183 
end

154 
184 


185 
##


186 
# returns the arc cosine of +z+

155 
187 
def acos(z)

156 
188 
if z.real? and z >= 1 and z <= 1

157 
189 
acos!(z)

...  ...  
160 
192 
end

161 
193 
end

162 
194 


195 
##


196 
# returns the arc tangent of +z+

163 
197 
def atan(z)

164 
198 
if z.real?

165 
199 
atan!(z)

...  ...  
168 
202 
end

169 
203 
end

170 
204 


205 
##


206 
# returns the arc tangent of +y+ / +x+ using the signs


207 
# of +y+ and +x+ to determine the quadrant

171 
208 
def atan2(y,x)

172 
209 
if y.real? and x.real?

173 
210 
atan2!(y,x)

...  ...  
176 
213 
end

177 
214 
end

178 
215 


216 
##


217 
# returns the inverse hyperbolic sine of +z+

179 
218 
def asinh(z)

180 
219 
if z.real?

181 
220 
asinh!(z)

...  ...  
184 
223 
end

185 
224 
end

186 
225 


226 
##


227 
# returns the inverse hyperbolic cosine of +z+

187 
228 
def acosh(z)

188 
229 
if z.real? and z >= 1

189 
230 
acosh!(z)

...  ...  
192 
233 
end

193 
234 
end

194 
235 


236 
##


237 
# returns the inverse hyperbolic tangent of +z+

195 
238 
def atanh(z)

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

197 
240 
atanh!(z)

198 


