Skip to content
Draken Wan edited this page Apr 12, 2024 · 13 revisions

Matrix Library Wiki

Introduction

The Matrix library provides a matrix class template with various matrix operations. The library is designed for numerical computations and supports common linear algebra operations and more are incoming. It aims to be user-friendly, efficient, and flexible. This documentation section will be expanded as more functionalities and features are added.

Table of Contents

Installation

  1. You just need to clone or downloade the repository.
     git clone https://github.com/DrakenWan/Matrix
    
  2. and include the matrix header file and get started. #include "matrix.h"
  3. For the above part, you can also add the path to the matrix.h file with compiler flag like so g++ -o yourexecutable yourfile.cpp -I'path/to/matrix.h'.

Basic Usage

All the Matrix library variables and functions belong to the linear namespace. You can either precede your program with using namespace linear or don't forget to add the linear namespace identifier with scope resolution (::) operator in front of every functionality belonging to the library. All the usage examples given below for reference assume we have gone with the former method.

Matrix Initialization

There are several ways to initialize a matrix instance. Below are examples show few ways to do so.

// create a 3x3 double matrix of zeros
matrix<double> mat_zeros = zeros(3);

// create a 4x4 integer matrix of ones
matrix<int> mat_ones = ones<int>(1);

// create a 2x2 double matrix filled with value `1.5`
matrix<double> mat_values(2, 2, 1.5);

// create a matrix from a 2D std::vector
std::vector<std::vector<double>> data = {{1., 2.}, {3., 5.}};
matrix<double> mat_from_vector(data);

// initialize using iniatilizer list
matrix<double> mat_from_list = {{1., 5., 3.}, {2., 1.5, 3.}};

// initialize an empty matrix (you can fill it later with some utility methods)
matrix<double> empty_mat;

Matrix Operations

Basic matrix operations are provided in the library. Below are usage examples of each of them

// Addition
matrix<double> C = mat_values + mat_zeros;

// Subtraction
matrix<double> E = mat_values - mat_ones;

// Matrix Multiplication
matrix<double> D = mat_values & mat_ones;

// Transpose of a matrix
std::vector<std::vector<double>> vec = {{1., 2., 3.}, {4., 5., 4.}};
matrix<double> E(vec); //creates a 2x3 matrix
matrix<double> transE = E.T(); // 3x2 transpose returned to `transE`
// or
matrix<double> transE2 = ~E; 
// ~ is the operator for calculating transpose (also works on constant matrices)

More comprehensive examples and usage cases can be found in the Examples folder. There are variety of code files there and more are being added.

Non-member operations

The library provides several standalone non-member functions for either creating specific matrices or perform a matrix operation or linear algebra algorithm. Below are usage examples, as reference, for some of them.

Identity matrix

//Initialize a 3x3 identity matrix
matrix<double> Identity = eye<double>(3);

Diagonal matrix

//Initialize a 3x3 diagonal matrix with specific value
matrix<double> diagonal_mat = diagonal<double>(3, 2.0);

Random matrix

//Initialize a 3x3 matrix with value in range [0,1] from uniform normal distribution.
matrix<double> random_matrix = randomUniform(3);

File Operations

The file operations member functions that help load or save a matrix into the filesystem. The file operations will be expanded soon. Currently, the code is rigid especially regarding save path and file extension type. The file extension of the saved matrix is '.linmat'.

Both saveMatrix and loadMatrix operations are similar in their signature. Both need a string literal that is the name of the file as and the second parameter is the folder path where you will save at or load from. The folder path by default is set as saves (you might have to create the folder where your source file is) or else you can give the path to your folder.

// save matrix to a file
mat_values.saveMatrix("matrix_data");

//load matrix from file
matrix<double> loaded_matrix;
loaded_matrix.loadMatrix("matrix_data");

Display a Matrix

You can use display member function to show the matrix contents. It takes a constant std::string as it's argument to display before the matrix. By default if you don't input any value it will print "Matrix:-" in the console.

//defining a 2x2 float matrix
matrix<float> floatMat = {{1., 2.}, {0.5, 1.}};
floatMat.display("floatMat:-");

Examples

Examples are provided as cpp source files in Examples folder. The folder will be kept updated with new example files.

Contribution

Contributions to the Matrix C++ library are welcome. Feel free to further customize or add specific details based on your library's features and structure.

Opening an Issue

If you encounter a bug, have a suggestion, or need assistance, please open an issue on the GitHub repository. Provide detailed information about the problem or suggestion to help us understand and address it promptly.

Making a Contribution

We encourage contributions to enhance the library. To contribute:

  1. Fork the repository: https://github.com/DrakenWan/Matrix/.
  2. Create a new branch for your changes: git checkout -b feature/your-feature.
  3. Make your modifications and improvements.
  4. Test your changes thoroughly.
  5. Commit your changes: git commit -m "Add your commit message here".
  6. Push the changes to your fork: git push origin feature/your-feature.
  7. Create a pull request on the GitHub repository to propose your changes.

Coding Guidelines

When contributing, follow the coding guidelines to maintain consistency and readability. Make sure to document new features and functions to help others understand and use them effectively.

Thank you for considering contributing to the Matrix C++ library! Your input and efforts make the library more robust and valuable for the community.