C++

Matrix Multiplication C++

You may have learned and done a lot of questions about matrices in your mathematics subjects while studying. Matrix is a collection of rows and columns. The matrix can have the equivalent number of rows and columns and be different. We can perform any mathematical operation on matrices, i.e. addition, subtraction, multiplication, and division. C++ also allows us to use matrices in our codes and perform these operations. Thus, we have decided to perform matrix multiplication in C++ programming while using the Ubuntu 20.04 Linux system. Let’s start with the C++ new file creation to add code. Launch the shell terminal first and use the “touch” instruction of the Shell terminal to generate a file. We have named this file “matrix.cc”. The file is held in the home folder of our Linux system. We have been opening it in the Gnu Nano editor using ubuntu’s nano editor, as demonstrated in the below image. The empty file will be directly opened in the Gnu nano editor in just 5 seconds.

Example # 01:

Let’s get started with the basic example of matrix multiplication in C++. C++ uses the header “iostream” for taking standard input and output through the input-output stream. So, it must be comprised in the code file as well. We have included it in our C++ empty file using the “#include” keyword at the top line. Within C++, input and output objects can be used only with the standard namespace.

So, we have to utilize the “Std” namespace using the word “using” after the header. We will do our matrix multiplication within the C++ main() method, which is also the source of execution starts. We have declared three matrices “x”, “y”, and “z” with the size of 5-5, i.e. rows*columns. But, we have also declared variables “r” and “c” as rows and columns and assigned both with the same value. Currently, there are no values in our matrices. We will be using the matrix “x” and “y” as input matrices, while the matrix “z” will be the product of both these matrices. Firstly, we must add values in the input matrix “x” and “y” separately using loops.

The cout statements show that the user will be inputting the values in the matrices “x” and “y” separately. The outer “for” loop will be used to iterate the rows up to “r” and the outer “for” loop up to iterate column value “c”. As both “r” and “c” have value 2, thus we will be creating an “x” and “y” matrix of 2*2. The “cin” object has been used to add the values in the matrix “x” and “y” using “I” and “j” loops. Through this, the user will add “2” row values and “2” column values in the matrices by the shell. After inputting values into “x” and “y” matrices, we have to find out the product of both the matrices. Firstly, we have to initialize all rows and columns of product matrix “z” to 0 on each iteration using both “I” and “j” for loops, i.e. r=2, and c=2.

On each iteration, the “k” loop is used to multiply matrix “x” with “y” and add this product value to a particular iteration index of matrix “z”. This will be continued up to the last row-column of matrix “z”. The last 2 “for” loops have been used to display the matrix “z” on the shell via the object “cout” statement. After all this, the last cout statement is used to add the end line. Our program is now ready to be compiled on the shell.

The g++ compiler in Ubuntu 20.04 has been used to compile the c++ code, and the “./a.out” query is used to execute the compiled code. We have added 2-row values and 2-column values for “x” and “y” matrices on execution. After that, the product matrix “z” of both the matrices “x” and “y” have been calculated and displayed on the shell the last.

Example # 02:

Within the above example, we have calculated matrix multiplication for two same matrices, “x” and “y”, of the same order, i.e. same number of rows and columns for both matrices. But, do you know the rules of calculating matrix multiplication? If not? Then this example will be the best help for you. You must know that we cannot calculate the matrix multiplication of two matrices with different rows into column order. To perform multiplication, the first matrix row value must be equal to the second matrix column value, i.e. r1=c2 or r2=c1. We have updated the value of column “c” to 3. Now, the rows and column values for matrix “x” and “y” are not the same. The product will not be calculated as the matrix “x”, and “y” will have 2 rows and 3 columns, i.e. r1 is not equal to c2, and r2 is not equal to c1. The remaining code will be unchanged and saved for compilation via Ctrl+S.

We have compiled this unmatched row-column matrix code and executed it so far. The user has added values for “x” and “y” matrices. We have got complicated unexpected multiplication results of matrix “x” and “y”. This output is inaccurate because we haven’t used the same order required for the matrix multiplication.

To resolve this concern, we must use the order r1=c2 and c1=r2 for input matrices in our code. Therefore, we have opened the same code and changed the rows and columns for the “x” and “y” matrix along with the variables “r=3” and “c=4”. Let’s save this updated code and compile it.

On compilation and execution, we have added input for matrix “x” in order 3-row*4-column and 4-row*3-column for matrix “y”. We have got the product matrix of order 3-row*4-column after the multiplication of matrix “x” and “y”.

Example # 03:

Let’s take a look at the last but not least example of matrix multiplication. We have initialized r1=3, c1=4, r2=4, c2=3, matrix “x”, and matrix “y” separately. The product matrix “M” is defined using r1 and c2. We have used the “for” loop to display the already initialized “x” and “y” matrices on our shell using the “cout” objects. As demonstrated in the attached image below, this has been done separately for “x” and “y” matrices to perform matrix multiplication.

We have calculated the product of both matrices and added the product to matrix “M”. At last, we have displayed the product matrix “M” on the shell using the “cout” object statement.

On code execution, we have been displayed with both “x” and “y” matrices first and then their product matrix “M”.

Conclusion:

Finally! We have completed the explanation of calculating the matrix multiplication in C++ code using the Ubuntu 20.04 system. We have explained the importance of rows into columns in order of matrices for the multiplication operation. Therefore, we have started from a simple example of taking the same order matrices and moved forward with the examples of different order matrices.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.