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

10 May, 2021
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: 6Input: 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++
Java
Python3
C#
Javascript
// 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 matrixint 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 codeint 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;}
// Java implementation of the approachimport java.io.*; class GFG{     static int N = 4; // Function to return the sum of eigen// values of the given matrixstatic 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 codepublic 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..
# Python3 implementation of the approach N=4 # Function to return the sum of eigen# values of the given matrixdef sumEigen(mat):     sum = 0     # Calculate the sum of    # the diagonal elements    for i in range(N):        sum += (mat[i][i])     return sum  # Driver codemat= [ [ 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
// C# implementation of the approachusing System; class GFG{         static int N = 4; // Function to return the sum of eigen// values of the given matrixstatic 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 codestatic 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...
<script> // Javascript implementation of the approachvar N = 4; // Function to return the sum of eigen// values of the given matrixfunction sumEigen(mat){    var sum = 0;     // Calculate the sum of    // the diagonal elements    for (var i = 0; i < N; i++)        sum += (mat[i][i]);     return sum;} // Driver codevar mat = [ [ 1, 2, 3, 4 ],                  [ 5, 6, 7, 8 ],                  [ 9, 10, 11, 12 ],                  [ 13, 14, 15, 16 ] ];document.write( sumEigen(mat)); </script>
34
 
mohit kumar 29
jit_t
rutvik_56
school-programming
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Efficiently compute sums of diagonals of a matrix
Flood fill Algorithm - how to implement fill() in paint?
Zigzag (or diagonal) traversal of Matrix
Mathematics | L U Decomposition of a System of Linear Equations
Python program to add two Matrices
Unique paths in a Grid with Obstacles
A Boolean Matrix Question
Breadth First Traversal ( BFS ) on a 2D array
Implementation of BFS using adjacency matrix
Check for possible path in 2D matrix