improved efficiency of determinant calculations
This commit is contained in:
1 parent
c5214cde2e
commit
2eb8e35b92
4 files changed
+162
-46
No files matched your search
+14
-6
@@ -95,14 +95,15 @@ The basic solver uses the LU decomposition with partial pivoting (\code{dgesv}/\
|
||||
\code{NRMat} and the Bunch--Kaufman factorization (\code{dspsv}) for packed symmetric \code{NRSMat}.
|
||||
\textbf{The right-hand sides are the rows of \code{B}}: \code{B} has dimension $n_{\mathrm{rhs}}\times n$ and
|
||||
each row $b$ is overwritten by the solution of $A x=b$ (i.e.\ $A X\Tr=B\Tr$). \code{A} is overwritten
|
||||
by its factorization. With \code{B==NULL} only the factorization (and determinant) is computed.
|
||||
by its factorization. With \code{B==NULL} only the factorization (and determinant) is computed, by a direct call of the
|
||||
factorization routine (\code{getrf}, \code{sptrf}, \code{hptrf}), since some implementations of the solvers skip the
|
||||
factorization when there are no right-hand sides. In that case a general matrix is overwritten by the factorization of its transpose.
|
||||
|
||||
\begin{lareference}
|
||||
\lafunc{void linear_solve(NRMat<T> &a, NRMat<T> *b, T *det=0, int n=0)}{Solve $A x=b$ for all rows $b$ of \code{*b}
|
||||
(\code{b->ncols()} must be $n$). If \code{det} is non-NULL, the determinant of $A$ is returned there (0 for a singular matrix,
|
||||
then no error is raised if \code{b==NULL}); a singular matrix with \code{b!=NULL} raises \code{laerror}.
|
||||
\code{n>0} restricts the problem to the leading $n\times n$ block of \code{a} and the first $n$ columns of \code{b}. Prints a warning when \code{b} is \code{NULL} (some LAPACK libraries skip the factorization for
|
||||
$n_{\mathrm{rhs}}=0$).}
|
||||
\code{n>0} restricts the problem to the leading $n\times n$ block of \code{a} and the first $n$ columns of \code{b}.}
|
||||
\lafunc{void linear_solve(NRMat<T> &a, NRVec<T> &b, T *det=0, int n=0)}{The same with a single right-hand side \code{b}.}
|
||||
\lafunc{void linear_solve(NRSMat<T> &a, NRMat<T> *b, T *det=0, int n=0); void linear_solve(NRSMat<T> &a, NRVec<T> &b, T *det=0, int n=0)}{Symmetric (real) or hermitian (complex), possibly indefinite $A$ in packed storage, \code{dspsv}/\code{zhpsv}
|
||||
(Bunch--Kaufman factorization; the determinant is computed from its $1\times1$ and $2\times2$ blocks and is real in the hermitian case).
|
||||
@@ -135,9 +136,16 @@ this is not tested, a nearly singular block gives a huge result (check \code{det
|
||||
solution $x=V\,\mathrm{diag}(1/s_i)\,U\Hc b$ discarding singular values $\le$\code{thres} (no contamination by the null space);
|
||||
the number of discarded ones is stored in \code{*nullity}. Here (unlike \code{linear_solve}) the right-hand sides are the
|
||||
\emph{columns} of the matrix \code{b} ($m\times n_{\mathrm{rhs}}$); \code{a} ($m\times n$, $m\ge n$) is destroyed.}
|
||||
\lafunc{const LA_traits<MAT>::elementtype determinant(MAT a)}{Determinant by LU (\code{NRMat}) or Bunch--Kaufman (\code{NRSMat}) factorization of a copy.}
|
||||
\lafunc{const LA_traits<MAT>::elementtype determinant_destroy(MAT &a)}{The same, overwriting \code{a}. Unless \code{HAS_MKL} is defined, a dummy right-hand side
|
||||
is passed to \code{linear_solve} (work-around for OpenBLAS, which skips the factorization without right-hand sides).}
|
||||
\lafunc{const LA_traits<MAT>::elementtype determinant(MAT a)}{Determinant of a copy of the matrix by \code{determinant_destroy}.}
|
||||
\lafunc{const LA_traits<MAT>::elementtype determinant_destroy(MAT &a)}{Determinant, overwriting \code{a}. A singular matrix gives 0,
|
||||
no error is raised. For an \code{NRMat} of dimension up to \code{LA_SMALL_DETERMINANT} (48 unless defined otherwise at compilation)
|
||||
it calls \code{small_determinant_destroy}, in all other cases \code{lapack_determinant_destroy}; the user code need not care.
|
||||
The limit is the measured crossover with OpenBLAS (between 48 and 64); with the reference LAPACK the inline elimination was not slower even at dimension 256.}
|
||||
\lafunc{const LA_traits<MAT>::elementtype lapack_determinant_destroy(MAT &a)}{Determinant from the LU (\code{NRMat}) or Bunch--Kaufman
|
||||
(\code{NRSMat}) factorization by LAPACK (\code{linear_solve} without right-hand sides), overwriting \code{a}.}
|
||||
\lafunc{const T small_determinant_destroy(NRMat<T> &a)}{Determinant by Gauss elimination with partial pivoting coded inline,
|
||||
overwriting \code{a}. It calls no LAPACK routine, which makes it several times faster for small matrices (about 5 times at dimension 6,
|
||||
3 times at 16).}
|
||||
\lafunc{void cholesky(NRMat<T> &a, bool upper=1)}{Cholesky factorization of a
|
||||
positive definite matrix in place (\code{dpotrf}/\code{zpotrf}). \code{upper=1}: \code{a} becomes the upper triangular $U$ with
|
||||
$A=U\Hc U$; \code{upper=0}: lower triangular $L$ with $A=L L\Hc$. The other triangle is zeroed. Raises \code{laerror} if $A$ is not positive definite.}
|
||||
|
||||
+71
-26
@@ -137,13 +137,19 @@ void lawritemat(FILE *file,const T *a,int r,int c,const char *form0,
|
||||
// LAPACK interface //
|
||||
//////////////////////
|
||||
|
||||
//LU decomposition alone, called when there are no right-hand sides (some gesv implementations skip the decomposition when nrhs==0)
|
||||
//fortran sees the row-major matrix transposed, which has the same determinant; the matrix is not transposed back
|
||||
extern "C" void FORNAME(zgetrf)(const FINT *M, const FINT *N, std::complex<double> *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(cgetrf)(const FINT *M, const FINT *N, std::complex<float> *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(dgetrf)(const FINT *M, const FINT *N, double *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(sgetrf)(const FINT *M, const FINT *N, float *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
|
||||
// A will be overwritten, B will contain the solutions, A is nxn, B is rhs x n
|
||||
static void linear_solve_do(NRMat<double> &A, double *B, const int nrhs, const int ldb, double *det, int n)
|
||||
{
|
||||
int r, *ipiv;
|
||||
int iswap=0;
|
||||
|
||||
if(nrhs==0) std::cout<<"Warning: some dgesv implementations might skip LU decomposition when nrhs==0\n";
|
||||
if (n==A.nrows() && A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
|
||||
A.copyonwrite();
|
||||
ipiv = new int[A.nrows()];
|
||||
@@ -153,7 +159,22 @@ static void linear_solve_do(NRMat<double> &A, double *B, const int nrhs, const i
|
||||
std::cout <<"A before clapack_dgesv = "<<A<<std::endl;
|
||||
std::cout <<"Active dimension n= "<<n<<std::endl;
|
||||
#endif
|
||||
r = clapack_dgesv(CblasRowMajor, n, nrhs, &A(0,0), A.ncols(), ipiv, B , ldb);
|
||||
if(nrhs) r = clapack_dgesv(CblasRowMajor, n, nrhs, &A(0,0), A.ncols(), ipiv, B , ldb);
|
||||
else
|
||||
{
|
||||
const FINT ntmp=n;
|
||||
const FINT ldatmp=A.ncols();
|
||||
FINT info;
|
||||
#ifdef FORINT
|
||||
FINT *ipivtmp = new FINT[n];
|
||||
FORNAME(dgetrf)(&ntmp,&ntmp,&A(0,0),&ldatmp,ipivtmp,&info);
|
||||
for(int i=0; i<n; ++i) ipiv[i]=ipivtmp[i];
|
||||
delete[] ipivtmp;
|
||||
#else
|
||||
FORNAME(dgetrf)(&ntmp,&ntmp,&A(0,0),&ldatmp,ipiv,&info);
|
||||
#endif
|
||||
r=info;
|
||||
}
|
||||
#ifdef IPIV_DEBUG
|
||||
std::cout <<"A after clapack_dgesv = "<<A<<std::endl;
|
||||
std::cout <<"ipiv = ";
|
||||
@@ -199,7 +220,6 @@ static void linear_solve_do(NRMat<float> &A, float *B, const int nrhs, const int
|
||||
int r, *ipiv;
|
||||
int iswap=0;
|
||||
|
||||
if(nrhs==0) std::cout<<"Warning: some dgesv implementations might skip LU decomposition when nrhs==0\n";
|
||||
if (n==A.nrows() && A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
|
||||
A.copyonwrite();
|
||||
ipiv = new int[A.nrows()];
|
||||
@@ -209,7 +229,22 @@ static void linear_solve_do(NRMat<float> &A, float *B, const int nrhs, const int
|
||||
std::cout <<"A before clapack_sgesv = "<<A<<std::endl;
|
||||
std::cout <<"Active dimension n= "<<n<<std::endl;
|
||||
#endif
|
||||
r = clapack_sgesv(CblasRowMajor, n, nrhs, &A(0,0), A.ncols(), ipiv, B , ldb);
|
||||
if(nrhs) r = clapack_sgesv(CblasRowMajor, n, nrhs, &A(0,0), A.ncols(), ipiv, B , ldb);
|
||||
else
|
||||
{
|
||||
const FINT ntmp=n;
|
||||
const FINT ldatmp=A.ncols();
|
||||
FINT info;
|
||||
#ifdef FORINT
|
||||
FINT *ipivtmp = new FINT[n];
|
||||
FORNAME(sgetrf)(&ntmp,&ntmp,&A(0,0),&ldatmp,ipivtmp,&info);
|
||||
for(int i=0; i<n; ++i) ipiv[i]=ipivtmp[i];
|
||||
delete[] ipivtmp;
|
||||
#else
|
||||
FORNAME(sgetrf)(&ntmp,&ntmp,&A(0,0),&ldatmp,ipiv,&info);
|
||||
#endif
|
||||
r=info;
|
||||
}
|
||||
#ifdef IPIV_DEBUG
|
||||
std::cout <<"A after clapack_sgesv = "<<A<<std::endl;
|
||||
std::cout <<"ipiv = ";
|
||||
@@ -290,10 +325,12 @@ extern "C" void FORNAME(dspsv)(const char *UPLO, const FINT *N, const FINT *NRHS
|
||||
double *AP, FINT *IPIV, double *B, const FINT *LDB, FINT *INFO);
|
||||
extern "C" void FORNAME(sspsv)(const char *UPLO, const FINT *N, const FINT *NRHS,
|
||||
float *AP, FINT *IPIV, float *B, const FINT *LDB, FINT *INFO);
|
||||
//Bunch-Kaufman decomposition alone, called when there are no right-hand sides
|
||||
extern "C" void FORNAME(dsptrf)(const char *UPLO, const FINT *N, double *AP, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(ssptrf)(const char *UPLO, const FINT *N, float *AP, FINT *IPIV, FINT *INFO);
|
||||
|
||||
static void linear_solve_do(NRSMat<double> &a, double *b, const int nrhs, const int ldb, double *det, int n)
|
||||
{
|
||||
if(nrhs==0) std::cout<<"Warning: some dspsv implementations might skip LU decomposition when nrhs==0\n";
|
||||
FINT r, *ipiv;
|
||||
a.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
@@ -302,9 +339,11 @@ static void linear_solve_do(NRSMat<double> &a, double *b, const int nrhs, const
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(dspsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp,&r);
|
||||
if(nrhs) FORNAME(dspsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp,&r);
|
||||
else FORNAME(dsptrf)(&U, &ntmp, a, ipiv, &r);
|
||||
#else
|
||||
FORNAME(dspsv)(&U, &n, &nrhs, a, ipiv, b, &ldb,&r);
|
||||
if(nrhs) FORNAME(dspsv)(&U, &n, &nrhs, a, ipiv, b, &ldb,&r);
|
||||
else FORNAME(dsptrf)(&U, &n, a, ipiv, &r);
|
||||
#endif
|
||||
// std::cout <<"A after dspsv = "<<a<<std::endl;
|
||||
if (r < 0) {
|
||||
@@ -329,7 +368,6 @@ static void linear_solve_do(NRSMat<double> &a, double *b, const int nrhs, const
|
||||
|
||||
static void linear_solve_do(NRSMat<float> &a, float *b, const int nrhs, const int ldb, float *det, int n)
|
||||
{
|
||||
if(nrhs==0) std::cout<<"Warning: some dspsv implementations might skip LU decomposition when nrhs==0\n";
|
||||
FINT r, *ipiv;
|
||||
a.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
@@ -338,9 +376,11 @@ static void linear_solve_do(NRSMat<float> &a, float *b, const int nrhs, const in
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(sspsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp,&r);
|
||||
if(nrhs) FORNAME(sspsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp,&r);
|
||||
else FORNAME(ssptrf)(&U, &ntmp, a, ipiv, &r);
|
||||
#else
|
||||
FORNAME(sspsv)(&U, &n, &nrhs, a, ipiv, b, &ldb,&r);
|
||||
if(nrhs) FORNAME(sspsv)(&U, &n, &nrhs, a, ipiv, b, &ldb,&r);
|
||||
else FORNAME(ssptrf)(&U, &n, a, ipiv, &r);
|
||||
#endif
|
||||
// std::cout <<"A after dspsv = "<<a<<std::endl;
|
||||
if (r < 0) {
|
||||
@@ -407,10 +447,12 @@ extern "C" void FORNAME(zhpsv)(const char *UPLO, const FINT *N, const FINT *NRHS
|
||||
std::complex<double> *AP, FINT *IPIV, std::complex<double> *B, const FINT *LDB, FINT *INFO);
|
||||
extern "C" void FORNAME(chpsv)(const char *UPLO, const FINT *N, const FINT *NRHS,
|
||||
std::complex<float> *AP, FINT *IPIV, std::complex<float> *B, const FINT *LDB, FINT *INFO);
|
||||
//Bunch-Kaufman decomposition alone, called when there are no right-hand sides
|
||||
extern "C" void FORNAME(zhptrf)(const char *UPLO, const FINT *N, std::complex<double> *AP, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(chptrf)(const char *UPLO, const FINT *N, std::complex<float> *AP, FINT *IPIV, FINT *INFO);
|
||||
|
||||
static void linear_solve_do(NRSMat<std::complex<double> > &a, std::complex<double> *b, const int nrhs, const int ldb, std::complex<double> *det, int n)
|
||||
{
|
||||
if(nrhs==0) std::cout<<"Warning: some zhpsv implementations might skip LU decomposition when nrhs==0\n";
|
||||
FINT r, *ipiv;
|
||||
a.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
@@ -421,7 +463,8 @@ static void linear_solve_do(NRSMat<std::complex<double> > &a, std::complex<doubl
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(zhpsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp, &r);
|
||||
if(nrhs) FORNAME(zhpsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp, &r);
|
||||
else FORNAME(zhptrf)(&U, &ntmp, a, ipiv, &r);
|
||||
for(int i=0; i<nrhs; ++i) for(int j=0; j<n; ++j) b[i*ldb+j] = std::conj(b[i*ldb+j]);
|
||||
if (r < 0) {
|
||||
delete[] ipiv;
|
||||
@@ -445,7 +488,6 @@ static void linear_solve_do(NRSMat<std::complex<double> > &a, std::complex<doubl
|
||||
|
||||
static void linear_solve_do(NRSMat<std::complex<float> > &a, std::complex<float> *b, const int nrhs, const int ldb, std::complex<float> *det, int n)
|
||||
{
|
||||
if(nrhs==0) std::cout<<"Warning: some zhpsv implementations might skip LU decomposition when nrhs==0\n";
|
||||
FINT r, *ipiv;
|
||||
a.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
@@ -456,7 +498,8 @@ static void linear_solve_do(NRSMat<std::complex<float> > &a, std::complex<float>
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(chpsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp, &r);
|
||||
if(nrhs) FORNAME(chpsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp, &r);
|
||||
else FORNAME(chptrf)(&U, &ntmp, a, ipiv, &r);
|
||||
for(int i=0; i<nrhs; ++i) for(int j=0; j<n; ++j) b[i*ldb+j] = std::conj(b[i*ldb+j]);
|
||||
if (r < 0) {
|
||||
delete[] ipiv;
|
||||
@@ -530,19 +573,22 @@ static void linear_solve_do(NRMat<std::complex<double> > &A, std::complex<double
|
||||
FINT r, *ipiv;
|
||||
int iswap=0;
|
||||
|
||||
if(nrhs==0) std::cout<<"Warning: some zgesv implementations might skip LU decomposition when nrhs==0\n";
|
||||
if (n==A.nrows() && A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
|
||||
A.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
const int lda=A.ncols();
|
||||
std::complex<double> *a= &A(0,0);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<double> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldatmp=lda;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(zgesv)(&ntmp, &nrhstmp, a, &ldatmp, ipiv, B, &ldbtmp, &r);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<double> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
if(nrhs)
|
||||
{
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<double> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
FORNAME(zgesv)(&ntmp, &nrhstmp, a, &ldatmp, ipiv, B, &ldbtmp, &r);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<double> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
}
|
||||
else FORNAME(zgetrf)(&ntmp, &ntmp, a, &ldatmp, ipiv, &r);
|
||||
if (r < 0) {
|
||||
delete[] ipiv;
|
||||
laerror("illegal argument in lapack_gesv");
|
||||
@@ -582,19 +628,22 @@ static void linear_solve_do(NRMat<std::complex<float> > &A, std::complex<float>
|
||||
FINT r, *ipiv;
|
||||
int iswap=0;
|
||||
|
||||
if(nrhs==0) std::cout<<"Warning: some zgesv implementations might skip LU decomposition when nrhs==0\n";
|
||||
if (n==A.nrows() && A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
|
||||
A.copyonwrite();
|
||||
ipiv = new FINT[n];
|
||||
const int lda=A.ncols();
|
||||
std::complex<float> *a= &A(0,0);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<float> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
const FINT ntmp=n;
|
||||
const FINT nrhstmp=nrhs;
|
||||
const FINT ldatmp=lda;
|
||||
const FINT ldbtmp=ldb;
|
||||
FORNAME(cgesv)(&ntmp, &nrhstmp, a, &ldatmp, ipiv, B, &ldbtmp, &r);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<float> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
if(nrhs)
|
||||
{
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<float> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
FORNAME(cgesv)(&ntmp, &nrhstmp, a, &ldatmp, ipiv, B, &ldbtmp, &r);
|
||||
for(int i=1; i<n; ++i) for(int j=0; j<i; ++j) {std::complex<float> t=a[j*lda+i]; a[j*lda+i]=a[i*lda+j]; a[i*lda+j]=t;}
|
||||
}
|
||||
else FORNAME(cgetrf)(&ntmp, &ntmp, a, &ldatmp, ipiv, &r);
|
||||
if (r < 0) {
|
||||
delete[] ipiv;
|
||||
laerror("illegal argument in lapack_gesv");
|
||||
@@ -3771,10 +3820,6 @@ LA_traits<NRMat<float> >::normtype MatrixNorm(const NRMat<float> &A, const char
|
||||
|
||||
|
||||
//condition number ||A|| ||A^{-1}|| in the 1-norm or infinity norm, from the LU factorization of a copy of A
|
||||
extern "C" void FORNAME(zgetrf)(const FINT *M, const FINT *N, std::complex<double> *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(cgetrf)(const FINT *M, const FINT *N, std::complex<float> *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(dgetrf)(const FINT *M, const FINT *N, double *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(sgetrf)(const FINT *M, const FINT *N, float *A, const FINT *LDA, FINT *IPIV, FINT *INFO);
|
||||
extern "C" void FORNAME(zgecon)( const char *norm, const FINT *n, const std::complex<double> *A, const FINT *LDA, const double *anorm, double *rcond, std::complex<double> *work, double *rwork, FINT *info);
|
||||
extern "C" void FORNAME(cgecon)( const char *norm, const FINT *n, const std::complex<float> *A, const FINT *LDA, const float *anorm, float *rcond, std::complex<float> *work, float *rwork, FINT *info);
|
||||
extern "C" void FORNAME(dgecon)( const char *norm, const FINT *n, const double *A, const FINT *LDA, const double *anorm, double *rcond, double *work, FINT *iwork, FINT *info);
|
||||
|
||||
+53
-14
@@ -365,27 +365,66 @@ typename LA_traits<MAT>::normtype MatrixNorm(const MAT &A, const char norm);
|
||||
template<class MAT>
|
||||
typename LA_traits<MAT>::normtype CondNumber(const MAT &A, const char norm);
|
||||
|
||||
#ifdef HAS_MKL
|
||||
#define NO_OPENBLAS_WORKAROUND
|
||||
//determinant by the LAPACK decomposition without right-hand sides (getrf, sptrf, hptrf), destructive on input
|
||||
template<class MAT>
|
||||
const typename LA_traits<MAT>::elementtype lapack_determinant_destroy(MAT &a) //passed by reference
|
||||
{
|
||||
typename LA_traits<MAT>::elementtype det;
|
||||
if(a.nrows()!=a.ncols()) laerror("determinant of non-square matrix");
|
||||
linear_solve(a,NULL,&det);
|
||||
return det;
|
||||
}
|
||||
|
||||
//determinant by Gauss elimination with partial pivoting coded inline, destructive on input
|
||||
//no LAPACK call: for small matrices it is several times faster than lapack_determinant_destroy()
|
||||
template<typename T>
|
||||
const T small_determinant_destroy(NRMat<T> &a)
|
||||
{
|
||||
const int n=a.nrows();
|
||||
if(n!=a.ncols()) laerror("determinant of non-square matrix");
|
||||
a.copyonwrite();
|
||||
T det=(T)1;
|
||||
for(int i=0; i<n; ++i)
|
||||
{
|
||||
int p=i;
|
||||
typename LA_traits<T>::normtype big=LA_traits<T>::abs2(a(i,i));
|
||||
for(int k=i+1; k<n; ++k) {const typename LA_traits<T>::normtype t=LA_traits<T>::abs2(a(k,i)); if(t>big) {big=t; p=k;}}
|
||||
if(!(big>=LA_precision<typename LA_traits<T>::normtype>::det_tiny())) return (T)0; //singular (or not finite), as in linear_solve
|
||||
T *ai=a[i];
|
||||
if(p!=i)
|
||||
{
|
||||
T *ap=a[p];
|
||||
for(int l=i; l<n; ++l) {const T t=ai[l]; ai[l]=ap[l]; ap[l]=t;}
|
||||
det= -det;
|
||||
}
|
||||
det*=ai[i];
|
||||
for(int k=i+1; k<n; ++k)
|
||||
{
|
||||
T *ak=a[k];
|
||||
const T f=ak[i]/ai[i];
|
||||
for(int l=i+1; l<n; ++l) ak[l]-=f*ai[l];
|
||||
}
|
||||
}
|
||||
return det;
|
||||
}
|
||||
|
||||
//largest dimension for which determinant_destroy() of a general matrix uses the inline elimination
|
||||
//(measured crossover with OpenBLAS between 48 and 64; the reference LAPACK was not faster even at 256)
|
||||
#ifndef LA_SMALL_DETERMINANT
|
||||
#define LA_SMALL_DETERMINANT 48
|
||||
#endif
|
||||
|
||||
//general determinant destructive on input
|
||||
template<class MAT>
|
||||
const typename LA_traits<MAT>::elementtype determinant_destroy(MAT &a) //passed by reference
|
||||
{
|
||||
typename LA_traits<MAT>::elementtype det;
|
||||
if(a.nrows()!=a.ncols()) laerror("determinant of non-square matrix");
|
||||
return lapack_determinant_destroy(a);
|
||||
}
|
||||
|
||||
//for openblas 0.3.31 we have to fake some RHS otherwise LU decomp. is not performed
|
||||
#ifdef NO_OPENBLAS_WORKAROUND
|
||||
linear_solve(a,NULL,&det);
|
||||
#else
|
||||
//fake rhs
|
||||
NRVec<typename LA_traits<MAT>::elementtype> b(a.ncols());
|
||||
for(int i=0; i<b.size(); ++i) b[i] = (typename LA_traits<MAT>::elementtype)i;
|
||||
linear_solve(a,b,&det);
|
||||
#endif
|
||||
return det;
|
||||
template<typename T>
|
||||
const T determinant_destroy(NRMat<T> &a)
|
||||
{
|
||||
return a.nrows()<=LA_SMALL_DETERMINANT ? small_determinant_destroy(a) : lapack_determinant_destroy(a);
|
||||
}
|
||||
|
||||
//general determinant
|
||||
|
||||
@@ -4820,6 +4820,30 @@ cout <<"adjoint error = "<<(a*adj).norm(abs(det))<<endl;
|
||||
|
||||
}
|
||||
|
||||
if(0) //determinants: LAPACK decomposition without right-hand sides against the inline elimination, general and packed storage, singular matrix
|
||||
{
|
||||
int n;
|
||||
cin>>n;
|
||||
NRMat<double> a(n,n);
|
||||
a.randomize(1.);
|
||||
NRMat<double> b(a),b2(a);
|
||||
const double d=lapack_determinant_destroy(b);
|
||||
cout <<"det = "<<d<<" relative difference of small_determinant_destroy = "<<(small_determinant_destroy(b2)-d)/d<<" of determinant = "<<(determinant(a)-d)/d<<endl;
|
||||
NRMat<complex<double> > c(n,n);
|
||||
c.randomize(1.);
|
||||
NRMat<complex<double> > cc(c),cc2(c);
|
||||
const complex<double> dc=lapack_determinant_destroy(cc);
|
||||
cout <<"complex det = "<<dc<<" relative difference of small_determinant_destroy = "<<abs(small_determinant_destroy(cc2)-dc)/abs(dc)<<" of determinant = "<<abs(determinant(c)-dc)/abs(dc)<<endl;
|
||||
NRMat<double> h=a+a.transpose();
|
||||
NRSMat<double> hs(h);
|
||||
cout <<"relative difference of the determinants in packed and general storage = "<<(determinant(hs)-determinant(h))/determinant(h)<<endl;
|
||||
a.copyonwrite();
|
||||
for(int j=0; j<n; ++j) a(n-1,j)=0;
|
||||
b=a;
|
||||
b2=a;
|
||||
cout <<"determinants of a singular matrix (no error should be raised) = "<<lapack_determinant_destroy(b)<<" "<<small_determinant_destroy(b2)<<endl;
|
||||
}
|
||||
|
||||
if(0) //pivot_transform: applied twice it returns the original matrix; inverse and Schur complement blocks; determinant of the pivot block
|
||||
{
|
||||
int n,m,k;
|
||||
|
||||
Reference in new issue
Block a user