/** * times * * Matrix multiplication * @param mixed $n Matrix/Array/Scalar * @return Matrix Product */ public function times() { if (func_num_args() > 0) { $args = func_get_args(); $match = implode(",", array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $B = $args[0]; } else { throw new PHPExcel_Calculation_Exception(self::ARGUMENT_TYPE_EXCEPTION); } if ($this->n == $B->m) { $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n); for ($j = 0; $j < $B->n; ++$j) { for ($k = 0; $k < $this->n; ++$k) { $Bcolj[$k] = $B->A[$k][$j]; } for ($i = 0; $i < $this->m; ++$i) { $Arowi = $this->A[$i]; $s = 0; for ($k = 0; $k < $this->n; ++$k) { $s += $Arowi[$k] * $Bcolj[$k]; } $C->A[$i][$j] = $s; } } return $C; } else { throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch)); } break; case 'array': $B = new PHPExcel_Shared_JAMA_Matrix($args[0]); if ($this->n == $B->m) { $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $s = "0"; for ($k = 0; $k < $C->n; ++$k) { $s += $this->A[$i][$k] * $B->A[$k][$j]; } $C->A[$i][$j] = $s; } } return $C; } else { throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionMismatch)); } return $M; break; case 'integer': $C = new PHPExcel_Shared_JAMA_Matrix($this->A); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] *= $args[0]; } } return $C; break; case 'double': $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $this->n); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] = $args[0] * $this->A[$i][$j]; } } return $C; break; case 'float': $C = new PHPExcel_Shared_JAMA_Matrix($this->A); for ($i = 0; $i < $C->m; ++$i) { for ($j = 0; $j < $C->n; ++$j) { $C->A[$i][$j] *= $args[0]; } } return $C; break; default: throw new PHPExcel_Calculation_Exception(self::POLYMORPHIC_ARGUMENT_EXCEPTION); break; } } else { throw new PHPExcel_Calculation_Exception(self::POLYMORPHIC_ARGUMENT_EXCEPTION); } }
/** * concat * * A = A & B * @param mixed $B Matrix/Array * @return Matrix Sum */ public function concat() { if (func_num_args() > 0) { $args = func_get_args(); $match = implode(",", array_map('gettype', $args)); switch ($match) { case 'object': if ($args[0] instanceof Matrix) { $M = $args[0]; } else { throw new Exception(JAMAError(ArgumentTypeException)); } case 'array': $M = new Matrix($args[0]); break; default: throw new Exception(JAMAError(PolymorphicArgumentException)); break; } $this->checkMatrixDimensions($M); for ($i = 0; $i < $this->m; ++$i) { for ($j = 0; $j < $this->n; ++$j) { // $this->A[$i][$j] = '"'.trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"').'"'; $this->A[$i][$j] = trim($this->A[$i][$j], '"') . trim($M->get($i, $j), '"'); } } return $this; } else { throw new Exception(JAMAError(PolymorphicArgumentException)); } }
/** * Solve A*X = B * * @param $B Row-equal matrix * @return Matrix L * L' * X = B */ public function solve($B = null) { if ($B instanceof Matrix) { if ($B->getRowDimension() == $this->m) { if ($this->isspd) { $X = $B->getArrayCopy(); $nx = $B->getColumnDimension(); for ($k = 0; $k < $this->m; ++$k) { for ($i = $k + 1; $i < $this->m; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k]; } } for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } } for ($k = $this->m - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i]; } } } return new Matrix($X, $this->m, $nx); } else { throw new Exception(JAMAError(MatrixSPDException)); } } else { throw new Exception(JAMAError(MatrixDimensionException)); } } else { throw new Exception(JAMAError(ArgumentTypeException)); } }
/** * 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)); } }
/** * Solve A*X = B * * @param $B Row-equal matrix * @return Matrix L * L' * X = B */ public function solve($B = null) { if ($B instanceof Matrix) { if ($B->getRowDimension() == $this->m) { if ($this->isspd) { $X = $B->getArrayCopy(); $nx = $B->getColumnDimension(); for ($k = 0; $k < $this->m; ++$k) { for ($i = $k + 1; $i < $this->m; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k]; } } for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } } for ($k = $this->m - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X[$k][$j] /= $this->L[$k][$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i]; } } } return new Matrix($X, $this->m, $nx); } else { throw new PHPExcel_Calculation_Exception(JAMAError(MatrixSPDException)); } } else { throw new PHPExcel_Calculation_Exception(JAMAError(MATRIX_DIMENSION_EXCEPTION)); } } else { throw new PHPExcel_Calculation_Exception(JAMAError(ARGUMENT_TYPE_EXCEPTION)); } }
/** * Solve A*X = B * * @param $B A Matrix with as many rows as A and any number of columns. * @return X so that L*U*X = B(piv,:) * @exception IllegalArgumentException Matrix row dimensions must agree. * @exception RuntimeException Matrix is singular. */ public function solve($B) { if ($B->getRowDimension() == $this->m) { if ($this->isNonsingular()) { // Copy right hand side with pivoting $nx = $B->getColumnDimension(); $X = $B->getMatrix($this->piv, 0, $nx - 1); // Solve L*Y = B(piv,:) for ($k = 0; $k < $this->n; ++$k) { for ($i = $k + 1; $i < $this->n; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k]; } } } // Solve U*X = Y; for ($k = $this->n - 1; $k >= 0; --$k) { for ($j = 0; $j < $nx; ++$j) { $X->A[$k][$j] /= $this->LU[$k][$k]; } for ($i = 0; $i < $k; ++$i) { for ($j = 0; $j < $nx; ++$j) { $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k]; } } } return $X; } else { throw new Exception(JAMAError(MatrixSingularException)); } } else { throw new Exception(JAMAError(MatrixSquareException)); } }