<?php
/**
* Basic class for matrix operations
*
*/
class Matrix {
/**
* the matrix, a two dimensional array
* @var array
*/
private $matrix;
/**
* total of rows of the matrix
* @var int
*/
private $rowCount = 0;
/**
* total of columns of the matrix
* @var int
*/
private $columnCount = 0;
/**
* Constructs a new Matrix based on the two-dimensional that is passed in
* @param array $matrix a two-dimensional array. $matrix is passed by reference
and should be indexed without gaps, starting from 0!
the first dimension contains arrays which are the rows
*/
function __construct(&$matrix) {
$this->matrix =& $matrix;
$this->rowCount = count($matrix);
$this->columnCount = count(current($matrix));
//parent::__construct($array);
}
/**
* returns the row of the matrix at position $index
* if $index is an invalid key of the row, an exception will be thrown
* @param int $index key of the row that has to be retrieved. possible values ranges from 0 to numberOfRows - 1
* @return array
*/
function getRow($index) {
if (!array_key_exists($index, $this->matrix)) {
throw new Exception('invalid row index provided');
}
return $this->matrix[$index];
}
/**
* returns the column of the matrix at position $index
* if $index is an invalid key of the row, an exception will be thrown
* @param int $index key of the row that has to be retrieved. possible values ranges from 0 to numberOfRows - 1
* @return array
*/
function getColumn($index) {
$res = array();
if ($index > $this->columnCount - 1 || $index < 0) {
throw new Exception('invalid column index provided');
}
foreach ($this->matrix as $row) {
$res[] = $row[$index];
}
return $res;
}
/**
* multiply $rhs with this Matrix
* @param mixed $rhs
if $rhs is numeric, each value of this matrix is multiplicated by $rhs (so called scalar multiplication)
if $rhs is a Matrix, a ordinary matrix product wil be returned
see <a href="http://en.wikipedia.org/wiki/Matrix_multiplication">http://en.wikipedia.org/wiki/Matrix_multiplication</a> for a formal explanation
* @return Matrix new Matrix-object as the result of the operation
*/
function multiply($rhs) {
if (is_numeric($rhs)) {
return $this->multiplyByNumber($rhs);
} elseif ($rhs instanceof Matrix) {
return $this->multiplyByMatrix($rhs);
} else {
throw new Exception('invalid operand');
}
}
/**
* sums this and $rhs matrix.
* The dimensions of the both matrices have to be te same.<br>see <a href="http://en.wikipedia.org/wiki/Matrix_%28mathematics%29#Sum">http://en.wikipedia.org/wiki/Matrix_%28mathematics%29#Sum</a> for a formal explanation
* @param Matrix $rhs
* @return Matrix new Matrix-object as the result of the operation
*/
function sum(Matrix $rhs) {
if ($rhs->rowCount !== $this->rowCount && $rhs->columnCount !== $this->columnCount) {
throw new Exception('matrices cannot be added, because they have different dimensions');
}
/**
* subtracts $rhs from this matrix
* The dimensions of the both matrices have to be te same. Works like sum(), but this time the operation is subtract<br>
* @param Matrix $rhs
* @return Matrix new Matrix-object as the result of the operation
*/
function minus(Matrix $rhs) {
if ($rhs->rowCount !== $this->rowCount && $rhs->columnCount !== $this->columnCount) {
throw new Exception('matrices cannot be subtracted, because they have different dimensions');
}
Joost Langen wrote :1341
I tried to test your script, but did`t succeed. this is the error:
" Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or `}` in /home/resellers/841289/841289/josefem.nl/www/test/matrix.php on line 12 "
I can`t see what`s wrong.
gr Joost
Exception e wrote :1342
In response to Joost Langen I`ve sent a private message, but I realised that more people can have the same problem as Joost has.
Therefore, I repeat my answer here: I think that your php version is too low. In order to run this code you need at least PHP5.
If you want to check your php version, just have a look at php_info or simply call <?php echo `my php version is: `.PHP_VERSION; ?>
In order to run this code under PHP4, you should strip of the `private` keywords and replace the exceptions with errors. For example:
trigger_error(`invalid row index provided`, E_USER_ERROR);
But I like to encourage the use of PHP5... :)