Graphitti
A toolkit/software architecture to ease creating high-performance neural network simulators
Loading...
Searching...
No Matches
VectorMatrix Class Reference

An efficient implementation of a dynamically-allocated 1D array. More...

#include <VectorMatrix.h>

Inheritance diagram for VectorMatrix:
Collaboration diagram for VectorMatrix:

Public Member Functions

 VectorMatrix (string t="complete", string i="const", int r=1, int c=1, BGFLOAT m=0.0, string v="")
 
 VectorMatrix (const VectorMatrix &oldV)
 Copy constructor. Performs a deep copy.
 
virtual ~VectorMatrix ()=default
 De-allocate storage.
 
const VectorMatrixoperator= (BGFLOAT c)
 Set elements of vector to a constant. Doesn't change its size.
 
const VectorMatrixoperator= (const VectorMatrix &rhs)
 Assignment operator.
 
virtual void Print (ostream &os) const
 Polymorphic output. Produces text output on stream "os".
 
virtual string toXML (string name="") const
 Produce XML representation of vector in string return value.
 
const BGFLOAT & operator[] (int i) const
 Access element of a VectorMatrix. Zero-based index; constant-time.
 
const BGFLOAT & at (int i) const
 access element of a VectorMatrix. Zero-based index; constant-time.
 
int Size (void) const
 The length of a VectorMatrix elements.
 
BGFLOAT & operator[] (int i)
 mutate element of a VectorMatrix. Zero-based index; constant time.
 
BGFLOAT & at (int i)
 mutate element of a VectorMatrix. Zero-based index.
 
virtual const VectorMatrix operator+ (const VectorMatrix &rhs) const
 Compute the sum of two VectorMatrices of the same length.
 
virtual const VectorMatrix operator+ (BGFLOAT c) const
 vector plus a constant. Adds each element of the vector plus a constant. Method version (vector on LHS).
 
virtual BGFLOAT operator* (const VectorMatrix &rhs) const
 There are two possible vector products. This is an inner product.
 
virtual const VectorMatrix operator* (const CompleteMatrix &rhs) const
 Vector times a Complete Matrix.
 
virtual const VectorMatrix ArrayMultiply (const VectorMatrix &rhs) const
 Element-by-element multiplication of two vectors.
 
virtual const VectorMatrix operator* (BGFLOAT c) const
 Constant times a vector. Multiplies each element of the vector by a constant. Method version (vector on LHS)
 
virtual const VectorMatrix operator/ (BGFLOAT c) const
 Vector divided by a constant. Divides each element of the vector by a constant.
 
virtual const VectorMatrix Limit (BGFLOAT low, BGFLOAT high) const
 Limit values of a vector. Clip values to lie within range.
 
virtual BGFLOAT Min (void) const
 Find minimum value of vector.
 
virtual BGFLOAT Max (void) const
 Find maximum value of vector.
 
virtual const VectorMatrixoperator+= (const VectorMatrix &rhs)
 Compute and assign the sum of two VectorMatrices of the same length.
 
template<class Archive >
void serialize (Archive &archive)
 Cereal serialization method.
 
virtual int getNumElements () const
 Interface for RecordableBase.
 
virtual void startNewEpoch ()
 
virtual variantTypes getElement (int index) const
 
virtual void setDataType ()
 Set up a string representing the basic data type.
 
virtual const string & getDataType () const override
 Get the run time data type info of the recordable variable.
 
- Public Member Functions inherited from Matrix
virtual ~Matrix ()=default
 Virtual Destructor.
 
template<class Archive >
void serialize (Archive &archive)
 Cereal serialization method.
 
- Public Member Functions inherited from RecordableBase
template<class Archive >
void serialize (Archive &archive)
 Cereal serialization method.
 

Protected Member Functions

void clear (void)
 Frees up all dynamically allocated storage.
 
void copy (const VectorMatrix &source)
 Performs a deep copy.
 
void alloc (int size)
 Allocates storage for internal Vector storage.
 
- Protected Member Functions inherited from Matrix
 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.
 
- Protected Member Functions inherited from RecordableBase
 RecordableBase ()=default
 prevents any code outside this class from creating a RecordableBase object
 

Protected Attributes

int columns
 Number of columns in Matrix (>0)
 
int dimensions
 One or two dimensional.
 
int rows
 Number of rows in Matrix (>0)
 
- Protected Attributes inherited from Matrix
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.
 
- Protected Attributes inherited from RecordableBase
std::string basicDataType_
 the basic data type in the recorded variable
 

Friends

const VectorMatrix operator* (BGFLOAT c, const VectorMatrix &rhs)
 Constant times a vector.
 
const VectorMatrix operator* (const VectorMatrix &v, const SparseMatrix &m)
 Vector times sparse matrix.
 
const VectorMatrix operator- (BGFLOAT c, const VectorMatrix &v)
 Constant minus a vector. Subtracts each element of the vector from a constant.
 
const VectorMatrix operator/ (BGFLOAT c, const VectorMatrix &v)
 Constant divided by a vector. Divides the constant by each element of a vector.
 
const VectorMatrix operator+ (BGFLOAT c, const VectorMatrix &rhs)
 Constant plus a vector. Adds each element of the vector and a constant.
 
const VectorMatrix sqrt (const VectorMatrix &v)
 Element-wise square root of vector. Computes square root of each element of vector.
 
const VectorMatrix exp (const VectorMatrix &v)
 Element-wise e^x for vector. Computes exp(v[i]) of each element of vector.
 

Detailed Description

An efficient implementation of a dynamically-allocated 1D array.

This is a self-allocating and de-allocating 1D array that is optimized for numerical computation. A bit of trial and error went into this. Originally, the idea was to manipulate VectorMatrices using superclass pointers, which would allow generic computation on mixtures of subclass objects. However, that doesn't work too well with numeric computation, because of the need to have "anonymous" intermediate results. So, instead this is implemented as most classes would be, with the hope that compilers will optimize out unnecessary copying of objects that are intermediate results in numerical computations. Note that, while this class keeps track of the number of rows and columns it has, no distinction is made between row and column vectors, and in fact it is treated as either, depending on the context of the mathematical operation.

Definition at line 56 of file VectorMatrix.h.

Constructor & Destructor Documentation

◆ VectorMatrix() [1/2]

VectorMatrix::VectorMatrix ( string t = "complete",
string i = "const",
int r = 1,
int c = 1,
BGFLOAT m = 0.0,
string values = "" )

Allocate storage and initialize attributes. Either "rows" or "columns" must be equal to 1. If "v" is not empty, it will be used as a source of data for initializing the vector (and must be a list of whitespace separated textual numeric data with the same number of elements as this VectorMatrix).

Exceptions
Matrix_bad_alloc
Matrix_invalid_argument
Parameters
tMatrix type
iMatrix initialization
rrows in Matrix
ccolumns in Matrix
mmultiplier used for initialization
vvalues for initializing VectorMatrix

Definition at line 31 of file VectorMatrix.cpp.

◆ VectorMatrix() [2/2]

VectorMatrix::VectorMatrix ( const VectorMatrix & oldV)

Copy constructor. Performs a deep copy.

Parameters
oldVThe source VectorMatrix

Definition at line 79 of file VectorMatrix.cpp.

Member Function Documentation

◆ alloc()

void VectorMatrix::alloc ( int size)
protected

Allocates storage for internal Vector storage.

Exceptions
Matrix_bad_alloc
MatrixException
Parameters
sizenumber of Vector elements

◆ ArrayMultiply()

const VectorMatrix VectorMatrix::ArrayMultiply ( const VectorMatrix & rhs) const
virtual

Element-by-element multiplication of two vectors.

Exceptions
Matrix_domain_error
Parameters
rhsright-hand argument to the vector/matrix product. Must be same size as current vector
Returns
A vector the same size as the current vector

Definition at line 203 of file VectorMatrix.cpp.

◆ at() [1/2]

BGFLOAT & VectorMatrix::at ( int i)
inline

mutate element of a VectorMatrix. Zero-based index.

Parameters
iindex
Returns
reference to item within this VectorMatrix

Definition at line 143 of file VectorMatrix.h.

◆ at() [2/2]

const BGFLOAT & VectorMatrix::at ( int i) const
inline

access element of a VectorMatrix. Zero-based index; constant-time.

Parameters
iindex
Returns
item within this VectorMatrix

Definition at line 113 of file VectorMatrix.h.

◆ copy()

void VectorMatrix::copy ( const VectorMatrix & source)
protected

Performs a deep copy.

Parameters
sourceVectorMatrix to copy from

Definition at line 103 of file VectorMatrix.cpp.

◆ getDataType()

virtual const string & VectorMatrix::getDataType ( ) const
inlineoverridevirtual

Get the run time data type info of the recordable variable.

Implements RecordableBase.

Definition at line 307 of file VectorMatrix.h.

◆ getElement()

virtual variantTypes VectorMatrix::getElement ( int index) const
inlinevirtual

Get the value of the recordable variable at the specified index.

Parameters
indexThe index of the recorded value to retrieve.
Returns
A variant representing the recorded value (uint64_t, double, or string).

Implements RecordableBase.

Definition at line 295 of file VectorMatrix.h.

◆ getNumElements()

virtual int VectorMatrix::getNumElements ( ) const
inlinevirtual

Interface for RecordableBase.

Implements RecordableBase.

Definition at line 281 of file VectorMatrix.h.

◆ Limit()

const VectorMatrix VectorMatrix::Limit ( BGFLOAT low,
BGFLOAT high ) const
virtual

Limit values of a vector. Clip values to lie within range.

Parameters
lowlower limit
highupper limit
Returns
A vector the same size as the current one

Definition at line 268 of file VectorMatrix.cpp.

◆ Max()

BGFLOAT VectorMatrix::Max ( void ) const
virtual

Find maximum value of vector.

Returns
A scalar

Definition at line 296 of file VectorMatrix.cpp.

◆ Min()

BGFLOAT VectorMatrix::Min ( void ) const
virtual

Find minimum value of vector.

Returns
A scalar

Definition at line 284 of file VectorMatrix.cpp.

◆ operator*() [1/3]

const VectorMatrix VectorMatrix::operator* ( BGFLOAT c) const
virtual

Constant times a vector. Multiplies each element of the vector by a constant. Method version (vector on LHS)

Parameters
cThe constant
Returns
A vector the same size as the current one

Definition at line 220 of file VectorMatrix.cpp.

◆ operator*() [2/3]

const VectorMatrix VectorMatrix::operator* ( const CompleteMatrix & rhs) const
virtual

Vector times a Complete Matrix.

Treats this vector as a row vector; multiplies by matrix with same number of rows as size of this vector to produce a new vector the same size as the number of columns of the matrix

Exceptions
Matrix_domain_error
Parameters
rhsright-hand argument to the vector/matrix product
Returns
A vector the same size as the number of columns of rhs

Definition at line 185 of file VectorMatrix.cpp.

◆ operator*() [3/3]

BGFLOAT VectorMatrix::operator* ( const VectorMatrix & rhs) const
virtual

There are two possible vector products. This is an inner product.

Exceptions
Matrix_domain_error
Parameters
rhsright-hand argument to the inner product.
Returns
A scalar of type BGFLOAT

Definition at line 167 of file VectorMatrix.cpp.

◆ operator+() [1/2]

const VectorMatrix VectorMatrix::operator+ ( BGFLOAT c) const
virtual

vector plus a constant. Adds each element of the vector plus a constant. Method version (vector on LHS).

Parameters
cThe constant
Returns
A vector the same size as the current one

Definition at line 155 of file VectorMatrix.cpp.

◆ operator+() [2/2]

const VectorMatrix VectorMatrix::operator+ ( const VectorMatrix & rhs) const
virtual

Compute the sum of two VectorMatrices of the same length.

Exceptions
Matrix_domain_error
Parameters
rhsright-hand argument to the addition. Must be same length as this.
Returns
A new VectorMatrix, with value equal to the sum of this one and rhs and length the same as both, returned by value.

Definition at line 138 of file VectorMatrix.cpp.

◆ operator+=()

const VectorMatrix & VectorMatrix::operator+= ( const VectorMatrix & rhs)
virtual

Compute and assign the sum of two VectorMatrices of the same length.

Exceptions
Matrix_domain_error
Parameters
rhsright-hand argument to the addition. Must be same length as this.
Returns
reference to this

Definition at line 331 of file VectorMatrix.cpp.

◆ operator/()

const VectorMatrix VectorMatrix::operator/ ( BGFLOAT c) const
virtual

Vector divided by a constant. Divides each element of the vector by a constant.

Parameters
cThe constant
Returns
A vector the same size as the current one

Definition at line 232 of file VectorMatrix.cpp.

◆ operator=() [1/2]

const VectorMatrix & VectorMatrix::operator= ( BGFLOAT c)

Set elements of vector to a constant. Doesn't change its size.

Parameters
cthe constant

Definition at line 86 of file VectorMatrix.cpp.

◆ operator=() [2/2]

const VectorMatrix & VectorMatrix::operator= ( const VectorMatrix & rhs)

Assignment operator.

Parameters
rhsright-hand side of assignment
Returns
returns reference to this VectorMatrix (after assignment)

Definition at line 93 of file VectorMatrix.cpp.

◆ operator[]() [1/2]

BGFLOAT & VectorMatrix::operator[] ( int i)
inline

mutate element of a VectorMatrix. Zero-based index; constant time.

Parameters
iindex
Returns
Reference to item within this VectorMatrix

Definition at line 135 of file VectorMatrix.h.

◆ operator[]() [2/2]

const BGFLOAT & VectorMatrix::operator[] ( int i) const
inline

Access element of a VectorMatrix. Zero-based index; constant-time.

Parameters
iindex
Returns
item within this VectorMatrix

Definition at line 105 of file VectorMatrix.h.

◆ Print()

void VectorMatrix::Print ( ostream & os) const
virtual

Polymorphic output. Produces text output on stream "os".

Parameters
osstream to output to

Implements Matrix.

Definition at line 115 of file VectorMatrix.cpp.

◆ serialize()

template<class Archive >
void VectorMatrix::serialize ( Archive & archive)

Cereal serialization method.

Definition at line 354 of file VectorMatrix.h.

◆ setDataType()

virtual void VectorMatrix::setDataType ( )
inlinevirtual

Set up a string representing the basic data type.

Implements RecordableBase.

Definition at line 301 of file VectorMatrix.h.

◆ Size()

int VectorMatrix::Size ( void ) const
inline

The length of a VectorMatrix elements.

Returns
The number of elements in the VectorMatrix

Definition at line 120 of file VectorMatrix.h.

◆ startNewEpoch()

virtual void VectorMatrix::startNewEpoch ( )
inlinevirtual

Start a new epoch for the recordable variable. This method is called at the beginning of each simulation epoch to prepare for recording new events.

Implements RecordableBase.

Definition at line 288 of file VectorMatrix.h.

◆ toXML()

string VectorMatrix::toXML ( string name = "") const
virtual

Produce XML representation of vector in string return value.

Definition at line 122 of file VectorMatrix.cpp.

Friends And Related Symbol Documentation

◆ exp

const VectorMatrix exp ( const VectorMatrix & v)
friend

Element-wise e^x for vector. Computes exp(v[i]) of each element of vector.

Parameters
vThe vector
Returns
A vector the same size as the v

Definition at line 320 of file VectorMatrix.cpp.

◆ operator* [1/2]

const VectorMatrix operator* ( BGFLOAT c,
const VectorMatrix & rhs )
friend

Constant times a vector.

Multiplies each element of the vector by a constant. Function version (constant on LHS)

Parameters
cThe constant
rhsThe vector
Returns
A vector the same size as the rhs

Definition at line 232 of file VectorMatrix.h.

◆ operator* [2/2]

const VectorMatrix operator* ( const VectorMatrix & v,
const SparseMatrix & m )
friend

Vector times sparse matrix.

Size of v must equal to number of rows of m. Size of resultant vector is equal to number Of columns of m.

Exceptions
Matrix_domain_error
Returns
A VectorMatrix with size equal to number of columns of m.

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.

Exceptions
Matrix_domain_error
Returns
A VectorMatrix with size equal to number of columns of m.

Definition at line 608 of file SparseMatrix.cpp.

◆ operator+

const VectorMatrix operator+ ( BGFLOAT c,
const VectorMatrix & rhs )
friend

Constant plus a vector. Adds each element of the vector and a constant.

Parameters
cThe constant
rhsThe vector
Returns
A vector the same size as the rhs

Definition at line 261 of file VectorMatrix.h.

◆ operator-

const VectorMatrix operator- ( BGFLOAT c,
const VectorMatrix & v )
friend

Constant minus a vector. Subtracts each element of the vector from a constant.

Parameters
cThe constant
vThe vector
Returns
A vector the same size as the rhs

Definition at line 244 of file VectorMatrix.cpp.

◆ operator/

const VectorMatrix operator/ ( BGFLOAT c,
const VectorMatrix & v )
friend

Constant divided by a vector. Divides the constant by each element of a vector.

Parameters
cThe constant
vThe vector
Returns
A vector the same size as the rhs

Definition at line 256 of file VectorMatrix.cpp.

◆ sqrt

const VectorMatrix sqrt ( const VectorMatrix & v)
friend

Element-wise square root of vector. Computes square root of each element of vector.

Parameters
vThe vector
Returns
A vector the same size as the v

Definition at line 308 of file VectorMatrix.cpp.

Member Data Documentation

◆ columns

int Matrix::columns
protected

Number of columns in Matrix (>0)

Definition at line 81 of file Matrix.h.

◆ dimensions

int Matrix::dimensions
protected

One or two dimensional.

Definition at line 83 of file Matrix.h.

◆ rows

int Matrix::rows
protected

Number of rows in Matrix (>0)

Definition at line 80 of file Matrix.h.


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