C++ Online Compiler
Example: Matrix Trace and Normal Calculation in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Matrix Trace and Normal Calculation #include <iostream> #include <vector> // For std::vector #include <cmath> // For std::sqrt // Function to calculate the trace of a matrix // Requires a square matrix double calculateTrace(const std::vector<std::vector<int>>& matrix) { // Step 1: Check if the matrix is square if (matrix.empty() || matrix[0].empty()) { std::cerr << "Error: Matrix is empty." << std::endl; return 0.0; // Or throw an exception } size_t rows = matrix.size(); size_t cols = matrix[0].size(); if (rows != cols) { std::cerr << "Error: Trace can only be calculated for square matrices." << std::endl; return 0.0; // Or throw an exception } // Step 2: Sum the diagonal elements double trace = 0.0; for (size_t i = 0; i < rows; ++i) { trace += matrix[i][i]; } return trace; } // Function to calculate the Frobenius normal of a matrix double calculateFrobeniusNormal(const std::vector<std::vector<int>>& matrix) { // Step 1: Check if the matrix is empty if (matrix.empty()) { std::cerr << "Error: Matrix is empty." << std::endl; return 0.0; // Or throw an exception } // Step 2: Sum the squares of all elements double sumOfSquares = 0.0; for (const auto& row : matrix) { for (int element : row) { sumOfSquares += static_cast<double>(element) * element; } } // Step 3: Return the square root of the sum of squares return std::sqrt(sumOfSquares); } int main() { // Step 1: Define a sample matrix // This is a 3x3 matrix. For non-square matrices, trace calculation will fail. std::vector<std::vector<int>> myMatrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Step 2: Display the matrix for verification std::cout << "Given Matrix:" << std::endl; for (const auto& row : myMatrix) { for (int element : row) { std::cout << element << "\t"; } std::cout << std::endl; } std::cout << std::endl; // Step 3: Calculate and display the trace double traceValue = calculateTrace(myMatrix); if (traceValue != 0.0 || (myMatrix.size() == myMatrix[0].size())) { // Simple check for valid trace std::cout << "Trace of the matrix: " << traceValue << std::endl; } // Step 4: Calculate and display the Frobenius normal double frobeniusNormalValue = calculateFrobeniusNormal(myMatrix); std::cout << "Frobenius Normal of the matrix: " << frobeniusNormalValue << std::endl; // Example with a non-square matrix for normal only std::cout << "\n--- Testing with a non-square matrix ---" << std::endl; std::vector<std::vector<int>> nonSquareMatrix = { {1, 2}, {3, 4}, {5, 6} }; std::cout << "Given Non-Square Matrix:" << std::endl; for (const auto& row : nonSquareMatrix) { for (int element : row) { std::cout << element << "\t"; } std::cout << std::endl; } std::cout << std::endl; double traceNonSquare = calculateTrace(nonSquareMatrix); // This will show an error if (traceNonSquare != 0.0 || (nonSquareMatrix.size() == nonSquareMatrix[0].size())) { std::cout << "Trace of the non-square matrix: " << traceNonSquare << std::endl; } double normalNonSquare = calculateFrobeniusNormal(nonSquareMatrix); std::cout << "Frobenius Normal of the non-square matrix: " << normalNonSquare << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS