Swift Program to Find the Trace and Normal of a given Matrix


In this article, we will learn how to write a swift program to find trace and normal of a given matrix.

Calculating the Trace of a given Matrix

Trace is known as the sum of primary diagonal elements of the given square matrix. For example, we have a 3x3 square matrix −

2 3 4
3 4 6
1 3 2

So the primary diagonal elements are 2, 4 and 2. Hence the trace of the given 3x3 matrix is 2+4+2 = 8.

Algorithm

  • Step 1 − Define the size of the matrix.

  • Step 2 − Create a function

  • Step 3 − Declare a variable to store the sum.

  • Step 4 − Run a for loop to iterate through each element and add all the diagonal elements with each other.

sum += mxt[y][y] 
  • Step 5 − Create a matrix

  • Step 6 − Call the function and pass the matrix into it.

  • Step 7 − Print the output

Example

Following Swift program to find trace of a given matrix.

import Foundation
import Glibc

// Size of the matrix 
var size = 5

// Function to find a trace of the given matrix
func Trace(mxt:[[Int]]){
   var sum = 0
   
   for y in 0..<size{
      sum += mxt[y][y] 
   }
   print("Trace of the matrix is:", sum) 
}

// Creating 5x5 matrix of integer type
var matrix : [[Int]] = [[1, 3, 4, 1, 2], [2, 8, 2, 5, 6], 
   [3, 4, 3, 4, 3], [8, 4, 3, 4, 6],
   [7, 5, 5, 9, 1]]

print("Matrix:")
for x in 0..<size{
   for y in 0..<size{
      print(matrix[x][y], terminator:" ")
   }
   print("\n")
}
// Calling the function
Trace(mxt:matrix)

Output

Matrix:
1 3 4 1 2 

2 8 2 5 6 

3 4 3 4 3 

8 4 3 4 6 

7 5 5 9 1 

Trace of the matrix is: 17

Here in the above code, we have a 5x5 matrix. Now we create a function to find the trace. So to calculate the trace we run a for loop to find the primary diagonal elements and then add them together.

Calculating the Normal of a given Matrix

Normal is defined as the square root of the sum of the square of the elements of the given matrix. For example, we have a 3x3 square matrix −

2 3 4
3 4 6
1 3 2

So first, we find the sum of the squares of the elements −

$\mathrm{Sq \:=\: 2^2 \:+\: 3^2\: +\: 4^2\: +\: 3^2\: + \:4^2\: + \:6^2\: +\: 1^2\: + \:3^2\: +\: 2^2 }$

$\mathrm{Sq\: = \:4 + \:9 \:+ \:16 \:+\: 9 \:+ \:16 \:+ \:36 \:+ \:1 \:+ \:9 \:+ \:4}$

$\mathrm{Sq \:=\: 104}$

So the normal is $\mathrm{\sqrt{104}\:=\: 10.198039027185}$

Algorithm

  • Step 1 − Define the size of the matrix.

  • Step 2 − Create a function

  • Step 3 − Declare a variable to store the sum.

  • Step 4 − Run a for loop to iterate through each element and add the square of all the elements with each other.

sum += mxt[x][y] * mxt[x][y]
  • Step 5 − Find the normal by calculating the square root of the sum of the square of the elements using sqrt() function. Int(sqrt(Double(sum)))

  • Step 6 − Create a matrix

  • Step 7 − Call the function and pass the matrix into it.

  • Step 8 − Print the output

Example

Following Swift program to find the normal of a given matrix.

import Foundation
import Glibc

// Size of the matrix 
var size = 4

// Function to find normal of the given matrix
func Normal(mxt:[[Int]]){
   var sum = 0

   for x in 0..<size{
      for y in 0..<size{
         sum += mxt[x][y] * mxt[x][y]
      }
   }
   print("Normal of the matrix is:", Int(sqrt(Double(sum)))) 
}

// Creating 4x4 matrix of integer type
var matrix : [[Int]] = [[1, 3, 4, 1], [2, 2, 2, 5], 
                  [3, 4, 3, 4], [9, 9, 9, 9]]

print("Matrix:")
for x in 0..<size{
   for y in 0..<size{
      print(matrix[x][y], terminator:" ")
   }
   print("\n")
}

// Calling the function
Normal(mxt:matrix)

Output

Matrix:
1 3 4 1 

2 2 2 5 

3 4 3 4 

9 9 9 9 

Normal of the matrix is: 20

Here in the above code, we have a 4x4 matrix. Now we create a function to find the normal. So to calculate normal first we find the sum of the squares of all the matrix elements and then we find the square root of the obtained sum using the sqrt() function. It is a predefined function to calculate the square root of any value. Hence, the normal of the given matrix is 20.

Updated on: 29-Dec-2022

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements