Math_Matrix
[ class tree: Math_Matrix ] [ index: Math_Matrix ] [ all elements ]

Class: Math_Matrix

Source Location: /Math_Matrix-0.8.7/Math/Matrix.php

Class Overview


Defines a matrix object.


Author(s):

Version:

  • 1.0

Methods


Inherited Variables

Inherited Methods


Class Details

[line 48]
Defines a matrix object.

A matrix is implemented as an array of arrays such that:

 [0][0] [0][1] [0][2] ... [0][M]
 [1][0] [1][1] [1][2] ... [1][M]
 ...
 [N][0] [n][1] [n][2] ... [n][M]

i.e. N rows, M colums

Originally this class was part of NumPHP (Numeric PHP package)



[ Top ]


Method Detail

Math_Matrix (Constructor)   [line 145]

object Math_Matrix Math_Matrix( [array|Math_Matrix $data = null])

Constructor for the matrix object

Parameters:

array|Math_Matrix   $data   —  a numeric array of arrays of a Math_Matrix object

[ Top ]

add   [line 1023]

boolean|PEAR_Error add( object Math_Matrix $m1)

Adds a matrix to this one

Parameters:

object Math_Matrix   $m1   — 

[ Top ]

cloneMatrix   [line 305]

object Math_Matrix|PEAR_Error cloneMatrix( )

Returns a new Math_Matrix object with the same data as the current one
  • Return: a Math_Matrix objects on succes, a PEAR_Error otherwise.
  • Access: public

[ Top ]

determinant   [line 691]

number|PEAR_Error determinant( )

Calculates the matrix determinant using Gaussian elimination with partial pivoting.

At each step of the pivoting proccess, it checks that the normalized determinant calculated so far is less than 10^-18, trying to detect singular or ill-conditioned matrices

  • Return: a number on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

getCol   [line 423]

array|Math_Vector|PEAR_Error getCol( integer $col, [optional $asVector = false])

Returns the column with the given index

This method checks that matrix has been initialized and that the column requested is not outside the range of column.

  • Return: an array of numbers or a Math_Vector on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $col   — 
optional   $asVector   —  boolean $asVector whether to return a Math_Vector or a simple array. Default = false.

[ Top ]

getData   [line 215]

array|PEAR_Error getData( )

Returns the array of arrays.
  • Return: an array of array of numbers on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

getDiagonal   [line 915]

object Math_Vector|PEAR_Error &getDiagonal( )

Returns the diagonal of a square matrix as a Math_Vector
  • Return: Math_Vector on success, PEAR_Error otherwise
  • Access: public

[ Top ]

getElement   [line 343]

number|PEAR_Error getElement( integer $row, integer $col)

Returns the value of the element at (row,col)
  • Return: a number on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $row   — 
integer   $col   — 

[ Top ]

getMax   [line 582]

number|PEAR_Error getMax( )

Returns the maximum value of the elements in the matrix
  • Return: a number on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

getMaxIndex   [line 630]

array|PEAR_Error getMaxIndex( )

Gets the position of the element with the maximum value
  • Return: an array of two numbers on success, FALSE if value is not found, and PEAR_Error otherwise
  • See: Math_Matrix::getValueIndex()
  • Access: public

[ Top ]

getMin   [line 569]

number|PEAR_Error getMin( )

Returns the minimum value of the elements in the matrix
  • Return: a number on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

getMinIndex   [line 616]

array|PEAR_Error getMinIndex( )

Gets the position of the element with the minimum value
  • Return: an array of two numbers on success, FALSE if value is not found, and PEAR_Error otherwise
  • See: Math_Matrix::getValueIndex()
  • Access: public

[ Top ]

getRow   [line 363]

array|Math_Vector|PEAR_Error getRow( integer $row, [optional $asVector = false])

Returns the row with the given index

This method checks that matrix has been initialized and that the row requested is not outside the range of rows.

  • Return: an array of numbers or a Math_Vector on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $row   — 
optional   $asVector   —  boolean $asVector whether to return a Math_Vector or a simple array. Default = false.

[ Top ]

getSize   [line 264]

array|PEAR_Error getSize( )

Returns an array with the number of rows and columns in the matrix
  • Return: an array of integers on success, a PEAR_Error object otherwise
  • Access: public

[ Top ]

getSubMatrix   [line 887]

object Math_Matrix|PEAR_Error &getSubMatrix( $row, $col, $nrows, $ncols)

Returns a submatrix from the position (row, col), with nrows and ncols
  • Return: Math_Matrix on success, PEAR_Error otherwise
  • Access: public

Parameters:

   $row   — 
   $col   — 
   $nrows   — 
   $ncols   — 

[ Top ]

getValueIndex   [line 596]

array|PEAR_Error getValueIndex( numeric $val)

Gets the position of the first element with the given value
  • Return: an array of two numbers on success, FALSE if value is not found, and PEAR_Error otherwise
  • Access: public

Parameters:

numeric   $val   — 

[ Top ]

getZeroThreshold   [line 245]

float getZeroThreshold( )

Returns the value of the upper bound used to minimize round off errors
  • Access: public

[ Top ]

invert   [line 788]

number|PEAR_Error invert( )

Inverts a matrix using Gauss-Jordan elimination with partial pivoting
  • Return: the value of the matrix determinant on success, PEAR_Error otherwise
  • See: Math_Matrix::scaleRow()
  • Access: public

[ Top ]

isEmpty   [line 254]

boolean isEmpty( )

Checks if the matrix has been initialized.
  • Return: TRUE on success, FALSE otherwise
  • Access: public

[ Top ]

isMatrix   [line 1311]

boolean isMatrix( &$matrix, object Math_Matrix $matrix)

Checks if the object is a Math_Matrix instance
  • Return: TRUE on success, FALSE otherwise
  • Access: public

Parameters:

object Math_Matrix   $matrix   — 
   &$matrix   — 

[ Top ]

isSquare   [line 276]

boolean isSquare( )

Checks if it is a square matrix (i.e. num rows == num cols)
  • Return: TRUE on success, FALSE otherwise
  • Access: public

[ Top ]

makeHankel   [line 1444]

object Math_Matrix|PEAR_Error &makeHankel( array $c, [optional $r = null])

Returns a Hankel matrix from a array of size m (C), and (optionally) of

an array if size n (R). C will define the first column and R the last row. If R is not defined, C will be used. Also, if the last element of C is not the same to the first element of R, the last element of C is used.

H(i,j) = C(i+j-1), i+j-1 <= m H(i,j) = R(i+j-m), otherwise where: i = 1..m j = 1..n

  • Return: a Hankel matrix on success, a PEAR_Error otherwise
  • Access: public

Parameters:

array   $c   —  first column of Hankel matrix
optional   $r   —  array $r last row of Hankel matrix

[ Top ]

makeHilbert   [line 1413]

object Math_Matrix|PEAR_Error &makeHilbert( integer $size)

Returns a Hilbert matrix of the given size: H(i,j) = 1 / (i + j - 1) where {i,j = 1..n}
  • Return: a Hilber matrix on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $size   —  number of rows and columns in the Hilbert matrix

[ Top ]

makeIdentity   [line 1401]

object Math_Matrix|PEAR_Error &makeIdentity( integer $size)

Returns the identity matrix of the given size. An alias of Math_Matrix::makeUnit()
  • Return: a square unit Math_Matrix instance on success, PEAR_Error otherwise
  • See: Math_Matrix::makeUnit()
  • Access: public

Parameters:

integer   $size   —  number of rows and columns in the generated matrix

[ Top ]

makeMatrix   [line 1327]

object Math_Matrix|PEAR_Error &makeMatrix( integer $nrows, integer $ncols, numeric $value)

Returns a Math_Matrix object of size (nrows, ncols) filled with a value
  • Return: Math_Matrix instance on success, PEAR_Error otherwise
  • Access: public

Parameters:

integer   $nrows   —  number of rows in the generated matrix
integer   $ncols   —  number of columns in the generated matrix
numeric   $value   —  the fill value

[ Top ]

makeOne   [line 1348]

object Math_Matrix|PEAR_Error &makeOne( integer $nrows, integer $ncols)

Returns the Math_Matrix object of size (nrows, ncols), filled with the value 1 (one)

Parameters:

integer   $nrows   —  number of rows in the generated matrix
integer   $ncols   —  number of columns in the generated matrix

[ Top ]

makeUnit   [line 1376]

object Math_Matrix|PEAR_Error &makeUnit( integer $size)

Returns a square unit Math_Matrix object of the given size

A unit matrix is one in which the elements follow the rules: e[i][j] = 1, if i == j e[i][j] = 0, if i != j Such a matrix is also called an 'identity matrix'


Parameters:

integer   $size   —  number of rows and columns in the generated matrix

[ Top ]

makeZero   [line 1360]

object Math_Matrix|PEAR_Error &makeZero( integer $nrows, integer $ncols)

Returns the Math_Matrix object of size (nrows, ncols), filled with the value 0 (zero)

Parameters:

integer   $nrows   —  number of rows in the generated matrix
integer   $ncols   —  number of columns in the generated matrix

[ Top ]

multiply   [line 1158]

boolean|PEAR_Error multiply( &$B, object Math_Matrix $m1)

Multiplies this matrix (A) by another one (B), and stores the result back in A

Parameters:

object Math_Matrix   $m1   — 
   &$B   — 

[ Top ]

norm   [line 291]

float|PEAR_Error norm( )

Returns the Euclidean norm of the matrix.

Euclidean norm = sqrt( sum( e[i][j]^2 ) )

  • Return: a number on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

normalizedDeterminant   [line 767]

number|PEAR_Error normalizedDeterminant( )

Returns the normalized determinant = abs(determinant)/(euclidean norm)
  • Return: a positive number on success, a PEAR_Error otherwise
  • Access: public

[ Top ]

readFromFile   [line 1229]

void &readFromFile( $filename, [ $format = 'serialized'])

Create a matrix from a file, using data stored in the given format
  • Access: public

Parameters:

   $filename   — 
   $format   — 

[ Top ]

scale   [line 1099]

boolean|PEAR_Error scale( numeric $scale)

Scales the matrix by a given factor

Parameters:

numeric   $scale   —  the scaling factor

[ Top ]

scaleRow   [line 1125]

boolean|PEAR_Error scaleRow( integer $row, numeric $factor)

Multiplies (scales) a row by the given factor

Parameters:

integer   $row   —  the row index
numeric   $factor   —  the scaling factor

[ Top ]

setCol   [line 456]

boolean|PEAR_Error setCol( integer $col, array $arr)

Sets the column with the given index to the array

This method checks that the column is less than the size of the matrix columns, and that the array size equals the number of rows in the matrix.

  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $col   —  index of the column
array   $arr   —  array of numbers

[ Top ]

setData   [line 162]

boolean|PEAR_Error setData( array $data)

Validates the data and initializes the internal variables (except for the determinant).

The validation is performed by by checking that each row (first dimension in the array of arrays) contains the same number of colums (e.g. arrays of the same size)

  • Return: true on success, a PEAR_Error object otherwise
  • Access: public

Parameters:

array   $data   —  array of arrays of numbers or a valid Math_Matrix object

[ Top ]

setElement   [line 322]

boolean|PEAR_Error setElement( integer $row, integer $col, numeric $value)

Sets the value of the element at (row,col)
  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $row   — 
integer   $col   — 
numeric   $value   — 

[ Top ]

setRow   [line 393]

boolean|PEAR_Error setRow( integer $row, array $arr)

Sets the row with the given index to the array

This method checks that the row is less than the size of the matrix rows, and that the array size equals the number of columns in the matrix.

  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $row   —  index of the row
array   $arr   —  array of numbers

[ Top ]

setZeroThreshold   [line 231]

boolean|PEAR_Error setZeroThreshold( number $epsilon)

Sets the threshold to consider a numeric value as zero: if number <= epsilon then number = 0
  • Return: true if successful, a PEAR_Error otherwise
  • Access: public

Parameters:

number   $epsilon   —  the upper bound value

[ Top ]

solve   [line 1509]

object Math_Vector|PEAR_Error solve( object Math_Matrix $a, object Math_Vector $b)

Solves a system of linear equations: Ax = b

A system such as:

     a11*x1 + a12*x2 + ... + a1n*xn = b1
     a21*x1 + a22*x2 + ... + a2n*xn = b2
     ...
     ak1*x1 + ak2*x2 + ... + akn*xn = bk
can be rewritten as:
     Ax = b
where:
  • A is matrix of coefficients (aij, i=1..k, j=1..n),
  • b a vector of values (bi, i=1..k),
  • x the vector of unkowns (xi, i=1..n)
Using: x = (Ainv)*b where:
  • Ainv is the inverse of A


Parameters:

object Math_Matrix   $a   —  the matrix of coefficients
object Math_Vector   $b   —  the vector of values

[ Top ]

solveEC   [line 1566]

object Math_Vector|PEAR_Error solveEC( object Math_Matrix $a, object Math_Vector $b)

Solves a system of linear equations: Ax = b, using an iterative error correction algorithm

A system such as:

     a11*x1 + a12*x2 + ... + a1n*xn = b1
     a21*x1 + a22*x2 + ... + a2n*xn = b2
     ...
     ak1*x1 + ak2*x2 + ... + akn*xn = bk
can be rewritten as:
     Ax = b
where:
  • A is matrix of coefficients (aij, i=1..k, j=1..n),
  • b a vector of values (bi, i=1..k),
  • x the vector of unkowns (xi, i=1..n)
Using: x = (Ainv)*b where:
  • Ainv is the inverse of A
The error correction algorithm uses the approach that if:
  • xp is the approximate solution
  • bp the values obtained from pluging xp into the original equation
We obtain


Parameters:

object Math_Matrix   $a   —  the matrix of coefficients
object Math_Vector   $b   —  the vector of values

[ Top ]

sub   [line 1061]

boolean|PEAR_Error sub( &$m1, object Math_Matrix $m1)

Substracts a matrix from this one

Parameters:

object Math_Matrix   $m1   — 
   &$m1   — 

[ Top ]

swapCols   [line 514]

boolean|PEAR_Error swapCols( integer $i, integer $j)

Swaps the columns with the given indices
  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $i   — 
integer   $j   — 

[ Top ]

swapRowCol   [line 541]

boolean|PEAR_Error swapRowCol( integer $row, integer $col)

Swaps a given row with a given column. Only valid for square matrices.
  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $row   —  index of row
integer   $col   —  index of column

[ Top ]

swapRows   [line 487]

boolean|PEAR_Error swapRows( integer $i, integer $j)

Swaps the rows with the given indices
  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

integer   $i   — 
integer   $j   — 

[ Top ]

toHTML   [line 960]

a toHTML( )

Returns an HTML table representation of the matrix elements
  • Return: string on success, PEAR_Error otherwise
  • Access: public

[ Top ]

toString   [line 936]

string|PEAR_Error toString( [optional $format = '%6.2f'])

Returns a simple string representation of the matrix
  • Return: a string on success, PEAR_Error otherwise
  • Access: public

Parameters:

optional   $format   —  string $format a sprintf() format used to print the matrix elements (default = '%6.2f')

[ Top ]

trace   [line 668]

number|PEAR_Error trace( )

Returns the trace of the matrix. Trace = sum(e[i][j]), for all i == j
  • Return: a number on success, PEAR_Error otherwise
  • Access: public

[ Top ]

transpose   [line 643]

boolean|PEAR_Error transpose( )

Transpose the matrix rows and columns
  • Return: TRUE on success, PEAR_Error otherwise
  • Access: public

[ Top ]

vectorMultiply   [line 1198]

object Math_Vector|PEAR_Error &vectorMultiply( &$v1, object Math_Vector $v1)

Multiplies a vector by this matrix

Parameters:

object Math_Vector   $v1   — 
   &$v1   — 

[ Top ]

writeToFile   [line 1276]

boolean|PEAR_Error writeToFile( object Math_Matrix $matrix, string $filename, [optional $format = 'serialized'])

Writes matrix object to a file using the given format
  • Return: TRUE on success, a PEAR_Error otherwise
  • Access: public

Parameters:

object Math_Matrix   $matrix   —  the matrix object to store
string   $filename   —  name of file to contain the matrix data
optional   $format   —  string $format one of 'serialized' (default) or 'csv'

[ Top ]

_maxElementIndex   [line 989]

an _maxElementIndex( $r)

Returns the index of the row with the maximum value under column of the element e[i][i]
  • Return: integer
  • Access: public

Parameters:

   $r   — 

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:39:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.