Project

General

Profile

Feature #10974 » remove_methods_from_cmath.patch

gogotanaka (Kazuki Tanaka), 03/15/2015 08:27 AM

View differences:

lib/cmath.rb
17 17

  
18 18
  include Math
19 19

  
20
  alias exp! exp
21
  alias log! log
22
  alias log2! log2
23
  alias log10! log10
24
  alias sqrt! sqrt
25
  alias cbrt! cbrt
26

  
27
  alias sin! sin
28
  alias cos! cos
29
  alias tan! tan
30

  
31
  alias sinh! sinh
32
  alias cosh! cosh
33
  alias tanh! tanh
34

  
35
  alias asin! asin
36
  alias acos! acos
37
  alias atan! atan
38
  alias atan2! atan2
39

  
40
  alias asinh! asinh
41
  alias acosh! acosh
42
  alias atanh! atanh
43

  
44 20
  ##
45 21
  # Math::E raised to the +z+ power
46 22
  #
......
50 26
  def exp(z)
51 27
    begin
52 28
      if z.real?
53
        exp!(z)
29
        Math.exp(z)
54 30
      else
55
        ere = exp!(z.real)
56
        Complex(ere * cos!(z.imag),
57
                ere * sin!(z.imag))
31
        ere = Math.exp(z.real)
32
        Complex(ere * Math.cos(z.imag),
33
                ere * Math.sin(z.imag))
58 34
      end
59 35
    rescue NoMethodError
60 36
      handle_no_method_error
......
69 45
  def log(z, b=::Math::E)
70 46
    begin
71 47
      if z.real? && z >= 0 && b >= 0
72
        log!(z, b)
48
        Math.log(z, b)
73 49
      else
74
        Complex(log!(z.abs), z.arg) / log(b)
50
        Complex(Math.log(z.abs), z.arg) / log(b)
75 51
      end
76 52
    rescue NoMethodError
77 53
      handle_no_method_error
......
83 59
  def log2(z)
84 60
    begin
85 61
      if z.real? and z >= 0
86
        log2!(z)
62
        Math.log2(z)
87 63
      else
88
        log(z) / log!(2)
64
        log(z) / Math.log(2)
89 65
      end
90 66
    rescue NoMethodError
91 67
      handle_no_method_error
......
97 73
  def log10(z)
98 74
    begin
99 75
      if z.real? and z >= 0
100
        log10!(z)
76
        Math.log10(z)
101 77
      else
102
        log(z) / log!(10)
78
        log(z) / Math.log(10)
103 79
      end
104 80
    rescue NoMethodError
105 81
      handle_no_method_error
......
115 91
    begin
116 92
      if z.real?
117 93
        if z < 0
118
          Complex(0, sqrt!(-z))
94
          Complex(0, Math.sqrt(-z))
119 95
        else
120
          sqrt!(z)
96
          Math.sqrt(z)
121 97
        end
122 98
      else
123 99
        if z.imag < 0 ||
......
126 102
        else
127 103
          r = z.abs
128 104
          x = z.real
129
          Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0))
105
          Complex(Math.sqrt((r + x) / 2.0), Math.sqrt((r - x) / 2.0))
130 106
        end
131 107
      end
132 108
    rescue NoMethodError
......
145 121
  def sin(z)
146 122
    begin
147 123
      if z.real?
148
        sin!(z)
124
        Math.sin(z)
149 125
      else
150
        Complex(sin!(z.real) * cosh!(z.imag),
151
                cos!(z.real) * sinh!(z.imag))
126
        Complex(Math.sin(z.real) * Math.cosh(z.imag),
127
                Math.cos(z.real) * Math.sinh(z.imag))
152 128
      end
153 129
    rescue NoMethodError
154 130
      handle_no_method_error
......
160 136
  def cos(z)
161 137
    begin
162 138
      if z.real?
163
        cos!(z)
139
        Math.cos(z)
164 140
      else
165
        Complex(cos!(z.real) * cosh!(z.imag),
166
                -sin!(z.real) * sinh!(z.imag))
141
        Complex(Math.cos(z.real) * Math.cosh(z.imag),
142
                -Math.sin(z.real) * Math.sinh(z.imag))
167 143
      end
168 144
    rescue NoMethodError
169 145
      handle_no_method_error
......
175 151
  def tan(z)
176 152
    begin
177 153
      if z.real?
178
        tan!(z)
154
        Math.tan(z)
179 155
      else
180 156
        sin(z) / cos(z)
181 157
      end
......
189 165
  def sinh(z)
190 166
    begin
191 167
      if z.real?
192
        sinh!(z)
168
        Math.sinh(z)
193 169
      else
194
        Complex(sinh!(z.real) * cos!(z.imag),
195
                cosh!(z.real) * sin!(z.imag))
170
        Complex(Math.sinh(z.real) * Math.cos(z.imag),
171
                Math.cosh(z.real) * Math.sin(z.imag))
196 172
      end
197 173
    rescue NoMethodError
198 174
      handle_no_method_error
......
204 180
  def cosh(z)
205 181
    begin
206 182
      if z.real?
207
        cosh!(z)
183
        Math.cosh(z)
208 184
      else
209
        Complex(cosh!(z.real) * cos!(z.imag),
210
                sinh!(z.real) * sin!(z.imag))
185
        Complex(Math.cosh(z.real) * Math.cos(z.imag),
186
                Math.sinh(z.real) * Math.sin(z.imag))
211 187
      end
212 188
    rescue NoMethodError
213 189
      handle_no_method_error
......
219 195
  def tanh(z)
220 196
    begin
221 197
      if z.real?
222
        tanh!(z)
198
        Math.tanh(z)
223 199
      else
224 200
        sinh(z) / cosh(z)
225 201
      end
......
233 209
  def asin(z)
234 210
    begin
235 211
      if z.real? and z >= -1 and z <= 1
236
        asin!(z)
212
        Math.asin(z)
237 213
      else
238 214
        (-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
239 215
      end
......
247 223
  def acos(z)
248 224
    begin
249 225
      if z.real? and z >= -1 and z <= 1
250
        acos!(z)
226
        Math.acos(z)
251 227
      else
252 228
        (-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
253 229
      end
......
261 237
  def atan(z)
262 238
    begin
263 239
      if z.real?
264
        atan!(z)
240
        Math.atan(z)
265 241
      else
266 242
        1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
267 243
      end
......
276 252
  def atan2(y,x)
277 253
    begin
278 254
      if y.real? and x.real?
279
        atan2!(y,x)
255
        Math.atan2(y,x)
280 256
      else
281 257
        (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
282 258
      end
......
290 266
  def asinh(z)
291 267
    begin
292 268
      if z.real?
293
        asinh!(z)
269
        Math.asinh(z)
294 270
      else
295 271
        log(z + sqrt(1.0 + z * z))
296 272
      end
......
304 280
  def acosh(z)
305 281
    begin
306 282
      if z.real? and z >= 1
307
        acosh!(z)
283
        Math.acosh(z)
308 284
      else
309 285
        log(z + sqrt(z * z - 1.0))
310 286
      end
......
318 294
  def atanh(z)
319 295
    begin
320 296
      if z.real? and z >= -1 and z <= 1
321
        atanh!(z)
297
        Math.atanh(z)
322 298
      else
323 299
        log((1.0 + z) / (1.0 - z)) / 2.0
324 300
      end
......
327 303
    end
328 304
  end
329 305

  
330
  module_function :exp!
331 306
  module_function :exp
332
  module_function :log!
333 307
  module_function :log
334
  module_function :log2!
335 308
  module_function :log2
336
  module_function :log10!
337 309
  module_function :log10
338
  module_function :sqrt!
339 310
  module_function :sqrt
340
  module_function :cbrt!
341 311
  module_function :cbrt
342 312

  
343
  module_function :sin!
344 313
  module_function :sin
345
  module_function :cos!
346 314
  module_function :cos
347
  module_function :tan!
348 315
  module_function :tan
349 316

  
350
  module_function :sinh!
351 317
  module_function :sinh
352
  module_function :cosh!
353 318
  module_function :cosh
354
  module_function :tanh!
355 319
  module_function :tanh
356 320

  
357
  module_function :asin!
358 321
  module_function :asin
359
  module_function :acos!
360 322
  module_function :acos
361
  module_function :atan!
362 323
  module_function :atan
363
  module_function :atan2!
364 324
  module_function :atan2
365 325

  
366
  module_function :asinh!
367 326
  module_function :asinh
368
  module_function :acosh!
369 327
  module_function :acosh
370
  module_function :atanh!
371 328
  module_function :atanh
372 329

  
373 330
  module_function :frexp
374
- 
test/ruby/test_complex.rb
966 966
      assert_in_delta(1.092, c.real, 0.001)
967 967
      assert_in_delta(-0.420, c.imag, 0.001)
968 968

  
969
      c = CMath.log2(Complex(1, 2))
970
      assert_in_delta(1.160, c.real, 0.001)
971
      assert_in_delta(1.597, c.imag, 0.001)
972

  
969 973
      c = CMath.log10(Complex(1, 2))
970 974
      assert_in_delta(0.349, c.real, 0.001)
971 975
      assert_in_delta(0.480, c.imag, 0.001)
972
-