# Swift Program to Multiply two Matrices by Passing Matrix to a Function

In this article, we will learn how to write a swift program to multiply two matrices by passing the matrix to a function.

A matrix is a mathematical structure in which the elements are present in rows and columns format. For example, the first element is present at the a00 location, the second at a01, and so on. So to multiply two matrices, we multiply the mth row of the first matrix by an nth column of the second matrix and add the products. This will create an element at the mth row and nth columns of the resultant matrix. For example −

Matrix 1 −

$\mathrm{\begin{bmatrix}2 & 2 & 2 \newline3 & 3 & 3 \newline4 & 4 & 4\end{bmatrix}}$

Matrix 2 −

$\mathrm{\begin{bmatrix}1 & 1 & 1 \newline1 & 1 & 1 \newline2 & 2 & 2\end{bmatrix}}$

So the product = Matrix 1 * Matrix 2

$\mathrm{\begin{bmatrix}(2^{*}1+2^{*}1+2^{*}2) & (2^{*}1+2^{*}1+2^{*}2) & (2^{*}1+2^{*}1+2^{*}2) \newline(3^{*}1+3^{*}1+3^{*}2) & (3^{*}1+3^{*}1+3^{*}2) & (3^{*}1+3^{*}1+3^{*}2) \newline(4^{*}1+4^{*}1+4^{*}2) & (4^{*}1+4^{*}1+4^{*}2) & (4^{*}1+4^{*}1+4^{*}2)\end{bmatrix}}$

$\mathrm{\begin{bmatrix}8 & 8 & 8 \newline12 & 12 & 12 \newline16 & 16 & 16\end{bmatrix}}$

## Algorithm

Step 1 − Define the size of the rows and columns.

Step 2 − Create a function.

Step 3 − In this function create an empty matrix to store result of same number of rows and columns.

Step 4 − Run nested for loop to iterate through each element of both matrices.

Step 5 − Multiple the element at [x][z] position of matrix1 with each element of the row of matrix2 and add the values, store the values at [x][y] position of the resultant matrix. This process will continue till the lest element of the matrix1.

Step 6 − Create two matrices of the same type along with same number of rows and columns.

Step 7 − Call the function and pass these matrices in it.

Step 8 − Print the resultant matrix.

## Example

Following the Swift program multiply two matrices by-passing the matrix to a function.

import Foundation
import Glibc

// Size of the matrix
var row = 4
var col = 4

// Function to multiply two matrices
func multiplyMatrix(mxt1:[[Int]], mxt2:[[Int]]) {

// Creating 4x4 matrix to store the result
var Mul = Array(repeating: Array(repeating: 0, count: 4), count: 4)

// Multiply two matrices

// Using * operator
for x in 0..<row {
for y in 0..<col {
for z in 0..<row {
Mul[x][y] += mxt1[x][z] * mxt2[z][y]
}
}
}
print("Resultant matrix:")
for x in 0..<row {
for y in 0..<col {
print(Mul[x][y], terminator:" ")
}
print("\n")
}
}

// Creating 4x4 matrix of integer type
var matrix1 : [[Int]] = [[1, 1, 4, 3], [1, 1, 1, 1], [5, 5, 2, 2], [4, 3, 3, 4]]
print("Matrix 1:")
for x in 0..<row {
for y in 0..<col {
print(matrix1[x][y], terminator:" ")
}
print("\n")
}
var matrix2 : [[Int]] = [[1, 1, 4, 4], [3, 2, 4, 5], [5, 6, 3, 2], [4, 5, 6, 3]]
print("Matrix 2:")
for x in 0..<row {
for y in 0..<col {
print(matrix2[x][y], terminator:" ")
}
print("\n")
}

// Calling the function to display result
multiplyMatrix(mxt1:matrix1, mxt2:matrix2)


## Output

Matrix 1:
1 1 4 3
1 1 1 1
5 5 2 2
4 3 3 4

Matrix 2:
1 1 4 4
3 2 4 5
5 6 3 2
4 5 6 3

Resultant matrix:
36 42 38 26
13 14 17 14
38 37 58 55
44 48 61 49


Here in the above code, we create two 4x4 matrices along with values. Now we create a function to find the product of two matrices. In this function, we create an empty matrix to store the result. In addition, run nested for loop to iterate through each element of both the matrices. Now we multiply the element of matrix1 at [x][z] position with each element of the row of the matrix2 using the * operator and add the values and store the result at [x][y] position of the resultant matrix. Repeat this process for all the elements of matrix1.

## Conclusion

Therefore, this is how we can multiply two matrices by passing the matrix to a function. Using this method we can also multiply matrices of different sizes like 4x4, 6x3, and 2x3.

Updated on: 09-Jan-2023

163 Views