## What is a matrix?

A matrix is an entity composed of components arranged in rows and columns. The rows and columns of a matrix determine the dimension of a matrix. A matrix containing 3 rows and 4 columns is of dimension 3x4. Dimensions in matrix arithmetic are very important since some operations are not possible unless matrices have identical dimensions.

The matrices are frequently used in computer graphics and the matrix transformations are one of the important mechanics of any 3D graphics, the chain of matrix transformations allows rendering a 3D object on a 2D monitor

## What is the order of a matrix?

In a matrix, the vertical and horizontal arrangements are called the columns and rows of the matrices respectively. The order of a matrix denotes the arrangement of elements as number of rows and columns in a matrix. So, it is known as dimension of a matrix. So, if a matrix has r rows and t columns then the matrix is said to be of order read as r cross/by t. The dimension of the matrix tells the total number of elements of the matrix and it can be obtained by multiplying the rows by columns.

The order of a matrix is useful to find the number of elements in any type of matrix. For example, the total number of elements in matrix $A=r\times t=rt$. In general, matrix can be displayed as,

$A=\left[\begin{array}{cccc}{a}_{11}& {a}_{12}& \dots & {a}_{1j}\\ {a}_{21}& {a}_{22}& \dots & {a}_{2j}\\ \vdots & \vdots & \ddots & \vdots \\ {a}_{i1}& {a}_{i2}& \dots & {a}_{ij}\end{array}\right]$

Matrix $A$ is a square (or) rectangular matrix based on their order. The ${i}^{th}$row elements are ${a}_{i1},{a}_{i2}....{a}_{it}$ while the ${j}^{th}$column elements are ${a}_{j1},{a}_{j2},......{a}_{it}$ . In general, is an element of matrix which is lying in the ${i}^{th}$row and ${j}^{th}$ column of a matrix.

## Multiplication of matrix:

### Scalar matrix multiplication:

Multiplying a vector by a scalar is perhaps one of the easiest operations we can perform using vectors. The process is called scalar multiplication but is sometimes simply called scaling because it simply either increases or decreases the magnitude of the vector, it does not alter the vector's direction. Scalar multiplication is performed by multiplying a scalar with each corresponding component in the matrix.

$M=\left[\begin{array}{ccc}{m}_{00}& {m}_{01}& {m}_{02}\\ {m}_{10}& {m}_{11}& {m}_{12}\\ {m}_{20}& {m}_{21}& {m}_{22}\end{array}\right]\phantom{\rule{0ex}{0ex}}KM=\left[\begin{array}{ccc}k{m}_{00}& k{m}_{01}& k{m}_{02}\\ k{m}_{10}& k{m}_{11}& k{m}_{12}\\ k{m}_{20}& k{m}_{21}& k{m}_{22}\end{array}\right]\phantom{\rule{0ex}{0ex}}WhereKisascalar\phantom{\rule{0ex}{0ex}}KM=3\left[\begin{array}{ccc}7& 6& 3\\ 4& 2& 5\\ 10& 6& 4\end{array}\right]\phantom{\rule{0ex}{0ex}}=\left[\begin{array}{ccc}21& 18& 9\\ 12& 6& 15\\ 30& 18& 12\end{array}\right]$

## Properties of scalar multiplication

Let $A={\left[{a}_{ij}\right]}_{r\times t}$and $B={\left[{b}_{ij}\right]}_{r\times t}$and $\alpha ,\beta \in \mathfrak{R}$ then

(i) $\alpha (A+B)=\alpha A+\alpha B$

(ii) $(\alpha +\beta )A=\alpha A+\beta A$

**Example**

If $A=\left[\begin{array}{cc}5& 2\\ 1& 3\end{array}\right],B=\left[\begin{array}{cc}-1& 0\\ 0& -1\end{array}\right]$then find the matrix such that X such that $2A-X=B.$

**Solution:**

**Given:$2A-X=B.$**

Adding on both sides, we get,

$2A+X-X=B+X\phantom{\rule{0ex}{0ex}}2A+0=B+X\phantom{\rule{0ex}{0ex}}2A=B+X$

Subtract matrix from both sides, we get,

$2A-B=B-B+X\phantom{\rule{0ex}{0ex}}2A-B=0+X\phantom{\rule{0ex}{0ex}}2A-B=X\phantom{\rule{0ex}{0ex}}X=2A-B$

$X=2\left[\begin{array}{cc}5& 2\\ 1& 3\end{array}\right]-\left[\begin{array}{cc}-1& 0\\ 0& -1\end{array}\right]\phantom{\rule{0ex}{0ex}}X=\left[\begin{array}{cc}10& 4\\ 2& 6\end{array}\right]+\left[\begin{array}{cc}1& 0\\ 0& 1\end{array}\right]\phantom{\rule{0ex}{0ex}}X=\left[\begin{array}{cc}11& 4\\ 2& 7\end{array}\right]$

## Multiplication of matrices

Let A,B be any matrices and matrix multiplication is defined only when the number of columns of matrix A is equal to the number of rows of matrix B. That is if $A={\left[{a}_{ij}\right]}_{r\times t}$, $B={\left[{b}_{ij}\right]}_{t\times s}$be $r\times t$matrix and $t\times s$ matrix respectively then the product of the matrix is C of order $r\times s$. Therefore, the product matrix $C={\left[{c}_{ij}\right]}_{r\times s}=AB$ where to get $\left(i,k\right)$th element ${c}_{ik}$ of matrix C we multiply $i$th row of the matrix A with the k th column of matrix B and then summing up their product to obtain ${c}_{ik}$.

**Example**

Let $A={\left[\begin{array}{cc}1& 2\\ 1& 1\end{array}\right]}_{2\times 2}andB={\left[\begin{array}{c}3\\ 2\end{array}\right]}_{2\times 1}$

Here, $2\times 2\times 2\times 1=2\times 1$

$AB=\left[\begin{array}{cc}1& 2\\ 1& 1\end{array}\right]\left[\begin{array}{c}3\\ 2\end{array}\right]\phantom{\rule{0ex}{0ex}}=\left[\begin{array}{cc}1\times 3+& 2\times 2\\ 1\times 3+& 1\times 2\end{array}\right]\phantom{\rule{0ex}{0ex}}=\left[\begin{array}{c}3+4\\ 3+2\end{array}\right]\phantom{\rule{0ex}{0ex}}AB={\left[\begin{array}{c}7\\ 5\end{array}\right]}_{2\times 1}$

Notice that $BA={\left[\begin{array}{c}3\\ 2\end{array}\right]}_{2\times 1}{\left[\begin{array}{cc}1& 2\\ 1& 1\end{array}\right]}_{2\times 2}$is not defined because the number of columns of B is not equal to the number of rows of A.

$AB=\left[\begin{array}{cc}1& 0\\ 0& 1\end{array}\right]\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]=\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]\phantom{\rule{0ex}{0ex}}BA=\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]\left[\begin{array}{cc}1& 0\\ 0& 1\end{array}\right]=\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]\phantom{\rule{0ex}{0ex}}AB=BA$

In general, product of two multiplication is defined but their commutativity fails.

**Special case**

The product of two non-zero matrices can be a zero matrix but not necessarily.

**Example**: We know that in real number system for all $a,b\in \mathfrak{R}$, if ab=0 then either a=0 (or) b=0

But this is not necessarily enough to show the case in matrices.

$LetA=\left[\begin{array}{cc}0& 1\\ 0& 2\end{array}\right],B=\left[\begin{array}{cc}1& 3\\ 0& 0\end{array}\right]then,\phantom{\rule{0ex}{0ex}}AB=\left[\begin{array}{cc}0& 1\\ 0& 2\end{array}\right]\left[\begin{array}{cc}1& 3\\ 0& 0\end{array}\right]=\left[\begin{array}{cc}0& 0\\ 0& 0\end{array}\right]$

## Properties of matrix multiplication

**Associative property:**The changes in the surrounding matrix multiplication will not affect the output. Associative property can be written as, A1(A2A3)=(A1A2)A3.**Intersecting property:**When the matrix M X N is multiplied by the N X N matrix, then the output will be the M X N matrix, and this property is known as multiplicative identity.**Distributive property:**The matrices can be distributed as we distribute the real numbers, the distributed property can be written as, A1(A2+A3) = A1A2+A1A3.

## The Naive matrix multiplication

### Pseudocode

Data: S[a][b], P[g][h]

Result: Q[][]

If B == G, then

For i=0; i<a; i++ do

For j=0; j<h; j++ do

Q[i][j] = 0;

For k =0; k<g; K++ do

Q[i][j]+= S[i][k] * p[k][j];

End

End

End

End

The above algorithm loops through P and S entities, and the resultant matrix Q is filled using the outermost loop. It uses an exhaustive approach.

### Time complexity

There are three nested loops in the naive matrix multiplication algorithm. For every outer loop iteration, the number of runs in the inner loops will be equivalent to the matrix length. O(1) is the time taken to perform an integer operation. The length of a matrix is N, then the time complexity is O(N*N*N) = O(N^{3}).

## Stassen algorithm

This algorithm uses the divide-and-conquer method along with some matrix tricks to solve the problems with low computation. Generally, the input matrix dimension will be N X N. Consider the two input matrices (S and P) with 2 X 2 order. The input matrix is divided into four submatrices with the order N/2 X N/2. Perform 10 subtraction/addition operation. calculate seven multiplication operations recursively using former results. R[m], m ϵ {1,2,…,7} with the size N/2 X N/2. Finally, the desired submatrix of the Q resultant matrix is calculated by subtracting and adding a different combination of R[m] submatrices.

### Time complexity

The solution of the Stassen algorithm is based on the recursion. O(1) is the time to perform the first step (dividing the input matrices into submatrices with N/2 X N/2 size.) to calculate 10 subtraction/addition takes O(N^{2}) time. R1 to R7 is calculated, the output loop of the step, where there will be 7 matrices with size N X 2 and it tales 7T(N/2) time. Finally, finding the Q matrix takes O(N^{2}) time. The time complexity is,

T(N) = 7T(N/2) + O(N^{2}) = O(N^{log}_{2}^{ 7})= O(N^{2..8074}) time

## Use of matrices

- Square matrices can represent the linear transformation of objects. They are used to project 3-D images into 2-D planes in graphics.
- In computer graphics, digital images are treated as a matrix where the columns and rows of matrices correspond to the columns and rows of the pixels. The numerical entries are the color values of the pixel.
- Matrix is used to manipulate the point, which is a common mathematical approach used in video game graphics. Graphs can be represented in a matrix, where columns and rows of the matrix is the node and value of their intersection.
- In computer graphics, the matrix operations like rotation and translation will be used.

## Context and Applications

This topic is significant in all the competitive exams, at school levels, both graduate and post-graduate levels especially for

- Bachelors in Computer Science or Mathematics
- Masters in Computer Science or Mathematics

## Practice Problems

**Q1** What is the time complexity of navie algorithm?

a) O(N^{3})

b) O(N^{2.8074})

c) O(N)

d) None of the above

**Answer:** Option a is correct.

**Explanation:** O(1) is the time taken to perform an integer operation. The length of a matrix is N, then the time complexity is O(N*N*N) = O(N^{3}).

**Q2** Divide-and-conquer approach is used by ______.

a) Navie algorithm

b) Strassen algorithm

c) Both a and b

d) None of the above

**Answer:** Option b is correct

**Explanation:** Solvay Strassen algorithm uses the divide-and-conquer method along with some matrix tricks to solve the problems with low computation.

**Q3** The associative property is written as____.

a) A1(A2+A3) = A1A2+A1A3

b) A1(A2A3)=(A1A2)A2.

c) A1(A2A3)=(A1A2)A3.

d) None of the above

**Answer:** Option c is correct.

**Explanation: **The changes in the surrounding matrix multiplication will not affect the output. Associative property can be written as, A1(A2A3)=(A1A2)A3.

**Q4** The Solvay Strassen algorithm is suitable for ___ matrices.

- Non-square
- Square
- Neither a nor b
- None of the above

**Answer: **Option b is correct.

**Explanation: **The Solvay Strassen algorithm use divide and conquer method with the maths tricks to solve matrix multiplication with low computation and this works only on the square matrices with the same dimensions.

**Q5** In which property, the matrices can be distributed as we distribute the real numbers?

- Associative
- Distributive
- Interesting
- All of the above

**Answer:** Option d is correct.

**Explanation:** The matrices can be distributed as we distribute the real numbers, the distributed property can be written as, A1(A2+A3) = A1A2+A1A3.

### Want more help with your computer science homework?

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

# Matrix Multiplication Homework Questions from Fellow Students

Browse our recently answered Matrix Multiplication homework questions.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.