Math-Bacovia

 view release on metacpan or  search on metacpan

lib/Math/Bacovia.pm  view on Meta::CPAN


    Math::Bacovia::_check_type(\$x);
    Math::Bacovia::_check_type(\$y);

    $s ? $y->add($x) : $x->add($y);
  },
  '-' => sub {
    my ($x, $y, $s) = @_;

    Math::Bacovia::_check_type(\$x);
    Math::Bacovia::_check_type(\$y);

    $s ? $y->sub($x) : $x->sub($y);
  },
  '*' => sub {
    my ($x, $y, $s) = @_;

    Math::Bacovia::_check_type(\$x);
    Math::Bacovia::_check_type(\$y);

    $s ? $y->mul($x) : $x->mul($y);
  },
  '/' => sub {
    my ($x, $y, $s) = @_;

    Math::Bacovia::_check_type(\$x);
    Math::Bacovia::_check_type(\$y);

    $s ? $y->div($x) : $x->div($y);
  },
  '**' => sub {
    my ($x, $y, $s) = @_;

    Math::Bacovia::_check_type(\$x);
    Math::Bacovia::_check_type(\$y);

    $s ? $y->pow($x) : $x->pow($y);
  },

  eq => sub { "$_[0]" eq "$_[1]" },
  ne => sub { "$_[0]" ne "$_[1]" },

  cmp => sub { $_[2] ? "$_[1]" cmp $_[0]->stringify : $_[0]->stringify cmp "$_[1]" },
  neg => sub { $_[0]->neg },

  sin => \&sin,
  cos => \&cos,
  exp => \&exp,
  log => \&log,
  int => \&int,

  atan2 => \&atan2,

  #abs  => \&abs,
  sqrt => \&sqrt;

#
## Import/export
#

sub i () {
    'Math::Bacovia::Number'->new('Math::AnyNum'->i);
}

sub pi () {
    'Math::Bacovia::Log'->new($MONE) * -i;
}

sub tau () {
    'Math::Bacovia::Log'->new($MONE) * -(i + i);
}

sub e () {
    'Math::Bacovia::Exp'->new($ONE);
}

my %exported_functions = (
                          Exp        => \&_exp,
                          Log        => \&_log,
                          Product    => \&_product,
                          Sum        => \&_sum,
                          Power      => \&_power,
                          Symbol     => \&_symbol,
                          Number     => \&_number,
                          Fraction   => \&_fraction,
                          Difference => \&_difference,
                         );

my %exported_constants = (
                          i   => \&i,
                          pi  => \&pi,
                          tau => \&tau,
                          e   => \&e,
                         );

sub import {
    shift;

    my $caller = caller(0);

    while (@_) {
        my $name = shift(@_);

        if ($name eq ':all') {
            push @_, keys(%exported_functions), keys(%exported_constants);
            next;
        }

        no strict 'refs';
        my $caller_sub = $caller . '::' . $name;
        if (exists($exported_functions{$name})) {
            my $sub = $exported_functions{$name};
            *$caller_sub = $exported_functions{$name};
        }
        elsif (exists($exported_constants{$name})) {
            my $value = $exported_constants{$name}->();
            *$caller_sub = sub() { $value };
        }
        else {
            die "unknown import: <<$name>>";
        }
    }

    return;
}

sub _log {
    'Math::Bacovia::Log'->new(@_);
}

sub _exp {
    'Math::Bacovia::Exp'->new(@_);
}



( run in 3.660 seconds using v1.01-cache-2.11-cpan-364913b4093 )