Giter Site home page Giter Site logo

eidla's Introduction

image

Another linear algebra library

This is an own C++ implementation of common linear algebra operations and data types. The main motivation behind this project is to refresh and deepen my knowledge in numerics and matrix operations. At a certain stage, I might be able to use my own implementation instead of including Eigen or OpenCV.

Each library function has corresponding unit tests. Google's gtest was used as a testing library.

Attention

As there were cloners... the Eigen value / vector computation works only for symmetric matrices and the singular value decomposition (SVD) is numerically unstable. Do not use this library in self driving cars or rockets :)

Usage

The main type is the templated class Matrix. There exist different constructors, such

#include "matrix.hpp"

// creates an empty 4 x 6 double matrix
Matrix<double> m1 = Matrix<double>(4, 6);

// creates 2 x 2 int matrix with content [1,2; 3,4]
int  data[] = {1, 2,  3, 4};
Matrix<int> m2 = Matrix<int>(2, 2, data);

Matrix elements can be accessed with

// assign the value 5 to element m = 0, n = 1
m2(0,1) = 5;

// read the value on position m = 1, n = 0
int v10 = m2(1,0);

// read entire row 1
Matrix<int> row1 = m2.row(1);

// write entire row 1 to row 0.
m2.setRow(0, row1);

//  1x2 submatrix starting at (0,0)
Matrix<int> subMat = m2.subMatrix(0,0, 1,2);

Assign special content

// creates a 3x3 identity matrix
Matrix<int> ident3x3 = Matrix<int>::identity(3);

// sets an existing matrix to identity
m2.setToIdentity();

// fills the entire matrix with the value "pi"
auto piMat = Matrix<double>(2,4);
piMat.fill(3.14);

// creates a 3x2 random matrix with values between 0 - 10
auto randomMat = Matrix<int>::random(3,2,0,10);

Arithmetic functions

int  d1[] = {1, 2, 3, 4};
Matrix<int> a = Matrix<int>(2, 2, d1);

int  d2[] = {1, 2, 3, 4};
Matrix<int> b = Matrix<int>(2, 2, d2);

// add the elements of two matrices
Matrix<int> sum = a + b;

// subtract
Matrix<int> sub = a - b;

// matrix multiplication
Matrix<int> prod = a * b;

// matrix multiplication with scalar
Matrix<int> scale = a * 5;

Matrix properties

auto mat = Matrix<int>(2,2);

// get number of rows and columns
mat->rows();
mat->cols();

// get matrix rank
size_t rank = mat.getRank();

// compute matrix inverse
bool invertable;
Matrix<double> inv = mat.inverted(&invertable);

// compute matrix determinant
bool   detOk;
double det = mat.determinant(&ok);

Matrix transformations

// LU decomposition
Decomposition::LUResult luRes = Decomposition::luDecomposition(mat);
Matrix<double> lowerTriangle = luRes.L;
Matrix<double> upperTriangle = luRes.U;

// Echelon transformations
Matrix<double> echelon = Transformation::echelon(mat);
Matrix<double> reducedEchelon = Transformation::reduced_echelon(mat);

// compute adjugate (also first minors and cofactor matrix)
Matrix<double> adjMat = mat.adjugate();

// Eigen value and Eigen vector computation. Only works for symmetric matrices (yet).
std::vector<Decomposition::EigenPair> eig = Decomposition::eigen(mat);

Example Application

Image compression by applying SVD (singular value decomposition). The computation of this 700 x 500 image took about 14 hours :)

image

License

MIT license: To my understanding, you can do whatever you wish with the code. However, no warranty is given that the written code is correct.

eidla's People

Contributors

eidelen avatar

Stargazers

LeeSeWoong avatar Pragnesh avatar philippe_nuaa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.