Example #1
0
 /**
  *	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 PHPExcel_Shared_JAMA_Matrix($X);
             return $X->getMatrix(0, $this->n - 1, 0, $nx);
         } else {
             throw new Exception(self::MatrixRankException);
         }
     } else {
         throw new Exception(PHPExcel_Shared_JAMA_Matrix::MatrixDimensionException);
     }
 }
Example #2
0
 /**
  * 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.
  */
 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 {
             trigger_error(MatrixRankException, ERROR);
         }
     } else {
         trigger_error(MatrixDimensionException, ERROR);
     }
 }