Matrix multiplication in C++

Matrices can be used to perform transformations. For example, in 2D and 3D space. Transformations include, but are not limited to, rotating, scaling and translating. Transformations can be combined by multiplying the matrices with each other.

For example, you might want to:

  1. scale an object with a factor of 2
  2. rotate it 45 degrees around the Y-axis
  3. translate it to (5,5,5)

Each of the above steps can be represented with a four by four matrix (four rows and four columns). Multiplication of matrices is done by multiplying each row of the first matrix with each column of the second matrix and summing up the results. The number of columns of the first matrix needs to be equal to the number of rows of the second matrix. For example (4 x 4) x (4 x 4) is allowed. However, (4 x 3) x (4 x 3) is not allowed.

Two matrices are multiplied with each other

In the above multiplication, the first row of the resulting matrix is calculated as follows.

[[1x1 + 2x0 + 3x0 + 4x0] = 1, [1x0 + 2x1 + 3x0 + 4x0] = 2, [1x0 + 2x0 + 3x1 + 4x0] = 3, [1x0 + 2x0 + 3x0 + 4x1] = 4]]

The second matrix is a special kind of matrix, also known as the identity matrix. When a matrix is multiplied with its identity matrix, the resulting matrix will be the same.

I’ve created a VC++ console application which includes a class called CMatrix to represent a 4×4 matrix. The console application will replicate the multiplication in the above image.

The CMatrix header file and class

// CMatrix.cpp - Used for multiplying 4x4 matrices

#include "StdAfx.h"
#include "CMatrix.h"

CMatrix::CMatrix(float elements[4][4]){
	for(int row=0;row<4;row++){
		for(int column=0;column<4;column++){
			this->elements[row][column] = elements[row][column];
		}
	}
}

// Returns the identity matrix
CMatrix* CMatrix::Identity(){
	float matrixElements[4][4] = {
									{1,0,0,0},
									{0,1,0,0},
									{0,0,1,0},
									{0,0,0,1}};

	CMatrix* matrix = new CMatrix(matrixElements);

	return matrix;
}

// Multiplies this matrix with another matrix
CMatrix* CMatrix::Multiply(CMatrix* matrix){
	CMatrix* resultMatrix = new CMatrix();

	for(int row=0;row<4;row++){
		for(int column=0;column<4;column++){
			resultMatrix->elements[row][column] = 0;

			for(int n=0;n<4;n++){
				resultMatrix->elements[row][column]+=
					elements[row][n]*matrix->elements[n][column];
			}
		}
	}

	return resultMatrix;
}

// Converts the matrix to a string representation
string CMatrix::ToString(){
	string matrixString = "";

	for(int row=0;row<4;row++){
		for(int column=0;column<4;column++){
			float matrixElement = this->elements[row][column];
			char* matrixElementChar = new char[32];
			sprintf(matrixElementChar,"%.0f",matrixElement);
			matrixString += matrixElementChar;
			matrixString += "\t";
		}

		matrixString += "\n";
	}

	return matrixString;
}

CMatrix::CMatrix(void)
{

}

CMatrix::~CMatrix(void)
{
}
#pragma once

#include <string>
// CMatrix.h - Used for multiplying 4x4 matrices

using namespace std;

class CMatrix
{
public:
	CMatrix(float elements[4][4]);
	CMatrix* Identity();
	CMatrix* Multiply(CMatrix* matrix);
	CMatrix(void);
	~CMatrix(void);
	string ToString();
	float elements[4][4];
private:
	int rows, columns;
};

The main program

// MatrixMultiplication.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "CMatrix.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	// Construct the first matrix
	float matrixElements[4][4] = {
									{1,2,3,4},
									{5,6,7,8},
									{8,7,6,5},
									{4,3,2,1}};

	CMatrix* firstMatrix = new CMatrix(matrixElements);

	// Construct the identity matrix
	CMatrix* secondMatrix = new CMatrix();
	secondMatrix = secondMatrix->Identity();

	// Multiply matrices
	CMatrix* multipliedMatrix = firstMatrix->Multiply(secondMatrix);

	cout << firstMatrix->ToString() << endl;
	cout << "X" << endl;
	cout << secondMatrix->ToString() << endl;
	cout << "=" << endl;

	// Print the results
	cout << multipliedMatrix->ToString() << endl;

	system("pause");
	return 0;
}

Output:

1       2       3       4
5       6       7       8
8       7       6       5
4       3       2       1

X
1       0       0       0
0       1       0       0
0       0       1       0
0       0       0       1

=
1       2       3       4
5       6       7       8
8       7       6       5
4       3       2       1

Press any key to continue . . .

The VS 2010 project can be downloaded from here.

This entry was posted in C++ and tagged , , , , , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted November 15, 2011 at 9:57 am | Permalink

    i intraset of reading matrix

  2. bufa
    Posted May 8, 2014 at 9:13 pm | Permalink

    thanks..
    but how to make this matrix accept “char” not just “float”

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?