https://github.com/raideno/matrix
C / Cpp program to make it easier to work with matrices and do operations on matrices
https://github.com/raideno/matrix
c cpp math mathematics matrix
Last synced: about 2 months ago
JSON representation
C / Cpp program to make it easier to work with matrices and do operations on matrices
- Host: GitHub
- URL: https://github.com/raideno/matrix
- Owner: raideno
- Created: 2022-11-28T18:13:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-27T18:01:46.000Z (over 2 years ago)
- Last Synced: 2025-05-21T02:09:35.875Z (about 1 year ago)
- Topics: c, cpp, math, mathematics, matrix
- Language: C++
- Homepage:
- Size: 730 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Matrix functions for C++
## Build and run
```sh
make
./program
```
## Quick start
```cpp
#include "lib/matrix/matrix.hpp"
#include "lib/matrix/matrix-double-value.hpp"
int main() {
MatrixClass::srand(1234);
auto *A = MatrixClass::create_matrix(MatrixType::NORMAL, 2, 2);
A->set(0, 0, MatrixDoubleValue(1.0));
A->set(0, 1, MatrixDoubleValue(2.0));
A->set(1, 0, MatrixDoubleValue(3.0));
A->set(1, 1, MatrixDoubleValue(4.0));
A->print();
auto det = A->determinent();
det.print();
printf("\n");
A->inverse()->print()->destroy();
A->destroy();
}
```
## Create and fill
```cpp
#include "lib/matrix/matrix.hpp"
#include "lib/matrix/matrix-int-value.hpp"
// Empty matrix then set values
auto *M = MatrixClass::create_matrix(MatrixType::NORMAL, 3, 3);
for (size_t i = 0; i < 3; ++i)
for (size_t j = 0; j < 3; ++j)
M->set(i, j, MatrixIntValue((int)(i*3 + j)));
// Matrix filled with a constant
auto *K = MatrixClass::create_matrix_with(MatrixType::NORMAL, 3, 3, MatrixIntValue(7));
// Random matrix
MatrixClass::srand(42);
auto *R = MatrixClass::create_matrix_random(MatrixType::NORMAL, 3, 3);
M->print();
K->print();
R->print();
M->destroy();
K->destroy();
R->destroy();
```
## Basic operations
```cpp
#include "lib/matrix/matrix.hpp"
#include "lib/matrix/matrix-double-value.hpp"
auto *A = MatrixClass::create_matrix_with(MatrixType::NORMAL, 2, 2, MatrixDoubleValue(2.0));
auto *B = MatrixClass::create_matrix_with(MatrixType::NORMAL, 2, 2, MatrixDoubleValue(3.0));
// Element-wise operations
A->operator+(B)->print()->destroy();
A->operator-(B)->print()->destroy();
A->operator*(B)->print()->destroy();
A->operator/(B)->print()->destroy();
// With a scalar value (use the value type)
A->operator+(MatrixDoubleValue(1.5))->print()->destroy();
A->operator*(MatrixDoubleValue(10.0))->print()->destroy();
// Transpose and inverse
A->transpose()->print()->destroy();
A->inverse()->print()->destroy();
// Determinant and trace
auto det = A->determinent();
det.print();
printf("\n");
auto tr = A->trace();
tr.print();
printf("\n");
// Matrix multiplication (dot)
auto *C = MatrixClass::matrix_multiplication(A, B);
C->print();
A->destroy();
B->destroy();
C->destroy();
```
## Map and reduce
```cpp
#include "lib/matrix/matrix.hpp"
#include "lib/matrix/matrix-double-value.hpp"
auto *M = MatrixClass::create_matrix_random(MatrixType::NORMAL, 2, 3);
// Map: add 1.0 to every element
M->map(MatrixType::NORMAL, [](size_t i, size_t j, MatrixDoubleValue v) {
return MatrixDoubleValue(v.data + 1.0);
}, true /* inplace */)->print();
// Reduce: sum of all elements (Reducer returns float)
float sum = M->reduce(MatrixType::NORMAL, [](float acc, size_t i, size_t j, MatrixDoubleValue v) {
return acc + (float)v.data;
}, 0.0f);
printf("sum=%f\n", sum);
M->destroy();
```
## Convolution (1D/2D)
```cpp
#include "lib/matrix/matrix.hpp"
#include "lib/math/math.hpp"
#include "lib/matrix/matrix-double-value.hpp"
// 2D convolution example
auto *img = MatrixClass::create_matrix(MatrixType::NORMAL, 3, 3);
img->set(0,0, {1}); img->set(0,1, {2}); img->set(0,2, {3});
img->set(1,0, {4}); img->set(1,1, {5}); img->set(1,2, {6});
img->set(2,0, {7}); img->set(2,1, {8}); img->set(2,2, {9});
auto *kernel = MatrixClass::create_matrix(MatrixType::NORMAL, 3, 3);
kernel->set(0,0, {0}); kernel->set(0,1, {-1}); kernel->set(0,2, {0});
kernel->set(1,0, {-1}); kernel->set(1,1, {5}); kernel->set(1,2, {-1});
kernel->set(2,0, {0}); kernel->set(2,1, {-1}); kernel->set(2,2, {0});
auto *out = matrix_convolution(img, kernel);
out->print();
img->destroy();
kernel->destroy();
out->destroy();
```
## Memory management
```cpp
auto *M = MatrixClass::create_matrix_random(MatrixType::NORMAL, 2, 2);
// ... use M ...
M->destroy();
```