C Program To Find Normal And Trace Of Matrix

  • Make a matrix and list every element in it.
  • Take the total of all the array's members and compute its square root to determine the matrix's normal.
  • Add the key diagonal members to assess the matrix's trace.
  • Example:

Filename: Normal_Trace.c

Example

#include <stdio.h>

#include <math.h>



//The Frobenius matrix normal can be calculated using this function.

double matrixNormal(int r, int c, double matrix[r][c]) {

    double normal = 0.0;

    for (int i = 0; i < r; i++) {

        for (int j = 0; j < c; j++) {

            normal += pow(matrix[i][j], 2);

        }

    }

    return sqrt(normal);

}



//function used for finding the trace

double matrix_Trace(int num, double matrix[num][num4]) {

    double traceofmatrix = 0.0;

    for (int i = 0; i < num; i++) {

        traceofmatrix += matrix[i][i];

    }

    return traceofmatrix;

}



int main() {

    int r, c;



    //declaring the rows and columns

    printf("Enter the rows of a matrix: ");

    scanf("%d", &r);

    printf("Enter the columns of a matrix:");

    scanf("%d", &c);



    // To perform a trace computation, verify that the matrix is square.

    if (r != c) {

        printf("Only square matrices can be used to calculate trace.\n");

        return 1;

    }



    double matrix[r][c];



    // input array of matrix elements

    printf("Enter the elements of the matrix:\n");

    for (int i = 0; i < r; i++) {

        for (int j = 0; j < c; j++) {

            scanf("%lf", &matrix[i][j]);

        }

    }



    //Calculation to find the normal of a matrix

    double normal = matrixNormal(r, c, matrix);

    printf("The Frobenius Norm: %.2lf\n", normal);



    //Identify the trace and print it (if the matrix is square).

    if (r == c) {

        double traceofmatrix = matrix_Trace(c, matrix);

        printf("The Trace of the matrix : %.2lf\n", traceofmatrix);

    }



    return 0;

}

Output:

Output

Enter the rows of a matrix: 4

Enter the columns of a matrix:4

Enter the elements of the matrix:

1 2 3 4

5 6 7 8

3 4 5 7

2 4 7 9

The Frobenius Norm: 21.28

The Trace of the matrix: 21.00

Explanation:

The given C code computes the Frobenius norm and trace of a matrix. It begins by including essential libraries for handling input/output and mathematical computations. The program takes inputs such as the matrix, the row count (r), the column count (c), and then proceeds to calculate the Frobenius norm. This norm is a measure of the "size" of a matrix.

The norm is calculated by taking the square root of the sum of the squares of each element in the matrix during iteration. When provided with a square matrix and its size represented by the variable num, the function matrix_Trace computes the trace of the matrix. This trace is determined by adding up the values along the diagonal of the matrix. The function then returns the sum of these diagonal elements after iterating through them.

The process to calculate the Frobenius norm of the matrix relies on the matrixNormal function, which takes the matrix dimensions r and c as inputs, in addition to the matrix. The Frobenius norm is obtained by displaying the outcome, stored in a variable named normal, and outputted to the console with two decimal places.

The software employs the matrix_Trace function to compute the trace value when dealing with a square matrix. This function requires the dimension 'c' as input (since it's a square matrix) along with the matrices to perform the calculation. The result, stored in a variable called traceofmatrix, is then displayed on the terminal with a precision of up to two decimal places, representing the trace of the matrix.

The program collects the matrix's components in the main function and checks to make sure they are square before tracing them. After that, it gathers the matrix elements the user enters and initializes a matrix array. MatrixNormal is used for calculating and displaying the Frobenius norm. Matrix_Trace is used to compute and display the trace if the matrix is square. To indicate that the program has run successfully, it returns 0 .

Input Required

This code uses input(). Please provide values below: