CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
gbl::VSymMatrix Class Reference

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

#include <VMatrix.h>

Public Member Functions

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

Private Attributes

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

Detailed Description

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

Definition at line 86 of file VMatrix.h.

Constructor & Destructor Documentation

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

Definition at line 164 of file VMatrix.cc.

164  :
165  numRows(nRows), theVec((nRows * nRows + nRows) / 2) {
166  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
gbl::VSymMatrix::~VSymMatrix ( )
virtual

Definition at line 168 of file VMatrix.cc.

168  {
169  }

Member Function Documentation

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

Get number of rows (= number of colums).

Returns
Number of rows.

Definition at line 184 of file VMatrix.cc.

References numRows.

184  {
185  return numRows;
186  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
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 348 of file VMatrix.cc.

References plotBeamSpotDB::first, mps_fire::i, findQualityFiles::jj, gen::k, GetRecoTauVFromDQM_MC_cff::kk, checklumidiff::l, plotBeamSpotDB::last, hpstanc_transforms::max, GetRecoTauVFromDQM_MC_cff::next, gbl::VVector::numRows, and gbl::VVector::theVec.

Referenced by gbl::BorderedBandMatrix::solveAndInvertBorderedBand().

348  {
349 
350  const double eps = 1.0E-10;
351  unsigned int aSize = numRows;
352  std::vector<int> next(aSize);
353  std::vector<double> diag(aSize);
354  int nSize = aSize;
355 
356  int first = 1;
357  for (int i = 1; i <= nSize; ++i) {
358  next[i - 1] = i + 1; // set "next" pointer
359  diag[i - 1] = fabs(theVec[(i * i + i) / 2 - 1]); // save abs of diagonal elements
360  }
361  next[aSize - 1] = -1; // end flag
362 
363  unsigned int nrank = 0;
364  for (int i = 1; i <= nSize; ++i) { // start of loop
365  int k = 0;
366  double vkk = 0.0;
367 
368  int j = first;
369  int previous = 0;
370  int last = previous;
371  // look for pivot
372  while (j > 0) {
373  int jj = (j * j + j) / 2 - 1;
374  if (fabs(theVec[jj]) > std::max(fabs(vkk), eps * diag[j - 1])) {
375  vkk = theVec[jj];
376  k = j;
377  last = previous;
378  }
379  previous = j;
380  j = next[j - 1];
381  }
382  // pivot found
383  if (k > 0) {
384  int kk = (k * k + k) / 2 - 1;
385  if (last <= 0) {
386  first = next[k - 1];
387  } else {
388  next[last - 1] = next[k - 1];
389  }
390  next[k - 1] = 0; // index is used, reset
391  nrank++; // increase rank and ...
392 
393  vkk = 1.0 / vkk;
394  theVec[kk] = -vkk;
395  int jk = kk - k;
396  int jl = -1;
397  for (int j = 1; j <= nSize; ++j) { // elimination
398  if (j == k) {
399  jk = kk;
400  jl += j;
401  } else {
402  if (j < k) {
403  ++jk;
404  } else {
405  jk += j - 1;
406  }
407 
408  double vjk = theVec[jk];
409  theVec[jk] = vkk * vjk;
410  int lk = kk - k;
411  if (j >= k) {
412  for (int l = 1; l <= k - 1; ++l) {
413  ++jl;
414  ++lk;
415  theVec[jl] -= theVec[lk] * vjk;
416  }
417  ++jl;
418  lk = kk;
419  for (int l = k + 1; l <= j; ++l) {
420  ++jl;
421  lk += l - 1;
422  theVec[jl] -= theVec[lk] * vjk;
423  }
424  } else {
425  for (int l = 1; l <= j; ++l) {
426  ++jl;
427  ++lk;
428  theVec[jl] -= theVec[lk] * vjk;
429  }
430  }
431  }
432  }
433  } else {
434  for (int k = 1; k <= nSize; ++k) {
435  if (next[k - 1] >= 0) {
436  int kk = (k * k - k) / 2 - 1;
437  for (int j = 1; j <= nSize; ++j) {
438  if (next[j - 1] >= 0) {
439  theVec[kk + j] = 0.0; // clear matrix row/col
440  }
441  }
442  }
443  }
444  throw 1; // singular
445  }
446  }
447  for (int ij = 0; ij < (nSize * nSize + nSize) / 2; ++ij) {
448  theVec[ij] = -theVec[ij]; // finally reverse sign of all matrix elements
449  }
450  return nrank;
451  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
int k[5][pyjets_maxn]
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
double & gbl::VSymMatrix::operator() ( unsigned int  i,
unsigned int  j 
)
inline

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

Definition at line 125 of file VMatrix.h.

References gbl::VVector::theVec.

125  {
126  return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
127  }
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
double gbl::VSymMatrix::operator() ( unsigned int  i,
unsigned int  j 
) const
inline

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

Definition at line 130 of file VMatrix.h.

References gbl::VVector::theVec.

131  {
132  return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
133  }
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
VVector gbl::VSymMatrix::operator* ( const VVector aVector) const

Multiplication SymMatrix*Vector.

Definition at line 216 of file VMatrix.cc.

References mps_fire::i, numRows, and theVec.

216  {
217  VVector aResult(numRows);
218  for (unsigned int i = 0; i < numRows; ++i) {
219  aResult(i) = theVec[(i * i + i) / 2 + i] * aVector(i);
220  for (unsigned int j = 0; j < i; ++j) {
221  aResult(j) += theVec[(i * i + i) / 2 + j] * aVector(i);
222  aResult(i) += theVec[(i * i + i) / 2 + j] * aVector(j);
223  }
224  }
225  return aResult;
226  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
VMatrix gbl::VSymMatrix::operator* ( const VMatrix aMatrix) const

Multiplication SymMatrix*Matrix.

Definition at line 229 of file VMatrix.cc.

References gbl::VMatrix::getNumCols(), mps_fire::i, checklumidiff::l, numRows, and theVec.

229  {
230  unsigned int nCol = aMatrix.getNumCols();
231  VMatrix aResult(numRows, nCol);
232  for (unsigned int l = 0; l < nCol; ++l) {
233  for (unsigned int i = 0; i < numRows; ++i) {
234  aResult(i, l) = theVec[(i * i + i) / 2 + i] * aMatrix(i, l);
235  for (unsigned int j = 0; j < i; ++j) {
236  aResult(j, l) += theVec[(i * i + i) / 2 + j] * aMatrix(i, l);
237  aResult(i, l) += theVec[(i * i + i) / 2 + j] * aMatrix(j, l);
238  }
239  }
240  }
241  return aResult;
242  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
VSymMatrix gbl::VSymMatrix::operator- ( const VMatrix aMatrix) const

Subtraction SymMatrix-(sym)Matrix.

Definition at line 205 of file VMatrix.cc.

References mps_fire::i, numRows, and theVec.

205  {
206  VSymMatrix aResult(numRows);
207  for (unsigned int i = 0; i < numRows; ++i) {
208  for (unsigned int j = 0; j <= i; ++j) {
209  aResult(i, j) = theVec[(i * i + i) / 2 + j] - aMatrix(i, j);
210  }
211  }
212  return aResult;
213  }
VSymMatrix(const unsigned int nRows=0)
Definition: VMatrix.cc:164
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
void gbl::VSymMatrix::print ( void  ) const

Print matrix.

Definition at line 189 of file VMatrix.cc.

References gather_cfg::cout, mps_fire::i, min(), numRows, and theVec.

Referenced by gbl::BorderedBandMatrix::printMatrix().

189  {
190  std::cout << " VSymMatrix: " << numRows << "*" << numRows << std::endl;
191  for (unsigned int i = 0; i < numRows; ++i) {
192  for (unsigned int j = 0; j <= i; ++j) {
193  if (j % 5 == 0) {
194  std::cout << std::endl << std::setw(4) << i << ","
195  << std::setw(4) << j << "-" << std::setw(4)
196  << std::min(j + 4, i) << ":";
197  }
198  std::cout << std::setw(13) << theVec[(i * i + i) / 2 + j];
199  }
200  }
201  std::cout << std::endl << std::endl;
202  }
T min(T a, T b)
Definition: MathUtil.h:58
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101
void gbl::VSymMatrix::resize ( const unsigned int  nRows)

Resize symmetric matrix.

Parameters
[in]nRowsNumber of rows.

Definition at line 175 of file VMatrix.cc.

References numRows, and theVec.

Referenced by Vispa.Gui.TextDialog.TextDialog::__init__(), Vispa.Plugins.ConfigEditor.ToolDialog.ToolDialog::__init__(), Vispa.Main.MainWindow.MainWindow::_loadIni(), gbl::BorderedBandMatrix::resize(), and Vispa.Gui.PortConnection.PointToPointConnection::updateConnection().

175  {
176  numRows = nRows;
177  theVec.resize((nRows * nRows + nRows) / 2);
178  }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:100
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:101

Member Data Documentation

unsigned int gbl::VSymMatrix::numRows
private

Number of rows.

Definition at line 100 of file VMatrix.h.

Referenced by getNumRows(), operator*(), operator-(), print(), and resize().

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

Data (symmetric storage)

Definition at line 101 of file VMatrix.h.

Referenced by operator*(), operator-(), print(), and resize().