|
|
|
<?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');
}
for ($i = 0; $i < $this->rowCount; $i++) {
$res[$i] = array();
for ($j = 0; $j < $this->columnCount; $j++) {
$res[$i][$j] = $this->matrix[$i][$j] + $rhs->matrix[$i][$j];
}
}
return new Matrix($res);
}
/**
* 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');
}
for ($i = 0; $i < $this->rowCount; $i++) {
$res[$i] = array();
for ($j = 0; $j < $this->columnCount; $j++) {
$res[$i][$j] = $this->matrix[$i][$j] - $rhs->matrix[$i][$j];
}
}
return new Matrix($res);
}
function numberOfRows() {
return $this->rowCount;
}
function numberOfColumns() {
return $this->columnCount;
}
/**
* HTML representation of the Matrix
* only for debugging purposes
* @return string table
*
*/
function __toString() {
$s = '<table border="1">';
for ($i = 0; $i < $this->rowCount; $i++) {
$s .= '<tr>';
for ($j = 0; $j < $this->columnCount; $j++) {
$s .= '<td>'.$this->matrix[$i][$j].'</td>';
}
$s .= '</tr>';
}
return $s.'</table>';
}
private function multiplyByNumber($number) {
for ($i = 0; $i < $this->rowCount; $i++) {
$res[$i] = array();
for ($j = 0; $j < $this->columnCount; $j++) {
$res[$i][$j] = $this->matrix[$i][$j] * $number;
}
}
return new Matrix($res);
}
private function multiplyByMatrix(Matrix $rhs) {
if ($this->columnCount !== $rhs->rowCount) {
throw new Exception('The matrices cannot be multiplied');
}
$res = array();
for ($i = 0; $i < $this->rowCount; $i++) {
$res[$i] = array();
for ($j = 0; $j < $rhs->columnCount ; $j++) {
$res[$i][$j] = 0;
for ($k = 0; $k < $this->columnCount; $k++) {
$res[$i][$j] += $this->matrix[$i][$k] * $rhs->matrix[$k][$j];
}
}
}
return new Matrix($res);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Matrix</title>
</head>
<h1>Example</h1>
<body>
<?php
$a = array(
array(1,2,3),
array(1,2,0)
);
$b = array(
array(2,4,6),
array(4,5,4),
array(1,1,0),
);
$mA = new Matrix($a);
$mB = new Matrix($b);
$multiply = $mA->multiply($mB);
// what's in it?
echo $multiply->__toString();
echo '<hr>';
echo $mA->multiply(6);
?>
</body>
</html>
|
| PHPDRAW, the php wannabe Photoshop ;-) Categories : PHP, PHP Classes, GD image library, Arrays | | | Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it. Categories : PHP Classes, Arrays, PHP | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | |
| | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout. Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs | | | Array Insertion Categories : PHP, PHP Classes, Arrays | | | How to get the source of a site into an array. Categories : Arrays, HTML, PHP | | | PHPShell - A class implementing a shell written in PHP Categories : PHP, PHP Classes, CLI | | | Render TTF Text to PNG. Text message, font, size, rotation, padding, color, background, and transparency can all be defined via URL. Categories : PHP, PHP Classes, Graphics | | | phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | Example Shopping cart class Categories : Ecommerce, PHP, PHP Classes | | | simple shopping cart for php3 Categories : PHP, PHP Classes, Complete Programs, Ecommerce | | | How to pass an array from one PHP Script to another via an HTML form Categories : PHP, HTML and PHP, Arrays | |
| | | | Shea Sollars wrote : 1339
VERY nice.
| | | | 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... :)
| |
|
|