Skip to content

Commit

Permalink
Added function to perform QR matrix decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Greve committed Apr 21, 2020
1 parent be5233c commit 3f8ab07
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ int MatrixIsZero(MATRIX *m) ;
int MatrixIsIdentity(MATRIX *m) ;
MATRIX *MatrixReshape(MATRIX *m_src, MATRIX *m_dst, int rows, int cols) ;
int MatrixCheck(MATRIX *m) ;
int MatrixQRdecomposition(const MATRIX *iMatrix, MATRIX *oQ, MATRIX *oR);
MATRIX *MatrixInverse( const MATRIX *mIn, MATRIX *mOut) ;
MATRIX *MatrixPseudoInverse(MATRIX *m, MATRIX *m_pseudo_inv) ;
MATRIX *MatrixSVDPseudoInverse(MATRIX *m, MATRIX *m_pseudo_inv) ;
Expand Down
2 changes: 2 additions & 0 deletions include/numerics.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
// this is only called from the matrix class
int OpenSvdcmp( MATRIX *a, VECTOR *w, MATRIX *v );

int OpenQRdecomposition(const MATRIX *iMatrix, MATRIX *oQ, MATRIX *oR);

float OpenMatrixDeterminant( MATRIX *matrix );

int OpenLUMatrixInverse( MATRIX *matrix, MATRIX *inverse );
Expand Down
7 changes: 7 additions & 0 deletions utils/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ MATRIX *MatrixCopy(const MATRIX *mIn, MATRIX *mOut)
return (mOut);
}

int MatrixQRdecomposition(const MATRIX *iMatrix, MATRIX *oQ, MATRIX *oR)
{
int r = OpenQRdecomposition(iMatrix,oQ,oR);
return(r);
}

MATRIX *MatrixInverse(const MATRIX *mIn, MATRIX *mOut)
{
// float **a, **y;
Expand Down Expand Up @@ -4707,3 +4713,4 @@ MATRIX *MatrixGlmFit(MATRIX *y, MATRIX *X, double *pRVar, MATRIX *beta)
MatrixFree(&res);
return(beta);
}

45 changes: 45 additions & 0 deletions utils/numerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,51 @@ int OpenLUMatrixInverse(MATRIX *iMatrix, MATRIX *oInverse)
return errorCode;
}

/*!
\fn int OpenQRdecomposition(const MATRIX *iMatrix, MATRIX *oQ, MATRIX *oR)
\brief Performs QR decomposition on the input matrix. The output
matricies must be allocated already (Q is nrowsxnows where nrows is
the number of rows in the input matrix, and R is same size as the
input matrix).
*/
int OpenQRdecomposition(const MATRIX *iMatrix, MATRIX *oQ, MATRIX *oR)
{
if(iMatrix==NULL){
printf("QRdecomposition(): input matrix is NULL\n");
return(1);
}
if(oQ==NULL){
printf("QRdecomposition(): output Q matrix is NULL\n");
return(1);
}
if(oR==NULL){
printf("QRdecomposition(): output R matrix is NULL\n");
return(1);
}
if(oQ->rows != iMatrix->rows){
printf("QRdecomposition(): Q rows != input Matrix rows\n");
return(1);
}
if(oQ->cols != oQ->rows){
printf("QRdecomposition(): Q cols != Q rows\n");
return(1);
}
if(oR->rows != iMatrix->rows){
printf("QRdecomposition(): R rows != input Matrix rows\n");
return(1);
}
if(oR->cols != iMatrix->cols){
printf("QRdecomposition(): R cols != input Matrix cols\n");
return(1);
}

vnl_matrix< float > vnlMatrix(iMatrix->data, iMatrix->rows, iMatrix->cols);
vnl_qr<float> qr( vnlMatrix );
qr.Q().copy_out(oQ->data);
qr.R().copy_out(oR->data);
return(0);
}

/**
* Returns the determinant of matrix.
* @param iMatrix
Expand Down

0 comments on commit 3f8ab07

Please sign in to comment.