Esempio n. 1
0
 /**
  * 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;
 }
 /**
  * Check matrix is not empty
  *
  * @param \Chippyash\Matrix\Matrix $matrix
  * @param string $msg optional error message`
  *
  * @return $this
  *
  * @throws MatrixException
  */
 protected function assertMatrixIsNotEmpty(Matrix $matrix, $msg = 'Matrix parameter is empty')
 {
     if ($matrix->is('empty')) {
         throw new MatrixException($msg);
     }
     return $this;
 }
 /**
  * @param Matrix $mA
  * @param Matrix $mB
  * @param string $msg Optional message
  * 
  * @return $this
  *
  * @throws MatrixException
  * @internal param Matrix $matrix
  */
 protected function assertMatrixRowsAreEqual(Matrix $mA, Matrix $mB, $msg = 'mA->rows != mB->rows')
 {
     if ($mA->rows() != $mB->rows()) {
         throw new MatrixException($msg);
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * Check that matrix is square
  *
  * @param \Chippyash\Matrix\Matrix $matrix
  * @param string $msg Optional message
  * @return $this
  *
  * @throws MatrixException
  */
 protected function assertMatrixIsSquare(Matrix $matrix, $msg = 'Matrix is not square')
 {
     if (!$matrix->is('square')) {
         throw new MatrixException($msg);
     }
     return $this;
 }
 /**
  * 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());
 }
Esempio n. 6
0
 /**
  * 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));
 }
Esempio n. 7
0
 /**
  * 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;
 }
Esempio n. 8
0
 /**
  * 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;
 }
Esempio n. 9
0
 /**
  * Format the matrix contents for outputting
  *
  * @param Matrix $mA Matrix to format
  * @param array $options Options for formatter
  *  - attribs => Collection of VertexDescription
  *  - optional: edgeFunc => function($weight){return $newWeight;}
  *  - optional: output:string script|object default = script
  *
  * If output is script, return the graphviz dot file contents
  * If output is object then return Fhaculty\Graph\Graph for your own
  * processing via Graphp\GraphViz\GraphViz
  *
  * @return Graph|string
  */
 public function format(Matrix $mA, array $options = array())
 {
     if (!$mA instanceof NumericMatrix) {
         throw new \InvalidArgumentException('Matrix is not NumericMatrix');
     }
     return FFor::create(['mA' => $mA, 'graph' => new Graph(), 'options' => $options])->attribs(function ($options) {
         $attribs = array_key_exists('attribs', $options) ? $options['attribs'] : new Collection([], 'string');
         if ($attribs instanceof Collection) {
             return $attribs;
         }
         throw new \InvalidArgumentException('options[attribs]');
     })->edgeFunc(function ($options) {
         $edgeFunc = array_key_exists('edgeFunc', $options) ? $options['edgeFunc'] : function ($w) {
             return $w;
         };
         if ($edgeFunc instanceof \Closure) {
             return $edgeFunc;
         }
         throw new \InvalidArgumentException('pptions[edgeFunc]');
     })->output(function ($options) {
         return array_key_exists('output', $options) ? $options['output'] : 'script';
     })->vertices(function (Collection $attribs, Matrix $mA, Graph $graph) {
         $vertices = [];
         foreach (range(0, $mA->rows() - 1) as $idx) {
             if (array_key_exists($idx, $attribs)) {
                 $attribute = $attribs[$idx];
                 $vertices[$idx + 1] = $graph->createVertex($attribute->getName());
                 foreach ($attribute->getAttributes() as $key => $val) {
                     $vertices[$idx + 1]->setAttribute($key, $val);
                 }
             } else {
                 $vertices[$idx + 1] = $graph->createVertex();
             }
         }
         return $vertices;
     })->graphViz(function (Graph $graph, Matrix $mA, array $vertices, \Closure $edgeFunc, $output) {
         $comp = new Comparator();
         $zero = TypeFactory::createInt(0);
         $rows = $mA->rows();
         for ($row = 1; $row <= $rows; $row++) {
             for ($col = 1; $col <= $rows; $col++) {
                 if ($comp->compare($zero, $mA->get($row, $col)) != 0) {
                     $vertices[$row]->createEdgeTo($vertices[$col])->setWeight($edgeFunc($mA->get($row, $col)->get()));
                 }
             }
         }
         return $output == 'script' ? (new GraphViz())->createScript($graph) : $graph;
     })->fyield('graphViz');
 }
Esempio n. 10
0
 /**
  * Rotate the matrix through 90, 180 or 270 degrees
  *
  * @param Matrix $mA First matrix operand - required
  * @param int $extra Rotation degrees - default null = self::ROT_90 (counter clockwise)
  *
  * @return Matrix
  *
  * @throws \Chippyash\Matrix\Exceptions\TransformationException
  */
 protected function doTransform(Matrix $mA, $extra = null)
 {
     if (is_null($extra)) {
         $extra = self::ROT_90;
     } else {
         if (!array_key_exists($extra, $this->rotationMatrices)) {
             throw new TransformationException('Rotation angle not supported');
         }
     }
     //simple cases
     if ($mA->is('empty') || $mA->is('singleitem')) {
         return clone $mA;
     }
     $this->assertMatrixIsComplete($mA);
     return $this->rotate($mA, $this->rotationMatrices[$extra]);
 }
Esempio n. 11
0
 /**
  * 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;
 }
Esempio n. 12
0
 /**
  * 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;
 }
Esempio n. 13
0
 /**
  * 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;
 }
Esempio n. 14
0
 /**
  * Take a row slice from the matrix
  *
  * @param Matrix $mA First matrix operand - required
  * @param array $extra [int startCol, int numCols = 1]
  *
  * @return Matrix
  *
  * @throws MatrixException
  */
 protected function doTransform(Matrix $mA, $extra = null)
 {
     if ($mA->is('empty')) {
         return new Matrix(array());
     }
     /** @noinspection PhpInternalEntityUsedInspection */
     $this->assertParameterIsArray($extra, 'Second operand is not an array');
     if (empty($extra)) {
         throw new MatrixException('Second operand does not contain col indicator');
     }
     $col = intval($extra[0]);
     $availableCols = $mA->columns();
     if ($col < 1 || $col > $availableCols) {
         throw new MatrixException('Col indicator out of bounds');
     }
     $numCols = isset($extra[1]) ? intval($extra[1]) : 1;
     if ($numCols < 1 || $numCols + $col - 1 > $availableCols) {
         throw new MatrixException('Numcols out of bounds');
     }
     $fT = new Transpose();
     $fR = new Rowslice();
     return $fT($fR($fT($mA), array($col, $numCols)));
 }
Esempio n. 15
0
 /**
  * Return cofactor matrix for a given vertice
  *
  * @param Matrix $mA First matrix operand - required
  * @param array $extra [int row, int col]
  *
  * @return Matrix
  *
  * @throws MatrixException
  */
 protected function doTransform(Matrix $mA, $extra = null)
 {
     if ($mA->is('empty')) {
         return new Matrix([]);
     }
     /** @noinspection PhpInternalEntityUsedInspection */
     $this->assertParameterIsArray($extra, 'Second operand is not an array');
     if (count($extra) != 2) {
         throw new MatrixException('Second operand does not contain row and column');
     }
     $row = intval($extra[0]);
     $col = intval($extra[1]);
     $size = $mA->rows();
     if ($row < 1 || $row > $size) {
         throw new MatrixException('Row indicator out of bounds');
     }
     if ($col < 1 || $col > $size) {
         throw new MatrixException('Col indicator out of bounds');
     }
     $fC = new Colreduce();
     $fR = new Rowreduce();
     //R(C(mA))
     return $fR($fC($mA, [$col]), [$row]);
 }
Esempio n. 16
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;
     }
 }
Esempio n. 17
0
 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());
 }
Esempio n. 18
0
 /**
  *
  * @param \Chippyash\Matrix\Interfaces\TransformationInterface $transformation
  * @param mixed $extra
  *
  * @return NumericMatrix
  */
 public function transform(TransformationInterface $transformation, $extra = null)
 {
     return new self(parent::transform($transformation, $extra)->toArray());
 }
Esempio n. 19
0
 /**
  * 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;
 }
Esempio n. 20
0
 /**
  * @dataProvider nonCompleteArrays
  */
 public function testConstructGivesNormalizedMatrix($testArray, $expectedArray)
 {
     $this->object = new NumericMatrix($testArray, 1);
     $this->assertEquals($expectedArray, $this->object->toArray());
 }
Esempio n. 21
0
 /**
  * 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;
 }
Esempio n. 22
0
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     return $mA->toArray() === array(array());
 }
Esempio n. 23
0
 /**
  * @param Matrix $mA Original Matrix
  * @param mixed $newValue new value to be used in replacement
  * @return Matrix Column Vector
  */
 protected function createCVector(Matrix $mA, $newValue)
 {
     $fTran = new Transpose();
     return $fTran(new Matrix(array_fill(0, $mA->rows(), $newValue)));
 }
Esempio n. 24
0
 /**
  * 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));
 }
Esempio n. 25
0
 public function testEqualityWithLooseCheckingReturnsFalseIfEqualityRulesAreNotMatched()
 {
     $mA = new Matrix([2]);
     $mB = new Matrix([3.6]);
     $this->assertFalse($mA->equality($mB, false));
     $mC = new Matrix([[1234], [456], [789]]);
     $mD = new Matrix([['123'], ['456'], ['789']]);
     $this->assertFalse($mC->equality($mD), false);
 }
Esempio n. 26
0
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     return $mA->rows() == 1 && $mA->columns() > 1;
 }
Esempio n. 27
0
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     $r = $mA->rows();
     $c = $mA->columns();
     return $r > 1 && $c > 1 && $r != $c;
 }
Esempio n. 28
0
 public function testShiftingAColumnVectorWillReturnACloneOfTheOriginalVector()
 {
     $mA = new Matrix([[1], [2], [3]]);
     $this->assertEquals($mA->toArray(), $this->sut->transform($mA)->toArray());
 }