Matrix

## **Matrices**

A matrix is a rectangular array of numbers (usually real) represented in forms of rows and columns. For example,

```
[1 2 3]
[4 5 6]

```

The dimension of a matrix is written as (number of rows) \* (number of columns). So the dimension of above matrix is 23. A general m\*n matrix is of the form

```
     [ a11 a12 .……. a1n ]
     [ a21 a22 .……….a2n ]
A  = [ ………….......…………  ]
     [ am1 am2 .……. amn ]

```

### **Square Matrix**

A matrix is called a square matrix if the number of rows is equal to the number of columns.

### **Identity Matrix**

An identity matrix, I of dimension n\*n is the matrix in which all the elements on principal diagonal are 1 and the rest are 0. An identity matrix is always a square matrix.

```
	   [ 1 0 0 ]
I (3*3) =  [ 0 1 0 ]
	   [ 0 0 1 ]

```

```
I (2*2) =  [ 1 0 ]
           [ 0 1 ]

```

### **Like Matrices**

The matrices with same dimensions are known as like matrices.

```
   A  =  [ 1 2 ]
         [ 3 4 ]

```

```
   B  =  [ 5 6 ]
	 [ 7 8 ]

Here, A(2*2) and B(2*2) are both like matrices.

```

### **Matrix Addition**

The addition of a m\*n matrix can only be done on like matrices. It is the sum of individual like terms. For Example, in the above example, C = A + B can be given as:

```
   C  =  [ 6  8  ]
	 [ 10 12 ]

```

Note: Subtraction is similar.

**Matrix Transposition** ===
Transpose of a m\*n matrix, A’ is the matrix obtained by interchanging rows and columns of A. It is called A transpose. Clearly, is a n\*m matrix.

```
If A =    [ 3 4 5 ]
  	  [ 1 3 5 ]

```

```
Then A’ =   [ 3 1 ]
	    [ 4 3 ]
	    [ 5 5 ]

```

### **Multiplication**

Multiplication of any two arbitrary matrices is not possible. For two matrices to be compatible for multiplication, the number of columns in the first matrix should be equal to the number of rows in the second matrix.

That is, for two matrices A and B, AB exists only if the dimension of A is m\*n and the dimension of B is n\*q. AB has dimension m\*q.

To multiply two matrices, we take the dot product of rows and columns, i.e., say,

```
If A  =   [ 1 2 3 ]
	  [ 4 5 6 ]

```

```
And B =   [  1 5 1 ]
	  [  3 2 0 ]
	  [ -1 1 3 ]

```

```

Then let C = A(2*3) * B(3*3). Clearly,  C would have dimensions 2*3.

```

```
C[1,1] would be given as dot product of  *the First row of A* and  *the First Column of B*

```

```
[1 2 3].[1 3 -1] = (1*1 + 2*3 + 3*-1) = 4

```

```
Similarly, C[1,2] = dot product of *First row of A* and *Second Column of B = 12*

```

```
C[2,1] = dot product of *Second row of A* and *First Column of B = 13* and so on.

```

```
So, C[i,j] = dot product of *i-th row of A* and *j-th Column of B*.

```

```
Solving,  C  =  [  4 12 10 ]
	 	[ 13 36 22 ]

```

### **Minors and Co-factors**

A Minor M[i,j] is the reduced matrix formed by eliminating i-th row and j-th column from the matrix.

```
For Example,    A    =    [ 1 2 3 ]
	   		  [ 3 4 5 ]
	   		  [ 5 6 7 ]

```

```
M[1,1] =  [ 4 5 ]    		M[1,2] = [ 3 5 ]
         [ 6 7 ]			 [ 5 7 ]

```

```
M[2,1] = [ 3 5 ]   		M[2,2] =  [ 1 3 ]
         [ 5 7 ]			  [ 5 7 ]

```

Co-factor in a matrix is the signed version of Minor.

> C[i,j] = (-1)^(i+j) \* M[i,j]
>
>

### **Adjoint**

Adjoint (or Adjugate) of a matrix is the matrix obtained by taking the transpose of the cofactor matrix of a given square matrix is called its Adjoint or Adjugate matrix. The Adjoint of any square matrix ‘A’ (say) is represented as Adj(A).

### **Determinant**

Determinant of a Matrix is a number associated with a Matrix. It is only defined for square matrices.

```
void minor(int mat[N][N], int temp[N][N], int p, int q, int n)
{
int i = 0, j = 0;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
if (row != p && col != q)
{
temp[i][j++] = mat[row][col];
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}

```

```
int determinant(int mat[N][N], int n)
{
int D = 0;
if (n == 1)
return mat[0][0];
int temp[N][N];
int sign = 1;
for (int f = 0; f < n; f++)
{
minor(mat, temp, 0, f, n);
D += sign * mat[0][f] * determinant(temp, n - 1);
sign = -sign;
}
return D;
}

```