Skip to content

Matrix

The Matrix class is a specialized 2nd-order tensor provided by the txeo library, facilitating intuitive handling of matrix-specific operations.

Overview

The Matrix class in txeo is a specialized tensor explicitly tailored for second-order data structures. It provides simplified constructors, clear interfaces, and seamless interoperability with tensors.


Creating Matrices

Basic Matrix Creation

#include <iostream>
#include "txeo/Matrix.h"

int main() {
    txeo::Matrix<int> matrix(3, 3); // Creates a 3x3 matrix
    std::cout << matrix << std::endl;
}

Initialization with Specific Values

txeo::Matrix<int> matrix(2, 3, 5);  // 2x3 matrix filled with value 5

Initialization from Vector

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6});

Nested Initializer Lists

txeo::Matrix<int> matrix{{1, 2, 3}, {4, 5, 6}};  // 2x3 matrix

Conversion between Matrix and Tensor

Matrix to Tensor (Move Constructor)

txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6});
txeo::Matrix<int> matrix(std::move(tensor));

Tensor to Matrix (Move Semantics)

txeo::Tensor<int> tensor({2, 3}, {1, 2, 3, 4, 5, 6});
auto matrix = txeo::Matrix<int>::to_matrix(std::move(tensor));

Matrix to Tensor (Copy)

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6});
txeo::Tensor<int> tensor = txeo::Matrix<int>::to_tensor(matrix);

Matrix to Tensor (Move Semantics)

txeo::Tensor<int> tensor = txeo::Matrix<int>::to_tensor(std::move(matrix));

Matrix Methods

Method Description
size() Returns total number of matrix elements
to_matrix(tensor) Converts a second-order tensor to matrix (move semantics)
to_tensor(matrix) Converts a matrix to tensor, supports copy and move semantics

Exception Handling

Matrix-related errors throw MatrixError exceptions:

try {
    txeo::Matrix<int> matrix(2, 3, {1,2,3});  // Invalid initialization
} catch (const txeo::MatrixError &e) {
    std::cerr << e.what() << std::endl;
}

Examples

Basic Matrix Manipulation

#include <iostream>
#include "txeo/Tensor.h"
#include "txeo/Matrix.h"

int main() {
    txeo::Matrix<int> mat{{1, 2}, {3, 4}};
    mat(0, 1) = 10;
    std::cout << "Matrix:\n" << mat << std::endl;
}

Matrix and Scalar Operations

#include <iostream>
#include "txeo/Matrix.h"

int main() {
    txeo::Matrix<int> mat(2, 2, {1, 2, 3, 4});
    auto mat2 = mat * 3;  // Scalar multiplication

    std::cout << "Resulting matrix: " << mat2 << std::endl;
    return 0;
}

For detailed API references, see individual method documentation at txeo::Matrix.