55 Error(
"TDecompSVD(Int_t,Int_t",
"matrix rows should be >= columns");
58 fU.ResizeTo(nrows,nrows);
60 fV.ResizeTo(nrows,ncols);
68 const Int_t nrows = row_upb-row_lwb+1;
69 const Int_t ncols = col_upb-col_lwb+1;
72 Error(
"TDecompSVD(Int_t,Int_t,Int_t,Int_t",
"matrix rows should be >= columns");
77 fU.ResizeTo(nrows,nrows);
79 fV.ResizeTo(nrows,ncols);
88 if (
a.GetNrows() <
a.GetNcols()) {
89 Error(
"TDecompSVD(const TMatrixD &",
"matrix rows should be >= columns");
100 const Int_t nRow =
a.GetNrows();
101 const Int_t nCol =
a.GetNcols();
103 fU.ResizeTo(nRow,nRow);
105 fV.ResizeTo(nRow,nCol);
108 memcpy(
fV.GetMatrixArray(),
a.GetMatrixArray(),nRow*nCol*
sizeof(
Double_t));
128 Error(
"Decompose()",
"Matrix has not been set");
139 else offDiag.
Use(nCol,work);
151 fV.ResizeTo(nCol,nCol);
fV.Shift(colLwb,colLwb);
153 fU.Transpose(
fU);
fU.Shift(rowLwb,colLwb);
194 const Int_t nRow_v =
v.GetNrows();
195 const Int_t nCol_v =
v.GetNcols();
201 for (
Int_t i = 0;
i < nCol_v;
i++) {
212 for (
Int_t j =
i; j < nCol_v; j++) {
218 for (
Int_t j = 0; j < nCol_u; j++)
237 for (
Int_t j =
i; j < nRow_v; j++) {
243 for (
Int_t k =
i+2; k < nCol_v; k++)
261 for (
Int_t i = nCol_v-1;
i >= 0;
i--) {
268 for (
Int_t k =
i; k < nCol_v; k++) {
313 const Int_t nCol_v =
v.GetNcols();
320 const Double_t eps = std::numeric_limits<double>::epsilon();
322 const Int_t niterm = 10*nCol_v;
323 for (
Int_t k = nCol_v-1; k >= 0; k--) {
337 for (
Int_t ll = k; ll >= 0 ; ll--) {
348 if (
l > 0 && !elzero)
354 if (niter <= niterm)
goto loop;
355 ::Error(
"TDecompSVD::Diagonalize",
"no convergence after %d steps",niter);
362 sDiag(k) = -sDiag(k);
376 const Int_t nCol_v =
v.GetNcols();
390 for (
Int_t j = 0; j < nCol_v; j++)
430 if (psl == 0.0 || pok == 0.0 || psk1 == 0.0) {
431 const Double_t b = ((psk1-psk)*(psk1+psk)+pok1*pok1)/2.0;
432 const Double_t c = (psk*pok1)*(psk*pok1);
435 if ((
b != 0.0) | (
c != 0.0)) {
442 f = (psl+psk)*(psl-psk)+shift;
444 f = ((psk1-psk)*(psk1+psk)+(pok1-pok)*(pok1+pok))/(2.*pok*psk1);
448 f = ((psl-psk)*(psl+psk)+pok*(psk1/t-pok))/psl;
451 const Int_t nCol_v =
v.GetNcols();
470 for (j = 0; j < nCol_v; j++)
482 for (j = 0; j < nCol_u; j++)
499 const Int_t nCol_v =
v.GetNcols();
513 while (!found &&
i < nCol_v) {
520 for (
i = 1;
i < nCol_v;
i++) {
523 for (j =
i; j < nCol_v; j++) {
534 for (j = 0; j < nCol_v; j++) {
535 const Int_t off_j = j*nCol_v;
537 pV[off_j+k] = pV[off_j+
i-1];
541 for (j = 0; j < nCol_u; j++) {
542 const Int_t off_k = k*nCol_u;
543 const Int_t off_i1 = (
i-1)*nCol_u;
545 pU[off_k+j] = pU[off_i1+j];
560 Error(
"GetMatrix()",
"Matrix is singular");
565 Error(
"GetMatrix()",
"Decomposition failed");
570 const Int_t nRows =
fU.GetNrows();
571 const Int_t nCols =
fV.GetNcols();
587 if (
a.GetNrows() <
a.GetNcols()) {
588 Error(
"TDecompSVD(const TMatrixD &",
"matrix rows should be >= columns");
597 const Int_t nRow =
a.GetNrows();
598 const Int_t nCol =
a.GetNcols();
600 fU.ResizeTo(nRow,nRow);
602 fV.ResizeTo(nRow,nCol);
605 memcpy(
fV.GetMatrixArray(),
a.GetMatrixArray(),nRow*nCol*
sizeof(
Double_t));
627 if (
fU.GetNrows() !=
b.GetNrows() ||
fU.GetRowLwb() !=
b.GetLwb())
629 Error(
"Solve(TVectorD &",
"vector and matrix incompatible");
637 const Int_t lwb =
fU.GetColLwb();
638 const Int_t upb = lwb+
fV.GetNcols()-1;
642 for (
Int_t irow = lwb; irow <= upb; irow++) {
644 if (
fSig(irow) > threshold) {
652 if (
b.GetNrows() >
fV.GetNrows()) {
654 tmp2.
Use(lwb,upb,
b.GetMatrixArray());
683 if (
fU.GetNrows() !=
b->GetNrows() ||
fU.GetRowLwb() !=
b->GetRowLwb())
685 Error(
"Solve(TMatrixDColumn &",
"vector and matrix incompatible");
693 const Int_t lwb =
fU.GetColLwb();
694 const Int_t upb = lwb+
fV.GetNcols()-1;
699 for (
Int_t irow = lwb; irow <= upb; irow++) {
701 if (
fSig(irow) > threshold) {
709 if (
b->GetNrows() >
fV.GetNrows()) {
735 if (
fU.GetNrows() !=
fV.GetNrows() ||
fU.GetRowLwb() !=
fV.GetRowLwb()) {
736 Error(
"TransSolve(TVectorD &",
"matrix should be square");
740 if (
fV.GetNrows() !=
b.GetNrows() ||
fV.GetRowLwb() !=
b.GetLwb())
742 Error(
"TransSolve(TVectorD &",
"vector and matrix incompatible");
750 const Int_t lwb =
fU.GetColLwb();
751 const Int_t upb = lwb+
fV.GetNcols()-1;
755 for (
Int_t i = lwb;
i <= upb;
i++) {
757 if (
fSig(
i) > threshold) {
785 if (
fU.GetNrows() !=
fV.GetNrows() ||
fU.GetRowLwb() !=
fV.GetRowLwb()) {
786 Error(
"TransSolve(TMatrixDColumn &",
"matrix should be square");
790 if (
fV.GetNrows() !=
b->GetNrows() ||
fV.GetRowLwb() !=
b->GetRowLwb())
792 Error(
"TransSolve(TMatrixDColumn &",
"vector and matrix incompatible");
800 const Int_t lwb =
fU.GetColLwb();
801 const Int_t upb = lwb+
fV.GetNcols()-1;
806 for (
Int_t i = lwb;
i <= upb;
i++) {
808 if (
fSig(
i) > threshold) {
867 return fU.GetNrows();
872 return fV.GetNcols();
885 const Int_t nRows =
fU.GetNrows();
887 if (
inv.GetNrows() != nRows ||
inv.GetNcols() != nRows ||
888 inv.GetRowLwb() != rowLwb ||
inv.GetColLwb() != colLwb) {
889 Error(
"Invert(TMatrixD &",
"Input matrix has wrong shape");
907 const Int_t rowUpb = rowLwb+
fU.GetNrows()-1;
911 inv.ResizeTo(rowLwb,rowLwb+
fV.GetNcols()-1,colLwb,colLwb+
fU.GetNrows()-1);
932 if (
this != &source) {
934 fU.ResizeTo(source.
fU);
936 fV.ResizeTo(source.
fV);
Bool_t DefHouseHolder(const TVectorD &vc, Int_t lp, Int_t l, Double_t &up, Double_t &b, Double_t tol=0.0)
Define a Householder-transformation through the parameters up and b .
void ApplyGivens(Double_t &z1, Double_t &z2, Double_t c, Double_t s)
Apply a Givens transformation as defined by c and s to the vector components v1 and v2 .
void ApplyHouseHolder(const TVectorD &vc, Double_t up, Double_t b, Int_t lp, Int_t l, TMatrixDRow &cr)
Apply Householder-transformation.
void DefGivens(Double_t v1, Double_t v2, Double_t &c, Double_t &s)
Defines a Givens-rotation by calculating 2 rotation parameters c and s.
void DefAplGivens(Double_t &v1, Double_t &v2, Double_t &c, Double_t &s)
Define and apply a Givens-rotation by calculating 2 rotation parameters c and s.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
TMatrixTBase< Double_t > TMatrixDBase
TMatrixTRow< Double_t > TMatrixDRow
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
TMatrixTDiag< Double_t > TMatrixDDiag
TMatrixTRow_const< Double_t > TMatrixDRow_const
TMatrixTColumn< Double_t > TMatrixDColumn
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
Array of doubles (64 bits per element).
static void DiagProd(const TVectorD &diag, Double_t tol, Double_t &d1, Double_t &d2)
virtual Bool_t MultiSolve(TMatrixD &B)
Solve set of equations with RHS in columns of B.
TDecompBase & operator=(const TDecompBase &source)
Assignment operator.
TDecompBase()
Default constructor.
void Print(Option_t *opt="") const override
Print class members.
Single Value Decomposition class.
void Det(Double_t &d1, Double_t &d2) override
Matrix determinant det = d1*TMath::Power(2.,d2)
static void Diag_3(TMatrixD &v, TMatrixD &u, TVectorD &sDiag, TVectorD &oDiag, Int_t k, Int_t l)
Step 3 in the matrix diagonalization.
static void Diag_2(TVectorD &sDiag, TVectorD &oDiag, Int_t k, Int_t l)
Step 2 in the matrix diagonalization.
TDecompSVD & operator=(const TDecompSVD &source)
Assignment operator.
Bool_t Solve(TVectorD &b) override
Solve Ax=b assuming the SVD form of A is stored .
static Bool_t Bidiagonalize(TMatrixD &v, TMatrixD &u, TVectorD &sDiag, TVectorD &oDiag)
Bidiagonalize the (m x n) - matrix a (stored in v) through a series of Householder transformations ap...
static void Diag_1(TMatrixD &v, TVectorD &sDiag, TVectorD &oDiag, Int_t k)
Step 1 in the matrix diagonalization.
static Bool_t Diagonalize(TMatrixD &v, TMatrixD &u, TVectorD &sDiag, TVectorD &oDiag)
Diagonalizes in an iterative fashion the bidiagonal matrix C as described through sDiag and oDiag,...
Double_t Condition() override
Matrix condition number.
Bool_t Decompose() override
SVD decomposition of matrix If the decomposition succeeds, bit kDecomposed is set ,...
Int_t GetNrows() const override
const TMatrixD GetMatrix()
Reconstruct the original matrix using the decomposition parts.
Bool_t TransSolve(TVectorD &b) override
Solve A^T x=b assuming the SVD form of A is stored . Solution returned in b.
virtual void SetMatrix(const TMatrixD &a)
Set matrix to be decomposed.
Int_t GetNcols() const override
static void SortSingular(TMatrixD &v, TMatrixD &u, TVectorD &sDiag)
Perform a permutation transformation on the diagonal matrix S', so that matrix S'' = U''^T .
void Print(Option_t *opt="") const override
Print class members.
virtual TMatrixTBase< Element > & Shift(Int_t row_shift, Int_t col_shift)
Shift the row index by adding row_shift and the column index by adding col_shift, respectively.
const TMatrixTBase< Element > * GetMatrix() const
void Assign(Element val)
Assign val to every element of the matrix row.
const Element * GetMatrixArray() const override
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
TVectorT< Element > & SetSub(Int_t row_lwb, const TVectorT< Element > &source)
Insert vector source starting at [row_lwb], thereby overwriting the part [row_lwb....
TVectorT< Element > & Use(Int_t lwb, Int_t upb, Element *data)
Use the array data to fill the vector lwb..upb].
Element * GetMatrixArray()
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Double_t Hypot(Double_t x, Double_t y)
Returns sqrt(x*x + y*y)
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
void inv(rsa_NUMBER *, rsa_NUMBER *, rsa_NUMBER *)