Mailing List Archive

Announcing Math::Matrix and Math::Approx 0.1
Hello!

Trying to "save the world" I made two modules public available I
currently wrote.

In a little one-shot-programs I had to invert a matrix. Since I did
not want to install Math::Pari for this little program I wrote my own
matrix module. I started with a script by Dan Carson posted in
comp.lang.perl. The script could not handle zero diagonal
elements. Also matrices with interdependent rows could not be handled.

The second module uses Math::Matrix to approximate a sample of
x,y-values by a set of functions given as parameter. E.g. polynomial
approximations can be generated.

I append parts of the documentation for convenience. Any help in
completing the modules is welcome.

You can get the modules from your favorite CPAN archive:

.../CPAN/modules/by-module/Math


Ulrich
------------------------------------------------------------------------
NAME
Math::Matrix

METHODS
new

Constructor arguments are a list of references to arrays
of the same length. The arrays are copied. The method
returns undef in case of error.

$a = new Math::Matrix ([rand,rand,rand],
[rand,rand,rand],
[rand,rand,rand]);

concat

Concatenates two matrices of same row count. The result is
a new matrix or undef in case of error.

$b = new Math::Matrix ([rand],[rand],[rand]);
$c = $a->concat($b);

transpose

Returns the transposed matrix. This is the matrix where
colums and rows of the argument matrix are swaped.

multiply

Multiplies two matrices where the length of the rows in
the first matrix is the same as the length of the columns
in the second matrix. Returns the product or undef in case
of error.

solve

Solves a equation system given by the matrix. The number
of colums must be greater than the number of rows. If
variables are dependent from each other, the second and
all further of the dependent coefficients are 0. This
means the method can handle such systems. The method
returns a matrix containing the solutions in its columns
or undef in case of error.
[...]
------------------------------------------------------------------------
NAME
Math::Approx

METHODS
new

new Math::Approx (\&poly, 5, %x);

The first argument after the class name must be a
reference to function which takes two arguments: The
degree and the x value.

For interpolation with plain polynomials poly can be
defined as:

sub poly {
my($n,$x) = @_;

return $x ** $n;
}

The second argument is the maximum degree which should be
used for interpolation. Degrees start with 0.

The rest of the arguments are treated as pairs of x and y
samples which should be approximated.

The method returns a Math::Approx reference.

approx

$approximation->approx(17);

The method returns the approximated y value for the x
value given as argument.

fit

$approximation->fit;

Returns the medim square error for the data points.

EXAMPLE
use Math::Approx;

sub poly {
my($n,$x) = @_;

return $x ** $n;
}

for (1..20) {
$x{$_} = sin($_/10)*cos($_/30)+0.3*rand;
}

$a = new Math::Approx (\&poly, 5, %x);
$a->print;
$a->plot("mist");
print "Fit: ", $a->fit, "\n";