ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
QRDecomposition Class Reference
+ Collaboration diagram for QRDecomposition:

Public Member Functions

 __construct ($A)
 QR Decomposition computed by Householder reflections.
 isFullRank ()
 Is the matrix full rank?
 getH ()
 Return the Householder vectors.
 getR ()
 Return the upper triangular factor.
 getQ ()
 Generate and return the (economy-sized) orthogonal factor.
 solve ($B)
 Least squares solution of A*X = B.

Private Attributes

 $QR = array()
 $m
 $n
 $Rdiag = array()

Detailed Description

Definition at line 19 of file QRDecomposition.php.

Constructor & Destructor Documentation

QRDecomposition::__construct (   $A)

QR Decomposition computed by Householder reflections.

Parameters
matrix$ARectangular matrix
Returns
Structure to access R and the Householder vectors and compute Q.

Definition at line 52 of file QRDecomposition.php.

References $m, $n, ArgumentTypeException, hypo(), JAMAError(), and n.

{
if($A instanceof Matrix) {
// Initialize.
$this->QR = $A->getArrayCopy();
$this->m = $A->getRowDimension();
$this->n = $A->getColumnDimension();
// Main loop.
for ($k = 0; $k < $this->n; ++$k) {
// Compute 2-norm of k-th column without under/overflow.
$nrm = 0.0;
for ($i = $k; $i < $this->m; ++$i) {
$nrm = hypo($nrm, $this->QR[$i][$k]);
}
if ($nrm != 0.0) {
// Form k-th Householder vector.
if ($this->QR[$k][$k] < 0) {
$nrm = -$nrm;
}
for ($i = $k; $i < $this->m; ++$i) {
$this->QR[$i][$k] /= $nrm;
}
$this->QR[$k][$k] += 1.0;
// Apply transformation to remaining columns.
for ($j = $k+1; $j < $this->n; ++$j) {
$s = 0.0;
for ($i = $k; $i < $this->m; ++$i) {
$s += $this->QR[$i][$k] * $this->QR[$i][$j];
}
$s = -$s/$this->QR[$k][$k];
for ($i = $k; $i < $this->m; ++$i) {
$this->QR[$i][$j] += $s * $this->QR[$i][$k];
}
}
}
$this->Rdiag[$k] = -$nrm;
}
} else {
}
} // function __construct()

+ Here is the call graph for this function:

Member Function Documentation

QRDecomposition::getH ( )

Return the Householder vectors.

Returns
Matrix Lower trapezoidal matrix whose columns define the reflections

Definition at line 114 of file QRDecomposition.php.

References $m, and $n.

{
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
if ($i >= $j) {
$H[$i][$j] = $this->QR[$i][$j];
} else {
$H[$i][$j] = 0.0;
}
}
}
return new Matrix($H);
} // function getH()
QRDecomposition::getQ ( )

Generate and return the (economy-sized) orthogonal factor.

Returns
Matrix orthogonal factor

Definition at line 154 of file QRDecomposition.php.

References $m, $n, and n.

{
for ($k = $this->n-1; $k >= 0; --$k) {
for ($i = 0; $i < $this->m; ++$i) {
$Q[$i][$k] = 0.0;
}
$Q[$k][$k] = 1.0;
for ($j = $k; $j < $this->n; ++$j) {
if ($this->QR[$k][$k] != 0) {
$s = 0.0;
for ($i = $k; $i < $this->m; ++$i) {
$s += $this->QR[$i][$k] * $Q[$i][$j];
}
$s = -$s/$this->QR[$k][$k];
for ($i = $k; $i < $this->m; ++$i) {
$Q[$i][$j] += $s * $this->QR[$i][$k];
}
}
}
}
/*
for($i = 0; $i < count($Q); ++$i) {
for($j = 0; $j < count($Q); ++$j) {
if(! isset($Q[$i][$j]) ) {
$Q[$i][$j] = 0;
}
}
}
*/
return new Matrix($Q);
} // function getQ()
QRDecomposition::getR ( )

Return the upper triangular factor.

Returns
Matrix upper triangular factor

Definition at line 133 of file QRDecomposition.php.

References $n, and elseif().

{
for ($i = 0; $i < $this->n; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
if ($i < $j) {
$R[$i][$j] = $this->QR[$i][$j];
} elseif ($i == $j) {
$R[$i][$j] = $this->Rdiag[$i];
} else {
$R[$i][$j] = 0.0;
}
}
}
return new Matrix($R);
} // function getR()

+ Here is the call graph for this function:

QRDecomposition::isFullRank ( )

Is the matrix full rank?

Returns
boolean true if R, and hence A, has full rank, else false.

Definition at line 99 of file QRDecomposition.php.

References $n.

Referenced by solve().

{
for ($j = 0; $j < $this->n; ++$j) {
if ($this->Rdiag[$j] == 0) {
return false;
}
}
return true;
} // function isFullRank()

+ Here is the caller graph for this function:

QRDecomposition::solve (   $B)

Least squares solution of A*X = B.

Parameters
Matrix$BA Matrix with as many rows as A and any number of columns.
Returns
Matrix Matrix that minimizes the two norm of Q*R*X-B.

Definition at line 192 of file QRDecomposition.php.

References $m, $n, $X, isFullRank(), JAMAError(), MatrixDimensionException, and MatrixRankException.

{
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 {
}
} else {
}
} // function solve()

+ Here is the call graph for this function:

Field Documentation

QRDecomposition::$m
private

Definition at line 31 of file QRDecomposition.php.

Referenced by __construct(), getH(), getQ(), and solve().

QRDecomposition::$n
private

Definition at line 37 of file QRDecomposition.php.

Referenced by __construct(), getH(), getQ(), getR(), isFullRank(), and solve().

QRDecomposition::$QR = array()
private

Definition at line 25 of file QRDecomposition.php.

QRDecomposition::$Rdiag = array()
private

Definition at line 43 of file QRDecomposition.php.


The documentation for this class was generated from the following file: