pivot_transform in nonclass.h

This commit is contained in:
jiri committed 2026-11-24 20:46:48 +01:00
1 parent cb83faa543
commit c5214cde2e
3 files changed
+87

No files matched your search

+11
View File
@@ -120,6 +120,17 @@ scaled). Returns \code{info}.}
\lafunc{const NRMat<T> calcsvdinverse(NRMat<T> a, typename LA_traits<T>::normtype thr=0, bool do_absadjoint=false)}{Inverse via SVD, $V\,\mathrm{diag}(1/s_i)\,U\Hc$, \lafunc{const NRMat<T> calcsvdinverse(NRMat<T> a, typename LA_traits<T>::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 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.} $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<T> pivot_transform(const NRMat<T> &a, const NRVec<int> &rows, const NRVec<int> &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<T> svd_solve(NRMat<T> &a, const NRVec<T> &b, normtype thres=LA_precision<normtype>::svd_threshold(), int *nullity=NULL); NRMat<T> svd_solve(NRMat<T> &a, const NRMat<T> &b, normtype thres=LA_precision<normtype>::svd_threshold(), int *nullity=NULL)}{Least-squares \lafunc{NRVec<T> svd_solve(NRMat<T> &a, const NRVec<T> &b, normtype thres=LA_precision<normtype>::svd_threshold(), int *nullity=NULL); NRMat<T> svd_solve(NRMat<T> &a, const NRMat<T> &b, normtype thres=LA_precision<normtype>::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); 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 the number of discarded ones is stored in \code{*nullity}. Here (unlike \code{linear_solve}) the right-hand sides are the
+53
View File
@@ -304,6 +304,59 @@ const NRMat<T> calcsvdinverse(NRMat<T> a, typename LA_traits<T>::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<typename T>
const NRMat<T> pivot_transform(const NRMat<T> &a, const NRVec<int> &rows, const NRVec<int> &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<int> other[2]; //complementary lists of rows and columns
for(int rc=0; rc<2; ++rc)
{
const NRVec<int> &sel = rc?cols:rows;
const int n = rc?a.ncols():a.nrows();
NRVec<char> used((char)0,n);
for(int i=0; i<k; ++i)
{
if(sel[i]<0||sel[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<n; ++i) if(!used[i]) other[rc][j++]=i;
}
NRMat<T> r(a.nrows(),a.ncols());
const NRMat<T> binv = calcinverse(a.submatrix(rows,cols),det);
r.storesubmatrix(rows,cols,binv);
NRMat<T> 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<T> a21 = a.submatrix(other[0],cols);
r.storesubmatrix(other[0],cols,a21*binv);
if(other[1].size())
{
NRMat<T> 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) //several matrix norms: norm = '1' or 'O' (max column sum), 'I' (max row sum), 'F' or 'E' (Frobenius), 'M' (max abs element)
template<class MAT> template<class MAT>
typename LA_traits<MAT>::normtype MatrixNorm(const MAT &A, const char norm); typename LA_traits<MAT>::normtype MatrixNorm(const MAT &A, const char norm);
+23
View File
@@ -4820,6 +4820,29 @@ cout <<"adjoint error = "<<(a*adj).norm(abs(det))<<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;
cin>>n>>m>>k;
NRMat<double> a(n,m);
a.randomize(1.);
NRPerm<int> pr(n),pc(m);
pr.randomize();
pc.randomize();
NRVec<int> rows(k),cols(k),orows(n-k),ocols(m-k);
for(int i=0; i<k; ++i) {rows[i]=pr[i+1]-1; cols[i]=pc[i+1]-1;}
for(int i=k; i<n; ++i) orows[i-k]=pr[i+1]-1;
for(int i=k; i<m; ++i) ocols[i-k]=pc[i+1]-1;
double det;
NRMat<double> r=pivot_transform(a,rows,cols,&det);
cout <<"Error of the double transform = "<<(pivot_transform(r,rows,cols)-a).norm()<<endl;
NRMat<double> b=a.submatrix(rows,cols);
cout <<"Error of the determinant = "<<det-determinant(b)<<endl;
NRMat<double> one(k,k); one=1.;
cout <<"Error of the inverse block = "<<(b*r.submatrix(rows,cols)-one).norm()<<endl;
if(n>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()<<endl;
}
if(0) //testbed of the general methods if(0) //testbed of the general methods
{ {
int seed; int seed;