Golang Program To Find the Trace and Normal of a given Matrix


In this tutorial we will write an article to find the normal and trace of a matrix. A matrix is considered to be normal if its square root equals the sum of the squares of each member and trace is the total of a matrix's diagonal elements.

Finding Normal of the given Matrix

Algorithm

STEP 1 − First we need to import the fmt and math package.

STEP 2 − Create a function to find the Normal of the matrix. This function uses two for loops to find the square of each element.

STEP 3 − Update the sum variable by adding the square of each element to it. Return the sum variable.

STEP 4 − Start the main() function. Initialize a matrix and print it on the screen.

STEP 5 − call the findNormal() function by passing the matrix as well as its rank to the function as arguments.

STEP 6 − Store the result from the function in a new variable called normal and print it on the screen using fmt.Println() function.

Example 1

Let us write a go program to find the normal of 2 X 2 matrix using for loops.

package main
import (
   "fmt"
   "math"
)

// function to create normal
func findNormal(mat [2][2]int, n int) float64 {
   var sum int = 0
   for i := 0; i < n; i++ {
      for j := 0; j < n; j++ {
         sum += mat[i][j] * mat[i][j]
      }
   }
   var sum1 float64
   sum1 = float64(sum)
   return math.Sqrt(sum1)
}
func main() {
   mat := [2][2]int{
      {1, 2},
      {5, 6},
   }
   fmt.Println("The given matrix is: \n")
   for i := 0; i < 2; i++ {
      for j := 0; j < 2; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   normal := findNormal(mat, 2)
   fmt.Println("Normal of Matrix is:", normal)
}

Output

The given matrix is: 

1	2	
5	6	

Normal of Matrix is: 8.12403840463596

Example 2

In this example we will write a go program to find normal of a 3 X 3 matrix.

package main
import (
   "fmt"
   "math"
)
func findNormal(mat [3][3]int, n int) float64 {
   var sum int = 0
   for i := 0; i < n; i++ {
      for j := 0; j < n; j++ {
         sum += mat[i][j] * mat[i][j]
      }
   }
   var sum1 float64
   sum1 = float64(sum)
   return math.Sqrt(sum1)
}
func main() {
   mat := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   fmt.Println("The given matrix is: \n")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
   normal := findNormal(mat, 3)
   fmt.Println("\n Normal of Matrix is:", normal)
}

Output

The given matrix is: 

0	1	2	
4	5	6	
8	9	10	

 Normal of Matrix is: 18.083141320025124

Finding Trace of the given Matrix

Algorithm

STEP 1 − First we need to import the fmt and math package.

STEP 2 − Create a function to find the tace of the matrix. This function uses a for loops to find the sum of diagonal elements.

STEP 3 − Update the sum variable by adding each diagonal element to it. Return the sum variable.

STEP 4 − Start the main() function. Initialize a matrix and print it on the screen.

STEP 5 − call the findNormal() function by passing the matrix as well as its rank as arguments to function.

STEP 6 − Store the result from the function in a new variable called trace and print it on the screen using fmt.Println() function.

Example

Let us write a golang program to find the trace of a 2 X 2 matrix.

package main
import (
   "fmt"
)

// function to create trace
func findTrace(mat [2][2]int, n int) int {
   var sum int = 0
   for i := 0; i < n; i++ {
      sum += mat[i][i]
   }
   return sum
}
func main() {
   mat := [2][2]int{
      {10, 20},
      {50, 60},
   }
   fmt.Println("The given matrix is: \n")
   for i := 0; i < 2; i++ {
      for j := 0; j < 2; j++ {
         fmt.Print(mat[i][j], "\t")
      }
      fmt.Println()
   }
   trace := findTrace(mat, 2)
   fmt.Println("\nTrace of Matrix is:", trace)
}

Output

The given matrix is: 

10	20	
50	60	

Trace of Matrix is: 70

Conclusion

Successfully compiled and executed a golang program to find the normal and trace of matrices along with examples.

Updated on: 10-Feb-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements