diff --git a/doc/nonclass.tex b/doc/nonclass.tex index c683b4b..3a2f27d 100644 --- a/doc/nonclass.tex +++ b/doc/nonclass.tex @@ -120,6 +120,17 @@ scaled). Returns \code{info}.} \lafunc{const NRMat calcsvdinverse(NRMat a, typename LA_traits::normtype thr=0, bool do_absadjoint=false)}{Inverse via SVD, $V\,\mathrm{diag}(1/s_i)\,U\Hc$, singular values $s_i<$\code{thr} are treated as zero (pseudoinverse). With \code{do_absadjoint} returns $V\,\mathrm{diag}(\prod_{j\ne i}s_j)\,U\Hc$, the adjugate matrix up to the phase $\det U\det V$. Square matrices only.} +\lafunc{const NRMat pivot_transform(const NRMat &a, const NRVec &rows, const NRVec &cols, T *det=NULL)}{Pivot transform +(exchange, sweep operator) of $A$ with respect to the square block $B=A(\mathtt{rows},\mathtt{cols})$; for \code{rows==cols} it is the +principal pivot transform. From $y=Ax$ it expresses $(x_{\mathtt{cols}},y_{\bar r})$ by $(y_{\mathtt{rows}},x_{\bar c})$, where $\bar r$, $\bar c$ +are the remaining rows and columns. The result has the dimensions of $A$ (which may be rectangular) and its blocks stay at the places +of the corresponding blocks of $A$: $R(\mathtt{rows},\mathtt{cols})=B^{-1}$, $R(\mathtt{rows},\bar c)=-B^{-1}A(\mathtt{rows},\bar c)$, +$R(\bar r,\mathtt{cols})=A(\bar r,\mathtt{cols})B^{-1}$ and the Schur complement +$R(\bar r,\bar c)=A(\bar r,\bar c)-A(\bar r,\mathtt{cols})B^{-1}A(\mathtt{rows},\bar c)$. The indices are counted from 0 as in +\code{NRMat::submatrix}; the $i$-th row of the block is paired with its $i$-th column, i.e.\ $R(\mathtt{rows}[i],\mathtt{cols}[j])=(B^{-1})_{ij}$. +If \code{det} is non-NULL, $\det B$ is returned there. Applied twice with the same lists it returns $A$. A minor of $A$ in which some +rows and columns of $B$ are replaced by other ones is $\det B$ times a small minor of $R$ (up to a sign). $B$ must be nonsingular: +this is not tested, a nearly singular block gives a huge result (check \code{det}); a wrong or repeated index raises \code{laerror}.} \lafunc{NRVec svd_solve(NRMat &a, const NRVec &b, normtype thres=LA_precision::svd_threshold(), int *nullity=NULL); NRMat svd_solve(NRMat &a, const NRMat &b, normtype thres=LA_precision::svd_threshold(), int *nullity=NULL)}{Least-squares 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 diff --git a/nonclass.h b/nonclass.h index f88ffc7..4f38837 100644 --- a/nonclass.h +++ b/nonclass.h @@ -304,6 +304,59 @@ const NRMat calcsvdinverse(NRMat a, typename LA_traits::normtype thr=0, } +//pivot transform (exchange, sweep operator) of a matrix with respect to the square block b=a(rows,cols): +//from y = a x it expresses (x_cols, y_otherrows) by (y_rows, x_othercols); with rows==cols it is the principal pivot transform +//the result has the dimensions of a and the blocks stay at their places: +//r(rows,cols) = b^-1, r(rows,othercols) = -b^-1 a(rows,othercols), +//r(otherrows,cols) = a(otherrows,cols) b^-1, r(otherrows,othercols) = a(otherrows,othercols) - a(otherrows,cols) b^-1 a(rows,othercols) (Schur complement) +//det, if not NULL, returns the determinant of b +template +const NRMat pivot_transform(const NRMat &a, const NRVec &rows, const NRVec &cols, T *det=NULL) +{ + const int k=rows.size(); + if(k!=cols.size()) laerror("pivot_transform() for non-square pivot block"); + if(k==0) {if(det) *det=(T)1; return a;} + NRVec other[2]; //complementary lists of rows and columns + for(int rc=0; rc<2; ++rc) + { + const NRVec &sel = rc?cols:rows; + const int n = rc?a.ncols():a.nrows(); + NRVec used((char)0,n); + for(int i=0; i=n) laerror("bad index in pivot_transform()"); + if(used[sel[i]]) laerror("repeated index in pivot_transform()"); + used[sel[i]]=1; + } + other[rc].resize(n-k); + int j=0; + for(int i=0; i r(a.nrows(),a.ncols()); + const NRMat binv = calcinverse(a.submatrix(rows,cols),det); + r.storesubmatrix(rows,cols,binv); + NRMat y; //-b^-1 a(rows,othercols) + if(other[1].size()) + { + y.resize(k,other[1].size()); + y.gemm((T)0,binv,'n',a.submatrix(rows,other[1]),'n',(T)-1); + r.storesubmatrix(rows,other[1],y); + } + if(other[0].size()) + { + const NRMat a21 = a.submatrix(other[0],cols); + r.storesubmatrix(other[0],cols,a21*binv); + if(other[1].size()) + { + NRMat s = a.submatrix(other[0],other[1]); + s.gemm((T)1,a21,'n',y,'n',(T)1); + r.storesubmatrix(other[0],other[1],s); + } + } + return r; +} + + //several matrix norms: norm = '1' or 'O' (max column sum), 'I' (max row sum), 'F' or 'E' (Frobenius), 'M' (max abs element) template typename LA_traits::normtype MatrixNorm(const MAT &A, const char norm); diff --git a/t.cc b/t.cc index e4be093..cdee257 100644 --- a/t.cc +++ b/t.cc @@ -4820,6 +4820,29 @@ cout <<"adjoint error = "<<(a*adj).norm(abs(det))<>n>>m>>k; +NRMat a(n,m); +a.randomize(1.); +NRPerm pr(n),pc(m); +pr.randomize(); +pc.randomize(); +NRVec rows(k),cols(k),orows(n-k),ocols(m-k); +for(int i=0; i r=pivot_transform(a,rows,cols,&det); +cout <<"Error of the double transform = "<<(pivot_transform(r,rows,cols)-a).norm()< b=a.submatrix(rows,cols); +cout <<"Error of the determinant = "< one(k,k); one=1.; +cout <<"Error of the inverse block = "<<(b*r.submatrix(rows,cols)-one).norm()<k&&m>k) cout <<"Error of the Schur complement = "<<(r.submatrix(orows,ocols)-a.submatrix(orows,ocols)+a.submatrix(orows,cols)*calcinverse(b)*a.submatrix(rows,ocols)).norm()<