A class for holding a matrix of doubles. More...
#include <DoubleMatrix.h>
Public Member Functions | |
DoubleMatrix () | |
Create an empty matrix. | |
DoubleMatrix (int r, int c, double fixedVal=0) | |
Create a matrix with a given size and fill it with a value. More... | |
DoubleMatrix (std::ifstream &matrixFile) | |
Create a matrix loaded from text file. More... | |
DoubleMatrix (const std::string &h5File, const std::string &path) | |
Create a matrix loaded from an h5 file. More... | |
DoubleMatrix (std::vector< double > v, int matrixRows) | |
Create a matrix given a flat vector of values, reshaped to a matrix with given number of rows. More... | |
DoubleMatrix (const std::vector< std::vector< float >> &matrix) | |
Create a matrix given a 2d array of float values. More... | |
DoubleMatrix (const std::vector< std::vector< double >> &matrix) | |
Create a matrix given a 2d array of double values. More... | |
DoubleMatrix (const std::vector< std::vector< double >> &matrix, int r, int c, double fixedVal=0) | |
Create a matrix with given size, and copy to it given 2d array. More... | |
DoubleMatrix (const boost::numeric::ublas::tensor< double > &t) | |
std::streamoff save (std::ostream &stream) const | |
Save to binary stream. More... | |
std::streamoff load (std::istream &stream) | |
Load from binary stream. More... | |
void load (const std::string &h5File, const std::string &path) | |
Load from an h5 file. More... | |
std::vector< std::vector< double > > getVals () const | |
Returns the matrix values as a 2d array. | |
boost::numeric::ublas::tensor< double > getTensor () const | |
double get (int i, int j) const | |
Returns the value at i,j. More... | |
double set (int i, int j, double val) | |
Sets the value at i,j. More... | |
int rows () const | |
Returns number of rows. | |
int cols () const | |
Returns number of columns. | |
DoubleMatrix & operator-= (const DoubleMatrix &other) | |
Subtracts other matrix elementwise. More... | |
DoubleMatrix & operator+= (const DoubleMatrix &other) | |
Adds other matrix elementwise. More... | |
void addAt (double val, int i, int j) | |
Adds a value at position i,j. More... | |
void addAt (const DoubleMatrix &other, int startRow, int startCol) | |
DoubleMatrix getTranspose () const | |
Returns a copy of the matrix transposed. | |
DoubleMatrix getSubMatrix (int row1, int col1, int row2, int col2) const | |
Returns a sub matrix (row1,col1)-(row2,col2), with exlusive end indices. More... | |
void transpose () | |
Transpose this matrix in place. | |
void testEqual (const std::string &title, const DoubleMatrix &other, double tolerance=1e-6) const | |
Asserts equality with another matrix up to given tolerance. More... | |
void testSameSize (const std::string &title, const DoubleMatrix &other) const | |
Asserts dimension equality with another matrix. More... | |
bool checkIfEqual (const DoubleMatrix &other, double tolerance=1e-6) const | |
Returns a boolean specifying whether all the elements in this DoubleMatrix are equal to the elements of "other", up to difference which is less than "tolerance". More... | |
bool checkIfSameSize (const DoubleMatrix &other) const | |
Returns a boolean specifying whether this DoubleMatrix has the same dimensions as the dimensions of "other". More... | |
void copy (const DoubleMatrix &other) | |
Verifies other has same size, and copies its values. More... | |
void init (const std::vector< std::vector< float >> matrix) | |
Initializes a matrix based on given 2d array of floats. More... | |
void init (const std::vector< std::vector< double >> matrix) | |
Initializes a matrix based on given 2d array of doubles. More... | |
void initRandom () | |
Fills the matrix with random values. More... | |
void initConst (double v) | |
Fills the matrix with a constant value. More... | |
double getMaxDiff (const DoubleMatrix &other) const | |
Returns the maximal absolute diff with other matrix. More... | |
double getMaxAbs () const | |
Returns maximal absolute value. | |
double getMaxRelDiff (const DoubleMatrix &other, double tolerance) const | |
Computes the maximal relative difference with other. More... | |
void flattenIntoVec (std::vector< double > &vals) const | |
Fills given vector with matrix values in row major order. More... | |
std::vector< double > getFlatten () const | |
Returns a vector filled with matrix values in row major order. | |
DoubleMatrix getMultiply (const DoubleMatrix &other) const | |
Returns the result of matrix-multiplication between this and other. More... | |
void multiply (const DoubleMatrix &other) | |
Computes matrix-multiplication between this and other. More... | |
void elementMultiply (const DoubleMatrix &other) | |
Computes elementwise multiplication between this and other. More... | |
void elementMultiplyAt (const DoubleMatrix &other, int startRow, int startCol) | |
Computes elementwise multiplication between this and other at a specified window. More... | |
void multiplyByScalar (double scalar) | |
Scalar multiplication. More... | |
void meanAlongRows () | |
Reduces the rows to a single row by calculating the mean of each column. | |
void meanAlongCols () | |
Reduces the columns to a single column by calculating the mean of each row. | |
void sumAlongRows () | |
Reduces the rows to a single row by calculating the sum of each column. | |
void sumAlongCols () | |
Reduces the columns to a single column by calculating the sum of each row. | |
DoubleMatrix getDuplicate (int dupRows, int dupCols) const | |
Returns a copy of the matrix with each element duplicated along rows and columns. More... | |
void unFlatten (const std::vector< double > &v) | |
Overwrites existing matrix with values from flat vector v in row major order. More... | |
int size () const | |
Returns number of elements in matrix. | |
DoubleMatrix getConvolution (const DoubleMatrix &filter, int strideRows, int strideCols) const | |
Returns the convolution result between this matrix and given filter. More... | |
DoubleMatrix getMeanAlongRows () const | |
Returns a copy of the matrix with the rows reduced to a single row by calculating the mean of each column. | |
DoubleMatrix getMeanAlongCols () const | |
Returns a copy of the matrix with the columns reduced to a single column by calculating the mean of each row. | |
DoubleMatrix getSumAlongRows () const | |
Returns a copy of the matrix with the rows reduced to a single row by calculating the sum of each column. | |
DoubleMatrix getSumAlongCols () const | |
Returns a copy of the matrix with the columns reduced to a single column by calculating the sum of each row. | |
void debugPrint (std::ostream &out, int verboseLevel) const | |
Prints matrix. More... | |
void appendRow (const std::vector< double > &vec) | |
Appends a row to the matrix. More... | |
void appendCols (const DoubleMatrix &other) | |
Appends new columns to the matrix. More... | |
void appendRows (const DoubleMatrix &other) | |
Appends new rows to the matrix. More... | |
void clear () | |
Sets the matrix to a 0 by 0 empty matrix. | |
int argmax () const | |
Returns the index of the maximal value. More... | |
Static Public Member Functions | |
static std::string niceFormat (double v) | |
Returns a nice string representation of given number. More... | |
Detailed Description
A class for holding a matrix of doubles.
Constructor & Destructor Documentation
◆ DoubleMatrix() [1/7]
helayers::DoubleMatrix::DoubleMatrix | ( | int | r, |
int | c, | ||
double | fixedVal = 0 |
||
) |
Create a matrix with a given size and fill it with a value.
- Parameters
-
[in] r rows [in] c cols [in] fixedVal value to fill
◆ DoubleMatrix() [2/7]
helayers::DoubleMatrix::DoubleMatrix | ( | std::ifstream & | matrixFile | ) |
Create a matrix loaded from text file.
- Parameters
-
[in] matrixFile text file name
◆ DoubleMatrix() [3/7]
helayers::DoubleMatrix::DoubleMatrix | ( | const std::string & | h5File, |
const std::string & | path | ||
) |
Create a matrix loaded from an h5 file.
- Parameters
-
[in] h5File h5 file name [in] path path inside file
◆ DoubleMatrix() [4/7]
helayers::DoubleMatrix::DoubleMatrix | ( | std::vector< double > | v, |
int | matrixRows | ||
) |
Create a matrix given a flat vector of values, reshaped to a matrix with given number of rows.
The flat vector is flattened in row-major order.
- Parameters
-
[in] v flat vector of values. [in] matrixRows number of rows.
◆ DoubleMatrix() [5/7]
helayers::DoubleMatrix::DoubleMatrix | ( | const std::vector< std::vector< float >> & | matrix | ) |
Create a matrix given a 2d array of float values.
First dimension is rows.
- Parameters
-
[in] matrix 2d array of values.
◆ DoubleMatrix() [6/7]
helayers::DoubleMatrix::DoubleMatrix | ( | const std::vector< std::vector< double >> & | matrix | ) |
Create a matrix given a 2d array of double values.
First dimension is rows.
- Parameters
-
[in] matrix 2d array of values.
◆ DoubleMatrix() [7/7]
helayers::DoubleMatrix::DoubleMatrix | ( | const std::vector< std::vector< double >> & | matrix, |
int | r, | ||
int | c, | ||
double | fixedVal = 0 |
||
) |
Create a matrix with given size, and copy to it given 2d array.
The array is assumed to be smaller than matrix.
- Parameters
-
[in] matrix 2d array of values to copy. [in] r number of rows to create. [in] c number of columns to create. [in] fixedVal initial values to fill the matrix with.
Member Function Documentation
◆ addAt() [1/2]
void helayers::DoubleMatrix::addAt | ( | const DoubleMatrix & | other, |
int | startRow, | ||
int | startCol | ||
) |
- Parameters
-
[in] other matrix to add [in] startRow row index where other is placed [in] startCol column index where other is placed
- Exceptions
-
invalid_argument if other matrix dimensions exceed this matrix
◆ addAt() [2/2]
|
inline |
Adds a value at position i,j.
- Parameters
-
[in] i row index [in] j column index [in] val value to add
◆ appendCols()
void helayers::DoubleMatrix::appendCols | ( | const DoubleMatrix & | other | ) |
Appends new columns to the matrix.
- Parameters
-
[in] other Values to add
- Exceptions
-
invalid_argument If matrix is not empty and dimensions mismatch.
◆ appendRow()
void helayers::DoubleMatrix::appendRow | ( | const std::vector< double > & | vec | ) |
Appends a row to the matrix.
- Parameters
-
[in] vec Values of row to add
- Exceptions
-
invalid_argument If matrix is not empty and dimensions mismatch.
◆ appendRows()
void helayers::DoubleMatrix::appendRows | ( | const DoubleMatrix & | other | ) |
Appends new rows to the matrix.
- Parameters
-
[in] other Values to add
- Exceptions
-
invalid_argument If matrix is not empty and dimensions mismatch.
◆ argmax()
int helayers::DoubleMatrix::argmax | ( | ) | const |
Returns the index of the maximal value.
Supported only for single row or single column matrices.
- Exceptions
-
invalid_argument If matrix is empty or not a single row or single column.
◆ checkIfEqual()
bool helayers::DoubleMatrix::checkIfEqual | ( | const DoubleMatrix & | other, |
double | tolerance = 1e-6 |
||
) | const |
Returns a boolean specifying whether all the elements in this DoubleMatrix are equal to the elements of "other", up to difference which is less than "tolerance".
- Parameters
-
[in] other matrix to compare with [in] tolerance maximal allowed diff
◆ checkIfSameSize()
bool helayers::DoubleMatrix::checkIfSameSize | ( | const DoubleMatrix & | other | ) | const |
Returns a boolean specifying whether this DoubleMatrix has the same dimensions as the dimensions of "other".
- Parameters
-
[in] other matrix to compare with
◆ copy()
void helayers::DoubleMatrix::copy | ( | const DoubleMatrix & | other | ) |
Verifies other has same size, and copies its values.
- Parameters
-
[in] other matrix to copy from
◆ debugPrint()
void helayers::DoubleMatrix::debugPrint | ( | std::ostream & | out, |
int | verboseLevel | ||
) | const |
Prints matrix.
- Parameters
-
[in] out Stream to print to [in] verboseLevel Verbosity level
◆ elementMultiply()
void helayers::DoubleMatrix::elementMultiply | ( | const DoubleMatrix & | other | ) |
Computes elementwise multiplication between this and other.
Result is stored in place.
- Parameters
-
[in] other matrix to multiply with
◆ elementMultiplyAt()
void helayers::DoubleMatrix::elementMultiplyAt | ( | const DoubleMatrix & | other, |
int | startRow, | ||
int | startCol | ||
) |
Computes elementwise multiplication between this and other at a specified window.
Result is stored in place. The other matrix is placed and multiplied against a specified location in this matrix, and is expected not to extend beyond this matrix edges.
- Parameters
-
[in] other matrix to multiply with [in] startRow row index in this matrix to start multipling [in] startCol column index in this matrix to start multipling
◆ flattenIntoVec()
void helayers::DoubleMatrix::flattenIntoVec | ( | std::vector< double > & | vals | ) | const |
Fills given vector with matrix values in row major order.
- Parameters
-
[out] vals resulting values.
◆ get()
|
inline |
Returns the value at i,j.
- Parameters
-
[in] i row index [in] j column index
◆ getConvolution()
DoubleMatrix helayers::DoubleMatrix::getConvolution | ( | const DoubleMatrix & | filter, |
int | strideRows, | ||
int | strideCols | ||
) | const |
Returns the convolution result between this matrix and given filter.
- Parameters
-
[in] filter A filter for convolution [in] strideRows Stride along row dimension [in] strideCols Stride along col dimension
◆ getDuplicate()
DoubleMatrix helayers::DoubleMatrix::getDuplicate | ( | int | dupRows, |
int | dupCols | ||
) | const |
Returns a copy of the matrix with each element duplicated along rows and columns.
- Parameters
-
[in] dupRows how many times to duplicate along rows [in] dupCols how many times to duplicate along cols
◆ getMaxDiff()
double helayers::DoubleMatrix::getMaxDiff | ( | const DoubleMatrix & | other | ) | const |
Returns the maximal absolute diff with other matrix.
- Parameters
-
[in] other matrix to compare with
◆ getMaxRelDiff()
double helayers::DoubleMatrix::getMaxRelDiff | ( | const DoubleMatrix & | other, |
double | tolerance | ||
) | const |
Computes the maximal relative difference with other.
For each pair of compared values v1 and v2: if max(abs(v1),abs(v2))<10*tolerance, then the diff is abs(v1-v2) otherwise, abs(v1-v2)/max(abs(v1),abs(v2))
- Parameters
-
[in] other matrix to compare with [in] tolerance controls when to measure relative diff
◆ getMultiply()
DoubleMatrix helayers::DoubleMatrix::getMultiply | ( | const DoubleMatrix & | other | ) | const |
Returns the result of matrix-multiplication between this and other.
- Parameters
-
[in] other matrix to multiply with
◆ getSubMatrix()
DoubleMatrix helayers::DoubleMatrix::getSubMatrix | ( | int | row1, |
int | col1, | ||
int | row2, | ||
int | col2 | ||
) | const |
Returns a sub matrix (row1,col1)-(row2,col2), with exlusive end indices.
- Parameters
-
[in] row1 First row index [in] col1 First column index [in] row2 Last row index (exclusive) [in] col2 Last column index (exclusive)
◆ init() [1/2]
void helayers::DoubleMatrix::init | ( | const std::vector< std::vector< double >> | matrix | ) |
Initializes a matrix based on given 2d array of doubles.
Overwrites any existing state.
- Parameters
-
[in] matrix 2d array defining the matrix.
◆ init() [2/2]
void helayers::DoubleMatrix::init | ( | const std::vector< std::vector< float >> | matrix | ) |
Initializes a matrix based on given 2d array of floats.
Overwrites any existing state.
- Parameters
-
[in] matrix 2d array defining the matrix
◆ initConst()
void helayers::DoubleMatrix::initConst | ( | double | v | ) |
Fills the matrix with a constant value.
Keeps current dimensions.
- Parameters
-
[in] v value to fill
◆ initRandom()
void helayers::DoubleMatrix::initRandom | ( | ) |
Fills the matrix with random values.
Keeps current dimensions.
◆ load() [1/2]
void helayers::DoubleMatrix::load | ( | const std::string & | h5File, |
const std::string & | path | ||
) |
Load from an h5 file.
- Parameters
-
[in] h5File file name [in] path path inside file
◆ load() [2/2]
std::streamoff helayers::DoubleMatrix::load | ( | std::istream & | stream | ) |
Load from binary stream.
- Parameters
-
[in] stream binary stream
◆ multiply()
void helayers::DoubleMatrix::multiply | ( | const DoubleMatrix & | other | ) |
Computes matrix-multiplication between this and other.
Result is stored in place.
- Parameters
-
[in] other matrix to multiply with
◆ multiplyByScalar()
void helayers::DoubleMatrix::multiplyByScalar | ( | double | scalar | ) |
Scalar multiplication.
- Parameters
-
[in] scalar scalar to multiply with
◆ niceFormat()
|
static |
Returns a nice string representation of given number.
- Parameters
-
[in] v the number
◆ operator+=()
DoubleMatrix & helayers::DoubleMatrix::operator+= | ( | const DoubleMatrix & | other | ) |
Adds other matrix elementwise.
- Parameters
-
[in] other matrix to add
- Exceptions
-
invalid_argument if dimensions mismatch
◆ operator-=()
DoubleMatrix & helayers::DoubleMatrix::operator-= | ( | const DoubleMatrix & | other | ) |
Subtracts other matrix elementwise.
- Parameters
-
[in] other matrix to subtract
- Exceptions
-
invalid_argument if dimensions mismatch
◆ save()
streamoff helayers::DoubleMatrix::save | ( | std::ostream & | stream | ) | const |
Save to binary stream.
- Parameters
-
[in] stream binary stream
◆ set()
|
inline |
Sets the value at i,j.
- Parameters
-
[in] i row index [in] j column index [in] val value to set
◆ testEqual()
void helayers::DoubleMatrix::testEqual | ( | const std::string & | title, |
const DoubleMatrix & | other, | ||
double | tolerance = 1e-6 |
||
) | const |
Asserts equality with another matrix up to given tolerance.
Prints maximal difference in either case.
- Parameters
-
[in] title Title to print along side maximal diff. [in] other matrix to compare with. [in] tolerance maximal tolerance
- Exceptions
-
invalid_argument if other dimensions mismatch runtime_error if matrices differ in values
◆ testSameSize()
void helayers::DoubleMatrix::testSameSize | ( | const std::string & | title, |
const DoubleMatrix & | other | ||
) | const |
Asserts dimension equality with another matrix.
- Parameters
-
[in] title Title to print in case of error [in] other matrix to compare with
- Exceptions
-
invalid_argument if other dimensions mismatch
◆ unFlatten()
void helayers::DoubleMatrix::unFlatten | ( | const std::vector< double > & | v | ) |
Overwrites existing matrix with values from flat vector v in row major order.
Dimensions remain the same.
- Parameters
-
[in] v vector of values to read from
The documentation for this class was generated from the following files:
- /opt/IBM/FHE-distro/ML-HElib/src/helayers/simple_nn/DoubleMatrix.h
- /opt/IBM/FHE-distro/ML-HElib/src/helayers/simple_nn/DoubleMatrix.cpp