CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 64 of file VMatrix.h.

Constructor & Destructor Documentation

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 }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
gbl::VSymMatrix::~VSymMatrix ( )
virtual

Definition at line 146 of file VMatrix.cc.

146  {
147 }

Member Function Documentation

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.

References numRows.

162  {
163  return numRows;
164 }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
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.

References plotBeamSpotDB::first, i, j, findQualityFiles::jj, relval_2017::k, GetRecoTauVFromDQM_MC_cff::kk, cmsLHEtoEOSManager::l, plotBeamSpotDB::last, bookConverter::max, GetRecoTauVFromDQM_MC_cff::next, numRows, and theVec.

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

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 j = first;
347  int previous = 0;
348  int last = previous;
349  // look for pivot
350  while (j > 0) {
351  int jj = (j * j + j) / 2 - 1;
352  if (fabs(theVec[jj]) > std::max(fabs(vkk), eps * diag[j - 1])) {
353  vkk = theVec[jj];
354  k = j;
355  last = previous;
356  }
357  previous = j;
358  j = next[j - 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 k = 1; k <= nSize; ++k) {
413  if (next[k - 1] >= 0) {
414  int kk = (k * k - k) / 2 - 1;
415  for (int j = 1; j <= nSize; ++j) {
416  if (next[j - 1] >= 0) {
417  theVec[kk + 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: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
double & gbl::VSymMatrix::operator() ( unsigned int  i,
unsigned int  j 
)
inline

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

Definition at line 103 of file VMatrix.h.

References theVec.

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

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

Definition at line 108 of file VMatrix.h.

References theVec.

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

Multiplication SymMatrix*Vector.

Definition at line 194 of file VMatrix.cc.

References i, j, numRows, and theVec.

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 }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
VMatrix gbl::VSymMatrix::operator* ( const VMatrix aMatrix) const

Multiplication SymMatrix*Matrix.

Definition at line 207 of file VMatrix.cc.

References gbl::VMatrix::getNumCols(), i, j, cmsLHEtoEOSManager::l, numRows, and theVec.

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 }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
VSymMatrix gbl::VSymMatrix::operator- ( const VMatrix aMatrix) const

Subtraction SymMatrix-(sym)Matrix.

Definition at line 183 of file VMatrix.cc.

References i, j, numRows, and theVec.

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 }
int i
Definition: DBlmapReader.cc:9
VSymMatrix(const unsigned int nRows=0)
Definition: VMatrix.cc:142
int j
Definition: DBlmapReader.cc:9
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
void gbl::VSymMatrix::print ( void  ) const

Print matrix.

Definition at line 167 of file VMatrix.cc.

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

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

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 }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
T min(T a, T b)
Definition: MathUtil.h:58
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79
tuple cout
Definition: gather_cfg.py:145
void gbl::VSymMatrix::resize ( const unsigned int  nRows)

Resize symmetric matrix.

Parameters
[in]nRowsNumber of rows.

Definition at line 153 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().

153  {
154  numRows = nRows;
155  theVec.resize((nRows * nRows + nRows) / 2);
156 }
unsigned int numRows
Number of rows.
Definition: VMatrix.h:78
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:79

Member Data Documentation

unsigned int gbl::VSymMatrix::numRows
private

Number of rows.

Definition at line 78 of file VMatrix.h.

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

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

Data (symmetric storage)

Definition at line 79 of file VMatrix.h.

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