CNum 0.2.1
CPU-optimized ML library for C++
Loading...
Searching...
No Matches
CNum::DataStructs::Matrix< T > Class Template Reference

2d array abstraction More...

#include <Matrix.h>

Public Member Functions

 Matrix (size_t rows=0, size_t cols=0, ::std::unique_ptr< T[]> ptr=nullptr)
 Default Overloaded Constructor.
 Matrix (const Matrix &other) noexcept
 Copy Constructor.
Matrix< T > & operator= (const Matrix &other) noexcept
 Copy Logic.
 Matrix (Matrix &&other) noexcept
 Move Constructor.
Matrix< T > & operator= (Matrix &&other) noexcept
 Move Assignment.
 ~Matrix ()
 Destructor.
Matrix< T > operator* (const Matrix &other) const
 Dot Product.
Matrix< T > operator* (T scale_factor) const noexcept
 Scale a matrix.
dot (const Matrix< T > &other) const
 Vector dot product (1d).
Matrix< T > operator+ (const Matrix &other) const
 Add two matrices element wise.
Matrix< T > operator+ (T a) const noexcept
 Add a value to every element in a matrix.
Matrix< T > operator- (const Matrix &other) const
 Subtract two matrices element wise.
Matrix< T > operator- (T a) const noexcept
 Subtract a value to every element in a matrix.
Matrix< T > abs () const
 Take the absolute value of all elements in a matrix.
Matrix< T > squared () const
 Square all elements in a matrix.
Matrix< T > standardize () const
 Standardize Matrix.
sum () const
 Get the sum of all elements in a matrix.
mean () const
 Get the mean of all values in a matrix.
std () const
 Get the standard deviation of all elements in a matrix.
get (size_t row, size_t col) const
 Get value of a matrix.
Matrix< T > get (Dim d, size_t idx) const
 Get a copy of a Row/Col (prefer views for memory effeciency).
CNum::DataStructs::Views::StrideView< T > get_col_view (size_t idx) const
 Get a column view.
::std::span< T > get_row_view (size_t idx) const
 Get a row view.
operator[] (size_t idx) const
 Get the value at index idx of a Matrix with shape=(n,1).
Matrix< T > operator[] (const BinaryMask &bin_mask) const
 Apply a binary mask.
Matrix< T > operator[] (const IndexMask &idx_mask) const noexcept
 Apply index mask.
Matrix< T > col_wise_mask_application (const IndexMask &idx_mask) const noexcept
 Apply IndexMask column wise.
BinaryMask operator< (T val) const
 Create a binary mask of values less than another.
BinaryMask operator<= (T val) const
 Create a binary mask of values less than or equal to another.
BinaryMask operator> (T val) const
 Create a binary mask of values greater than another.
BinaryMask operator>= (T val) const
 Create a binary mask of values greater than or equal to another.
BinaryMask operator== (T val) const
 Create a binary mask of values equal to another.
BinaryMask operator!= (T val) const
 Create a binary mask of values not equal to another.
IndexMask argsort (bool descending=false) const
 Argsort.
Matrix< T > transpose () const noexcept
 Transpose a matrix.
size_t get_rows () const
 Get the number of rows in a matrix.
size_t get_cols () const
 Get the number of rows in a matrix.
const T * begin () const
 Get an iterator (pointer) to the beginning of a matrix.
const T * end () const
 Get a const iterator (pointer) to the end of a matrix.
T * begin ()
 Get an iterator (pointer) to the beginning of a matrix.
T * end ()
 Get an iterator (pointer) to the end of a matrix.
size_t size () const
 Get the number of rows in a Matrix (helpful for stl algorithms).
::std::unique_ptr< T[]> && move_ptr ()
 Relinquish ownership of the unique pointer with the matrix data.
void print_matrix () const
 Print a matrix.

Static Public Member Functions

static Matrix< T > init_const (size_t rows, size_t cols, T val)
 Initialize a matrix with a constant value in each element.
static Matrix< T > identity (size_t dim)
 Get an identity matrix.
static Matrix< T > join_cols (::std::vector< Matrix< T > > &cols)
 Join a list of column matrices.
static Matrix< T > combine_vertically (::std::vector< Matrix< T > > &matrices, size_t total_rows)
 Combine a list of row matrices.

Detailed Description

template<typename T>
class CNum::DataStructs::Matrix< T >

2d array abstraction

Used for storing 2d, tabular data. Used in conjuction with CNum ML models and linear algebra operations

Template Parameters
TThe type of the data stored

Constructor & Destructor Documentation

◆ Matrix() [1/3]

template<typename T>
CNum::DataStructs::Matrix< T >::Matrix ( size_t rows = 0,
size_t cols = 0,
::std::unique_ptr< T[]> ptr = nullptr )

Default Overloaded Constructor.

Parameters
rowsNumber of rows in the matrix
colsNumber of columns in the matrix
ptrThe unique pointer containing the matrix data

◆ Matrix() [2/3]

template<typename T>
CNum::DataStructs::Matrix< T >::Matrix ( const Matrix< T > & other)
noexcept

Copy Constructor.

◆ Matrix() [3/3]

template<typename T>
CNum::DataStructs::Matrix< T >::Matrix ( Matrix< T > && other)
noexcept

Move Constructor.

◆ ~Matrix()

template<typename T>
CNum::DataStructs::Matrix< T >::~Matrix ( )

Destructor.

Member Function Documentation

◆ abs()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::abs ( ) const

Take the absolute value of all elements in a matrix.

Returns
The matrix with all non-negative values

◆ argsort()

template<typename T>
IndexMask CNum::DataStructs::Matrix< T >::argsort ( bool descending = false) const

Argsort.

Parameters
descendingWhether or not to sort in descending order
Returns
An index mask with the sorting order

◆ begin() [1/2]

template<typename T>
T * CNum::DataStructs::Matrix< T >::begin ( )

Get an iterator (pointer) to the beginning of a matrix.

Returns
Raw pointer

◆ begin() [2/2]

template<typename T>
const T * CNum::DataStructs::Matrix< T >::begin ( ) const

Get an iterator (pointer) to the beginning of a matrix.

Returns
Raw pointer

◆ col_wise_mask_application()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::col_wise_mask_application ( const IndexMask & idx_mask) const
noexcept

Apply IndexMask column wise.

Parameters
idx_maskThe mask containing the column indeces to preserve
Returns
The masked matrix

◆ combine_vertically()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::combine_vertically ( ::std::vector< Matrix< T > > & matrices,
size_t total_rows )
static

Combine a list of row matrices.

Parameters
Thelist of row matrices
Returns
The merged matrix

◆ dot()

template<typename T>
T CNum::DataStructs::Matrix< T >::dot ( const Matrix< T > & other) const

Vector dot product (1d).

Parameters
otherThe other vector with which to perform the dot product (shape=(n, 1))
Returns
The result of the dot product (single value)

◆ end() [1/2]

template<typename T>
T * CNum::DataStructs::Matrix< T >::end ( )

Get an iterator (pointer) to the end of a matrix.

Returns
Raw pointer

◆ end() [2/2]

template<typename T>
const T * CNum::DataStructs::Matrix< T >::end ( ) const

Get a const iterator (pointer) to the end of a matrix.

Returns
Raw pointer

◆ get() [1/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::get ( Dim d,
size_t idx ) const

Get a copy of a Row/Col (prefer views for memory effeciency).

Parameters
deither ROW or COL
idxthe index of the row/col
Returns
The row or column

◆ get() [2/2]

template<typename T>
T CNum::DataStructs::Matrix< T >::get ( size_t row,
size_t col ) const

Get value of a matrix.

Parameters
rowThe row of the value
colThe column of the value
Returns
The value of matrix[i][j]

◆ get_col_view()

template<typename T>
CNum::DataStructs::Views::StrideView< T > CNum::DataStructs::Matrix< T >::get_col_view ( size_t idx) const

Get a column view.

Parameters
idxThe index of the column
Returns
The view

◆ get_cols()

template<typename T>
size_t CNum::DataStructs::Matrix< T >::get_cols ( ) const

Get the number of rows in a matrix.

◆ get_row_view()

template<typename T>
std::span< T > CNum::DataStructs::Matrix< T >::get_row_view ( size_t idx) const

Get a row view.

Parameters
idxThe index of the row
Returns
The view

◆ get_rows()

template<typename T>
size_t CNum::DataStructs::Matrix< T >::get_rows ( ) const

Get the number of rows in a matrix.

Returns
The number of rows

◆ identity()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::identity ( size_t dim)
static

Get an identity matrix.

Parameters
dimThe dimensionality of the identity matrix
Returns
Identity matrix

◆ init_const()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::init_const ( size_t rows,
size_t cols,
T val )
static

Initialize a matrix with a constant value in each element.

Parameters
rowsAmount of rows in the matrix
colsAmount of columns in the matrix
Returns
The matrix

◆ join_cols()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::join_cols ( ::std::vector< Matrix< T > > & cols)
static

Join a list of column matrices.

Parameters
colsThe list of column matrices
Returns
The merged matrix

◆ mean()

template<typename T>
T CNum::DataStructs::Matrix< T >::mean ( ) const

Get the mean of all values in a matrix.

Returns
The mean

◆ move_ptr()

template<typename T>
std::unique_ptr< T[]> && CNum::DataStructs::Matrix< T >::move_ptr ( )

Relinquish ownership of the unique pointer with the matrix data.

Returns
The unique pointer with the matrix data

◆ operator!=()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator!= ( T val) const

Create a binary mask of values not equal to another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator*() [1/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator* ( const Matrix< T > & other) const

Dot Product.

Parameters
otherAnother matrix with which to perform a dot product
Returns
The result of the dot product

◆ operator*() [2/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator* ( T scale_factor) const
noexcept

Scale a matrix.

Parameters
scale_factorThe factor with which to scale the matrix

◆ operator+() [1/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator+ ( const Matrix< T > & other) const

Add two matrices element wise.

Parameters
otherThe matrix to add
Returns
The resultant matrix

◆ operator+() [2/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator+ ( T a) const
noexcept

Add a value to every element in a matrix.

Parameters
aThe value to add
Returns
The resultant matrix

◆ operator-() [1/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator- ( const Matrix< T > & other) const

Subtract two matrices element wise.

Parameters
otherThe matrix to subtract
Returns
The resultant matrix

◆ operator-() [2/2]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator- ( T a) const
noexcept

Subtract a value to every element in a matrix.

Parameters
aThe element to subtract
Returns
The resultant matrix

◆ operator<()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator< ( T val) const

Create a binary mask of values less than another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator<=()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator<= ( T val) const

Create a binary mask of values less than or equal to another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator=() [1/2]

template<typename T>
Matrix< T > & CNum::DataStructs::Matrix< T >::operator= ( const Matrix< T > & other)
noexcept

Copy Logic.

◆ operator=() [2/2]

template<typename T>
Matrix< T > & CNum::DataStructs::Matrix< T >::operator= ( Matrix< T > && other)
noexcept

Move Assignment.

◆ operator==()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator== ( T val) const

Create a binary mask of values equal to another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator>()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator> ( T val) const

Create a binary mask of values greater than another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator>=()

template<typename T>
BinaryMask CNum::DataStructs::Matrix< T >::operator>= ( T val) const

Create a binary mask of values greater than or equal to another.

Parameters
valThe value with which to compare the elements of the Matrix to
Returns
The resultant binary mask

◆ operator[]() [1/3]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator[] ( const BinaryMask & bin_mask) const

Apply a binary mask.

Parameters
mThe mask to apply
Returns
The masked matrix

◆ operator[]() [2/3]

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::operator[] ( const IndexMask & idx_mask) const
noexcept

Apply index mask.

Parameters
idx_maskThe index mask to apply
Returns
The resultant matrix

◆ operator[]() [3/3]

template<typename T>
T CNum::DataStructs::Matrix< T >::operator[] ( size_t idx) const

Get the value at index idx of a Matrix with shape=(n,1).

Parameters
idxThe index of the value
Returns
The value at idx

◆ print_matrix()

template<typename T>
void CNum::DataStructs::Matrix< T >::print_matrix ( ) const

Print a matrix.

◆ size()

template<typename T>
size_t CNum::DataStructs::Matrix< T >::size ( ) const

Get the number of rows in a Matrix (helpful for stl algorithms).

Returns
The number of rows in the Matrix

◆ squared()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::squared ( ) const

Square all elements in a matrix.

Returns
The matrix with all squared values

◆ standardize()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::standardize ( ) const

Standardize Matrix.

Returns
The standardized matrix

◆ std()

template<typename T>
T CNum::DataStructs::Matrix< T >::std ( ) const

Get the standard deviation of all elements in a matrix.

Returns
The standard deviation

◆ sum()

template<typename T>
T CNum::DataStructs::Matrix< T >::sum ( ) const

Get the sum of all elements in a matrix.

Returns
The sum

◆ transpose()

template<typename T>
Matrix< T > CNum::DataStructs::Matrix< T >::transpose ( ) const
noexcept

Transpose a matrix.

Returns
Transposed matrix

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