Module Math
In: complex.rb
mathn.rb

Methods

acos   acosh   asin   asinh   atan   atan2   atanh   cos   cosh   exp   log   log10   rsqrt   sin   sinh   sqrt   sqrt   tan   tanh  

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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 463
463:   def cos(z)
464:     if Complex.generic?(z)
465:       cos!(z)
466:     else
467:       Complex(cos!(z.real)*cosh!(z.image),
468:               -sin!(z.real)*sinh!(z.image))
469:     end
470:   end

[Source]

     # 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

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 454
454:   def exp(z)
455:     if Complex.generic?(z)
456:       exp!(z)
457:     else
458:       Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image))
459:     end
460:   end

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 516
516:   def log(z)
517:     if Complex.generic?(z) and z >= 0
518:       log!(z)
519:     else
520:       r, theta = z.polar
521:       Complex(log!(r.abs), theta)
522:     end
523:   end

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 526
526:   def log10(z)
527:     if Complex.generic?(z)
528:       log10!(z)
529:     else
530:       log(z)/log!(10)
531:     end
532:   end

[Source]

     # 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

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 473
473:   def sin(z)
474:     if Complex.generic?(z)
475:       sin!(z)
476:     else
477:       Complex(sin!(z.real)*cosh!(z.image),
478:               cos!(z.real)*sinh!(z.image))
479:     end
480:   end

[Source]

     # 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

[Source]

     # 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.

[Source]

     # 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

Redefined to handle a Complex argument.

[Source]

     # File complex.rb, line 483
483:   def tan(z)
484:     if Complex.generic?(z)
485:       tan!(z)
486:     else
487:       sin(z)/cos(z)
488:     end
489:   end

[Source]

     # File complex.rb, line 507
507:   def tanh(z)
508:     if Complex.generic?(z)
509:       tanh!(z)
510:     else
511:       sinh(z)/cosh(z)
512:     end
513:   end

Search

Google

Ruby API Docs

Links