Skip to content
Newer
Older
100644 255 lines (222 sloc) 4.05 KB
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
1 module CMath
2
3 include Math
4
5 alias exp! exp
6 alias log! log
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
7 alias log2! log2
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
8 alias log10! log10
9 alias sqrt! sqrt
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
10 alias cbrt! cbrt
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
11
12 alias sin! sin
13 alias cos! cos
14 alias tan! tan
15
16 alias sinh! sinh
17 alias cosh! cosh
18 alias tanh! tanh
19
20 alias asin! asin
21 alias acos! acos
22 alias atan! atan
23 alias atan2! atan2
24
25 alias asinh! asinh
26 alias acosh! acosh
27 alias atanh! atanh
28
29 def exp(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
30 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
31 exp!(z)
32 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
33 ere = exp!(z.real)
34 Complex(ere * cos!(z.imag),
35 ere * sin!(z.imag))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
36 end
37 end
38
39 def log(*args)
40 z, b = args
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
41 if z.real? and z >= 0 and (b.nil? or b >= 0)
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
42 log!(*args)
43 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
44 a = Complex(log!(z.abs), z.arg)
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
45 if b
46 a /= log(b)
47 end
48 a
49 end
50 end
51
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
52 def log2(z)
53 if z.real? and z >= 0
54 log2!(z)
55 else
56 log(z) / log!(2)
57 end
58 end
59
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
60 def log10(z)
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
61 if z.real? and z >= 0
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
62 log10!(z)
63 else
64 log(z) / log!(10)
65 end
66 end
67
68 def sqrt(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
69 if z.real?
70 if z < 0
71 Complex(0, sqrt!(-z))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
72 else
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
73 sqrt!(z)
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
74 end
75 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
76 if z.imag < 0 ||
77 (z.imag == 0 && z.imag.to_s[0] == '-')
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
78 sqrt(z.conjugate).conjugate
79 else
80 r = z.abs
81 x = z.real
82 Complex(sqrt!((r + x) / 2), sqrt!((r - x) / 2))
83 end
84 end
85 end
86
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
87 def cbrt(z)
0faba41 @Watson1978 Fixed a bug of CMath.cbrt, when is passed a negative value to an argu…
Watson1978 authored
88 if z.real?
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
89 cbrt!(z)
90 else
91 Complex(z) ** (1.0/3)
92 end
93 end
94
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
95 def sin(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
96 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
97 sin!(z)
98 else
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
99 Complex(sin!(z.real) * cosh!(z.imag),
100 cos!(z.real) * sinh!(z.imag))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
101 end
102 end
103
104 def cos(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
105 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
106 cos!(z)
107 else
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
108 Complex(cos!(z.real) * cosh!(z.imag),
109 -sin!(z.real) * sinh!(z.imag))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
110 end
111 end
112
113 def tan(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
114 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
115 tan!(z)
116 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
117 sin(z) / cos(z)
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
118 end
119 end
120
121 def sinh(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
122 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
123 sinh!(z)
124 else
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
125 Complex(sinh!(z.real) * cos!(z.imag),
126 cosh!(z.real) * sin!(z.imag))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
127 end
128 end
129
130 def cosh(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
131 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
132 cosh!(z)
133 else
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
134 Complex(cosh!(z.real) * cos!(z.imag),
135 sinh!(z.real) * sin!(z.imag))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
136 end
137 end
138
139 def tanh(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
140 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
141 tanh!(z)
142 else
143 sinh(z) / cosh(z)
144 end
145 end
146
147 def asin(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
148 if z.real? and z >= -1 and z <= 1
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
149 asin!(z)
150 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
151 (-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
152 end
153 end
154
155 def acos(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
156 if z.real? and z >= -1 and z <= 1
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
157 acos!(z)
158 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
159 (-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
160 end
161 end
162
163 def atan(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
164 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
165 atan!(z)
166 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
167 1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
168 end
169 end
170
171 def atan2(y,x)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
172 if y.real? and x.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
173 atan2!(y,x)
174 else
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
175 (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
176 end
177 end
178
179 def asinh(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
180 if z.real?
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
181 asinh!(z)
182 else
183 log(z + sqrt(1.0 + z * z))
184 end
185 end
186
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
187 def acosh(z)
188 if z.real? and z >= 1
189 acosh!(z)
190 else
191 log(z + sqrt(z * z - 1.0))
192 end
193 end
194
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
195 def atanh(z)
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
196 if z.real? and z >= -1 and z <= 1
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
197 atanh!(z)
198 else
199 log((1.0 + z) / (1.0 - z)) / 2.0
200 end
201 end
202
203 module_function :exp!
204 module_function :exp
205 module_function :log!
206 module_function :log
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
207 module_function :log2!
208 module_function :log2
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
209 module_function :log10!
210 module_function :log10
211 module_function :sqrt!
212 module_function :sqrt
467bc1b Update library and removing working tags
Thibault Martin-Lagardette authored
213 module_function :cbrt!
214 module_function :cbrt
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
215
216 module_function :sin!
217 module_function :sin
218 module_function :cos!
219 module_function :cos
220 module_function :tan!
221 module_function :tan
222
223 module_function :sinh!
224 module_function :sinh
225 module_function :cosh!
226 module_function :cosh
227 module_function :tanh!
228 module_function :tanh
229
230 module_function :asin!
231 module_function :asin
232 module_function :acos!
233 module_function :acos
234 module_function :atan!
235 module_function :atan
236 module_function :atan2!
237 module_function :atan2
238
239 module_function :asinh!
240 module_function :asinh
241 module_function :acosh!
242 module_function :acosh
243 module_function :atanh!
244 module_function :atanh
245
8f21162 @richkilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues usi…
richkilmer authored
246 module_function :frexp
247 module_function :ldexp
248 module_function :hypot
249 module_function :erf
250 module_function :erfc
251 module_function :gamma
252 module_function :lgamma
253
26d0e1f merge with ruby trunk r16762 + better/faster objc-like dispatcher
Laurent Sansonetti authored
254 end
Something went wrong with that request. Please try again.