| Module | Math |
| In: |
complex.rb
mathn.rb |
External Aliases
| sqrt | -> | sqrt! |
| exp | -> | exp! |
| log | -> | log! |
| log10 | -> | log10! |
| cos | -> | cos! |
| sin | -> | sin! |
| tan | -> | tan! |
| cosh | -> | cosh! |
| sinh | -> | sinh! |
| tanh | -> | tanh! |
| acos | -> | acos! |
| asin | -> | asin! |
| atan | -> | atan! |
| atan2 | -> | atan2! |
| acosh | -> | acosh! |
| asinh | -> | asinh! |
| atanh | -> | atanh! |
Public Instance methods
# File complex.rb, line 534
534: def acos(z)
535: if Complex.generic?(z) and z >= -1 and z <= 1
536: acos!(z)
537: else
538: -1.0.im * log( z + 1.0.im * sqrt(1.0-z*z) )
539: end
540: end
# File complex.rb, line 566
566: def acosh(z)
567: if Complex.generic?(z) and z >= 1
568: acosh!(z)
569: else
570: log( z + sqrt(z*z-1.0) )
571: end
572: end
# File complex.rb, line 542
542: def asin(z)
543: if Complex.generic?(z) and z >= -1 and z <= 1
544: asin!(z)
545: else
546: -1.0.im * log( 1.0.im * z + sqrt(1.0-z*z) )
547: end
548: end
# File complex.rb, line 574
574: def asinh(z)
575: if Complex.generic?(z)
576: asinh!(z)
577: else
578: log( z + sqrt(1.0+z*z) )
579: end
580: end
# File complex.rb, line 550
550: def atan(z)
551: if Complex.generic?(z)
552: atan!(z)
553: else
554: 1.0.im * log( (1.0.im+z) / (1.0.im-z) ) / 2.0
555: end
556: end
# File complex.rb, line 558
558: def atan2(y,x)
559: if Complex.generic?(y) and Complex.generic?(x)
560: atan2!(y,x)
561: else
562: -1.0.im * log( (x+1.0.im*y) / sqrt(x*x+y*y) )
563: end
564: end
# File complex.rb, line 582
582: def atanh(z)
583: if Complex.generic?(z) and z >= -1 and z <= 1
584: atanh!(z)
585: else
586: log( (1.0+z) / (1.0-z) ) / 2.0
587: end
588: end
# File complex.rb, line 499
499: def cosh(z)
500: if Complex.generic?(z)
501: cosh!(z)
502: else
503: Complex( cosh!(z.real)*cos!(z.image), sinh!(z.real)*sin!(z.image) )
504: end
505: end
# File mathn.rb, line 255
255: def rsqrt(a)
256: if a.kind_of?(Float)
257: sqrt!(a)
258: elsif a.kind_of?(Rational)
259: rsqrt(a.numerator)/rsqrt(a.denominator)
260: else
261: src = a
262: max = 2 ** 32
263: byte_a = [src & 0xffffffff]
264: # ruby's bug
265: while (src >= max) and (src >>= 32)
266: byte_a.unshift src & 0xffffffff
267: end
268:
269: answer = 0
270: main = 0
271: side = 0
272: for elm in byte_a
273: main = (main << 32) + elm
274: side <<= 16
275: if answer != 0
276: if main * 4 < side * side
277: applo = main.div(side)
278: else
279: applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
280: end
281: else
282: applo = sqrt!(main).to_i + 1
283: end
284:
285: while (x = (side + applo) * applo) > main
286: applo -= 1
287: end
288: main -= x
289: answer = (answer << 16) + applo
290: side += applo * 2
291: end
292: if main == 0
293: answer
294: else
295: sqrt!(a)
296: end
297: end
298: end
# File complex.rb, line 491
491: def sinh(z)
492: if Complex.generic?(z)
493: sinh!(z)
494: else
495: Complex( sinh!(z.real)*cos!(z.image), cosh!(z.real)*sin!(z.image) )
496: end
497: end
# File mathn.rb, line 232
232: def sqrt(a)
233: if a.kind_of?(Complex)
234: abs = sqrt(a.real*a.real + a.image*a.image)
235: # if not abs.kind_of?(Rational)
236: # return a**Rational(1,2)
237: # end
238: x = sqrt((a.real + abs)/Rational(2))
239: y = sqrt((-a.real + abs)/Rational(2))
240: # if !(x.kind_of?(Rational) and y.kind_of?(Rational))
241: # return a**Rational(1,2)
242: # end
243: if a.image >= 0
244: Complex(x, y)
245: else
246: Complex(x, -y)
247: end
248: elsif a >= 0
249: rsqrt(a)
250: else
251: Complex(0,rsqrt(-a))
252: end
253: end
Redefined to handle a Complex argument.
# File complex.rb, line 435
435: def sqrt(z)
436: if Complex.generic?(z)
437: if z >= 0
438: sqrt!(z)
439: else
440: Complex(0,sqrt!(-z))
441: end
442: else
443: if z.image < 0
444: sqrt(z.conjugate).conjugate
445: else
446: r = z.abs
447: x = z.real
448: Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) )
449: end
450: end
451: end