void DiagonalMatrixTemplate<T>::copyDiagonal(const MatrixT& m) { if(!m.isSquare()) { FatalError(MatrixError_ArgIncompatibleDimensions); } if(BaseT::n == 0) { resize(m.m); } else if(BaseT::n != m.m) { FatalError(MatrixError_DestIncompatibleDimensions); } m.getDiagCopy(0,*this); }
bool NRQRDecomposition<T>::set(const MatrixT& A) { Assert(A.isSquare()); int n=A.n; VectorT d(n); c.resize(n); QR.copy(A); int i,j,k; T scale,sigma,sum,tau; singular=false; for (k=0;k<n-1;k++) { scale=0; for (i=k;i<n;i++) scale=Max(scale,Abs(this->R(i,k))); if (scale == 0) { //Singular case. singular=true; c(k)=d(k)=0.0; } else { //Form Qk and Qk*A. for (i=k;i<n;i++) QR(i,k) /= scale; for (sum=0,i=k;i<n;i++) sum += Sqr(QR(i,k)); sigma=SIGN(sqrt(sum),QR(k,k)); QR(k,k) += sigma; c(k)=sigma*QR(k,k); d(k) = -scale*sigma; for (j=k+1;j<n;j++) { for (sum=0,i=k;i<n;i++) sum += QR(i,k)*QR(i,j); tau=sum/c(k); for (i=k;i<n;i++) QR(i,j) -= tau*QR(i,k); } } } d(n-1)=QR(n-1,n-1); if (d(n-1) == 0) singular=true; return true; }