4 #include "TRSymMatrix.h"
5 #include "TRDiagMatrix.h"
9 TRSymMatrix::TRSymMatrix(Int_t nrows,Double_t a0, ...) :
10 TRArray(nrows*(nrows+1)/2), fNrows(nrows) {
14 TRSymMatrix::TRSymMatrix(Int_t nrows,
const Double_t *Array) :
TRArray() {
15 fNrows = TMath::Abs(nrows);
16 if (nrows > 0) Set(fNrows*(fNrows+1)/2,Array);
18 Set(fNrows*(fNrows+1)/2);
21 for (Int_t l = 0; l < fN; l++) {
23 fArray[ij] = Array[l];
24 if (i < fNrows - 1) i++;
33 TRSymMatrix::TRSymMatrix(Int_t nrows,
const Float_t *Array) :
TRArray() {
34 fNrows = TMath::Abs(nrows);
36 Set(fNrows*(fNrows+1)/2);
37 TCL::ucopy(Array,fArray,fNrows*(fNrows+1)/2);
40 Set(fNrows*(fNrows+1)/2);
43 for (Int_t l = 0; l < fN; l++) {
45 fArray[ij] = Array[l];
46 if (i < fNrows - 1) i++;
55 TRSymMatrix::TRSymMatrix(Int_t nrows,
const Char_t *s) :
TRArray(nrows*(nrows+1)/2,s), fNrows(nrows) {}
57 TRSymMatrix::TRSymMatrix(ETRMatrixCreatorsOp kop,Int_t nrows) :
58 TRArray(nrows*(nrows+1)/2), fNrows(nrows){
63 for (
int i=0; i<fNrows; i++) fArray[i*(i+1)/2+i] = 1;
66 Error(
"TRSymMatrix(ETRMatrixCreatorsOp)",
"operation %d not yet implemented", kop);
71 TRSymMatrix::TRSymMatrix(
const TRSymMatrix& S,ETRMatrixCreatorsOp kop) {
77 fNrows = S.GetNcols();
78 Set(fNrows*(fNrows+1)/2);
82 fNrows = S.GetNcols();
83 Set(fNrows*(fNrows+1)/2);
84 fValid = ! TrsInv(S.GetArray(),fArray, fNrows);
87 Error(
"TRSymMatrix(ETRMatrixCreatorsOp)",
"operation %d not yet implemented", kop);
91 TRSymMatrix::TRSymMatrix(
const TRMatrix& A) {
92 Int_t NI = A.GetNrows(); fNrows = NI;
93 Int_t NJ = A.GetNcols();
95 Set(fNrows*(fNrows+1)/2);
105 assert(N == A.GetNcols());
107 Set(fNrows*(fNrows+1)/2);
113 assert(N == A.GetNrows());
115 Set(fNrows*(fNrows+1)/2);
119 Error(
"TRSymMatrix(ETRMatrixCreatorsOp)",
"operation %d not yet implemented", kop);
124 assert (kop == kRxSxR);
125 Int_t M = Q.GetNcols();
126 assert(M == T.GetNcols());
128 Set(fNrows*(fNrows+1)/2);
129 TCL::trqsq(Q.GetArray(),T.GetArray(),fArray,M);
132 TRSymMatrix::TRSymMatrix(
const TRMatrix& A,ETRMatrixCreatorsOp kop) {
139 Set(fNrows*(fNrows+1)/2);
146 Set(fNrows*(fNrows+1)/2);
150 Error(
"TRSymMatrix(ETRMatrixCreatorsOp)",
"operation %d not yet implemented", kop);
154 Double_t TRSymMatrix::Product(
const TRVector& A,ETRMatrixCreatorsOp ) {
161 assert(N == A.GetNrows());
166 ostream& operator<<(ostream& s,
const TRSymMatrix &target) {
167 static const int width = 10;
168 Int_t Nrows = target.GetNrows();
169 const Double_t *Array = target.GetArray();
170 s <<
"Semi Positive DefinedSymMatrix Size \t["
171 << Nrows <<
"," << Nrows <<
"]" << endl;
173 s.setf(std::ios::fixed,std::ios::scientific);
174 s.setf(std::ios::showpos);
175 for (
int i = 0; i< Nrows; i++) {
176 for (
int j = 0; j <= i; j++)
177 s << std::setw(width) << std::setprecision(width-3) << Array[i*(i+1)/2 + j] <<
":\t";
180 s.unsetf(std::ios::showpos);
186 void TRSymMatrix::Print(Option_t *opt)
const {
if (opt) {}; cout << *
this << endl;}
189 if (&S !=
this) *
this = S;
190 Double_t *diag =
new Double_t[fNrows];
191 Bool_t *flag =
new Bool_t[fNrows];
193 if (B) b = B->GetArray();
195 spminv(fArray, b, fNrows, nrank, diag, flag);
201 Int_t TRSymMatrix::spminv(Double_t *v, Double_t *b, Int_t n,
202 Int_t &nrank, Double_t *diag, Bool_t *flag) {
205 static Int_t i, j, k, l, jj, jk, kk, jl, lk;
206 static Double_t vjk, vkk;
236 for (i = 1; i <= n; ++i) {
238 diag[i] = TMath::Abs(v[(i * i + i) / 2]);
241 for (i = 1; i <= n; ++i) {
244 for (j = 1; j <= n; ++j) {
247 if (TMath::Abs(v[jj]) > TMath::Max(TMath::Abs(vkk),diag[j] * 1e-10)) {
262 for (j = 1; j <= n; ++j) {
274 if (b) b[j] -= b[k] * vjk;
276 for (l = 1; l <= j; ++l) {
286 v[jl] -= v[lk] * vjk;
292 for (k = 1; k <= n; ++k) {
295 for (j = 1; j <= k; ++j) {
297 v[(k * k - k) / 2 + j] = 0.;
305 L10: Int_t nn = (n * n + n) / 2;
306 for (Int_t ij = 1; ij <= nn; ++ij) {
312 Int_t TRSymMatrix::TrsInv(
const Double_t *g, Double_t *gi, Int_t n) {
313 Int_t fail = TrchLU(g, gi, n);
314 fail += TrInv(gi, gi, n);
319 Int_t TRSymMatrix::TrchLU(
const double *a,
double *b,
int n) {
322 int ipiv, kpiv, i__, j;
346 for (j = i__; j <= n; ++j) {
348 if (i__ == 1)
goto L40;
349 if (r__ == 0.)
goto L42;
354 sum += b[kd] * b[id];
361 if (j != i__) b[kpiv] = sum * r__;
363 if (sum > 0) dc = TMath::Sqrt(sum);
366 if (r__ > 0. && dc > 0) r__ = (double)1. / dc;
367 else {r__ = 0; fail++;}
376 Int_t TRSymMatrix::TrInv(
const Double_t *g, Double_t *gi, Int_t n) {
TCL::trinv(g, gi, n);
return 0;}
378 Int_t TRSymMatrix::TrsmUL(
const Double_t *g, Double_t *gi, Int_t n) {
TCL::trsmul(g, gi, n);
return 0;}
381 Double_t &TRSymMatrix::operator()(Int_t i,Int_t j){
383 if (j < 0 || j >= fNrows) {
384 ::Error(
"TRSymMatrix::operator()",
"index j %d out of bounds (size: %d, this: %p)",
389 if (i < 0 || i >= fNrows) {
390 ::Error(
"TRSymMatrix::operator()",
"index i %d out of bounds (size: %d, this: %p)",
396 if (i > j) {m = j; l = i;}
397 return TArrayD::operator[](m + (l+1)*l/2);
static float * traat(const float *a, float *s, int m, int n)
static float * trsmul(const float *g, float *gi, int n)
static float * trata(const float *a, float *r, int m, int n)
static float * trinv(const float *t, float *s, int n)
static float * trpck(const float *s, float *u, int n)
static float * trqsq(const float *q, const float *s, float *r, int m)
static float * trsinv(const float *g, float *gi, int n)
static float * trasat(const float *a, const float *s, float *r, int m, int n)
static float * tratsa(const float *a, const float *s, float *r, int m, int n)