# NaiveMatrixLib 帆帆的简易矩阵计算库

A simple C++ stdlib-based complex & real matrix library, with matrix inversion, left division (A\b) and determinant calculation.

## Features 特点

• Designed for users who don't want to use large linear algebra libs.
• Only used C++ standard library, easy to learn and modify (Each file less than 600 lines).
• Header files only, separated complex and real matrix library.
• No recursive algorithm (using LU and Cholesky decomposition). Reliable for 1000 x 1000 and larger matrices.
• 如果你不想使用大型线性代数库来计算这些，那你来对地方了。（什么，你只是想交作业？）
• 仅使用C++标准库，无论是学习思维还是修改都很简单（每份代码都少于600行）。
• 仅使用头文件即可，复数和实数矩阵库是分开的。
• 没有递归运算（基于 LU 和 Cholesky 分解）。可对1000x1000及更大的矩阵使用。

## Available Functions 可用函数

• rank: Matrix rank (Cholesky decomposition)
• det: Matrix determinant calculation
• inv: LU decomposition-based matrix inversion
• pinv: pinv(G) = inv(G' * G) * G' (WARNING: full-rank matrix only!)
• pinv2: Moore-Penrose pseudoinversion (same as pinv(G) in MATLAB)
• leftDiv: x = A \ b, using Moore-Penrose pinv, NOT same as MATLAB for a singular matrix
• rank: 计算矩阵秩 (Cholesky 分解)
• det: 计算矩阵行列式
• inv: 求逆矩阵，基于 LU 分解
• pinv: 经典伪逆，pinv(G) = inv(G' * G) * G' (警告：只能用于满秩矩阵！)
• pinv2: Moore-Penrose 伪逆 (与 MATLAB 中的 pinv(G) 相同)
• leftDiv: 反斜杠除法 x = A \ b, 使用 Moore-Penrose 伪逆, 对奇异矩阵的处理与 MATLAB 不同

## Examples (Complex Matrices Only) 用例（仅列举复数矩阵）

```#include "matBasic_complex.hpp"
int main(int argc, char **argv)
{
i_complex_matrix matA = {
{{1.0, 0.0}, {0.0, 1.0}, {4.0, 0.0}},
{{0.0, 5.0}, {1.0, 0.0}, {4.0, 0.0}},
{{8.0, 0.0}, {1.0, 1.0}, {0.0, 0.0}}};
showMatrix(matA, "A");
std::cout << "rank(A) = " << rank(matA) << "\n";
std::cout << "det(A) = " << det(matA) << "\n";
showMatrix(inv(matA), "inv(A)");
showMatrix(pinv(matA), "pinv(A)");
showMatrix(pinv2(matA), "pinv2(A)");

i_complex_matrix matb = {
{{1.0, 0.0}},
{{1.0, 2.0}},
{{0.0, 3.0}}};
showMatrix(matb, "b");
showMatrix(leftDiv(matA, matb), "A \\ b");
showMatrix(matMul(inv(matA), matb), "inv(A) * b");
showMatrix(matMul(pinv(matA), matb), "pinv(A) * b");
showMatrix(matMul(pinv2(matA), matb), "pinv2(A) * b (== A \\ b)");
std::cin.get();
return 0;
}```

### Output 输出

``````A : 3 x 3 Complex Matrix:
row[1]: 1,  0+1i,  4;
row[2]: 0+5i,  1,  4;
row[3]: 8,  1+1i,  0;

rank(A) = 3
det(A) = (-56,48)
inv(A) : 3 x 3 Complex Matrix:
row[1]: 0.00588235+0.0764706i,  -0.00588235-0.0764706i,  0.0764706-0.00588235i;
row[2]: -0.329412-0.282353i,  0.329412+0.282353i,  0.217647-0.170588i;
row[3]: 0.177941+0.0632353i,  0.0720588-0.0632353i,  -0.0617647-0.0529412i;

pinv(A) : 3 x 3 Complex Matrix:
row[1]: 0.00588235+0.0764706i,  -0.00588235-0.0764706i,  0.0764706-0.00588235i;
row[2]: -0.329412-0.282353i,  0.329412+0.282353i,  0.217647-0.170588i;
row[3]: 0.177941+0.0632353i,  0.0720588-0.0632353i,  -0.0617647-0.0529412i;

pinv2(A) : 3 x 3 Complex Matrix:
row[1]: 0.00588235+0.0764706i,  -0.00588235-0.0764706i,  0.0764706-0.00588235i;
row[2]: -0.329412-0.282353i,  0.329412+0.282353i,  0.217647-0.170588i;
row[3]: 0.177941+0.0632353i,  0.0720588-0.0632353i,  -0.0617647-0.0529412i;

b : 3 x 1 Complex Matrix:
row[1]: 1;
row[2]: 1+2i;
row[3]: 0+3i;

A \ b : 3 x 1 Complex Matrix:
row[1]: 0.170588+0.217647i;
row[2]: -0.0529412+1.31176i;
row[3]: 0.535294-0.0411765i;

inv(A) * b : 3 x 1 Complex Matrix:
row[1]: 0.170588+0.217647i;
row[2]: -0.0529412+1.31176i;
row[3]: 0.535294-0.0411765i;

pinv(A) * b : 3 x 1 Complex Matrix:
row[1]: 0.170588+0.217647i;
row[2]: -0.0529412+1.31176i;
row[3]: 0.535294-0.0411765i;

pinv2(A) * b (== A \ b) : 3 x 1 Complex Matrix:
row[1]: 0.170588+0.217647i;
row[2]: -0.0529412+1.31176i;
row[3]: 0.535294-0.0411765i;
``````

## Reference 参考资料

*** Project by Fanseline in Ericsson ***

##### Header only, single file, simple and efficient C++ library to compute the signed distance function to a triangle mesh

TriangleMeshDistance Header only, single file, simple and efficient C++11 library to compute the signed distance function to a triangle mesh. The dist

##### Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib

matplotlib-cpp Welcome to matplotlib-cpp, possibly the simplest C++ plotting library. It is built to resemble the plotting API used by Matlab and matp

##### Simple long integer math library for C++

SLIMCPP Simple long integer math library for C++ SLIMCPP is C++ header-only library that implements long integers that exceed maximum size of native t

##### Windows Calculator: A simple yet powerful calculator that ships with Windows

The Windows Calculator app is a modern Windows app written in C++ that ships pre-installed with Windows. The app provides standard, scientific, and programmer calculator functionality, as well as a set of converters between various units of measurement and currencies.

##### A C library for statistical and scientific computing

Apophenia is an open statistical library for working with data sets and statistical or simulation models. It provides functions on the same level as t

##### a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions

linmath.h -- A small library for linear math as required for computer graphics linmath.h provides the most used types required for programming compute

##### C++ Mathematical Expression Parsing And Evaluation Library

C++ Mathematical Expression Toolkit Library Documentation Section 00 - Introduction Section 01 - Capabilities Section 02 - Example Expressions

##### libmpc++ is a C++ header-only library to solve linear and non-linear MPC

libmpc++ libmpc++ is a C++ library to solve linear and non-linear MPC. The library is written in modern C++17 and it is tested to work on Linux, macOS

• #### Cross platform tests

(1) I changed matBasic_testUtil.hpp so that it will be able to compile and run on VC++. (2) I also added a check to the test itself ( make sure that A^-1 * A == Identity) and not just show the results.

opened by axelrodR 0

###### A matrix header-only library, uses graphs internally, helpful when your matrix is part of a simulation where it needs to grow many times (or auto expand)

GraphMat Header-only Library Matrix implemented as a graph, specially for the use case when it should be auto expanding at custom rate, specially in s

3 Oct 25, 2021
###### nml is a simple matrix and linear algebra library written in standard C.

nml is a simple matrix and linear algebra library written in standard C.

45 Dec 9, 2022
###### Library for nonconvex constrained optimization using the augmented Lagrangian method and the matrix-free PANOC algorithm.

alpaqa Alpaqa is an efficient implementation of the Augmented Lagrangian method for general nonlinear programming problems, which uses the first-order

21 Dec 9, 2022
###### Kraken is an open-source modern math library that comes with a fast-fixed matrix class and math-related functions.

Kraken ?? Table of Contents Introduction Requirement Contents Installation Introduction Kraken is a modern math library written in a way that gives ac

24 Nov 30, 2022
###### A modern, C++20-native, single-file header-only dense 2D matrix library.

A modern, C++20-native, single-file header-only dense 2D matrix library. Contents Example usage creating matrices basic operations row, col, size, sha

62 Dec 17, 2022
###### Minimal matrix implementation in C++

min-matrix Minimal matrix implementation in C++. ?? Pull Request ?? ?? Post Issues ?? Brief ?? Eigen compiles too slow? ?? ?? Just want something simp

5 Dec 17, 2021
###### Some out-of-core matrix operations via HDF5.

hdfmat Version: 0.2-2 License: BSD 2-Clause Author: Drew Schmidt This package provides some out-of-core matrix operations via HDF5. The main purpose o

5 Jan 9, 2022
###### A Code Base for Matrix operations in C++

SimpM A Code Base for Matrix operations in C++ Dependencies: GNU Bignum Library: https://gmplib.org Needed to be installed on you computer. Check your

3 Oct 27, 2022
###### A toolkit for making real world machine learning and data analysis applications in C++

dlib C++ library Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real worl

11.6k Dec 31, 2022
###### Bolt is an algorithm for compressing vectors of real-valued data and running mathematical operations directly on the compressed representations.

Bolt is an algorithm for compressing vectors of real-valued data and running mathematical operations directly on the compressed representations.

2.3k Dec 30, 2022