// 'matrix.h' includes / preproc
#include <iostream>
#include <sstream>
#include <initializer_list>
#pragma once
matrix(ushort rows, ushort columns, _NumberType zeroEquivalent = 0)
matrix(initializer_list<initializer_list< _NumberType >> list2d)
matrix()
clean()
fill(_NumberType value)
getDeterminant()
getMatrixMinor(ushort rowIndexPos, ushort columnIndexPos)
transpose()
minors()
cofactors()
inverse()
rowsCount()
columnsCount()
toString()
at(ushort rowIndex, ushort columnIndex)
initializeContents(ushort rows, ushort columns)
emplaceRow(ushort rowIndex, _NumberType* dest)
emplaceColumn(ushort columnIndex, _NumberType* dest)
matrix()
operator*(const _InNumberType& n)
operator*(matrix<_InMatrixType>& n)
operator/(const _InNumberType& n)
operator+(const matrix<_InMatrixType>& n)
operator-(const matrix<_InMatrixType>& n)
operator=(matrix<_InMatrixType>& n)
operator~()
operator*=(const _InNumberType& n)
operator/=(const _InNumberType& n)
operator*=(matrix<_InMatrixType>& n)
operator+=(matrix<_InMatrixType>& n)
operator-=(matrix<_InMatrixType>& n)
matrix.h
’integralToConstChar(long long l)
evaluateMatrixNumberGrouping(_NumberType* n1, _NumberType* n2, uint len, bool performVectorDelete = false)
// creates a 3 x 3 matrix with '10' in every position
matrix<> m(3, 3, 10);
// creates a 3 x 3 matrix with '10' in every position
matrix<> m_fill(3, 3);
m.fill(10);
// creates a 3 x 3 matrix with '10' in every position, but
// it uses the type 'double' instead of the default 'float'
matrix<double> m_double(3, 3, 10);
// creates a 2 x 2 matrix, and uses manual
// 'at()' calls to fill the matrix
matrix<> m_manual(2, 2);
m_manual.at(1, 1) = 10;
m_manual.at(1, 2) = 10;
m_manual.at(2, 1) = 10;
m_manual.at(2, 2) = 10;
// creates a 3 x 3 matrix with the corresponding
// values as shown below, 1 2 3 4 5...
matrix<> m_initializer_list = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// creates a 3 x 3 matrix with the corresponding
// values as shown below, 1 2 3 4 5...
matrix<> m = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// creates a 3 x 3 matrix with the corresponding
// values as shown below, 1 2 3 4 5...
matrix<> m_1 = {
{ 1, 2, 1 },
{ 3, 4, 1 },
{ 5, 6, 3 }
};
// scalar multiplication
m = m * 10;
// scalar division
m = m / 10;
// matrix 'dot' multiplication
m = m * m_1;
// matrix addition
m = m + m_1;
// matrix subtraction
m = m - m_1;
// matrix inverse using 'NOT' bitwise overload
~m;
// matrix inverse
m.inverse();
// matrix minors
m.minors();
// matrix cofactors
m.cofactors();
// matrix transpose
m.transpose();
// creates a 3 x 3 matrix with the corresponding
// values as shown below, 1 2 3 4 5...
matrix<> m = {
{ 3, 2, 3 },
{ 4, 1, 6 },
{ 7, 1, 9 }
};
// makes inverse
m.inverse();
// prints inverse matrix
cout << m.toString() << endl;
// Output:
// [ [ 0.25, -1.25, 0.75 ], [ 0.5, 0.5, -0.5 ], [ -0.25, 0.916667, -0.416667 ] ]
/*
System of Equations:
3x + 1y = 6
4x - 3y = -5
A = [ [ 3, 1 ], [ 4, -3 ] ]
B = [ [ 6 ], [ -5 ] ]
A x C = B
We want to find 'C'
C = ~A x B
'C' is equal to the inverse
if 'A' dot multiplied by 'B'
*/
matrix<> A = {
{ 3, 1 },
{ 4, -3 }
};
matrix<> B = {
{ 6 },
{ -5 }
};
A.inverse();
matrix<> C = A * B;
cout << C.toString() << endl;
// Output:
// [ [ 1 ], [ 3 ] ]
// X = 1 ; Y = 3
/*
System of Equations:
4w + 1x + 2y - 3z= -16
-3w + 3x - 1y + 4z= 20
-1w + 2x + 5y + 1z= -4
5w + 4x + 3y - 1z= -10
A = [ [ 4, 1, 2, -3 ], [ -3, 3, -1, 4 ], [ -1, 2, 5, 1 ], [ 5, 4, 3, -1 ] ]
B = [ [ -16 ], [ 20 ], [ -4 ], [ -10 ] ]
A x C = B
We want to find 'C'
C = ~A x B
'C' is equal to the inverse
if 'A' dot multiplied by 'B'
*/
matrix<> A = {
{ 4, 1, 2, -3 },
{ -3, 3, -1, 4 },
{ -1, 2, 5, 1 },
{ 5, 4, 3, -1 }
};
matrix<> B = {
{ -16 },
{ 20 },
{ -4 },
{ -10 }
};
A.inverse();
matrix<> C = A * B;
cout << C.toString() << endl;
// Output:
// [ [ -1 ], [ 1 ], [ -2 ], [ 3 ] ]
// W = -1 ; X = 1 ; Y = -2 ; Z = 3
matrix::rows
’Type : unsigned short
Usage : Keeps track of initial row size passed to constructor
matrix::columns
’Type : unsigned short
Usage : Keeps track of initial column size passed to constructor
matrix::contents
’Type : _NumberType**
: An array of memory addresses which point to additional values, effectively a 2d array when initialized
Usage : Holds contents of matrix
matrix(ushort rows, ushort columns, _NumberType zeroEquivalent = 0)
’rows
unsigned short
columns
unsigned short
zeroEquivalent
_NumberType
0
value, 0
may not be default for classes0
’matrix(initializer_list<initializer_list< _NumberType >> list2d)
’list2d
initializer_list
containing an initializer_list
that contains _NumberType
'smatrix()
’none
Private constructor used for in-class operations only
matrix::clean()
’none
none
Frees the dynamically allocated memory ‘matrix::contents
’
matrix::fill(_NumberType value)
’value
_NumberType
none
Sets every element in the matrix to value
matrix::getDeterminant()
’none
_NumberType
Calculates the matrix’s determinant, recursively calls itself in smaller minor matricies until
the minor matricies reach 2 by 2 in dimension at which point ad - bc
is returned, special case for 1 by 1 where the [0][0]
of ‘matrix::contents
’ is returned
matrix::getMatrixMinor(ushort rowIndexPos, ushort columnIndexPos)
’rowIndexPos
unsigned short
columnIndexPos
unsigned short
matrix<_NumberType>
Calculates the minor matrix with row and column excluded, used in ‘matrix::getDeterminant()
’ and ‘matrix::minors()
’
matrix::transpose()
’none
none
Transposes the matrix, all columns become rows and vice versa, square matrix required
matrix::minors()
’none
none
Calculates matrix of minors and sets current matrix’s value to its matrix of minor’s values
matrix::cofactors()
’none
none
Calculates matrix of cofactors by first calling ‘matrix::minors()
’ then applying a ‘negative’ grid to the matrix
matrix::inverse()
’none
none
Calculates matrix inverse by getting the matrix’s deteminant by calling ‘matrix::getDeterminant()
’, then calling ‘matrix::cofactors()
’, ‘matrix::transpose()
’, and finally performing scalar multiplication with 1 over the original matrix’s determinant
matrix::rowsCount()
’none
unsigned short
Returns row count
matrix::columnsCount()
’none
unsigned short
Returns column count
matrix::toString()
’none
std::string
Returns an ascii string representative of the matrix, see Examples
for usage
matrix::at(ushort rowIndex, ushort columnIndex)
’rowIndex
unsigned short
columnIndex
unsigned short
_NumberType&
matrix::contents
’ array, can be used to set or get a valueUsed to get and set the value at <rowIndex
: columnIndex
>, references can be, well, referenced for value while also being set
int int_value;
int int_array[2] = { 1, 2 };
int& int_reference = int_array[0];
int_reference = 10; // sets reference value
int_value = int_reference; // gets value of reference
matrix::initializeContents(ushort rows, ushort columns)
’rows
unsigned short
columns
unsigned short
none
Allocates memory for matrix, pointer to allocated memory stored at ‘matrix::contents
’
matrix::emplaceRow(ushort rowIndex, _NumberType* dest)
’rowIndex
unsigned short
dest
’dest
_NumberType*
_NumberType[]
’none
Places contents of the matrix’s row in the ‘dest
’ array , used by matrix class to perform dot multiplication
matrix::emplaceColumn(ushort columnIndex, _NumberType* dest)
’columnIndex
unsigned short
dest
’dest
_NumberType*
_NumberType[]
’none
Places contents of the matrix’s column in the ‘dest
’ array, used by matrix class to perform dot multiplication
matrix::operator*(const _InNumberType& n)
’n
const _InNumberType&
matrix<_NumberType>
n
’matrix::operator*(matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
matrix<_NumberType>
n
’matrix::operator/(const _InNumberType& n)
’n
const _InNumberType&
matrix<_NumberType>
n
’matrix::operator+(const matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
matrix<_NumberType>
n
’matrix::operator-(const matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
matrix<_NumberType>
n
’matrix::operator=(matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
none
matrix::operator~()
’none
none
Used as shorthand for matrix inverse
matrix::operator*=(const _InNumberType& n)
’n
const _InNumberType&
none
Performs scalar multiplication on base matrix with ‘n
’
matrix::operator/=(const _InNumberType& n)
’n
const _InNumberType&
none
Performs scalar division on base matrix with ‘n
’
matrix::operator*=(matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
none
Performs dot multiplication on base matrix with ‘n
’
matrix::operator+=(matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
none
Performs matrix addition on base matrix with ‘n
’
matrix::operator-=(matrix<_InMatrixType>& n)
’n
matrix<_InMatrixType>&
none
Performs matrix subtraction on base matrix with ‘n
’
integralToConstChar(long long l)
’l
long long
const char*
l
’Used to convert integral types to ‘const char*
’ on the fly. Doesn’t actually make ‘const char*
’, it dynamically allocates memory
which it never intends to free. Effectively being used as a ‘const char*
’. Idealy it would be called right before the program exits
as to free memory as soon as possible. Leaks memory, thats what it is supposed to do.
evaluateMatrixNumberGrouping(_NumberType* n1, _NumberType* n2, uint len, bool performVectorDelete = false)
’n1
_NumberType*
_NumberType[]
’n2
_NumberType*
_NumberType[]
’len
unsigned int
n1
’ and ‘n2
’performVectorDelete
bool
delete[]
’ to free memory allocated : ‘delete[] n1, n2;
’false
_NumberType
Used to calculate dot multiplication on matricies, see ‘matrix::operator*(matrix<_InMatrixType>& n)
’