/** * Return all possibility for the matrix * * @return array */ public function compute() { $dimensions = $this->dimensions; if (empty($dimensions)) { return array(); } // Pop first dimension $values = reset($dimensions); $name = key($dimensions); unset($dimensions[$name]); // Create all possiblites for the first dimension $posibilities = array(); foreach ($values as $v) { $posibilities[] = array($name => $v); } // If only one dimension return simple all the possibilites created (break point of recursivity) if (empty($dimensions)) { return $posibilities; } // If not create a new matrix with remaining dimension $matrix = new Matrix(); foreach ($dimensions as $name => $values) { $matrix->setDimension($name, $values); } $result = $matrix->compute(); $newResult = array(); foreach ($result as $value) { foreach ($posibilities as $possiblity) { $newResult[] = $value + $possiblity; } } return $newResult; }
/** * Get D - the diagonal matrix. * * @return matrix D */ public function getD() { $D = new Matrix($this->_matrix->rows(), $this->_matrix->columns()); for ($i = 0; $i < $D->rows(); $i++) { $D->set($i, $i, $this->_matrix->get($i, $i)); } return $D; }
public function testShouldReturnOne() { $matrix = new Matrix(); $matrix->setSize(5); $matrix->setValues(array(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5)); $analyzer = new MatrixAnalyzer(); $analyzer->getGreaterProduct($matrix); }
/** * @dataProvider dataProviderForConstructor */ public function testConstructor(array $M, array $V) { $R = new RowVector($M); $V = new Matrix($V); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\RowVector', $R); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\Matrix', $R); $this->assertEquals($V[0], $R[0]); $this->assertEquals(1, $V->getM()); $this->assertEquals(count($M), $V->getN()); }
public function testMatrixGetReturnsCorrectValue() { $testArray = array(array(1, 2, 3), array(0, 2, 1), array(2.5, 1, 3)); $this->object = new RationalMatrix($testArray); for ($r = 1; $r < 4; $r++) { for ($c = 1; $c < 4; $c++) { $this->assertEquals(RationalTypeFactory::create($testArray[$r - 1][$c - 1]), $this->object->get($r, $c)); } } }
public function testJackingAHumanIncreasesTheCount() { $human = $this->getMock('Human', null, array(), 'MockHuman', null, false); $humanFactory = $this->getMock('HumanFactory'); $humanFactory->expects($this->once())->method('create')->will($this->returnValue($human)); $matrix = new Matrix(); $matrix->setHumanFactory($humanFactory); $expectedCount = $matrix->count(); $matrix->jackIn('robin'); $this->assertEquals($expectedCount + 1, $matrix->count()); }
public function multiplicationScalar($multiplier) { $result = new Matrix($this->matrix->getNumRows(), $this->matrix->getNumCols(), $this->precision); for ($row = 1; $row <= $this->matrix->getNumRows(); $row++) { for ($col = 1; $col <= $this->matrix->getNumCols(); $col++) { $newValue = bcmul($this->matrix->getPoint($row, $col), $multiplier, $this->precision); $result->setPoint($row, $col, $newValue); } } return $result; }
private function _construct_view_matrix(Vertex $origin, Matrix $orientation) { $this->_origin = $origin; $this->_tT = new Matrix(array('preset' => Matrix::TRANSLATION, 'vtc' => (new Vector(array('dest' => $origin)))->opposite())); $this->_tR = $orientation->transpose(); print $this->_tT; print $this->_tR; $this->_view_matrix = $this->_tR->mult($this->_tT); if (self::$verbose) { echo "Camera instance constructed." . PHP_EOL; } }
/** * @dataProvider dataProviderForConstructor */ public function testConstructor(array $M, array $V) { $C = new ColumnVector($M); $V = new Matrix($V); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\ColumnVector', $C); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\Matrix', $C); foreach ($M as $row => $value) { $this->assertEquals($value, $V[$row][0]); } $this->assertEquals(count($M), $V->getM()); $this->assertEquals(1, $V->getN()); }
public function testCompute() { $matrix = new Matrix(); $matrix->setDimension('a', array(1, 2, 3)); $matrix->setDimension('b', array(1, 2, 3)); $matrix->setDimension('c', array(1, 2, 3)); $possibilities = $matrix->compute(); $expected = array(array('a' => 1, 'b' => 1, 'c' => 1), array('a' => 1, 'b' => 1, 'c' => 2), array('a' => 1, 'b' => 1, 'c' => 3), array('a' => 1, 'b' => 2, 'c' => 1), array('a' => 1, 'b' => 2, 'c' => 2), array('a' => 1, 'b' => 2, 'c' => 3), array('a' => 1, 'b' => 3, 'c' => 1), array('a' => 1, 'b' => 3, 'c' => 2), array('a' => 1, 'b' => 3, 'c' => 3), array('a' => 2, 'b' => 1, 'c' => 1), array('a' => 2, 'b' => 1, 'c' => 2), array('a' => 2, 'b' => 1, 'c' => 3), array('a' => 2, 'b' => 2, 'c' => 1), array('a' => 2, 'b' => 2, 'c' => 2), array('a' => 2, 'b' => 2, 'c' => 3), array('a' => 2, 'b' => 3, 'c' => 1), array('a' => 2, 'b' => 3, 'c' => 2), array('a' => 2, 'b' => 3, 'c' => 3), array('a' => 3, 'b' => 1, 'c' => 1), array('a' => 3, 'b' => 1, 'c' => 2), array('a' => 3, 'b' => 1, 'c' => 3), array('a' => 3, 'b' => 2, 'c' => 1), array('a' => 3, 'b' => 2, 'c' => 2), array('a' => 3, 'b' => 2, 'c' => 3), array('a' => 3, 'b' => 3, 'c' => 1), array('a' => 3, 'b' => 3, 'c' => 2), array('a' => 3, 'b' => 3, 'c' => 3)); $this->assertCount(count($expected), $possibilities); foreach ($expected as $value) { $this->assertContains($value, $possibilities); } }
function polyfit($X, $Y, $n) { for($i = 0; $i < sizeof ( $X ); ++ $i) for($j = 0; $j <= $n; ++ $j) $A [$i] [$j] = pow ( $X [$i], $j ); for($i = 0; $i < sizeof ( $Y ); ++ $i) $B [$i] = array ( $Y [$i] ); $matrixA = new Matrix ( $A ); $matrixB = new Matrix ( $B ); $C = $matrixA->solve ( $matrixB ); return $C->getMatrix ( 0, $n, 0, 1 ); }
/** * @dataProvider dataProviderMulti */ public function testConstructor(array $A, array $R) { $D = new DiagonalMatrix($A); $R = new Matrix($R); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\DiagonalMatrix', $D); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\Matrix', $D); $m = $D->getM(); for ($i = 0; $i < $m; $i++) { $this->assertEquals($R[$i], $D[$i]); } $m = $R->getM(); for ($i = 0; $i < $m; $i++) { $this->assertEquals($R[$i], $D[$i]); } }
/** * @dataProvider dataProviderForTestConstructor */ public function testConstructor($M, int $n, $V) { $M = new VandermondeMatrix($M, $n); $V = new Matrix($V); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\VandermondeMatrix', $M); $this->assertInstanceOf('MathPHP\\LinearAlgebra\\Matrix', $M); $m = $V->getM(); for ($i = 0; $i < $m; $i++) { $this->assertEquals($V[$i], $M[$i]); } $m = $M->getM(); for ($i = 0; $i < $m; $i++) { $this->assertEquals($V[$i], $M[$i]); } }
public function testMap() { $callback = function ($elem) { return $elem * 2; }; $c = MatrixFactory::fromString('2, 4; 6, 8'); $this->assertEquals($c->getArray(), $this->B->map($callback)->getArray()); }
/** * 确保已经加载了配置 */ public function ensureLoaded() { Lazy::init($this->_items, function () { /** @var CDbConnection $db */ $db = Yii::app()->db; $items = $db->createCommand()->select(array('key', 'value'))->from('t_config')->queryAll(); return Matrix::from($items)->indexedBy('key')->column('value'); }); }
/** * Assembles Q using the single givens rotations. * * @return matrix Q */ public function getQ() { // Q is an mxm matrix if m is the maximum of the number of rows and thenumber of columns. $m = max($this->_matrix->columns(), $this->_matrix->rows()); $Q = new Matrix($m, $m); $Q->setAll(0.0); // Begin with the identity matrix. for ($i = 0; $i < $Q->rows(); $i++) { $Q->set($i, $i, 1.0); } for ($j = $this->_matrix->columns() - 1; $j >= 0; $j--) { for ($i = $this->_matrix->rows() - 1; $i > $j; $i--) { // Get c and s which are stored in the i-th row, j-th column. $aij = $this->_matrix->get($i, $j); $c = 0; $s = 0; if ($aij == 0) { $c = 0.0; $s = 1.0; } else { if (abs($aij) < 1) { $s = 2.0 * abs($aij); $c = sqrt(1 - pow($s, 2)); if ($aij < 0) { $c = -$c; } } else { $c = 2.0 / $aij; $s = sqrt(1 - pow($c, 2)); } } for ($k = 0; $k < $this->_matrix->columns(); $k++) { $jk = $Q->get($j, $k); $ik = $Q->get($i, $k); $Q->set($j, $k, $c * $jk - $s * $ik); $Q->set($i, $k, $s * $jk + $c * $ik); } } } return $Q; }
public static function ex4() { header('Content-type: application/json; charset=utf-8'); //input matricea A , A init $A = self::getMatrixFromString($_POST['matrice']); $n = $_POST['n']; $epsilon = $_POST['epsilon']; $arrayS = self::getArrayFromString($_POST['arr']); $b = self::getB($A, $arrayS, $n, $epsilon); $QR = self::HouseholderDecomposition($A, $epsilon, $n, $b); $x = self::getX($QR["r"], $n, $QR["b"]); $matrix = new Matrix($n, $n); $matrix->fromArray($A); $QRlibf = array(); $QRlib = new QRGivens($matrix); $QRlibf['Qlib'] = $QRlib->getQ()->asArray(); $QRlibf['rlib'] = $QRlib->getR()->asArray(); $xlib = self::getX($QR["r"], $n, $QR["b"]); $vector1 = array(); $ainit = Util::getInit($A); for ($i = 0; $i < $n; $i++) { $vector1[$i] = $ainit[$i] * $x[$i] - $b[$i]; } $norm1 = Util::getNorm($vector1, $n); $vector2 = array(); for ($i = 0; $i < $n; $i++) { $vector2[$i] = $ainit[$i] * $xlib[$i] - $b[$i]; } $norm2 = Util::getNorm($vector2, $n); $vector3 = array(); for ($i = 0; $i < $n; $i++) { $vector3[$i] = $x[$i] - $arrayS[$i]; } $norm3 = Util::getNorm($vector3, $n) / Util::getNorm($arrayS, $n); $vector4 = array(); for ($i = 0; $i < $n; $i++) { $vector4[$i] = $xlib[$i] - $arrayS[$i]; } $norm4 = Util::getNorm($vector4, $n) / Util::getNorm($arrayS, $n); echo json_encode(array("A" => Util::getStringFromMatrix($A), "epsilon" => $epsilon, "Q" => self::getStringFromMatrix($QR['Q']), "r" => self::getStringFromMatrix($QR['r']), "Qlib" => self::getStringFromMatrix($QRlibf['Qlib']), "rlib" => self::getStringFromMatrix($QRlibf['rlib']), "x" => Util::getStringFromArray($x), "xlib" => Util::getStringFromArray($xlib), "norm1" => $norm1, "norm2" => $norm2, "norm3" => $norm3, "norm4" => $norm4)); }
public function findPolynomialFactors($x, $y) { $n = count($x); $data = array(); // double[n][n]; $rhs = array(); // double[n]; for ($i = 0; $i < $n; ++$i) { $v = 1; for ($j = 0; $j < $n; ++$j) { $data[$i][$n - $j - 1] = $v; $v *= $x[$i]; } $rhs[$i] = $y[$i]; } // Solve m * s = b $m = new Matrix($data); $b = new Matrix($rhs, $n); $s = $m->solve($b); return $s->getRowPackedCopy(); }
public function log_in() { $objdata = new Database(); $sth = $objdata->prepare('SELECT * FROM users WHERE logUser = :login AND pasUser = :pass'); $sth->execute(array(':login' => $_POST['Usern'], ':pass' => $_POST['passU'])); $data = $sth->fetch(); $count = $sth->rowCount(); if ($count > 0) { if ($data['id_matrix'] == 0) { $objMatrix = new Matrix(); $matrix = $objMatrix->create_matrix(); $objMatrix->insert_matri($matrix); $datos = $objMatrix->return_matrix(); $objMatrix->matrix_assign($data['idUser'], $datos[0]['id_matrix']); } else { header('location: ' . URL . 'matrix.php?m=' . $data['id_matrix'] . '&u=' . $data['idUser']); } } else { header('location: ' . URL . 'index.php?iderr=1'); } }
public function apply($resource) { @(list(, $method) = func_get_args()); switch ($method) { case self::FILTER: if (!function_exists('imagefilter')) { throw new Exception("It seems your PHP version is not compiled with the bundled version of the GD library, therefore you cannot use this graphic effect"); } if (!imagefilter($resource, IMG_FILTER_MEAN_REMOVAL)) { throw new Exception("MEAN_REMOVAL filter failed"); } return $resource; case self::MATRIX: default: return parent::apply(func_get_arg(0), array(0, -1, 0, -1, 5, -1, 0, -1, 0)); } }
public function apply($resource) { @(list(, $method) = func_get_args()); switch ($method) { case self::FILTER: if (!function_exists('imagefilter')) { throw new Exception("It seems your PHP version is not compiled with the bundled version of the GD library"); } if (!imagefilter($resource, IMG_FILTER_EMBOSS)) { throw new Exception("EMBOSS filter failed"); } return $resource; case self::MATRIX: default: return parent::apply($resource, array(-2, -1, 0, -1, 1, 1, 0, 1, 2)); } }
/** * Compute the covariance matrix for the row vectors. * @param $matrix Could be RealMatrix or SparseMatrix. * @return Matrix a new m by m covariance matrix. * don't have to return by ref, because the engine will take care of it. * Note that no matter what's the input matrix, the returned matrix is always a sparse matrix. */ static function correlation($matrix) { $vectors = $matrix->row_vectors(); $m = $matrix->row; // dimension of the correlation matrix $cor_matrix = Matrix::create('SparseMatrix', $m, $m); for ($v1 = 0; $v1 < $m; $v1++) { for ($v2 = $v1; $v2 < $m; $v2++) { if (isset($vectors[$v1]) && isset($vectors[$v2])) { // note, some value (such as std) is cached, so it won't be too much performance problem. $cor = $vectors[$v1]->correlation($vectors[$v2]); if (!is_nan($cor)) { $cor_matrix->set($v1, $v2, $cor); $cor_matrix->set($v2, $v1, $cor); } } } } return $cor_matrix; }
/** * @return Object[] $a */ function testdata() { $npts = 25; $a[0] = 0.0; $a[1] = 0.0; $a[2] = 0.9; $i = 0; for ($r = -2; $r <= 2; ++$r) { for ($c = -2; $c <= 2; ++$c) { $x[$i][0] = $c; $x[$i][1] = $r; $y[$i] = $this->val($x[$i], $a); print "Quad " . $c . "," . $r . " -> " . $y[$i] . "<br />"; $s[$i] = 1.0; ++$i; } } print "quad x= "; $qx = new Matrix($x); $qx->print(10, 2); print "quad y= "; $qy = new Matrix($y, $npts); $qy->print(10, 2); $o[0] = $x; $o[1] = $a; $o[2] = $y; $o[3] = $s; return $o; }
private function _polynomial_regression($order, $yValues, $xValues, $const) { // calculate sums $x_sum = array_sum ( $xValues ); $y_sum = array_sum ( $yValues ); $xx_sum = $xy_sum = 0; for($i = 0; $i < $this->_valueCount; ++ $i) { $xy_sum += $xValues [$i] * $yValues [$i]; $xx_sum += $xValues [$i] * $xValues [$i]; $yy_sum += $yValues [$i] * $yValues [$i]; } /* * This routine uses logic from the PHP port of polyfit version 0.1 * written by Michael Bommarito and Paul Meagher The function fits a * polynomial function of order $order through a series of x-y data * points using least squares. */ for($i = 0; $i < $this->_valueCount; ++ $i) { for($j = 0; $j <= $order; ++ $j) { $A [$i] [$j] = pow ( $xValues [$i], $j ); } } for($i = 0; $i < $this->_valueCount; ++ $i) { $B [$i] = array ( $yValues [$i] ); } $matrixA = new Matrix ( $A ); $matrixB = new Matrix ( $B ); $C = $matrixA->solve ( $matrixB ); $coefficients = array (); for($i = 0; $i < $C->m; ++ $i) { $r = $C->get ( $i, 0 ); if (abs ( $r ) <= pow ( 10, - 9 )) { $r = 0; } $coefficients [] = $r; } $this->_intersect = array_shift ( $coefficients ); $this->_slope = $coefficients; $this->_calculateGoodnessOfFit ( $x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum ); foreach ( $this->_xValues as $xKey => $xValue ) { $this->_yBestFitValues [$xKey] = $this->getValueOfYForX ( $xValue ); } } // function _polynomial_regression()
/** * MDETERM * * @param array $matrixValues A matrix of values * @return float */ public static function MDETERM($matrixValues) { $matrixData = array(); if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); } $row = $maxColumn = 0; foreach ($matrixValues as $matrixRow) { $column = 0; foreach ($matrixRow as $matrixCell) { if (is_string($matrixCell) || $matrixCell === null) { return self::$_errorCodes['value']; } $matrixData[$column][$row] = $matrixCell; ++$column; } if ($column > $maxColumn) { $maxColumn = $column; } ++$row; } if ($row != $maxColumn) { return self::$_errorCodes['value']; } try { $matrix = new Matrix($matrixData); return $matrix->det(); } catch (Exception $ex) { return self::$_errorCodes['value']; } }
/** * checkMatrixDimensions * * Is matrix B the same size? * @param Matrix $B Matrix B * @return boolean */ public function checkMatrixDimensions($B = null) { if ($B instanceof PHPExcel_Shared_JAMA_Matrix) { if ($this->m == $B->getRowDimension() && $this->n == $B->getColumnDimension()) { return true; } else { throw new PHPExcel_Calculation_Exception(self::MATRIX_DIMENSION_EXCEPTION); } } else { throw new PHPExcel_Calculation_Exception(self::ARGUMENT_TYPE_EXCEPTION); } }
/** * @test * @covers Matrix::count */ function canCount() { $matrix = new Matrix(array('jack' => new Human('jack', true), 'jill' => new Human('jill', true), 'someone else' => new Human('someone else', true))); $this->assertEquals(3, $matrix->count()); }
/** * Multiply the given matrices. * * @param matrix left matrix * @param matrix right matrix * @return matrix product matrix $a*$b */ public static function multiply($a, $b) { // First check dimensions. new Assertion($a instanceof Matrix, 'Given first matrix not of class Matrix.'); new Assertion($b instanceof Matrix, 'Given second matrix not of class Matrix.'); new Assertion($a->columns() == $b->rows(), 'Given dimensions are not compatible.'); $c = new Matrix($a->rows(), $b->columns()); $c->setAll(0.0); for ($i = 0; $i < $a->rows(); $i++) { for ($j = 0; $j < $b->columns(); $j++) { for ($k = 0; $k < $a->columns(); $k++) { $c->set($i, $j, $c->get($i, $j) + $a->get($i, $k) * $b->get($k, $j)); } } } return $c; }
/** * plus * A + B * @param mixed $B Matrix/Array * @return Matrix Sum */ function plus() { if (func_num_args() > 0) { $args = func_get_args(); $match = implode(",", array_map('gettype', $args)); switch ($match) { case 'object': $M = is_a($args[0], 'Matrix') ? $args[0] : trigger_error(ArgumentTypeException, ERROR); //$this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; $i++) { for ($j = 0; $j < $this->n; $j++) { $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]); } } return $M; break; case 'array': $M = new Matrix($args[0]); //$this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; $i++) { for ($j = 0; $j < $this->n; $j++) { $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]); } } return $M; break; default: trigger_error(PolymorphicArgumentException, ERROR); break; } } else { trigger_error(PolymorphicArgumentException, ERROR); } }
public function __construct(array $diagonalValues) { $diagonalCount = count($diagonalValues); $rowCount = $diagonalCount; $colCount = $rowCount; parent::__construct($rowCount, $colCount); for ($currentRow = 0; $currentRow < $rowCount; $currentRow++) { for ($currentCol = 0; $currentCol < $colCount; $currentCol++) { if ($currentRow == $currentCol) { $this->setValue($currentRow, $currentCol, $diagonalValues[$currentRow]); } else { $this->setValue($currentRow, $currentCol, 0); } } } }