C++ Matrix Implementation

// 'matrix.h' includes / preproc

#include <iostream>
#include <sstream>
#include <initializer_list>

#pragma once

Examples


Matrix Class Variable Members


Matrix Class Constructors


Matrix Class General Methods


Matrix Class Private Methods


Matrix Class Operator Overloads


General Functions In ‘matrix.h


Matrix Creation

// 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 }
};

Matrix Operations

// 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();

Matrix Inverse

// 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 ] ]

Matrix 2x2 System Of Equations

/*
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

Matrix 4x4 System Of Equations

/*
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 

Variable ‘matrix::rows

Type : unsigned short

Usage : Keeps track of initial row size passed to constructor


Variable ‘matrix::columns

Type : unsigned short

Usage : Keeps track of initial column size passed to constructor


Variable ‘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


Constructor ‘matrix(ushort rows, ushort columns, _NumberType zeroEquivalent = 0)

Passed Arguments


Constructor ‘matrix(initializer_list<initializer_list< _NumberType >> list2d)

Passed Arguments


Constructor ‘matrix()

Passed Arguments

none
Private constructor used for in-class operations only


Method ‘matrix::clean()

Passed Arguments

none

Returns

none

What?

Frees the dynamically allocated memory ‘matrix::contents


Method ‘matrix::fill(_NumberType value)

Passed Arguments

Returns

none

What?

Sets every element in the matrix to value


Method ‘matrix::getDeterminant()

Passed Arguments

none

Returns

What?

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


Method ‘matrix::getMatrixMinor(ushort rowIndexPos, ushort columnIndexPos)

Passed Arguments

Returns

What?

Calculates the minor matrix with row and column excluded, used in ‘matrix::getDeterminant()’ and ‘matrix::minors()


Method ‘matrix::transpose()

Passed Arguments

none

Returns

none

What?

Transposes the matrix, all columns become rows and vice versa, square matrix required


Method ‘matrix::minors()

Passed Arguments

none

Returns

none

What?

Calculates matrix of minors and sets current matrix’s value to its matrix of minor’s values


Method ‘matrix::cofactors()

Passed Arguments

none

Returns

none

What?

Calculates matrix of cofactors by first calling ‘matrix::minors()’ then applying a ‘negative’ grid to the matrix


Method ‘matrix::inverse()

Passed Arguments

none

Returns

none

What?

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


Method ‘matrix::rowsCount()

Passed Arguments

none

Returns

What?

Returns row count


Method ‘matrix::columnsCount()

Passed Arguments

none

Returns

What?

Returns column count


Method ‘matrix::toString()

Passed Arguments

none

Returns

What?

Returns an ascii string representative of the matrix, see Examples for usage


Method ‘matrix::at(ushort rowIndex, ushort columnIndex)

Passed Arguments

Returns

What?

Used 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

Method ‘matrix::initializeContents(ushort rows, ushort columns)

Passed Arguments

Returns

none

What?

Allocates memory for matrix, pointer to allocated memory stored at ‘matrix::contents


Method ‘matrix::emplaceRow(ushort rowIndex, _NumberType* dest)

Passed Arguments

Returns

none

What?

Places contents of the matrix’s row in the ‘dest’ array , used by matrix class to perform dot multiplication


Method ‘matrix::emplaceColumn(ushort columnIndex, _NumberType* dest)

Passed Arguments

Returns

none

What?

Places contents of the matrix’s column in the ‘dest’ array, used by matrix class to perform dot multiplication


Operator Overload ‘matrix::operator*(const _InNumberType& n)

Passed Arguments

Returns


Operator Overload ‘matrix::operator*(matrix<_InMatrixType>& n)

Passed Arguments

Returns


Operator Overload ‘matrix::operator/(const _InNumberType& n)

Passed Arguments

Returns


Operator Overload ‘matrix::operator+(const matrix<_InMatrixType>& n)

Passed Arguments

Returns


Operator Overload ‘matrix::operator-(const matrix<_InMatrixType>& n)

Passed Arguments

Returns


Operator Overload ‘matrix::operator=(matrix<_InMatrixType>& n)

Passed Arguments

Returns

none


Operator Overload ‘matrix::operator~()

Passed Arguments

none

Returns

none

What?

Used as shorthand for matrix inverse


Operator Overload ‘matrix::operator*=(const _InNumberType& n)

Passed Arguments

Returns

none

What?

Performs scalar multiplication on base matrix with ‘n


Operator Overload ‘matrix::operator/=(const _InNumberType& n)

Passed Arguments

Returns

none

What?

Performs scalar division on base matrix with ‘n


Operator Overload ‘matrix::operator*=(matrix<_InMatrixType>& n)

Passed Arguments

Returns

none

What

Performs dot multiplication on base matrix with ‘n


Operator Overload ‘matrix::operator+=(matrix<_InMatrixType>& n)

Passed Arguments

Returns

none

What

Performs matrix addition on base matrix with ‘n


Operator Overload ‘matrix::operator-=(matrix<_InMatrixType>& n)

Passed Arguments

Returns

none

What

Performs matrix subtraction on base matrix with ‘n


General Function ‘integralToConstChar(long long l)

Passed Arguments

Returns

What

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.


General Function ‘evaluateMatrixNumberGrouping(_NumberType* n1, _NumberType* n2, uint len, bool performVectorDelete = false)

Passed Arguments

Returns

What

Used to calculate dot multiplication on matricies, see ‘matrix::operator*(matrix<_InMatrixType>& n)