Simple C++ implementation of complex numbers matrix using vector of vectors from std.
The purpose of this project is to create Matrix
class that will be helpful in future projects.
Matrix is presented via std::vector<>
of std::vector<>
of std::complex<>
of double
All of Matrix
class fields are private
rows
- number of rows in matrix
unsigned rows;
cols
- number of columns in matrix
unsigned cols;
mat
- matrix of elements (rows
xcols
sized)
std::vector<std::vector<std::complex<double>>> mat;
- The Constructor receiving the number of
rows
,cols
and initial value for elements.
By default have values0
,0
and0+0i
respectively.
Matrix(unsigned = 0, unsigned = 0, std::complex<double> = 0.0 + 0.0i);
- The constructor receiving a vector that represents a matrix.
Vector should be square.
Matrix(std::vector<std::vector<std::complex<double>>>*);
- The constructor receiving the number of
rows
,cols
, vector that represents a matrix and initial value for elements.
Makesrows
xcols
sized matrix, fills it elementwise from given vector.
If there are not enough elements in the given vector, then fills remaining elements with initial value.
Matrix(unsigned, unsigned, std::vector<std::vector<std::complex<double>>>*, std::complex<double> = 0.0 + 0.0i);
- The constructor receiving the number of
rows
,cols
, vector of elements and initial value for elements.
Makesrows
xcols
sized matrix, fills it elementwise from given vector.
If there are not enough elements in the given vector, then fills remaining elements with initial value.
Matrix(unsigned, unsigned, std::vector<std::complex<double>>*, std::complex<double> = 0.0 + 0.0i);
- Copy constructor
Matrix(const Matrix&);
- Destructor
virtual ~Matrix();
- Call operator for non-const objects receiving
row
andcol
.
Returnsmat[row][col]
.
Indices are cycled, so there is no out-of-range error
std::complex<double>& operator () (const unsigned&, const unsigned&);
- Call operator for const objects receiving
row
andcol
.
Returnsmat[row][col]
.
Indices are cycled, so there is no out-of-range error
const std::complex<double>& operator () (const unsigned&, const unsigned&) const;
- Copy assignment operator.
Matrix& operator = (const Matrix&);
- Addition operator.
Sums matrices elementwise.
Matrix operator + (const Matrix&);
- Addition assignment operator.
Sums matrices elementwise.
Matrix& operator += (const Matrix&);
- Subtraction operator.
Subtracts matrices elementwise.
Matrix operator - (const Matrix&);
- Subtraction assignment operator.
Subtracts matrices elementwise.
Matrix& operator -= (const Matrix&);
- Multiplication operator.
Multiplies matrices elementwise.
Matrix operator * (const Matrix&);
- Multiplication assignment operator.
Multiplies matrices elementwise.
Matrix& operator *= (const Matrix&);
- Assignment operator.
Assigns matrix elements to given value.
Matrix& operator = (const std::complex<double>&);
- Addition operator.
Sums matrix elements with given value.
Matrix operator + (const std::complex<double>&);
- Addition assignment operator.
Sums matrix elements with given value.
Matrix& operator += (const std::complex<double>&);
- Subtraction operator.
Subtracts given value from matrix elements.
Matrix operator - (const std::complex<double>&);
- Subtraction assignment operator.
Subtracts given value from matrix elements.
Matrix& operator -= (const std::complex<double>&);
- Multiplication operator.
Multiplies matrix elements by given value.
Matrix operator * (const std::complex<double>&);
- Multiplication assignment operator.
Multiplies matrix elements by given value.
Matrix& operator *= (const std::complex<double>&);
- Division operator.
Divides matrix elements by given value.
Matrix operator / (const std::complex<double>&);
- Division assignment operator.
Divides matrix elements by given value.
Matrix& operator /= (const std::complex<double>&);
- Equal operator.
Determines if matrices are equal.
bool operator == (const Matrix&);
- Non-equal operator.
Determines if matrices are not-equal.
bool operator != (const Matrix&);
- Output operator.
Prints matrix to output.
friend std::ostream& operator << (std::ostream&, const Matrix&);
elementwiseProduct
Multipliesthis->mat
by given matrix elementwise.
void elementwiseProduct(const Matrix&);
elementwiseProduct
Returns elementwise product of given matrices.
static Matrix elementwiseProduct(const Matrix&, const Matrix&);
elementwiseDivision
Dividesthis->mat
by given matrix elementwise.
void elementwiseDivision(const Matrix&);
elementwiseDivision
Return elemetwise division of given matrices.
static Matrix elementwiseDivision(const Matrix&, const Matrix&);
conjugate
Transforms elements ofthis->mat
to it's conjugate.
void conjugate();
conjugate
Returns conjugate of given matrix.
static Matrix conjugate(const Matrix&);
transpose
Transposesthis->mat
.
void transpose();
transpose
Returns transposition of given matrix.
static Matrix transpose(const Matrix&);
hermitianConjugate
Transformthis->mat
to it's Hermitian-conjugate.
void hermitianConjugate();
hermitianConjugate
Returns Hermitian-conjugate of given matrix.
static Matrix hermitianConjugate(const Matrix&);
tensorProduct
Produces a tensor product with the given matrix.
void tensorProduct(const Matrix&);
tensorProduct
Returns tensor product of given matrices.
static Matrix tensorProduct(const Matrix&, const Matrix&);
cofactor
Returnsfalse
if matrix is not square.
Returnstrue
and transformsthis->mat
to it's co-factor matrix if matrix is square.
bool cofactor();
adjugate
Returnsfalse
if matrix is not square.
Returnstrue
and transformsthis->mat
to it's adjugate matrix if matrix is square.
bool adjugate();
inverse
Returnsfalse
if matrix is not square.
Returnstrue
and transformsthis->mat
to it's inverse matrix if matrix is square.
bool inverse();
resize
Receiverow
,col
and initial valueinit
.
Resizes matrix torow
xcol
.
If matrix becomes greater (in any dimension), then new empty elements are filled withinit
value.
void resize(const unsigned&, const unsigned&, const std::complex<double>& = 0.0 + 0.0i);
reshape
Returnsfalse
if reshaping is impossible.
Returnstrue
and reshapesthis->mat
if it is possible.
bool reshape(const unsigned&, const unsigned&);
trace
Returns trace of matrix.
std::complex<double> trace();
determinant
Returns0+0i
if matrix is not square.
Returns determinant of matrix if it is square.
std::complex<double> determinant();
identity
Returns identity matrix with given size.
static Matrix identity(const unsigned& = 1);
getRows
Returnsrows
value.
unsigned getRows();
getCols
Returnscols
value.
unsigned getCols();