#include <iostream>
#include <vector>
#include <stdexcept>
#include <numeric> // For std::inner_product
template <typename T>
class Matrix {
private:
int rows;
int cols;
std::vector<std::vector<T>> data;
public:
// Constructor
Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, std::vector<T>(cols, T())) {}
// Getter for Rows
int getRows() const { return rows; }
// Getter for Columns
int getCols() const { return cols; }
// Overload [] for easier access (Row Major)
std::vector<T>& operator[](int row) {
if (row < 0 || row >= rows) {
throw std::out_of_range("Row index out of range.");
}
return data[row];
}
const std::vector<T>& operator[](int row) const {
if (row < 0 || row >= rows) {
throw std::out_of_range("Row index out of range.");
}
return data[row];
}
// Matrix Addition
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw std::invalid_argument("Matrices are not the same size for addition.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = data[i][j] + other[i][j];
}
}
return result;
}
// Matrix Subtraction
Matrix operator-(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw std::invalid_argument("Matrices are not the same size for subtraction.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = data[i][j] - other[i][j];
}
}
return result;
}
// Matrix Multiplication
Matrix operator*(const Matrix& other) const {
if (cols != other.rows) {
throw std::invalid_argument("Incompatible matrices for multiplication.");
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.cols; ++j) {
result[i][j] = std::inner_product(data[i].begin(), data[i].end(), other[j].begin(), T());
}
}
return result;
}
// Matrix Transpose
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[j][i] = data[i][j];
}
}
return result;
}
// Determinant Calculation (for 2x2 matrices)
T determinant() const {
if (rows != 2 || cols != 2) {
throw std::invalid_argument("Determinant is only supported for 2x2 matrices.");
}
return data[0][0] * data[1][1] - data[0][1] * data[1][0];
}
// Matrix Inversion (for 2x2 matrices)
Matrix inverse() const {
if (rows != 2 || cols != 2) {
throw std::invalid_argument("Inverse is only supported for 2x2 matrices.");
}
T det = determinant();
if (det == T()) {
throw std::runtime_error("Matrix is singular and cannot be inverted.");
}
Matrix inv(2, 2);
inv[0][0] = data[1][1] / det;
inv[0][1] = -data[0][1] / det;
inv[1][0] = -data[1][0] / det;
inv[1][1] = data[0][0] / det;
return inv;
}
// Output Matrix (for debugging/testing)
void print() const {
for (const auto& row : data) {
for (const T& val : row) {
std::cout << val << " ";
}
std::cout << "\n";
}
}
};
// Example Usage
int main() {
try {
// Create Matrices
Matrix<double> matA(2, 3);
Matrix<double> matB(3, 2);
// Initialize Matrices (Example Values)
matA[0][0] = 1; matA[0][1] = 2; matA[0][2] = 3;
matA[1][0] = 4; matA[1][1] = 5; matA[1][2] = 6;
matB[0][0] = 7; matB[0][1] = 8;
matB[1][0] = 9; matB[1][1] = 10;
matB[2][0] = 11; matB[2][1] = 12;
// Operations
std::cout << "Matrix A:\n";
matA.print();
std::cout << "\nMatrix B:\n";
matB.print();
Matrix<double> matC = matA * matB; // Multiplication Example
std::cout << "\nMatrix A * B:\n";
matC.print();
Matrix<double> matD = matA.transpose(); // Transpose Example
std::cout << "\nMatrix A Transpose:\n";
matD.print();
// 2x2 Matrix Example for Determinant and Inverse
Matrix<double> matE(2, 2);
matE[0][0] = 4; matE[0][1] = 7;
matE[1][0] = 2; matE[1][1] = 6;
std::cout << "\nMatrix E:\n";
matE.print();
std::cout << "\nDeterminant of Matrix E: " << matE.determinant() << "\n";
Matrix<double> matF = matE.inverse(); // Inverse Example
std::cout << "\nMatrix E Inverse:\n";
matF.print();
// Attempting to add incompatible matrices will throw an exception
// Matrix<double> matG = matA + matB; // Uncomment to see exception handling
} catch (const std::exception& e) {
std::cerr << "Exception Caught: " << e.what() << '\n';
}
return 0;
}