From 2eb8e35b92b414f1b7910172aeb93765d0bd77af Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Tue, 24 Nov 2026 22:08:43 +0100 Subject: [PATCH] improved efficiency of determinant calculations --- doc/nonclass.tex | 20 +++++++--- nonclass.cc | 97 +++++++++++++++++++++++++++++++++++------------- nonclass.h | 67 ++++++++++++++++++++++++++------- t.cc | 24 ++++++++++++ 4 files changed, 162 insertions(+), 46 deletions(-) diff --git a/doc/nonclass.tex b/doc/nonclass.tex index 3a2f27d..1d173ea 100644 --- a/doc/nonclass.tex +++ b/doc/nonclass.tex @@ -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 &a, NRMat *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 &a, NRVec &b, T *det=0, int n=0)}{The same with a single right-hand side \code{b}.} \lafunc{void linear_solve(NRSMat &a, NRMat *b, T *det=0, int n=0); void linear_solve(NRSMat &a, NRVec &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::elementtype determinant(MAT a)}{Determinant by LU (\code{NRMat}) or Bunch--Kaufman (\code{NRSMat}) factorization of a copy.} -\lafunc{const LA_traits::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::elementtype determinant(MAT a)}{Determinant of a copy of the matrix by \code{determinant_destroy}.} +\lafunc{const LA_traits::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::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 &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 &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.} diff --git a/nonclass.cc b/nonclass.cc index d747fc6..267c632 100644 --- a/nonclass.cc +++ b/nonclass.cc @@ -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 *A, const FINT *LDA, FINT *IPIV, FINT *INFO); +extern "C" void FORNAME(cgetrf)(const FINT *M, const FINT *N, std::complex *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 &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 &A, double *B, const int nrhs, const i std::cout <<"A before clapack_dgesv = "< &A, float *B, const int nrhs, const int std::cout <<"A before clapack_sgesv = "< &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 &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, double *b, const int nrhs, const static void linear_solve_do(NRSMat &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 &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 = "< *AP, FINT *IPIV, std::complex *B, const FINT *LDB, FINT *INFO); extern "C" void FORNAME(chpsv)(const char *UPLO, const FINT *N, const FINT *NRHS, std::complex *AP, FINT *IPIV, std::complex *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 *AP, FINT *IPIV, FINT *INFO); +extern "C" void FORNAME(chptrf)(const char *UPLO, const FINT *N, std::complex *AP, FINT *IPIV, FINT *INFO); static void linear_solve_do(NRSMat > &a, std::complex *b, const int nrhs, const int ldb, std::complex *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 > &a, std::complex > &a, std::complex > &a, std::complex *b, const int nrhs, const int ldb, std::complex *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 > &a, std::complex 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 > &A, std::complex *a= &A(0,0); - for(int i=1; i 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 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 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 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 > &A, std::complex 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 *a= &A(0,0); - for(int i=1; i 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 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 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 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 >::normtype MatrixNorm(const NRMat &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 *A, const FINT *LDA, FINT *IPIV, FINT *INFO); -extern "C" void FORNAME(cgetrf)(const FINT *M, const FINT *N, std::complex *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 *A, const FINT *LDA, const double *anorm, double *rcond, std::complex *work, double *rwork, FINT *info); extern "C" void FORNAME(cgecon)( const char *norm, const FINT *n, const std::complex *A, const FINT *LDA, const float *anorm, float *rcond, std::complex *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); diff --git a/nonclass.h b/nonclass.h index 4f38837..7eda8e4 100644 --- a/nonclass.h +++ b/nonclass.h @@ -365,27 +365,66 @@ typename LA_traits::normtype MatrixNorm(const MAT &A, const char norm); template typename LA_traits::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 +const typename LA_traits::elementtype lapack_determinant_destroy(MAT &a) //passed by reference +{ +typename LA_traits::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 +const T small_determinant_destroy(NRMat &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::normtype big=LA_traits::abs2(a(i,i)); + for(int k=i+1; k::normtype t=LA_traits::abs2(a(k,i)); if(t>big) {big=t; p=k;}} + if(!(big>=LA_precision::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 const typename LA_traits::elementtype determinant_destroy(MAT &a) //passed by reference { -typename LA_traits::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::elementtype> b(a.ncols()); -for(int i=0; i::elementtype)i; -linear_solve(a,b,&det); -#endif -return det; +template +const T determinant_destroy(NRMat &a) +{ +return a.nrows()<=LA_SMALL_DETERMINANT ? small_determinant_destroy(a) : lapack_determinant_destroy(a); } //general determinant diff --git a/t.cc b/t.cc index cdee257..054cf02 100644 --- a/t.cc +++ b/t.cc @@ -4820,6 +4820,30 @@ cout <<"adjoint error = "<<(a*adj).norm(abs(det))<>n; +NRMat a(n,n); +a.randomize(1.); +NRMat b(a),b2(a); +const double d=lapack_determinant_destroy(b); +cout <<"det = "<