Golang Program To Find The Trace And Normal Of A Matrix


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

Golang Program To Find The Normal

Finding Normal of 2x2 Matrix

In this example, we will see how we can find the normal of a 2 X 2 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

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() {

   // initializing a 2 X 2 matrix
   mat := [2][2]int{
      {1, 2},
      {5, 6},
   }
   // printing matrix
   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

Find Normal Of A 3 X 3 Matrix

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

Example

package main
import (
   "fmt"
   "math"
)

// function to find normal
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() {

   // initializing a 3 X 3 matrix
   mat := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   
   // printing matrix
   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

Golang Program To Find The Trace

Finding Trace of 2x2 Matrix

In this example, we will find the trace of a 2 X 2 matrix using user defined function.

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 a 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

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() {

   // initializing a 2 X 2 matrix
   mat := [2][2]int{
      {10, 20},
      {50, 60},
   }
   
   // printing matrix
   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

Find Trace Of A 3 X 3 Matrix

In this example, we will write a go program code to find the trace of 3 X 3 matrix.

Example

package main
import (
   "fmt"
)

// function to create trace
func findTrace(mat [3][3]int, n int) int {
   var sum int = 0
   for i := 0; i < n; i++ {
      sum += mat[i][i]
   }
   return sum
}
func main() {
   
   // initializing a 3 X 3 matrix
   mat := [3][3]int{
      {5, 7, 9},
      {2, 4, 6},
      {1, 3, 8},
   }
   
   // printing matrix
   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()
   }
   trace := findTrace(mat, 3)
   fmt.Println("\nTrace of Matrix is:", trace)
}

Output

The given matrix is: 

5	7	9	
2	4	6	
1	3	8	

Trace of Matrix is: 17

Conclusion

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

Updated on: 02-Jan-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements