コード例 #1
0
 /**
  * Convert matrix entries if required
  *
  * @param \Chippyash\Matrix\Matrix $mA
  * @return \Chippyash\Matrix\Matrix
  */
 protected function convertEntries(Matrix $mA)
 {
     switch ($this->options['outputType']) {
         case self::TP_INT:
             return $this->toInt($mA->toArray());
         case self::TP_FLOAT:
             return $this->toFloat($mA->toArray());
         case self::TP_RATIONAL:
             return $this->toRational($mA->toArray());
         case self::TP_COMPLEX:
             return $this->toComplex($mA->toArray());
         case self::TP_NONE:
         default:
             return $mA;
     }
 }
コード例 #2
0
ファイル: IsComplete.php プロジェクト: chippyash/matrix
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     //empty matrix is ok
     if ($mA->is('empty')) {
         return true;
     }
     $data = $mA->toArray();
     //check we don't have empty/missing rows
     $tmp = array_keys($data);
     //to get past error_reporting E_STRICT for Travis
     $top = array_pop($tmp);
     if ($top >= count($data)) {
         $this->errRow = count($data);
         return false;
     }
     //check that each row has same number of columns
     $numcols = count($data[0]);
     $ret = true;
     $r = 0;
     array_walk($data, function ($value, $index, $matchCols) use(&$ret, &$r) {
         if ($ret && count($value) != $matchCols) {
             $ret = false;
             $r = $index;
         }
     }, $numcols);
     if (!$ret) {
         $this->errRow = $r + 1;
     }
     return $ret;
 }
コード例 #3
0
 /**
  * Required so that upstream matrices can use the transformations and be returned
  * in a matrix type they understand
  *
  * @param Matrix $original
  * @param Matrix $result
  * 
  * @return Matrix
  */
 protected function returnOriginalMatrixType(Matrix $original, Matrix $result)
 {
     $oClass = get_class($original);
     if ($oClass == 'Chippyash\\Matrix\\Matrix') {
         return $result;
     }
     return new $oClass($result->toArray());
 }
コード例 #4
0
ファイル: Rowreduce.php プロジェクト: chippyash/matrix
 /**
  * Carry out the transformation
  * 
  * @param \Chippyash\Matrix\Matrix $mA
  * @param int $row Start row
  * @param int $numRows Number of rows
  * 
  * @return \Chippyash\Matrix\Matrix
  */
 protected function doTransformation(Matrix $mA, $row, $numRows)
 {
     $data = $mA->toArray();
     $rowEnd = $row - 1 + $numRows;
     for ($r = $row - 1; $r < $rowEnd; $r++) {
         unset($data[$r]);
     }
     return new Matrix(array_values($data));
 }
コード例 #5
0
ファイル: Mutability.php プロジェクト: chippyash/matrix
 /**
  * Set a matrix vertex, row or column vector
  * If row == 0 && col > 0, then set the column vector indicated by col
  * If col == 0 && row > 0, then set the row vector indicated by row
  * if row > 0 && col > 0, set the vertex
  * row == col == 0 is an error
  *
  * @param int $row
  * @param int $col
  * @param mixed|Matrix $data If setting a vector, supply the correct vector matrix
  *
  * @return Matrix
  * @throws VerticeOutOfBoundsException
  * @throws MatrixException
  */
 public function set($row, $col, $data)
 {
     if ($row < 0 || $row > $this->rows()) {
         throw new VerticeOutOfBoundsException('row', $row);
     }
     if ($col < 0 || $col > $this->columns()) {
         throw new VerticeOutOfBoundsException('col', $col);
     }
     if ($row == 0 && $col == 0) {
         throw new VerticeOutOfBoundsException('row & col', 0);
     }
     if (($row == 0 || $col == 0) && !$data instanceof Matrix) {
         throw new MatrixException('$data for set method must be a matrix');
     }
     if ($row == 0 && $col > 0) {
         if (!$data->is('columnvector')) {
             throw new MatrixException('$data for set method must be a column vector');
         }
         $dArr = $data->toArray();
         $col--;
         $nRows = $this->rows();
         for ($n = 0; $n < $nRows; $n++) {
             $this->data[$n][$col] = $dArr[$n][0];
         }
         return $this;
     }
     if ($col == 0 && $row > 0) {
         if (!$data->is('rowvector')) {
             throw new MatrixException('$data for set method must be a row vector');
         }
         $dArr = $data->toArray();
         $row--;
         $nCols = $this->columns();
         for ($m = 0; $m < $nCols; $m++) {
             $this->data[$row][$m] = $dArr[0][$m];
         }
         return $this;
     }
     $this->data[$row - 1][$col - 1] = $data;
     return $this;
 }
コード例 #6
0
ファイル: VectorSet.php プロジェクト: chippyash/matrix
 /**
  * Add vector set entries from a Matrix
  *
  * @param \Chippyash\Matrix\Matrix $mA
  * @return \Chippyash\Matrix\Vector\VectorSet $this
  */
 public function fromMatrix(Matrix $mA)
 {
     $data = $mA->toArray();
     $rows = $mA->rows();
     $cols = $mA->columns();
     for ($y = 0; $y < $rows; $y++) {
         for ($x = 0; $x < $cols; $x++) {
             $this->append(new Vector2D($x, $y, $data[$y][$x]));
         }
     }
     return $this;
 }
コード例 #7
0
ファイル: IsSingleitem.php プロジェクト: chippyash/matrix
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     if ($mA->toArray() === array(array())) {
         //empty array is not a single item
         return false;
     }
     if ($mA->rows() > 1) {
         return false;
     }
     if ($mA->columns() > 1) {
         return false;
     }
     return true;
 }
コード例 #8
0
ファイル: IsDiagonal.php プロジェクト: chippyash/matrix
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     if (!$mA->is('square')) {
         return false;
     }
     $size = $mA->rows();
     $data = $mA->toArray();
     for ($row = 0; $row < $size; $row++) {
         for ($col = 0; $col < $size; $col++) {
             if ($row == $col) {
                 if ($data[$row][$col] == 0) {
                     return false;
                 }
             } else {
                 if ($data[$row][$col] != 0) {
                     return false;
                 }
             }
         }
     }
     return true;
 }
コード例 #9
0
ファイル: IsIdentity.php プロジェクト: chippyash/math-matrix
 /**
  * Does the matrix have this attribute
  * @link http://en.wikipedia.org/wiki/Identity_matrix
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     if (!$mA instanceof NumericMatrix) {
         return false;
     }
     $rows = $mA->rows();
     $cols = $mA->columns();
     $data = $mA->toArray();
     for ($r = 0; $r < $rows; $r++) {
         for ($c = 0; $c < $cols; $c++) {
             $item = $this->checkItem($data[$r][$c]);
             //diagonal == 1
             if ($r == $c && $item != 1) {
                 return false;
                 //non-diagonal == 0
             } elseif ($r != $c && $item != 0) {
                 return false;
             }
         }
     }
     return true;
 }
コード例 #10
0
ファイル: Matrix.php プロジェクト: chippyash/matrix
 /**
  * Check equality of each matrix entry
  * Overide in child class if required
  *
  * @param \Chippyash\Matrix\Matrix $mB
  * @param boolean $strict
  *
  * @return boolean
  */
 protected function checkEntryEquality(Matrix $mB, $strict)
 {
     $dA = $this->toArray();
     $dB = $mB->toArray();
     $m = $this->rows();
     $n = $this->columns();
     for ($i = 0; $i < $m; $i++) {
         for ($j = 0; $j < $n; $j++) {
             if ($strict) {
                 if ($dA[$i][$j] !== $dB[$i][$j]) {
                     return false;
                 }
             } else {
                 if ($dA[$i][$j] != $dB[$i][$j]) {
                     return false;
                 }
             }
         }
     }
     return true;
 }
コード例 #11
0
ファイル: CircshiftTest.php プロジェクト: chippyash/matrix
 public function testShiftingAColumnVectorWillReturnACloneOfTheOriginalVector()
 {
     $mA = new Matrix([[1], [2], [3]]);
     $this->assertEquals($mA->toArray(), $this->sut->transform($mA)->toArray());
 }
コード例 #12
0
ファイル: ShiftTest.php プロジェクト: chippyash/matrix
 public function testYouCanSupplyAReplacementValue()
 {
     $test = new Matrix(array(array('foo', 1, 2), array('foo', 4, 5), array('foo', 7, 8)));
     $this->assertEquals($test->toArray(), $this->sut->transform($this->mA, [null, 'foo'])->toArray());
 }
コード例 #13
0
ファイル: IsEmpty.php プロジェクト: chippyash/matrix
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     return $mA->toArray() === array(array());
 }
コード例 #14
0
ファイル: Ascii.php プロジェクト: chippyash/matrix
 /**
  * Covert each row of matrix into a line
  *
  * @param \Chippyash\Matrix\Matrix $mA
  * @return array
  */
 protected function getLines(Matrix $mA)
 {
     $lines = array();
     $data = $mA->toArray();
     $rows = $mA->rows();
     $cols = $mA->columns();
     for ($row = 0; $row < $rows; $row++) {
         $line = '';
         for ($col = 0; $col < $cols; $col++) {
             $line .= $data[$row][$col];
         }
         $lines[$row] = $line;
     }
     return $lines;
 }
コード例 #15
0
 /**
  * @dataProvider nonCompleteArrays
  */
 public function testConstructGivesNormalizedMatrix($testArray, $expectedArray)
 {
     $this->object = new NumericMatrix($testArray, 1);
     $this->assertEquals($expectedArray, $this->object->toArray());
 }
コード例 #16
0
ファイル: MatrixTest.php プロジェクト: chippyash/matrix
 /**
  * @dataProvider nonCompleteArraysForNormalizationWithUserData
  */
 public function testConstructForcingNormalizationWithUserDataNotCompleteGivesNormalizedMatrix($testArray, $expectedArray)
 {
     $this->object = new Matrix($testArray, false, true, 'foo', false);
     $this->assertEquals($expectedArray, $this->object->toArray());
 }
コード例 #17
0
ファイル: Rowslice.php プロジェクト: chippyash/matrix
 /**
  * Carry out the transformation
  * 
  * @param \Chippyash\Matrix\Matrix $mA
  * @param int $row Start row
  * @param int $numRows Number of rows
  * 
  * @return \Chippyash\Matrix\Matrix
  */
 protected function doTransformation(Matrix $mA, $row, $numRows)
 {
     $data = $mA->toArray();
     return new Matrix(array_slice($data, $row - 1, $numRows));
 }