LinearSystemsToolkit is a library for solving systems of linear equations or SLE for short. It includes methods for matrix factorization and iterative methods like Preconditioned Conjugate Gradients method(PCG).
Clone this repository into some folder, for example, LST
:
git clone https://github.com/MaxGoryunov/LinearSystemsToolkit.git
Then in your Visual Studio project's properties in All configurations
>
C/C++
> General
> Additional Include Directories
add
*path_to_LST/*LST/LinearSystemsToolkit
folder.
After that in All configurations
> Linker
> General
>
Additional Library Directories
add *path_to_LST/*LST
folder.
If you have any questions, ask them at Discussions.
To add two vectors:
Vector* left = vectorCreate(3); // {1, 2, 3}
Vector* right = vectorCreate(3); // {4, 5, 6}
Vector* sum = vectorSum(left, right); // {5, 7, 9}
To subtract a vector from another vector:
Vector* left = vectorCreate(3); // {7, 8, 9}
Vector* right = vectorCreate(3); // {4, 5, 6}
Vector* diff = vectorDiff(left, right); // {3, 3, 3}
To calculate vector's second norm:
Vector* vec = vectorCreate(3); // {3, 4, 12}
double norm = secondNorm(vec); // norm == 13
To scale a vector by some factor:
Vector* vec = vectorCreate(3); // {3, 4, 5}
Vector* scaled = vectorScale(vec, 3); // {9, 12, 15}
To create a linearly spaced vector:
Vector* points = linspace(0, 1, 5); // {0, 0.25, 0.5, 0.75, 1.0}
To multiply two vectors:
Vector* row = vectorCreate(3); // {1, 2, 3}
Vector* col = vectorCreate(3); // {4, 5, 6}
double mult = scalarMultiplication(row, col); // 1*4 + 2*5 + 3*6 == 32
To check for vector equality:
Vector* left = vectorCreate(3); // {1, 2, 3}
Vector* right = vectorCreate(3); // {1, 2, 3}
Vector* other = vectorCreate(3); // {4, 5, 6}
assert(1 == vectorEqual(left, right));
assert(0 == vectorEqual(left, other));
To read a sparse matrix from a file:
Sparse* S = sparseFromFile("source.txt", 3); // a 3x3 matrix
To add or subtract two matrices:
Sparse* A = sparseCreate(3); // a 3x3 matrix
Sparse* B = sparseCreate(3); // a 3x3 matrix
Sparse* S = sparseSum(A, B); // S == A + B
Sparse* D = sparseDiff(A, B); // D == A - B
To scale a matrix by some factor:
double scale = 3;
Sparse* A = sparseCreate(3);
Sparse* B = sparseScale(A, scale); // B == 3 * A
To multiply a matrix by a vector:
Sparse* A = sparseCreate(3); // a 3x3 matrix
Vector* x = vectorCreate(3); // a 3x1 vector
Vector* b = sparseMultiplyByVector(A, x); // A * x == b
To create a normalized orthogonal matrix from a given vector seed:
Vector* w = vectorCreate(3); // a 3x1 vector
Sparse* Q = sparseOrthogonalFromSeed(w);
To create a diagonal matrix from a vector of given values:
Vector* diag = vectorCreate(3); // {1, 2, 3}
Sparse* D = sparseDiagonalFromVector(diag);
// {1, 0, 0;
// 0, 2, 0;
// 0, 0, 3}
All aforementioned operations are also accessible for regular matrices in Matrix.h
module.
Fork this repository, then create a folder for it and install GCC. After it run
gcc -fprofile-arcs -ftest-coverage -O0 -o AllTest LinearSystemsToolkit/*.c -lm
./AllTest
This script will run all tests, they are not supposed to fail. Commit changes, open a Pull Request. Your Pull Request will be reviewed and eventually accepted if it does not fail our build pipeline.