/** * @param Matrix $mA * @param Matrix $mB * @param string $msg Optional message * * @return $this * * @throws MatrixException * @internal param Matrix $matrix */ protected function assertMatrixColumnsAreEqual(Matrix $mA, Matrix $mB, $msg = 'mA->cols != mB->cols') { if ($mA->columns() != $mB->columns()) { throw new MatrixException($msg); } return $this; }
/** * @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()); }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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))); }
/** * 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); }
/** * 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; }
/** * Does the matrix have this attribute * * @param Matrix $mA * @return boolean */ public function is(Matrix $mA) { return $mA->rows() == 1 && $mA->columns() > 1; }
/** * 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; }