Home > Linux > Programming > Perl Modules

# Math::LinearCombination 0.03

 User Rating:Rated by: NOT RATED0 user(s)
 Developer: License / Price: Last Updated: Category: Wim Verhaegen | More programs Perl Artistic License / FREE March 17th, 2011, 06:59 GMT ROOT / Programming / Perl Modules

 Read user reviews (0)  Refer to a friend  Subscribe

## Sum of variables with a numerical coefficient

Math::LinearCombination is a Perl module for representing mathematical linear combinations of variables, i.e. expressions of the format

a1 * x1 + a2 * x2 + ... + an * xn

with x1, x2, ..., xn variables, and a1, a2, ..., an numerical coefficients. Evaluation and manipulation of linear combinations is also supported. The numerical coefficients a_i and variables x_i are stored as pairs in an internal data structure and should not be manipulated directly. All access and manipulation should be performed through the methods.

SYNOPSIS

use Math::LinearCombination;
use Math::SimpleVariable; # for the variable objects

# build a linear combination
my \$x1 = new Math::SimpleVariable(name => 'x1');
my \$x2 = new Math::SimpleVariable(name => 'x2');
my \$lc = new Math::LinearCombination();
\$lc->add_entry(var => \$x1, coeff => 3.0);
\$lc->add_entry(var => \$x2, coeff => 1.7);
\$lc->add_entry(var => \$x2, coeff => 0.3); # so x2 has a coefficient of 2.0
print \$lc->stringify(), "\n";

# do some manipulations
\$lc->negate_inplace(); # reverts the coefficient signs
\$lc->multiply_with_constant_inplace(2.0); # doubles all coefficients

# evaluate the linear combination
\$x1->{value} = 3;
\$x2->{value} = -1;
print \$lc->evaluate(), "\n"; # prints -14

Product's homepage

Requirements:

· Perl

### TAGS:

numerical coefficient | variables sum | Perl module | liniar | variables | sum

Go to top