Image

Imageastrro wrote in Imagephp_dev

Listens: Jimmy Eat World - Rockstar

Good .. evening all. Here is my dilemma. I'm writing a matrix class and it includes the powerful rref() function. I am having trouble with this function sometimes. It doesn't always work. For example, the following work:

| 2 | 0 | 2 | 0 |
| 0 | 2 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 |

| 2 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 0 | 2 | 3 | -1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 | 0 |

However these do not:
| 1 | 0 | 3 | 0 |
| 3 | 0 | 9 | 0 |
| -2 | 3 | 0 | 0 |
| 0 | 1 | 2 | 0 |
| 1 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 |

| 1 | 0 | 2 | 0 | 0 |
| 3 | 0 | 0 | 1 | 0 |
| 9 | 3 | 9 | 3 | 0 |
| 0 | 2 | 0 | 1 | 0 |
| 0 | 1 | 3 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 | 0 |

| 1 | 0 | 0 | 1 | 0 | 0 |
| 1 | 0 | 4 | 4 | 1 | 0 |
| 0 | 1 | 0 | 0 | 2 | 0 |
| 0 | 1 | -2 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 | 0 | 0 |

I've tested some others, but to now avail. I've combed over the function and any other functions pertaining to it countless times. Any help would be greatly appreciated. My Matrix class is here:
class Matrix
{
	var $mat;		/* [][] double	*/
	var $rows, $cols;	/* int		*/

	function Matrix($rows, $cols=false){
		if ($cols == false){
			$this->Matrix_from_array($rows);
		} else {
			$this->Matrix_from_nothing( $rows , $cols );
		}
	}

	function Matrix_from_nothing( $r , $c ){
		$this->mat = array();
		$this->rows = $r;
		$this->cols = $c;

		for ($i = 0; $i < $this->rows; $i++){
			for ($j = 0; $j < $this->cols; $j++){
				$this->mat[$i][$j] = 0;
			}
		}
	}

	//from an array
	function Matrix_from_array( $array ){
		$this->mat = $array;

		$this->rows = count($this->mat);
		$this->cols = count($this->mat[0]);
	}

	//return int - number of rows
	function get_row_dimension(){
		return $this->rows;
	}

	//return int - number of cols
	function get_col_dimension(){
		return $this->cols;
	}

	//void - fills the matrix with a Real number
	function fill( $c ){
		for ($i = 0; $i < $this->rows; $i++)
			for ($j = 0; $j < $this->cols; $j++)
				$this->mat[$i][$j] = $c;
	}

	//void - multiplies the matrix by a real
	function multiply( $m ){
		for ($i = 0; $i < $this->rows; $i++){
			for ($j = 0; $j < $this->cols; $j++){
@				$this->set( $i+1, $j+1, ($this->mat[$i][$j] * $m) );
			}
		}
	}

	//void - divides the matrix by a real
	function divide( $d ){
		for ($i = 0; $i < $this->rows; $i++){
			for ($j = 0; $j < $this->cols; $j++){
				$this->set( $i+1, $j+1, ($this->mat[$i][$j] / $d ) );
			}
		}
	}

	//void - sets a specific index of the matrix to a given Real
	function set( $r, $c, $num ){
		$this->mat[$r-1][$c-1] = $num;
	}

	//void - sets a row to another matrix object (dimensions 1x)
	function set_row( $r, $m ){
		/* if ($m->get_row_dimension() != 1 || $m->get_col_dimension() != $this->cols)
			return false; */
		if(count($m->mat[0]) > 1)
			$this->mat[$r-1] = $m->mat[0];
		else
			$this->mat[$r-1] = $m->mat;
	}

	//void - sets a column to another matrix object (dimensions 1x)
	function set_column( $c, $m ){
		/* if ($m->get_col_dimension() != 1 || $m->get_row_dimension() != $this->rows)
			return false; */

		$tran = $this->transpose();

		$tran->mat[$c-1] = $m->mat[0];

		$tran = $tran->transpose();
		$this->mat = $tran->mat;
	}

	//returns number - real at the given index of the matrix
	function get( $r, $c ){
		return $this->mat[$r-1][$c-1];
	}

	//returns Matrix - transpose of the matrix
	function transpose(){
		$return_matrix = new Matrix( $this->cols, $this->rows );
		for ($i = 0; $i < $this->rows; $i++){
			for ($j = 0; $j < $this->cols; $j++){
				$return_matrix->set( $j+1, $i+1, $this->mat[$i][$j] );
			}
		}
		return $return_matrix;
	}

	//return Matrix - returns the given row of this Matrix
	function get_row( $r ){
		$row = new Matrix( 1, $this->cols );
		for ($i = 0; $i < $this->cols; $i++){
			$row->set( 1, $i+1, $this->mat[$r-1][$i] );
		}
		return $row;
	}

	//return Matrix - returns the given column of this Matrix
	function get_column( $c ){
		$col = new Matrix( $this->rows, 1 );
		for ($i = 0; $i < $this->rows; $i++){
			$col->set( $i+1, 1, $this->mat[$i][$c-1] );
		}
		return $col;
	}

	//return Matrix - subtracts the current matrix from the given one
	//and returns the result.
	function subtract( $m ){
		/* if($m->get_row_dimension() != $this->rows || $m_.get_column_dimension() != $this->cols)
			{ return false; } */

		$sum = new Matrix( $this->rows, $this->cols );

		for ($i = 1; $i <= $this->rows; $i++){
			for ($j = 1; $j <= $this->cols; $j++){
				$sum->set( $i, $j, ($this->get($i,$j))-($m->get($i,$j)) );
			}
		}
		return $sum;
	}

	//return number - adds all the number elements in the matrix and returns the real object
	function sum(){
		$tmp = 0;
		for ($i = 0; $i < $this->rows; $i++){
			for($j = 0; $j < $this->cols; $j++){
				$tmp += $this->get($i+1,$j+1);
			}
		}
		return $tmp;
	}

	//return Matrix - finds and returns the rref of this matrix
	function rref(){
		$rref = new Matrix($this->rows,$this->cols);
		$rref->mat = $this->mat;
		
		for ($i = 1; $i <= $this->rows && $i <= $this->cols; $i++){
			$row = $this->rows;
	
			while( $row > $i ){
				$row--;
			}

			$temp = $rref->get_row($row);
			$rref->set_row( $row, $rref->get_row($i));
			$rref->set_row( $i, $temp );

				$inverse = ($temp->get(1,$i) != 0) ? 1 / $temp->get(1,$i) : 0;
			$temp->multiply($inverse);
			$rref->set_row( $i, $temp );
			
			for ($j = $this->rows; $j > 0; $j--){
				if($j != $i){
					$mult = $temp;
					$mult->multiply( $rref->get($j, $i) );
						$to_subtract = $rref->get_row($j);
					$new_row = $to_subtract->subtract($mult);
					$rref->set_row( $j, $new_row);
				}
			}
		}
	return $rref;
	}

	//return string - returns a string representation of this matrix.
	function to_string(){
		$str = "";
		for ($i = 0; $i < $this->rows; $i++){
			$str .= "-br-|";
			for ($j = 0; $j < $this->cols; $j++){
				$str .= " ".$this->mat[$i][$j]." |";
			}
		}
		return $str;
	}

	function e_r( $line ,$var ){
		echo('-font color="#ff0000"-Line: '.$line.' - Value: '.$var.'-/font--br-');
	}

	function e_a( $line ,$var ){
		echo('Line: '.$line.' - Value: '); print_r($var);echo('
'); } }



Any comments pertaining to my coding and not the Matrix class are welcome as well. PHP isn't a forte... yet.