Math-Bacovia

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Build.PL
Changes
examples/basic.pl
examples/binomial_theorem.pl
examples/faulhaber_s_formula.pl
examples/faulhaber_s_formula_2.pl
examples/generalized_fibonacci.pl
examples/log_factorial_asymptotic.pl
examples/quadratic_polynomials_zeros.pl
examples/summation.pl
examples/trigonometric_function_simplifications.pl
examples/zeta_2n.pl
lib/Math/Bacovia.pm
lib/Math/Bacovia.pod
lib/Math/Bacovia/Difference.pm
lib/Math/Bacovia/Difference.pod

examples/faulhaber_s_formula.pl  view on Meta::CPAN

#!/usr/bin/perl

# The formula for calculating the sum of consecutive
# numbers raised to a given power, such as:
#    1^p + 2^p + 3^p + ... + n^p
# where p is a positive integer.

# See also:
#   https://en.wikipedia.org/wiki/Faulhaber%27s_formula

use 5.010;
use strict;
use warnings;

use lib qw(../lib);
use Math::Bacovia qw(:all);
use Math::AnyNum qw(binomial bernfrac);

# The Faulhaber's formula
# See: https://en.wikipedia.org/wiki/Faulhaber%27s_formula
sub faulhaber_s_formula {
    my ($p, $n) = @_;

    my $sum = Sum();
    for my $j (0 .. $p) {
        $sum += Number(binomial($p + 1, $j)) * Number(bernfrac($j)) * $n**($p + 1 - $j);
    }

    Fraction($sum, ($p + 1));
}

# Alternate expression using Bernoulli polynomials
# See: https://en.wikipedia.org/wiki/Faulhaber%27s_formula#Alternate_expressions
sub bernoulli_polynomials {
    my ($n, $x) = @_;

    my $sum = Sum();
    for my $k (0 .. $n) {
        $sum += Number(binomial($n, $k)) * Number(bernfrac($n - $k)) * $x**$k;
    }

    $sum;
}

sub faulhaber_s_formula_2 {
    my ($p, $n) = @_;
    1 + Fraction((bernoulli_polynomials($p + 1, $n) - bernoulli_polynomials($p + 1, 1)), ($p + 1));
}

foreach my $i (0 .. 10) {
    say "F($i) = ", faulhaber_s_formula($i, Symbol('n'))->simple->pretty;
    say "F($i) = ", faulhaber_s_formula_2($i, Symbol('n'))->simple->pretty;
}

examples/faulhaber_s_formula_2.pl  view on Meta::CPAN

#!/usr/bin/perl

use utf8;
use 5.014;

use lib qw(../lib);
use Math::Bacovia qw(:all);
use Math::AnyNum qw(bernfrac binomial);

sub faulhaber_s_formula {
    my ($p, $n) = @_;

    my $sum = Sum();
    foreach my $j (0 .. $p) {
        $sum += Number(binomial($p + 1, $j) * bernfrac($j)) * $n**($p + 1 - $j);
    }

    $sum * Fraction(1, ($p + 1));
}

my $n = Symbol('n');

foreach my $p (0 .. 10) {
    say "F($p) = ", faulhaber_s_formula($p, $n)->simple->pretty;
}

examples/quadratic_polynomials_zeros.pl  view on Meta::CPAN

# Then:
#   P(x) = c * (1 - x/m) * (1 - x/n)

use 5.014;
use strict;
use warnings;

use Math::Bacovia qw(:all);
use Math::AnyNum qw(isqrt);

sub integer_quadratic_formula {
    my ($x, $y, $z) = @_;
    (
        Fraction((-$y + isqrt($y**2 - 4 * $x * $z)), (2 * $x)),
        Fraction((-$y - isqrt($y**2 - 4 * $x * $z)), (2 * $x)),
    );
}

my @poly = (
    [  3, -15,   -42],
    [ 20, -97, -2119],
    [-43,  29, 14972],
);

my $x = Symbol('x');

foreach my $t (@poly) {
    my ($x1, $x2) = integer_quadratic_formula(@$t);

    my $expr = $t->[0] * $x**2 + $t->[1] * $x + $t->[2];

    my $f1 = (1 - $x / $x1);
    my $f2 = (1 - $x / $x2);

    printf("%s = %s * %s * %s\n",
        $expr->pretty,
        $f1->simple->pretty,
        $f2->simple->pretty,



( run in 0.235 second using v1.01-cache-2.11-cpan-26ccb49234f )