Find the sum of Eigen Values of the given N*N matrix

Given an N*N matrix mat[][], the task is to find the sum of Eigen values of the given matrix.

Examples:

Input: mat[][] = {
{2, -1, 0},
{-1, 2, -1},
{0, -1, 2}}
Output: 6

Input: mat[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}}
Output: 34

Approach: The sum of Eigen values of a matrix is equal to the trace of the matrix. The trace of an n × n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define N 4
  
// Function to return the sum of eigen
// values of the given matrix
int sumEigen(int mat[N][N])
{
    int sum = 0;
  
    // Calculate the sum of
    // the diagonal elements
    for (int i = 0; i < N; i++)
        sum += (mat[i][i]);
  
    return sum;
}
  
// Driver code
int main()
{
    int mat[N][N] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
  
    cout << sumEigen(mat);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
static int N = 4;
  
// Function to return the sum of eigen
// values of the given matrix
static int sumEigen(int mat[][])
{
    int sum = 0;
  
    // Calculate the sum of
    // the diagonal elements
    for (int i = 0; i < N; i++)
        sum += (mat[i][i]);
  
    return sum;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int mat[][] = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
  
    System.out.println (sumEigen(mat));
}
}
  
// The code is contributed by Tushil.. 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
N=4
  
# Function to return the sum of eigen
# values of the given matrix
def sumEigen(mat):
  
    sum = 0
  
    # Calculate the sum of
    # the diagonal elements
    for i in range(N):
        sum += (mat[i][i])
  
    return sum
  
  
# Driver code
mat= [ [ 1, 2, 3, 4 ],
    [ 5, 6, 7, 8 ],
    [ 9, 10, 11, 12 ],
    [ 13, 14, 15, 16 ] ]
  
print(sumEigen(mat))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
          
static int N = 4;
  
// Function to return the sum of eigen
// values of the given matrix
static int sumEigen(int [,]mat)
{
    int sum = 0;
  
    // Calculate the sum of
    // the diagonal elements
    for (int i = 0; i < N; i++)
        sum += (mat[i,i]);
  
    return sum;
}
  
// Driver code
static public void Main ()
{
      
    int [,]mat = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
  
    Console.Write(sumEigen(mat));
}
}
  
// The code is contributed by ajit... 

chevron_right


Output:

34


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.