Math-Series
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Math/Series.pm view on Meta::CPAN
This parameter indicates whether or not to cache the calculated series'
elements for faster direct access. It defaults to true. At run-time,
the caching behaviour may be altered using the cached() method.
=item start_index
The lower boundary for the series' summation. It defaults to 0, but may be
set to any positive integer or zero.
=back
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
croak "Invalid number of arguments to Math::Series->new()."
if @_ % 2;
my %args = @_;
my $formula = $args{formula};
croak "Math::Series->new() requires a formula parameter."
if not defined $formula;
my $parsed = $Parser->parse($formula);
croak "Error parsing formula." if not defined $parsed;
my $start = $args{start_value};
croak "A starting value must be supplied to Math::Series->new() as\n"
. "second argument."
if not defined $start;
$start = $Parser->parse($start);
croak "Error parsing starting value." if not defined $start;
my $variable = $args{previous_var};
$variable = 'x' if not defined $variable;
my $iter_var = $args{iteration_var};
$iter_var = 'n' if not defined $iter_var;
my $cached = 1;
$cached = $args{cached} if exists $args{cached};
my $start_index = $args{start_index};
$start_index = 0 if not defined $start_index;
my $self = {
cached => $cached,
var => $variable,
formula => $parsed,
current => $start_index,
current_value => $start,
cache => [$start],
iter_var => $iter_var,
start_index => $start_index,
};
return bless $self => $class;
}
=item next()
The next() method returns the next element of the series and advances the
iterator by one. This is the prefered method of walking down a series'
recursion.
=cut
sub next {
my $self = shift;
my $current_index = $self->{current};
my $current_value = $self->{current_value};
my $next_index = $current_index + 1;
my $start_index = $self->{start_index};
if ( $self->{cached}
and defined $self->{cache}[ $next_index - $start_index ] )
{
$self->{current_value} = $self->{cache}[ $next_index - $start_index ];
$self->{current} = $next_index;
return $current_value;
}
my $next_value = $current_value->new();
my $add = $self->{formula}->new();
$add->implement(
$self->{var} => $current_value,
$self->{iter_var} => $current_index + 1
);
$add = $add->simplify();
$next_value += $add;
$next_value = $next_value->simplify();
$self->{cache}[ $next_index - $start_index ] = $next_value
if $self->{cached};
$self->{current} = $next_index;
$self->{current_value} = $next_value;
return $current_value;
}
=item cached()
Returns a true value if the series is currently being cached, false if it
isn't. By default, new objects have caching enabled. It is suggested that you
only disable caching if space is an issue and you will only walk the series
uni-directionally and only once.
cached() can be used to change the caching behaviour. If the first argument is
true, caching will be enabled. If it is false, caching will be disabled.
=cut
# cached inherited.
=item current_index()
Returns the index of the current element. That is, the index of the element
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.206 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )