本文整理汇总了PHP中Matrix::solve方法的典型用法代码示例。如果您正苦于以下问题:PHP Matrix::solve方法的具体用法?PHP Matrix::solve怎么用?PHP Matrix::solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix::solve方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: polyfit
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 );
}
示例2: findPolynomialFactors
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();
}
示例3: _polynomial_regression
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()
示例4: Matrix
/**
* Fit first derivatives to minimise second derivatives.
*/
function fit_derivs_badly()
{
$n = $this->size() - 1;
// there are 3*$n parameters to fit: polynomial coeffs of orders from 1 to 3 for each spline piece
$M = new Matrix(3 * $n, 3 * $n);
$b = array_fill(0, 3 * $n, 0);
for ($i = 0; $i < $n; ++$i) {
$dx = $this->points[$i + 1]->x - $this->points[$i]->x;
$dx2 = $dx * $dx;
$dx3 = $dx2 * $dx;
$j = 3 * $i;
$b[$j] = $this->points[$i + 1]->y - $this->points[$i]->y;
$M->add($j, $j, $dx);
// c_i^1
$M->add($j, $j + 1, $dx2);
// c_i^2
$M->add($j, $j + 2, $dx3);
// c_i^3
$M->add($j + 1, $j, 1);
// c_i^1
$M->add($j + 1, $j + 1, 2 * $dx);
// c_i^2
$M->add($j + 1, $j + 2, 3 * $dx2);
// c_i^3
if ($i != $n - 1) {
$M->add($j + 1, $j + 3, -1);
}
// c_{i+1}^1
$M->add($j + 2, $j + 1, 1);
// c_i^2
$M->add($j + 2, $j + 2, 3 * $dx);
// c_i^3
if ($i != $n - 1) {
$M->add($j + 2, $j + 4, -1);
}
// c_{i+1}^2
}
//$M->log();
//$M0 = $M->copy();
//echo 'b='.json_encode($b).'<br>';
$x = $M->solve($b);
//echo 'b='.json_encode($M0->mulVect($x)).'<br>';
for ($i = 0; $i < $n; ++$i) {
$j = 3 * $i;
$p = $this->points[$i];
array_push($this->coeffs, array($p->y, $x[$j], $x[$j + 1], $x[$j + 2]));
}
$p = $this->points[$n];
array_push($this->coeffs, array($p->y, 0, 0, 0));
}
示例5: testSolveExceptionNotVectorOrArray
/**
* @dataProvider dataProviderForSolveExceptionNotVectorOrArray
*/
public function testSolveExceptionNotVectorOrArray($b)
{
$A = new Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]]);
$this->setExpectedException('MathPHP\\Exception\\IncorrectTypeException');
$A->solve($b);
}