COPASI API  4.16.103
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
CSparseMatrix Class Reference

#include <CSparseMatrix.h>

Collaboration diagram for CSparseMatrix:
Collaboration graph
[legend]

Public Member Functions

 CSparseMatrix (const size_t &rows=0, const size_t &cols=0)
 
 CSparseMatrix (const CCompressedColumnFormat &ccf)
 
const std::vector< std::vector
< CSparseMatrixElement * > > & 
getColumns () const
 
std::vector< std::vector
< CSparseMatrixElement * > > & 
getColumns ()
 
const std::vector< std::vector
< CSparseMatrixElement * > > & 
getRows () const
 
std::vector< std::vector
< CSparseMatrixElement * > > & 
getRows ()
 
const C_FLOAT64getTreshold () const
 
bool insert (const size_t &row, const size_t &col, const C_FLOAT64 &value)
 
const size_t & numCols () const
 
size_t numNonZeros () const
 
const size_t & numRows () const
 
 operator const CMatrix< C_FLOAT64 > () const
 
CSparseMatrixElementoperator() (const size_t &row, const size_t &col)
 
const CSparseMatrixElementoperator() (const size_t &row, const size_t &col) const
 
CSparseMatrixoperator= (const CCompressedColumnFormat &ccf)
 
CSparseMatrixoperator= (const CMatrix< C_FLOAT64 > &matrix)
 
bool remove (const size_t &row, const size_t &col)
 
bool resize (const size_t &rows, const size_t &columns)
 
bool setTreshold (const C_FLOAT64 &threshold)
 
 ~CSparseMatrix ()
 

Private Member Functions

void cleanup ()
 

Private Attributes

std::vector< size_t > mColIndex
 
std::vector< std::vector
< CSparseMatrixElement * > > 
mCols
 
CSparseMatrixElement mElement
 
size_t mNumCols
 
size_t mNumRows
 
std::vector< size_t > mRowIndex
 
std::vector< std::vector
< CSparseMatrixElement * > > 
mRows
 
size_t mSearchCol
 
size_t mSearchRow
 
C_FLOAT64 mThreshold
 

Friends

std::ostream & operator<< (std::ostream &os, const CSparseMatrix &A)
 

Detailed Description

Definition at line 67 of file CSparseMatrix.h.

Constructor & Destructor Documentation

CSparseMatrix::CSparseMatrix ( const size_t &  rows = 0,
const size_t &  cols = 0 
)

Definition at line 78 of file CSparseMatrix.cpp.

References resize().

79  :
80  mThreshold(std::numeric_limits< C_FLOAT64 >::epsilon()),
81  mNumRows(0),
82  mNumCols(0),
83  mRowIndex(),
84  mColIndex(),
85  mRows(),
86  mCols(),
87  mSearchRow(0),
88  mSearchCol(0),
89  mElement(*this, this->mSearchRow, this->mSearchCol, 0.0)
90 {resize(rows, cols);}
bool resize(const size_t &rows, const size_t &columns)
std::vector< std::vector< CSparseMatrixElement * > > mRows
C_FLOAT64 mThreshold
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
CSparseMatrix::CSparseMatrix ( const CCompressedColumnFormat ccf)

Definition at line 92 of file CSparseMatrix.cpp.

92  :
93  mThreshold(std::numeric_limits< C_FLOAT64 >::epsilon()),
94  mNumRows(0),
95  mNumCols(0),
96  mRowIndex(),
97  mColIndex(),
98  mRows(),
99  mCols(),
100  mSearchRow(0),
101  mSearchCol(0),
102  mElement(*this, this->mSearchRow, this->mSearchCol, 0.0)
103 {*this = ccf;}
std::vector< std::vector< CSparseMatrixElement * > > mRows
C_FLOAT64 mThreshold
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
CSparseMatrix::~CSparseMatrix ( )

Definition at line 105 of file CSparseMatrix.cpp.

References cleanup().

106 {cleanup();}

Member Function Documentation

void CSparseMatrix::cleanup ( )
private

Definition at line 108 of file CSparseMatrix.cpp.

References mRows.

Referenced by ~CSparseMatrix().

109 {
110  std::vector< std::vector< CSparseMatrixElement * > >::iterator itRow;
111  std::vector< std::vector< CSparseMatrixElement * > >::iterator endRow;
112 
113  std::vector< CSparseMatrixElement * >::iterator itElement;
114  std::vector< CSparseMatrixElement * >::iterator endElement;
115 
116  for (itRow = mRows.begin(), endRow = mRows.end(); itRow != endRow; ++itRow)
117  for (itElement = itRow->begin(), endElement = itRow->end(); itElement != endElement; ++itElement)
118  delete *itElement;
119 }
std::vector< std::vector< CSparseMatrixElement * > > mRows
const std::vector< std::vector< CSparseMatrixElement * > > & CSparseMatrix::getColumns ( ) const

Definition at line 162 of file CSparseMatrix.cpp.

References mCols.

Referenced by CCompressedColumnFormat::operator=().

163 {return mCols;}
std::vector< std::vector< CSparseMatrixElement * > > mCols
std::vector< std::vector< CSparseMatrixElement * > > & CSparseMatrix::getColumns ( )

Definition at line 165 of file CSparseMatrix.cpp.

References mCols.

166 {return mCols;}
std::vector< std::vector< CSparseMatrixElement * > > mCols
const std::vector< std::vector< CSparseMatrixElement * > > & CSparseMatrix::getRows ( ) const

Definition at line 168 of file CSparseMatrix.cpp.

References mRows.

169 {return mRows;}
std::vector< std::vector< CSparseMatrixElement * > > mRows
std::vector< std::vector< CSparseMatrixElement * > > & CSparseMatrix::getRows ( )

Definition at line 171 of file CSparseMatrix.cpp.

References mRows.

172 {return mRows;}
std::vector< std::vector< CSparseMatrixElement * > > mRows
const C_FLOAT64 & CSparseMatrix::getTreshold ( ) const

Definition at line 279 of file CSparseMatrix.cpp.

References mThreshold.

Referenced by CSparseMatrixElement::operator=().

280 {return mThreshold;}
C_FLOAT64 mThreshold
bool CSparseMatrix::insert ( const size_t &  row,
const size_t &  col,
const C_FLOAT64 value 
)

Definition at line 312 of file CSparseMatrix.cpp.

References CSparseMatrixElement::compareCol(), CSparseMatrixElement::compareRow(), mColIndex, mCols, mElement, mRowIndex, mRows, mSearchCol, and mSearchRow.

Referenced by CSparseMatrixElement::operator=().

315 {
316  mSearchRow = row;
317  mSearchCol = col;
318 
319  std::vector< CSparseMatrixElement * >::iterator found
320  = std::lower_bound(mCols[col].begin(), mCols[col].end(), &mElement, CSparseMatrixElement::compareRow);
321 
322  if (found != mCols[col].end() &&
323  (*found)->row() == row) return false; // The element already exists.
324 
325  CSparseMatrixElement * pElement = new CSparseMatrixElement(*this, mRowIndex[row], mColIndex[col], value);
326  mCols[col].insert(found, pElement);
327 
328  found = std::lower_bound(mRows[row].begin(), mRows[row].end(), &mElement, CSparseMatrixElement::compareCol);
329  mRows[row].insert(found, pElement);
330 
331  return true;
332 }
static bool compareRow(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< std::vector< CSparseMatrixElement * > > mRows
static bool compareCol(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
const size_t & CSparseMatrix::numCols ( ) const

Definition at line 146 of file CSparseMatrix.cpp.

References mNumCols.

Referenced by CCompressedColumnFormat::operator=().

147 {return mNumCols;}
size_t CSparseMatrix::numNonZeros ( ) const

Definition at line 149 of file CSparseMatrix.cpp.

References mRows.

Referenced by CCompressedColumnFormat::operator=().

150 {
151  size_t NonZeros = 0;
152 
153  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itRow;
154  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endRow;
155 
156  for (itRow = mRows.begin(), endRow = mRows.end(); itRow != endRow; ++itRow)
157  NonZeros += itRow->size();
158 
159  return NonZeros;
160 }
std::vector< std::vector< CSparseMatrixElement * > > mRows
const size_t & CSparseMatrix::numRows ( ) const

Definition at line 143 of file CSparseMatrix.cpp.

References mNumRows.

Referenced by CCompressedColumnFormat::operator=().

144 {return mNumRows;}
CSparseMatrix::operator const CMatrix< C_FLOAT64 > ( ) const

Definition at line 226 of file CSparseMatrix.cpp.

References CMatrix< CType >::array(), C_FLOAT64, and CMatrix< CType >::resize().

227 {
230 
231  C_FLOAT64 * pTmp = M.array();
232  size_t i;
233 
234  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itRow;
235  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endRow;
236 
237  std::vector< CSparseMatrixElement * >::const_iterator itElement;
238  std::vector< CSparseMatrixElement * >::const_iterator endElement;
239 
240  for (itRow = mRows.begin(), endRow = mRows.end(); itRow != endRow; ++itRow)
241  {
242  for (i = 0, itElement = itRow->begin(), endElement = itRow->end();
243  itElement != endElement; ++itElement, i++, pTmp++)
244  {
245  for (; i < (*itElement)->col(); i++, pTmp++)
246  *pTmp = 0.0;
247 
248  *pTmp = **itElement;
249  }
250 
251  for (; i < mNumCols; i++, pTmp++)
252  *pTmp = 0.0;
253  }
254 
255  return M;
256 }
std::vector< std::vector< CSparseMatrixElement * > > mRows
virtual void resize(size_t rows, size_t cols, const bool &copy=false)
Definition: CMatrix.h:151
#define C_FLOAT64
Definition: copasi.h:92
virtual CType * array()
Definition: CMatrix.h:337
CSparseMatrixElement & CSparseMatrix::operator() ( const size_t &  row,
const size_t &  col 
)

Definition at line 282 of file CSparseMatrix.cpp.

References CSparseMatrixElement::compareRow(), mCols, mElement, mSearchCol, and mSearchRow.

284 {
285  mSearchRow = row;
286  mSearchCol = col;
287 
288  std::vector< CSparseMatrixElement * >::iterator found
289  = std::lower_bound(mCols[col].begin(), mCols[col].end(), &mElement, CSparseMatrixElement::compareRow);
290 
291  if (found != mCols[col].end() &&
292  (*found)->row() == row) return **found;
293 
294  return mElement;
295 }
static bool compareRow(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
const CSparseMatrixElement & CSparseMatrix::operator() ( const size_t &  row,
const size_t &  col 
) const

Definition at line 297 of file CSparseMatrix.cpp.

References CSparseMatrixElement::compareRow(), mCols, mElement, mSearchCol, and mSearchRow.

299 {
300  const_cast<CSparseMatrix *>(this)->mSearchRow = row;
301  const_cast<CSparseMatrix *>(this)->mSearchCol = col;
302 
303  std::vector< CSparseMatrixElement * >::const_iterator found
304  = std::lower_bound(mCols[col].begin(), mCols[col].end(), &mElement, CSparseMatrixElement::compareRow);
305 
306  if (found != mCols[col].end() &&
307  (*found)->row() == row) return **found;
308 
309  return mElement;
310 }
static bool compareRow(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
CSparseMatrix & CSparseMatrix::operator= ( const CCompressedColumnFormat ccf)

Definition at line 174 of file CSparseMatrix.cpp.

References C_FLOAT64, CSparseMatrixElement::compareCol(), CCompressedColumnFormat::getColumnStart(), CCompressedColumnFormat::getRowIndex(), CCompressedColumnFormat::getValues(), mColIndex, mCols, mNumCols, mNumRows, mRowIndex, mRows, CCompressedColumnFormat::numCols(), CCompressedColumnFormat::numRows(), and resize().

175 {
176  resize(ccf.numRows(), ccf.numCols());
177 
178  const C_FLOAT64 * pValue = ccf.getValues();
179  const size_t * pRowIndex = ccf.getRowIndex();
180  const size_t * pColumnStart = ccf.getColumnStart() + 1;
181 
182  size_t i, j, k;
183  CSparseMatrixElement * pElement;
184 
185  for (j = 0, i = 0; j < mNumCols; j++, pColumnStart++)
186  {
187  mCols[j].resize(*pColumnStart - * (pColumnStart - 1));
188 
189  for (k = 0; i < *pColumnStart; i++, k++, pRowIndex++, pValue++)
190  {
191  pElement = new CSparseMatrixElement(*this, mRowIndex[*pRowIndex], mColIndex[j], *pValue);
192  mCols[j][k] = pElement;
193  mRows[*pRowIndex].push_back(pElement); // Note, these are out of order and must be sorted.
194  }
195  }
196 
197  for (i = 0; i < mNumRows; i++)
198  std::sort(mRows[i].begin(), mRows[i].end(), &CSparseMatrixElement::compareCol);
199 
200  return *this;
201 }
bool resize(const size_t &rows, const size_t &columns)
const size_t * getColumnStart() const
std::vector< std::vector< CSparseMatrixElement * > > mRows
static bool compareCol(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
const size_t * getRowIndex() const
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
#define C_FLOAT64
Definition: copasi.h:92
std::vector< std::vector< CSparseMatrixElement * > > mCols
const C_FLOAT64 * getValues() const
CSparseMatrix & CSparseMatrix::operator= ( const CMatrix< C_FLOAT64 > &  matrix)

Definition at line 203 of file CSparseMatrix.cpp.

References CMatrix< CType >::array(), C_FLOAT64, CSparseMatrixElement::compareRow(), mColIndex, mCols, mNumCols, mNumRows, mRowIndex, mRows, mThreshold, CMatrix< CType >::numCols(), CMatrix< CType >::numRows(), and resize().

204 {
205  size_t i, j;
206  CSparseMatrixElement * pElement;
207 
208  resize(matrix.numRows(), matrix.numCols());
209  const C_FLOAT64 * pTmp = matrix.array();
210 
211  for (i = 0; i < mNumRows; i++)
212  for (j = 0; j < mNumCols; j++, pTmp++)
213  if (fabs(*pTmp) >= mThreshold)
214  {
215  pElement = new CSparseMatrixElement(*this, mRowIndex[i], mColIndex[j], *pTmp);
216  mRows[i].push_back(pElement);
217  mCols[j].push_back(pElement); // Note, these are out of order and must be sorted.
218  }
219 
220  for (j = 0; j < mNumCols; j++)
221  std::sort(mCols[j].begin(), mCols[j].end(), &CSparseMatrixElement::compareRow);
222 
223  return *this;
224 }
bool resize(const size_t &rows, const size_t &columns)
static bool compareRow(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
virtual size_t numRows() const
Definition: CMatrix.h:138
std::vector< std::vector< CSparseMatrixElement * > > mRows
C_FLOAT64 mThreshold
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
#define C_FLOAT64
Definition: copasi.h:92
std::vector< std::vector< CSparseMatrixElement * > > mCols
virtual size_t numCols() const
Definition: CMatrix.h:144
virtual CType * array()
Definition: CMatrix.h:337
bool CSparseMatrix::remove ( const size_t &  row,
const size_t &  col 
)

Definition at line 334 of file CSparseMatrix.cpp.

References CSparseMatrixElement::compareCol(), CSparseMatrixElement::compareRow(), mCols, mElement, mRows, mSearchCol, and mSearchRow.

Referenced by CSparseMatrixElement::operator=().

336 {
337  mSearchRow = row;
338  mSearchCol = col;
339 
340  std::vector< CSparseMatrixElement * >::iterator found
341  = std::lower_bound(mCols[col].begin(), mCols[col].end(), &mElement, CSparseMatrixElement::compareRow);
342 
343  if (found == mCols[col].end()) return false; // The element does not exist.
344 
345  mCols[col].erase(found);
346 
347  found = std::lower_bound(mRows[row].begin(), mRows[row].end(), &mElement, CSparseMatrixElement::compareCol);
348  mRows[row].erase(found);
349 
350  delete *found;
351 
352  return true;
353 }
static bool compareRow(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< std::vector< CSparseMatrixElement * > > mRows
static bool compareCol(const CSparseMatrixElement *pLhs, const CSparseMatrixElement *pRhs)
std::vector< std::vector< CSparseMatrixElement * > > mCols
CSparseMatrixElement mElement
bool CSparseMatrix::resize ( const size_t &  rows,
const size_t &  columns 
)

Definition at line 121 of file CSparseMatrix.cpp.

References mColIndex, mCols, mNumCols, mNumRows, mRowIndex, and mRows.

Referenced by CSparseMatrix(), and operator=().

122 {
123  size_t i;
124  mNumRows = rows;
125  mNumCols = cols;
126 
127  mRowIndex.resize(mNumRows);
128 
129  for (i = 0; i < mNumRows; i++)
130  mRowIndex[i] = i;
131 
132  mColIndex.resize(mNumCols);
133 
134  for (i = 0; i < mNumCols; i++)
135  mColIndex[i] = i;
136 
137  mRows.resize(mNumRows);
138  mCols.resize(mNumCols);
139 
140  return true;
141 }
std::vector< std::vector< CSparseMatrixElement * > > mRows
std::vector< size_t > mColIndex
std::vector< size_t > mRowIndex
std::vector< std::vector< CSparseMatrixElement * > > mCols
bool CSparseMatrix::setTreshold ( const C_FLOAT64 threshold)

Definition at line 258 of file CSparseMatrix.cpp.

References mRows, and mThreshold.

259 {
260  if (threshold < mThreshold)
261  {
262  std::vector< std::vector< CSparseMatrixElement * > >::iterator itRow;
263  std::vector< std::vector< CSparseMatrixElement * > >::iterator endRow;
264 
265  std::vector< CSparseMatrixElement * >::reverse_iterator itElement;
266  std::vector< CSparseMatrixElement * >::reverse_iterator endElement;
267 
268  for (itRow = mRows.begin(), endRow = mRows.end(); itRow != endRow; ++itRow)
269  for (itElement = itRow->rbegin(), endElement = itRow->rend(); itElement != endElement; ++itElement)
270  if (fabs(**itElement) < threshold)
271  remove((*itElement)->row(), (*itElement)->col());
272  }
273 
274  mThreshold = threshold;
275 
276  return true;
277 }
std::vector< std::vector< CSparseMatrixElement * > > mRows
C_FLOAT64 mThreshold

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const CSparseMatrix A 
)
friend

Definition at line 355 of file CSparseMatrix.cpp.

356 {
357  os << "Matrix(" << A.mNumRows << "x" << A.mNumCols << ")" << std::endl;
358 
359  size_t i;
360 
361  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itRow;
362  std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endRow;
363 
364  std::vector< CSparseMatrixElement * >::const_iterator itElement;
365  std::vector< CSparseMatrixElement * >::const_iterator endElement;
366 
367  for (itRow = A.mRows.begin(), endRow = A.mRows.end(); itRow != endRow; ++itRow)
368  {
369  for (i = 0, itElement = itRow->begin(), endElement = itRow->end();
370  itElement != endElement; ++itElement, i++)
371  {
372  for (; i < (*itElement)->col(); i++)
373  os << "\t" << 0.0;
374 
375  os << "\t" << **itElement;
376  }
377 
378  for (; i < A.mNumCols; i++)
379  os << "\t" << 0.0;
380 
381  os << std::endl;
382  }
383 
384  return os;
385 }
std::vector< std::vector< CSparseMatrixElement * > > mRows

Member Data Documentation

std::vector< size_t> CSparseMatrix::mColIndex
private

Definition at line 118 of file CSparseMatrix.h.

Referenced by insert(), operator=(), and resize().

std::vector< std::vector< CSparseMatrixElement *> > CSparseMatrix::mCols
private

Definition at line 121 of file CSparseMatrix.h.

Referenced by getColumns(), insert(), operator()(), operator=(), remove(), and resize().

CSparseMatrixElement CSparseMatrix::mElement
private

Definition at line 125 of file CSparseMatrix.h.

Referenced by insert(), operator()(), and remove().

size_t CSparseMatrix::mNumCols
private

Definition at line 115 of file CSparseMatrix.h.

Referenced by numCols(), operator<<(), operator=(), and resize().

size_t CSparseMatrix::mNumRows
private

Definition at line 114 of file CSparseMatrix.h.

Referenced by numRows(), operator<<(), operator=(), and resize().

std::vector< size_t > CSparseMatrix::mRowIndex
private

Definition at line 117 of file CSparseMatrix.h.

Referenced by insert(), operator=(), and resize().

std::vector< std::vector< CSparseMatrixElement *> > CSparseMatrix::mRows
private
size_t CSparseMatrix::mSearchCol
private

Definition at line 124 of file CSparseMatrix.h.

Referenced by insert(), operator()(), and remove().

size_t CSparseMatrix::mSearchRow
private

Definition at line 123 of file CSparseMatrix.h.

Referenced by insert(), operator()(), and remove().

C_FLOAT64 CSparseMatrix::mThreshold
private

Definition at line 112 of file CSparseMatrix.h.

Referenced by getTreshold(), operator=(), and setTreshold().


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