# NAME

Math::Matrix - Multiply and invert Matrices

# SYNOPSIS

use Math::Matrix;

# DESCRIPTION

The following methods are available:

## 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]);
```

If you call `new`

as method, a zero filled matrix with identical deminsions is returned.

## clone

You can clone a matrix by calling:

` $b = $a->clone;`

## diagonal

A constructor method that creates a diagonal matrix from a single list or array of numbers.

```
$p = Math::Matrix->diagonal(1, 4, 4, 8);
$q = Math::Matrix->diagonal([1, 4, 4, 8]);
```

The matrix is zero filled except for the diagonal members, which take the value of the vector

The method returns **undef** in case of error.

## tridiagonal

A constructor method that creates a matrix from vectors of numbers.

```
$p = Math::Matrix->tridiagonal([1, 4, 4, 8]);
$q = Math::Matrix->tridiagonal([1, 4, 4, 8], [9, 12, 15]);
$r = Math::Matrix->tridiagonal([1, 4, 4, 8], [9, 12, 15], [4, 3, 2]);
```

In the first case, the main diagonal takes the values of the vector, while both of the upper and lower diagonals's values are all set to one.

In the second case, the main diagonal takes the values of the first vector, while the upper and lower diagonals are each set to the values of the second vector.

In the third case, the main diagonal takes the values of the first vector, while the upper diagonal is set to the values of the second vector, and the lower diagonal is set to the values of the third vector.

The method returns **undef** in case of error.

## size

You can determine the dimensions of a matrix by calling:

` ($m, $n) = $a->size;`

## 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.

## invert

Invert a Matrix using `solve`

.

## multiply_scalar

Multiplies a matrix and a scalar resulting in a matrix of the same dimensions with each element scaled with the scalar.

` $a->multiply_scalar(2); scale matrix by factor 2`

## add

Add two matrices of the same dimensions.

## subtract

Shorthand for `add($other->negative)`

## equal

Decide if two matrices are equal. The criterion is, that each pair of elements differs less than $Math::Matrix::eps.

## slice

Extract columns:

` a->slice(1,3,5);`

## diagonal_vector

Extract the diagonal as an array:

` $diag = $a->diagonal_vector;`

## tridiagonal_vector

Extract the diagonals that make up a tridiagonal matrix:

` ($main_d, $upper_d, $lower_d) = $a->tridiagonal_vector;`

## determinant

Compute the determinant of a matrix.

## dot_product

Compute the dot product of two vectors.

## absolute

Compute the absolute value of a vector.

## normalizing

Normalize a vector.

## cross_product

Compute the cross-product of vectors.

Prints the matrix on STDOUT. If the method has additional parameters, these are printed before the matrix is printed.

## pinvert

Compute the pseudo-inverse of the matrix: ((A'A)^-1)A'

# EXAMPLE

```
use Math::Matrix;
srand(time);
$a = new Math::Matrix ([rand,rand,rand],
[rand,rand,rand],
[rand,rand,rand]);
$x = new Math::Matrix ([rand,rand,rand]);
$a->print("A\n");
$E = $a->concat($x->transpose);
$E->print("Equation system\n");
$s = $E->solve;
$s->print("Solutions s\n");
$a->multiply($s)->print("A*s\n");
```

# AUTHOR

Ulrich Pfeifer <*pfeifer@ls6.informatik.uni-dortmund.de*>

Brian J. Watson <*bjbrew@power.net*>

Matthew Brett <matthew.brett@mrc-cbu.cam.ac.uk>