SND@LHC Software
Loading...
Searching...
No Matches
gbl::VSymMatrix Class Reference

Simple symmetric Matrix based on std::vector<double> More...

#include <VMatrix.h>

Public Member Functions

 VSymMatrix (const unsigned int nRows=0)
 
virtual ~VSymMatrix ()
 
void resize (const unsigned int nRows)
 Resize symmetric matrix.
 
unsigned int invert ()
 Matrix inversion.
 
double & operator() (unsigned int i, unsigned int j)
 access element (i,j) assuming i>=j
 
double operator() (unsigned int i, unsigned int j) const
 access element (i,j) assuming i>=j
 
unsigned int getNumRows () const
 Get number of rows (= number of colums).
 
void print () const
 Print matrix.
 
VSymMatrix operator- (const VMatrix &aMatrix) const
 Subtraction SymMatrix-(sym)Matrix.
 
VVector operator* (const VVector &aVector) const
 Multiplication SymMatrix*Vector.
 
VMatrix operator* (const VMatrix &aMatrix) const
 Multiplication SymMatrix*Matrix.
 

Private Attributes

unsigned int numRows
 Number of rows.
 
std::vector< double > theVec
 Data (symmetric storage)
 

Detailed Description

Simple symmetric Matrix based on std::vector<double>

Definition at line 65 of file VMatrix.h.

Constructor & Destructor Documentation

◆ VSymMatrix()

gbl::VSymMatrix::VSymMatrix ( const unsigned int  nRows = 0)

Definition at line 142 of file VMatrix.cc.

142 :
143 numRows(nRows), theVec((nRows * nRows + nRows) / 2) {
144}
std::vector< double > theVec
Data (symmetric storage)
Definition VMatrix.h:80
unsigned int numRows
Number of rows.
Definition VMatrix.h:79

◆ ~VSymMatrix()

gbl::VSymMatrix::~VSymMatrix ( )
virtual

Definition at line 146 of file VMatrix.cc.

146 {
147}

Member Function Documentation

◆ getNumRows()

unsigned int gbl::VSymMatrix::getNumRows ( ) const

Get number of rows (= number of colums).

Returns
Number of rows.

Definition at line 162 of file VMatrix.cc.

162 {
163 return numRows;
164}

◆ invert()

unsigned int gbl::VSymMatrix::invert ( )

Matrix inversion.

Invert symmetric N-by-N matrix V in symmetric storage mode V(1) = V11, V(2) = V12, V(3) = V22, V(4) = V13, . . . replaced by inverse matrix

Method of solution is by elimination selecting the pivot on the diagonal each stage. The rank of the matrix is returned in NRANK. For NRANK ne N, all remaining rows and cols of the resulting matrix V are set to zero.

Exceptions
1: matrix is singular.
Returns
Rank of matrix.

Definition at line 326 of file VMatrix.cc.

326 {
327
328 const double eps = 1.0E-10;
329 unsigned int aSize = numRows;
330 std::vector<int> next(aSize);
331 std::vector<double> diag(aSize);
332 int nSize = aSize;
333
334 int first = 1;
335 for (int i = 1; i <= nSize; ++i) {
336 next[i - 1] = i + 1; // set "next" pointer
337 diag[i - 1] = fabs(theVec[(i * i + i) / 2 - 1]); // save abs of diagonal elements
338 }
339 next[aSize - 1] = -1; // end flag
340
341 unsigned int nrank = 0;
342 for (int i = 1; i <= nSize; ++i) { // start of loop
343 int k = 0;
344 double vkk = 0.0;
345
346 int jCandidate = first;
347 int previous = 0;
348 int last = previous;
349 // look for pivot
350 while (jCandidate > 0) {
351 int jj = (jCandidate * jCandidate + jCandidate) / 2 - 1;
352 if (fabs(theVec[jj]) > std::max(fabs(vkk), eps * diag[jCandidate - 1])) {
353 vkk = theVec[jj];
354 k = jCandidate;
355 last = previous;
356 }
357 previous = jCandidate;
358 jCandidate = next[jCandidate - 1];
359 }
360 // pivot found
361 if (k > 0) {
362 int kk = (k * k + k) / 2 - 1;
363 if (last <= 0) {
364 first = next[k - 1];
365 } else {
366 next[last - 1] = next[k - 1];
367 }
368 next[k - 1] = 0; // index is used, reset
369 nrank++; // increase rank and ...
370
371 vkk = 1.0 / vkk;
372 theVec[kk] = -vkk;
373 int jk = kk - k;
374 int jl = -1;
375 for (int j = 1; j <= nSize; ++j) { // elimination
376 if (j == k) {
377 jk = kk;
378 jl += j;
379 } else {
380 if (j < k) {
381 ++jk;
382 } else {
383 jk += j - 1;
384 }
385
386 double vjk = theVec[jk];
387 theVec[jk] = vkk * vjk;
388 int lk = kk - k;
389 if (j >= k) {
390 for (int l = 1; l <= k - 1; ++l) {
391 ++jl;
392 ++lk;
393 theVec[jl] -= theVec[lk] * vjk;
394 }
395 ++jl;
396 lk = kk;
397 for (int l = k + 1; l <= j; ++l) {
398 ++jl;
399 lk += l - 1;
400 theVec[jl] -= theVec[lk] * vjk;
401 }
402 } else {
403 for (int l = 1; l <= j; ++l) {
404 ++jl;
405 ++lk;
406 theVec[jl] -= theVec[lk] * vjk;
407 }
408 }
409 }
410 }
411 } else {
412 for (int n = 1; n <= nSize; ++n) {
413 if (next[n - 1] >= 0) {
414 int nn = (n * n - n) / 2 - 1;
415 for (int j = 1; j <= nSize; ++j) {
416 if (next[j - 1] >= 0) {
417 theVec[nn + j] = 0.0; // clear matrix row/col
418 }
419 }
420 }
421 }
422 throw 1; // singular
423 }
424 }
425 for (int ij = 0; ij < (nSize * nSize + nSize) / 2; ++ij) {
426 theVec[ij] = -theVec[ij]; // finally reverse sign of all matrix elements
427 }
428 return nrank;
429}
int i
Definition ShipAna.py:86
real(dp), parameter, public eps

◆ operator()() [1/2]

double & gbl::VSymMatrix::operator() ( unsigned int  i,
unsigned int  j 
)
inline

access element (i,j) assuming i>=j

Definition at line 104 of file VMatrix.h.

104 {
105 return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
106}

◆ operator()() [2/2]

double gbl::VSymMatrix::operator() ( unsigned int  i,
unsigned int  j 
) const
inline

access element (i,j) assuming i>=j

Definition at line 109 of file VMatrix.h.

110 {
111 return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
112}

◆ operator*() [1/2]

VMatrix gbl::VSymMatrix::operator* ( const VMatrix aMatrix) const

Multiplication SymMatrix*Matrix.

Definition at line 207 of file VMatrix.cc.

207 {
208 unsigned int nCol = aMatrix.getNumCols();
209 VMatrix aResult(numRows, nCol);
210 for (unsigned int l = 0; l < nCol; ++l) {
211 for (unsigned int i = 0; i < numRows; ++i) {
212 aResult(i, l) = theVec[(i * i + i) / 2 + i] * aMatrix(i, l);
213 for (unsigned int j = 0; j < i; ++j) {
214 aResult(j, l) += theVec[(i * i + i) / 2 + j] * aMatrix(i, l);
215 aResult(i, l) += theVec[(i * i + i) / 2 + j] * aMatrix(j, l);
216 }
217 }
218 }
219 return aResult;
220}

◆ operator*() [2/2]

VVector gbl::VSymMatrix::operator* ( const VVector aVector) const

Multiplication SymMatrix*Vector.

Definition at line 194 of file VMatrix.cc.

194 {
195 VVector aResult(numRows);
196 for (unsigned int i = 0; i < numRows; ++i) {
197 aResult(i) = theVec[(i * i + i) / 2 + i] * aVector(i);
198 for (unsigned int j = 0; j < i; ++j) {
199 aResult(j) += theVec[(i * i + i) / 2 + j] * aVector(i);
200 aResult(i) += theVec[(i * i + i) / 2 + j] * aVector(j);
201 }
202 }
203 return aResult;
204}

◆ operator-()

VSymMatrix gbl::VSymMatrix::operator- ( const VMatrix aMatrix) const

Subtraction SymMatrix-(sym)Matrix.

Definition at line 183 of file VMatrix.cc.

183 {
184 VSymMatrix aResult(numRows);
185 for (unsigned int i = 0; i < numRows; ++i) {
186 for (unsigned int j = 0; j <= i; ++j) {
187 aResult(i, j) = theVec[(i * i + i) / 2 + j] - aMatrix(i, j);
188 }
189 }
190 return aResult;
191}
VSymMatrix(const unsigned int nRows=0)
Definition VMatrix.cc:142

◆ print()

void gbl::VSymMatrix::print ( ) const

Print matrix.

Definition at line 167 of file VMatrix.cc.

167 {
168 std::cout << " VSymMatrix: " << numRows << "*" << numRows << std::endl;
169 for (unsigned int i = 0; i < numRows; ++i) {
170 for (unsigned int j = 0; j <= i; ++j) {
171 if (j % 5 == 0) {
172 std::cout << std::endl << std::setw(4) << i << ","
173 << std::setw(4) << j << "-" << std::setw(4)
174 << std::min(j + 4, i) << ":";
175 }
176 std::cout << std::setw(13) << theVec[(i * i + i) / 2 + j];
177 }
178 }
179 std::cout << std::endl << std::endl;
180}

◆ resize()

void gbl::VSymMatrix::resize ( const unsigned int  nRows)

Resize symmetric matrix.

Parameters
[in]nRowsNumber of rows.

Definition at line 153 of file VMatrix.cc.

153 {
154 numRows = nRows;
155 theVec.resize((nRows * nRows + nRows) / 2);
156}

Member Data Documentation

◆ numRows

unsigned int gbl::VSymMatrix::numRows
private

Number of rows.

Definition at line 79 of file VMatrix.h.

◆ theVec

std::vector<double> gbl::VSymMatrix::theVec
private

Data (symmetric storage)

Definition at line 80 of file VMatrix.h.


The documentation for this class was generated from the following files: