Graphitti
A toolkit/software architecture to ease creating high-performance neural network simulators
|
An efficient implementation of a dynamically-allocated sparse 2D array. More...
#include <SparseMatrix.h>
Public Member Functions | |
SparseMatrix (int r, int c, BGFLOAT m, TiXmlElement *e) | |
SparseMatrix (int r, int c, BGFLOAT m, const char *v=nullptr) | |
SparseMatrix (int r=1, int c=1) | |
SparseMatrix (const SparseMatrix &oldM) | |
Copy constructor. Performs a deep copy. | |
virtual | ~SparseMatrix () |
De-allocate storage. | |
SparseMatrix & | operator= (const SparseMatrix &rhs) |
Assignment operator. | |
BGFLOAT & | operator() (int r, int c) |
Access value of element at (row, column) – mutator. | |
int | size (void) const |
Returns the number of elements in the sparse matrix. | |
virtual void | Print (ostream &os) const override |
Polymorphic output. Produces text output on stream "os". Output is by row. | |
virtual string | toXML (string name="") const |
Produce XML representation of Matrix in string return value. | |
virtual const SparseMatrix | operator- () const |
virtual const SparseMatrix | operator+ (const SparseMatrix &rhs) const |
virtual const SparseMatrix | operator* (const SparseMatrix &rhs) const |
![]() | |
virtual | ~Matrix ()=default |
Virtual Destructor. | |
template<class Archive > | |
void | serialize (Archive &archive) |
Cereal serialization method. | |
![]() | |
template<class Archive > | |
void | serialize (Archive &archive) |
Cereal serialization method. | |
Protected Member Functions | |
void | clear (void) |
Frees up all dynamically allocated storage. | |
void | remove_lists (Element *el) |
void | copy (const SparseMatrix &source) |
void | alloc (void) |
void | rowFromXML (TiXmlElement *rowElement) |
Reads a row of the sparse Matrix from XML. | |
![]() | |
Matrix (string t="", string i="", int r=0, int c=0, BGFLOAT m=0.0) | |
void | SetAttributes (string t, string i, int r, int c, BGFLOAT m, int d) |
Convenience mutator. | |
![]() | |
RecordableBase ()=default | |
prevents any code outside this class from creating a RecordableBase object | |
Protected Attributes | |
int | dimensions |
One or two dimensional. | |
![]() | |
string | type |
string | init |
int | rows |
Number of rows in Matrix (>0) | |
int | columns |
Number of columns in Matrix (>0) | |
BGFLOAT | multiplier |
Constant used for initialization. | |
int | dimensions |
One or two dimensional. | |
![]() | |
std::string | basicDataType_ |
the basic data type in the recorded variable | |
Friends | |
const VectorMatrix | operator* (const VectorMatrix &v, const SparseMatrix &m) |
An efficient implementation of a dynamically-allocated sparse 2D array.
This is a self-allocating and de-allocating 2D array that is optimized for numerical computation. It is modified from CompleteMatrix, but with a totally different, sparse implementation, including optimization of the math operations to take advantage of sparseness. More specifically, if the size of the array is NxN and the average number of non-zero entries in a row or column is M, then this implementation is intended to provide O(M) time scanning of all non-zero entries in a row or column and O(1) time access of the item at arbitrary location (i,j). The maximum number of items that a SparseMatrix can hold is sqrt(rows * columns).
This magic is accomplished by storing non-zero elements as part of three data structures simultaneously:
Definition at line 49 of file SparseMatrix.h.
SparseMatrix::SparseMatrix | ( | int | r, |
int | c, | ||
BGFLOAT | m, | ||
TiXmlElement * | e ) |
Allocate storage and initialize attributes for a sparse matrix with explicit row data. The parameter e is used as a source of data for initializing the matrix (and must be the pointer to the Matrix element in the XML).
Matrix_bad_alloc | |
Matrix_invalid_argument |
r | rows in Matrix |
c | columns in Matrix |
m | multiplier used for initialization |
e | pointer to Matrix element in XML |
@method SparseMatrix @discussion Allocate storage and initialize attributes for a sparse matrix with explicit row data. The parameter e is used as a source of data for initializing the matrix (and must be the pointer to the Matrix element in the XML).
Matrix_bad_alloc | |
Matrix_invalid_argument |
r | rows in Matrix |
c | columns in Matrix |
m | multiplier used for initialization |
e | pointer to Matrix element in XML |
Definition at line 174 of file SparseMatrix.cpp.
SparseMatrix::SparseMatrix | ( | int | r, |
int | c, | ||
BGFLOAT | m, | ||
const char * | v = nullptr ) |
Allocate storage and initialize attributes for a diagonal sparse matrix with explicit row data. The parameter v is used as a source of data for initializing the matrix (and must be a string of numbers equal to the number of rows or columns). If v is nullptr, then the multiplier is used to initialize the diagonal elements.
Matrix_bad_alloc | |
Matrix_invalid_argument |
r | rows in Matrix |
c | columns in Matrix |
m | multiplier used for initialization (and must be non-zero) |
v | string of initialization values |
@method SparseMatrix @discussion Allocate storage and initialize attributes for a diagonal sparse matrix with explicit row data. The parameter v is used as a source of data for initializing the matrix (and must be a string of numbers equal to the number of rows or columns).
Matrix_bad_alloc | |
Matrix_invalid_argument |
r | rows in Matrix |
c | columns in Matrix |
m | multiplier used for initialization |
v | string of initialization values |
Definition at line 215 of file SparseMatrix.cpp.
SparseMatrix::SparseMatrix | ( | int | r = 1, |
int | c = 1 ) |
Allocate storage and initialize attributes for an empty sparse matrix. This is also the default constructor.
Matrix_bad_alloc | |
Matrix_invalid_argument |
@method SparseMatrix @discussion Allocate storage and initialize attributes for an empty sparse matrix. This is also the default constructor.
Matrix_bad_alloc | |
Matrix_invalid_argument |
Definition at line 281 of file SparseMatrix.cpp.
SparseMatrix::SparseMatrix | ( | const SparseMatrix & | oldM | ) |
Copy constructor. Performs a deep copy.
oldM | The source SparseMatrix |
Definition at line 307 of file SparseMatrix.cpp.
|
virtual |
De-allocate storage.
Definition at line 335 of file SparseMatrix.cpp.
|
protected |
Allocates storage for internal storage. Assumes that no storage has already been allocated (i.e., must call clear() before this if not in a constructor) and that all simple data members (specifically, rows and columns) have been correctly set.
Matrix_bad_alloc | |
MatrixException |
Definition at line 475 of file SparseMatrix.cpp.
|
protected |
Frees up all dynamically allocated storage.
Definition at line 372 of file SparseMatrix.cpp.
|
protected |
Performs a deep copy. It is assumed that the correct storage has already been allocated and all "simple" data members have already had their values copied.
source | VectorMatrix to copy from |
Definition at line 414 of file SparseMatrix.cpp.
BGFLOAT & SparseMatrix::operator() | ( | int | r, |
int | c ) |
Access value of element at (row, column) – mutator.
Constant time as long as number of items in the N x M SparseMatrix is less than 4*sqrt(N*M). If there is no element at the given location, then a zero value one is created. This is done because this method is usable as a mutator: it returns a reference to the value in the specified element. If this becomes a problem in other code, then the solution would be to add an accessor that either throws an exception if an element doesn't exist or that returns both the element value and a success/failure flag.
r | element row |
c | element column |
@method operator() @discussion Access value of element at (row, column) – mutator. Constant time as long as number of items in the N x M SparseMatrix is less than 4*sqrt(N*M). If there is no element at the given location, then a zero value one is created.
r | element row |
c | element column |
Matrix_bad_alloc |
Definition at line 549 of file SparseMatrix.cpp.
|
virtual |
Matrix product. Number of rows of "rhs" must equal to number of columns of this.
Matrix_domain_error |
rhs | right-hand argument to the product. |
Definition at line 601 of file SparseMatrix.cpp.
|
virtual |
Compute the sum of two SparseMatrices of the same rows and columns.
Matrix_domain_error |
rhs | right-hand argument to the addition. Must have same dimensions as this. |
Definition at line 594 of file SparseMatrix.cpp.
|
virtual |
Unary minus. Negate all elements of the SparseMatrix.
Definition at line 575 of file SparseMatrix.cpp.
SparseMatrix & SparseMatrix::operator= | ( | const SparseMatrix & | rhs | ) |
Assignment operator.
rhs | right-hand side of assignment |
Definition at line 342 of file SparseMatrix.cpp.
|
overridevirtual |
Polymorphic output. Produces text output on stream "os". Output is by row.
os | stream to output to |
Implements Matrix.
Definition at line 500 of file SparseMatrix.cpp.
|
protected |
Remove an Element from the SparseMatrix lists (but not the hash table). This is meant to be called from a HashTable method.
@method remove_lists @discussion Remove an Element from the SparseMatrix lists (but not the hash table). This is meant to be called from a HashTable method.
Definition at line 405 of file SparseMatrix.cpp.
|
protected |
Reads a row of the sparse Matrix from XML.
rowElement | pointer to Row XML element |
Matrix_invalid_argument |
Definition at line 445 of file SparseMatrix.cpp.
|
inline |
Returns the number of elements in the sparse matrix.
Definition at line 280 of file SparseMatrix.h.
|
virtual |
Produce XML representation of Matrix in string return value.
Definition at line 520 of file SparseMatrix.cpp.
|
friend |
Matrix times vector. Size of v must equal to number of rows of m. Size of resultant vector is equal to number Of columns of m.
Matrix_domain_error |
Definition at line 608 of file SparseMatrix.cpp.