Search Results

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.
 
DoubleMatrixoperator-= (const DoubleMatrix &other)
Subtracts other matrix elementwise. More...
 
DoubleMatrixoperator+= (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]rrows
[in]ccols
[in]fixedValvalue to fill

◆ DoubleMatrix() [2/7]

helayers::DoubleMatrix::DoubleMatrix ( std::ifstream &  matrixFile)

Create a matrix loaded from text file.

Parameters
[in]matrixFiletext 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]h5Fileh5 file name
[in]pathpath 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]vflat vector of values.
[in]matrixRowsnumber 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]matrix2d 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]matrix2d 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]matrix2d array of values to copy.
[in]rnumber of rows to create.
[in]cnumber of columns to create.
[in]fixedValinitial 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]othermatrix to add
[in]startRowrow index where other is placed
[in]startColcolumn index where other is placed
Exceptions
invalid_argumentif other matrix dimensions exceed this matrix

◆ addAt() [2/2]

void helayers::DoubleMatrix::addAt ( double  val,
int  i,
int  j 
)
inline

Adds a value at position i,j.

Parameters
[in]irow index
[in]jcolumn index
[in]valvalue to add

◆ appendCols()

void helayers::DoubleMatrix::appendCols ( const DoubleMatrix other)

Appends new columns to the matrix.

Parameters
[in]otherValues to add
Exceptions
invalid_argumentIf 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]vecValues of row to add
Exceptions
invalid_argumentIf matrix is not empty and dimensions mismatch.

◆ appendRows()

void helayers::DoubleMatrix::appendRows ( const DoubleMatrix other)

Appends new rows to the matrix.

Parameters
[in]otherValues to add
Exceptions
invalid_argumentIf 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_argumentIf 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]othermatrix to compare with
[in]tolerancemaximal 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]othermatrix to compare with

◆ copy()

void helayers::DoubleMatrix::copy ( const DoubleMatrix other)

Verifies other has same size, and copies its values.

Parameters
[in]othermatrix to copy from

◆ debugPrint()

void helayers::DoubleMatrix::debugPrint ( std::ostream &  out,
int  verboseLevel 
) const

Prints matrix.

Parameters
[in]outStream to print to
[in]verboseLevelVerbosity level

◆ elementMultiply()

void helayers::DoubleMatrix::elementMultiply ( const DoubleMatrix other)

Computes elementwise multiplication between this and other.

Result is stored in place.

Parameters
[in]othermatrix 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]othermatrix to multiply with
[in]startRowrow index in this matrix to start multipling
[in]startColcolumn 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]valsresulting values.

◆ get()

double helayers::DoubleMatrix::get ( int  i,
int  j 
) const
inline

Returns the value at i,j.

Parameters
[in]irow index
[in]jcolumn 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]filterA filter for convolution
[in]strideRowsStride along row dimension
[in]strideColsStride 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]dupRowshow many times to duplicate along rows
[in]dupColshow 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]othermatrix 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]othermatrix to compare with
[in]tolerancecontrols 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]othermatrix 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]row1First row index
[in]col1First column index
[in]row2Last row index (exclusive)
[in]col2Last 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]matrix2d 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]matrix2d array defining the matrix

◆ initConst()

void helayers::DoubleMatrix::initConst ( double  v)

Fills the matrix with a constant value.

Keeps current dimensions.

Parameters
[in]vvalue 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]h5Filefile name
[in]pathpath inside file

◆ load() [2/2]

std::streamoff helayers::DoubleMatrix::load ( std::istream &  stream)

Load from binary stream.

Parameters
[in]streambinary stream

◆ multiply()

void helayers::DoubleMatrix::multiply ( const DoubleMatrix other)

Computes matrix-multiplication between this and other.

Result is stored in place.

Parameters
[in]othermatrix to multiply with

◆ multiplyByScalar()

void helayers::DoubleMatrix::multiplyByScalar ( double  scalar)

Scalar multiplication.

Parameters
[in]scalarscalar to multiply with

◆ niceFormat()

string helayers::DoubleMatrix::niceFormat ( double  v)
static

Returns a nice string representation of given number.

Parameters
[in]vthe number

◆ operator+=()

DoubleMatrix & helayers::DoubleMatrix::operator+= ( const DoubleMatrix other)

Adds other matrix elementwise.

Parameters
[in]othermatrix to add
Exceptions
invalid_argumentif dimensions mismatch

◆ operator-=()

DoubleMatrix & helayers::DoubleMatrix::operator-= ( const DoubleMatrix other)

Subtracts other matrix elementwise.

Parameters
[in]othermatrix to subtract
Exceptions
invalid_argumentif dimensions mismatch

◆ save()

streamoff helayers::DoubleMatrix::save ( std::ostream &  stream) const

Save to binary stream.

Parameters
[in]streambinary stream

◆ set()

double helayers::DoubleMatrix::set ( int  i,
int  j,
double  val 
)
inline

Sets the value at i,j.

Parameters
[in]irow index
[in]jcolumn index
[in]valvalue 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]titleTitle to print along side maximal diff.
[in]othermatrix to compare with.
[in]tolerancemaximal tolerance
Exceptions
invalid_argumentif other dimensions mismatch
runtime_errorif 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]titleTitle to print in case of error
[in]othermatrix to compare with
Exceptions
invalid_argumentif 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]vvector 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