Exemplo n.º 1
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');
 }
Exemplo n.º 2
0
 /**
  * @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;
 }
Exemplo n.º 3
0
 /**
  * @dataProvider matrixDimensions
  */
 public function testConstructNonCompleteMatrixWithVariousArraysGivesCorrectDimensions($array, $columns, $rows, $vertices)
 {
     $this->object = new Matrix($array);
     $this->assertEquals($rows, $this->object->rows());
     $this->assertEquals($columns, $this->object->columns());
     $this->assertEquals($vertices, $this->object->vertices());
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
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;
 }
Exemplo n.º 7
0
 /**
  * Take a row slice from the matrix
  *
  * @param Matrix $mA First matrix operand - required
  * @param array $extra [int startRow, int numRows = 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 row indicator');
     }
     $row = intval($extra[0]);
     $availableRows = $mA->rows();
     if ($row < 1 || $row > $availableRows) {
         throw new MatrixException('Row indicator out of bounds');
     }
     $numRows = isset($extra[1]) ? intval($extra[1]) : 1;
     if ($numRows < 1 || $numRows + $row - 1 > $availableRows) {
         throw new MatrixException('Numrows out of bounds');
     }
     $this->assertMatrixIsComplete($mA);
     return $this->doTransformation($mA, $row, $numRows);
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
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]);
 }
Exemplo n.º 10
0
 /**
  * Test equality of this matrix with another
  * mA == mB iff
  * - mA->rows() == mB->rows()
  * - mA->columns() == mB->columns()
  * - mA->get(i,j) ==[=] mB->get(i,j)
  *
  * @param \Chippyash\Matrix\Matrix $mB
  * @param boolean $strict Check type and value
  *
  * @return boolean
  */
 public function equality(Matrix $mB, $strict = true)
 {
     if ($this->rows() != $mB->rows()) {
         return false;
     }
     if ($this->columns() != $mB->columns()) {
         return false;
     }
     return $this->checkEntryEquality($mB, $strict);
 }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
0
 /**
  * Does the matrix have this attribute
  *
  * @param Matrix $mA
  * @return boolean
  */
 public function is(Matrix $mA)
 {
     return $mA->rows() == 1 && $mA->columns() > 1;
 }
Exemplo n.º 13
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)));
 }
Exemplo n.º 14
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;
 }