當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Matrix::getMatrix方法代碼示例

本文整理匯總了PHP中Matrix::getMatrix方法的典型用法代碼示例。如果您正苦於以下問題:PHP Matrix::getMatrix方法的具體用法?PHP Matrix::getMatrix怎麽用?PHP Matrix::getMatrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Matrix的用法示例。


在下文中一共展示了Matrix::getMatrix方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: TestMatrix


//.........這裏部分代碼省略.........
      *   readObject
      */
     print "<p>Testing I/O methods...</p>";
     /**
      * Test linear algebra methods
      */
     echo "<p>Testing linear algebra methods...<p>";
     $A = new Matrix($columnwise, 3);
     if ($this->checkMatrices($A->transpose(), $T)) {
         $this->try_success("Transpose check...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal");
     }
     if ($this->checkScalars($A->norm1(), $columnsummax)) {
         $this->try_success("Maximum column sum...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax);
     }
     if ($this->checkScalars($A->normInf(), $rowsummax)) {
         $this->try_success("Maximum row sum...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax);
     }
     if ($this->checkScalars($A->normF(), sqrt($sumofsquares))) {
         $this->try_success("Frobenius norm...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares));
     }
     if ($this->checkScalars($A->trace(), $sumofdiagonals)) {
         $this->try_success("Matrix trace...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals);
     }
     $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension());
     if ($B->det() == 0) {
         $this->try_success("Matrix determinant...");
     } else {
         $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0);
     }
     $A = new Matrix($columnwise, 3);
     $SQ = new Matrix($square);
     if ($this->checkMatrices($SQ, $A->times($A->transpose()))) {
         $this->try_success("times(Matrix)...");
     } else {
         $errorCount = $this->try_failure($errorCount, "times(Matrix)...", "Unable to multiply matrices");
         $SQ->toHTML();
         $AT->toHTML();
     }
     $A = new Matrix($columnwise, 4);
     $QR = $A->qr();
     $R = $QR->getR();
     $Q = $QR->getQ();
     if ($this->checkMatrices($A, $Q->times($R))) {
         $this->try_success("QRDecomposition...", "");
     } else {
         $errorCount = $this->try_failure($errorCount, "QRDecomposition...", "incorrect qr decomposition calculation");
     }
     $A = new Matrix($columnwise, 4);
     $SVD = $A->svd();
     $U = $SVD->getU();
     $S = $SVD->getS();
     $V = $SVD->getV();
     if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) {
         $this->try_success("SingularValueDecomposition...", "");
     } else {
         $errorCount = $this->try_failure($errorCount, "SingularValueDecomposition...", "incorrect singular value decomposition calculation");
開發者ID:sysraj86,項目名稱:carnivalcrm,代碼行數:67,代碼來源:TestMatrix.php

示例2: solve

 /**
  *	Least squares solution of A*X = B
  *
  *	@param Matrix $B A Matrix with as many rows as A and any number of columns.
  *	@return Matrix Matrix that minimizes the two norm of Q*R*X-B.
  */
 public function solve($B)
 {
     if ($B->getRowDimension() == $this->m) {
         if ($this->isFullRank()) {
             // Copy right hand side
             $nx = $B->getColumnDimension();
             $X = $B->getArrayCopy();
             // Compute Y = transpose(Q)*B
             for ($k = 0; $k < $this->n; ++$k) {
                 for ($j = 0; $j < $nx; ++$j) {
                     $s = 0.0;
                     for ($i = $k; $i < $this->m; ++$i) {
                         $s += $this->QR[$i][$k] * $X[$i][$j];
                     }
                     $s = -$s / $this->QR[$k][$k];
                     for ($i = $k; $i < $this->m; ++$i) {
                         $X[$i][$j] += $s * $this->QR[$i][$k];
                     }
                 }
             }
             // Solve R*X = Y;
             for ($k = $this->n - 1; $k >= 0; --$k) {
                 for ($j = 0; $j < $nx; ++$j) {
                     $X[$k][$j] /= $this->Rdiag[$k];
                 }
                 for ($i = 0; $i < $k; ++$i) {
                     for ($j = 0; $j < $nx; ++$j) {
                         $X[$i][$j] -= $X[$k][$j] * $this->QR[$i][$k];
                     }
                 }
             }
             $X = new Matrix($X);
             return $X->getMatrix(0, $this->n - 1, 0, $nx);
         } else {
             throw new Exception(JAMAError(MatrixRankException));
         }
     } else {
         throw new Exception(JAMAError(MatrixDimensionException));
     }
 }
開發者ID:linhanwei,項目名稱:TP,代碼行數:46,代碼來源:QRDecomposition.php

示例3: testDirectProduct

 /**
  * @dataProvider dataProviderForDirectProduct
  */
 public function testDirectProduct(array $A, array $B, array $expected)
 {
     $A = new Vector($A);
     $B = new Vector($B);
     $AB = $A->directProduct($B);
     $expected = new Matrix($expected);
     $this->assertEquals($expected->getMatrix(), $AB->getMatrix());
 }
開發者ID:markrogoyski,項目名稱:math-php,代碼行數:11,代碼來源:VectorOperationsTest.php

示例4: MatrixFitRoute

 function MatrixFitRoute($route)
 {
     require_once 'matrix.php';
     $this->refreshPosibleTruck();
     $route_possible_trucks = $this->getPossibleTrucks($route);
     $copy_possible_trucks = $this->truck_possible;
     $list_all_possible_trucks = array();
     foreach ($copy_possible_trucks as $value) {
         $list_all_possible_trucks = array_merge($list_all_possible_trucks, $value);
     }
     $list_all_possible_trucks = array_unique(array_merge($list_all_possible_trucks, $route_possible_trucks));
     $pending_routes = array_keys($this->routes);
     array_push($pending_routes, "new");
     $assigned_trucks = array();
     //create white_matrix
     $white_matrix = array();
     foreach ($list_all_possible_trucks as $truck_id) {
         $white_matrix[$truck_id] = array();
         foreach ($pending_routes as $route_id) {
             if ($route_id == "new") {
                 $white_matrix[$truck_id][$route_id] = in_array($truck_id, $route_possible_trucks) ? 1 : 0;
             } else {
                 $white_matrix[$truck_id][$route_id] = in_array($truck_id, $copy_possible_trucks[$route_id]) ? 1 : 0;
             }
         }
     }
     $grey_matrix = new Matrix($white_matrix);
     //basic required condition for solution
     if (!$grey_matrix->possible()) {
         return false;
     }
     //cardinality==1 check for $route. this means that the route has only one truck that it can use.
     foreach ($grey_matrix->getColumnKeys() as $route_id) {
         if ($grey_matrix->getColumnCardinal($route_id) == 1) {
             $truck_id = $grey_matrix->getFirstPositiveRow($route_id);
             if ($truck_id === false) {
                 trigger_error("Looked for positive row but found none", E_USER_ERROR);
             } else {
                 $assigned_trucks[$truck_id] = $route_id;
                 $grey_matrix->deleteRow($truck_id);
                 $grey_matrix->deleteColumn($route_id);
             }
         }
     }
     //cardinality==1 check for $truck. this means that there is only one route that can use this truck
     foreach ($grey_matrix->getRowKeys() as $truck_id) {
         if ($grey_matrix->getRowCardinal($truck_id) == 1) {
             $route_id = $grey_matrix->getFirstPositiveColumn($truck_id);
             if ($route_id === false) {
                 trigger_error("Looked for positive col but found none", E_USER_ERROR);
             } else {
                 $assigned_trucks[$truck_id] = $route_id;
                 $grey_matrix->deleteRow($truck_id);
                 $grey_matrix->deleteColumn($route_id);
             }
         }
     }
     //trivial solution found
     if (count($grey_matrix->getColumnKeys()) < 1) {
         return $assigned_trucks;
     }
     //check again for basic condition before continuing
     if (!$grey_matrix->possible()) {
         return false;
     }
     //iterate while there are still routes to be fit, we will include exit statments under certain conditions inside the loop
     $black_matrix = new Matrix($grey_matrix->getMatrix());
     $ignores = array();
     $routes_ids = $black_matrix->getColumnKeys();
     $last_route_id = end($routes_ids);
     $first_route_id = reset($routes_ids);
     $max_loop = $black_matrix->maximumIterations();
     $loop_count = 0;
     while (true) {
         if ($loop_count >= $max_loop) {
             trigger_error("loops count exceeded logical max loop, please check the code for errors", E_USER_ERROR);
         }
         $black_matrix = new Matrix($grey_matrix->getMatrix());
         //recreate for loop
         $routes_ids = $black_matrix->getColumnKeys();
         //recreate for loop
         $assignments = array();
         // list of assignments for solution
         $previous_route_id = false;
         $previous_route_value = false;
         $n = 1;
         //for slice porpuses
         foreach ($routes_ids as $route_id) {
             if ($black_matrix->possible() === false) {
                 return false;
             }
             if (count($black_matrix->getColumnKeys()) == 0 || count($black_matrix->getRowKeys()) == 0) {
                 return false;
             }
             if (!isset($ignores[$route_id])) {
                 $ignores[$route_id] = array();
             }
             $fix_truck = $black_matrix->getFirstPositiveRow($route_id, $ignores[$route_id]);
             //there are no more positive values availables
             if ($fix_truck === false) {
//.........這裏部分代碼省略.........
開發者ID:benjaminvatterj,項目名稱:cwheuristic,代碼行數:101,代碼來源:matrixcw.php

示例5: testKroneckerProduct

 /**
  * @dataProvider dataProviderForKroneckerProduct
  */
 public function testKroneckerProduct(array $A, array $B, array $expected)
 {
     $A = new Matrix($A);
     $B = new Matrix($B);
     $A⊗B = $A->kroneckerProduct($B);
     $expected = new Matrix($expected);
     $this->assertEquals($expected->getMatrix(), $A⊗B->getMatrix());
 }
開發者ID:markrogoyski,項目名稱:math-php,代碼行數:11,代碼來源:MatrixOperationsTest.php

示例6: testGetMatrix

 /**
  * @dataProvider dataProviderMulti
  */
 public function testGetMatrix(array $A)
 {
     $S = new SquareMatrix($A);
     $M = new Matrix($A);
     $this->assertEquals($M->getMatrix(), $S->getMatrix());
 }
開發者ID:markrogoyski,項目名稱:math-php,代碼行數:9,代碼來源:SquareMatrixTest.php


注:本文中的Matrix::getMatrix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。