Overview

SiLi is a great autostorage only library (no heap allocation).
It uses c++20 and its purpose is to be used on embedded system
The main idea is to use fixed size matrices and fixed size views onto these matrices.

Examples

Example of Element Access

Example

#include <SiLi/SiLi.h> #include <SiLi/ostream.h> int main() { auto a = SiLi::Matrix{{{3, 4}, {5, 6}}}; std::cout << a << "\n"; // prints {{3, 4}, // {5, 6}} a(0, 0) = a(0, 1) * 2; std::cout << a << "\n"; // prints {{8, 4}, // {5, 6}} }
Example of Matrix Multiplication:

Example

#include <SiLi/SiLi.h> #include <SiLi/ostream.h> int main() { auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2}, {4, 5}, {3, 6}}; auto c = a * b; // c is of type SiLi::Matrix<2, 2, int> std::cout << c << "\n"; // prints {{40, 68}, // {53, 88}} }
Example of Matrix inversion:

Example

#include <SiLi/SiLi.h> #include <SiLi/ostream.h> int main() { auto a = SiLi::Matrix{{{3, 4}, {5, 6}}}; auto b = inv(a); std::cout << b << "\n"; \\ prints {{ -3, 2}, {2.4, -1.5}} }
Example of view

Example

#include <SiLi/SiLi.h> #include <SiLi/ostream.h> int main() { auto a = SiLi::Matrix{{{ 3, 4, 5}, { 6, 7, 8}, { 9, 10, 11}}; std::cout << a << "\n"; // prints {{ 3, 4, 5}, // { 6, 7, 8}, // { 9, 10, 11}} auto v = view<1, 1, 3, 3>(a); // view to the lower right 2x2 (7, 8, 10, 11) matrix std::cout << v << "\n"; // prints {{ 7, 8}, // {10, 11}} v *= 2.; std::cout << v << "\n"; // prints {{ 7, 8}, // {10, 11}} std::cout << a << "\n"; // prints {{ 3, 4, 5}, // { 6, 14, 16}, // { 9, 20, 22}} }

Concept of a _concept::Matrix. - Matrix

template <typename T> concept Matrix = is_matrix<T>::value or is_view<T>::value;
Abstract concept of a matrix. This can be either a Matrix or a View. Both can be used everywhere the _concept::Matrix concept is needed.

Concept of a _concept::Vector. - Vector

template <typename T> concept Vector = is_vector_v<T>;
Abstract concept of a vector. This can be either a Matrix or a View where either columns or rows is 1.

Represents a matrix - Matrix

template<int _rows, int _cols, typename T> requires (_rows >= 0 and _cols >= 0) class Matrix<_rows, _cols, T>;
Fullfills the _concept::Matrix concept.
* If _rows or _cols is 1 it fullfills _concept::Vector.

Template Parameters

  • _rows - number of rows of the matrix, must be larger or equal to zero
  • _cols - number of columns of the matrix, must be larger or equal to zero
  • T - type of the elements

Methods

  • data() - returns pointer to the underlying data structure
  • m(row,col) - access element at row and col

Represents a view onto a matrix - View

template<int _rows, int _cols, int _stride, typename T, bool _transposed> requires (_rows >= 0 and _cols >= 0 and _stride >= 0) class View<_rows, _cols, _stride, T, _transposed>;
Fullfills the _concept::Matrix concept.
If _rows or _cols is 1 it fullfills the _concept::Vector concept.

Template Parameters

  • _rows - number of rows of the view, must be larger or equal to zero
  • _cols - number of columns of the view, must be larger or equal to zero
  • _stride - length of the stride
  • T - type of the elements
  • _transposed - indicates if index access of _rows and _cols should be inverted to simulate transpose access

Methods

  • data() - returns pointer to the underlying data structure
  • m(row,col) - access element at row and col

Access element - at(M&& m)

template <int Row, int Col, _concept::Matrix M> constexpr auto at(M&& m) -> auto&;
Checking accessibility at compile time.

Template Parameters

  • Row - access row. Valid value [0-Row)
  • Col - access column. Validvalue [0-Col)

Parameters

Return

returns reference to underlying element

Example

auto m = SiLi::Matrix{{{3, 4}, {5, 6}}}; at<0, 0>(m) = 5; at<0, 1>(m) = 6; std::cout << m << "\n"; // prints {{5, 6}, {5, 6}}

Number of rows - rows(M const& m)

template <_concept::Matrix M> constexpr auto rows(M const& m);

Parameters

Return

number of rows

Example

auto m = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; std::cout << rows(m) << "\n"; // prints 2

Number of columns - cols(M const& m)

template <_concept::Matrix M> constexpr auto cols(M const& m);

Parameters

Return

number of columns

Example

auto m = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; std::cout << cols(m) << "\n"; // prints 3

Elementwise comparision - operator==(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> requires (L::Rows == R::Rows and L::Cols == R::Cols) constexpr auto operator==(L const& l, R const& r) -> bool;

Parameters

Return

true if all elements are equal, otherwise false
l and r must have same dimensions

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto b = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto c = SiLi::Matrix{{{15, 20, 35}, {25, 30, 43}}}; std::cout << (a == b) << "\n"; // prints 1 std::cout << (a == c) << "\n"; // prints 0

Elementwise comparision - operator!=(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> requires (L::Rows == R::Rows and L::Cols == R::Cols) constexpr auto operator!=(L const& l, R const& r) -> bool;

Parameters

Return

false if all element are equal, otherwise true
l and r must have same dimensions

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto b = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto c = SiLi::Matrix{{{15, 20, 35}, {25, 30, 43}}}; std::cout << (a != b) << "\n"; // prints 0 std::cout << (a != c) << "\n"; // prints 1

Diagonal view - view_diag(M&& m)

template <_concept::Matrix M> constexpr auto view_diag(M&& m);

Parameters

Return

View representing a column vector that gives access to the diagonal of m

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto v = view_diag(a); std::cout << rows(v) << "x" << cols(v) << "\n"; // prints 2x1 std::cout << v(0) << " " << v(1) << "\n"; // prints 15 and 30 v(0) = 7; v(1) = 8; std::cout << a << "\n"; // prints {{ 7, 20, 35} {25, 8, 40}}

Diagonal - diag(M const& m)

template <_concept::Matrix M> constexpr auto diag(M const& m);

Parameters

Return

Matrix as a column vector with the diagonal values of m

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto v = diag(a); std::cout << rows(v) << "x" << cols(v) << "\n"; // prints 2x1 std::cout << v(0) << " " << v(1) << "\n"; // prints 15 and 30

Super diagonal view - view_upper_diag(M&& m)

template <_concept::Matrix M> constexpr auto view_upper_diag(M&& m);

Parameters

Return

View representing a column vector that gives access to the super diagonal of m
The super diagonal is the diagonal above the diagonal

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto v = view_upper_diag(a); std::cout << rows(v) << "x" << cols(v) << "\n"; // prints 2x1 std::cout << v(0) << " " << v(1) << "\n"; // prints 20 and 40 v(0) = 7; v(1) = 8; std::cout << a << "\n"; // prints {{15, 7, 35} {25, 30, 8}}

View - view(M&& m)

template <int start_row, int start_col, int end_row, int end_col, _concept::Matrix M> requires (end_row <= rows_v<M> and end_col <= cols_v<M>) constexpr auto view(M&& m);

Parameters

  • start_row - starting row
  • start_col - starting column
  • end_row - end row (exclusive). Use SiLi::End to indicate rows(v).
  • end_col - end column (exclusive). Use SiLi::End to indicate cols(v).
  • v - _concept::Matrix

Return

View
Returns a view onto a matrix

Example

auto a = SiLi::Matrix{{{ 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}}; auto v0 = view<1, 1, 3, 3>(a); std::cout << rows(v0) << "x" << cols(v0) << "\n"; // prints 2x2 std::cout << v0 << "\n"; // prints {{ 7, 8}, 12, 13} auto v1 = view<2, 3, SiLi::End, SiLi::End>(a); std::cout << rows(v1) << "x" << cols(v1) << "\n"; // prints 2x2 std::cout << v1 << "\n"; // prints {{14, 15}, 19, 20}

Joins rows - join_rows(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> requires (L::Rows == R::Rows) constexpr auto join_rows(L const& l, R const& r);

Parameters

Return

Matrix of joined rows of l and r
l and r must have same number of rows.

Example

auto a = SiLi::Matrix{{{ 1, 2, 3}, {11, 12, 13}}}; auto b = SiLi::Matrix{{{ 4, 5, 6}, {14, 15, 16}}}; auto v = join_rows(a, b); std::cout << rows(v) << "x" << cols(v) << "\n"; // prints 2x6 std::cout << v << "\n"; // prints {{ 1, 2, 3, 4, 5, 6}, {11, 12, 13, 14, 15, 16}}

Joins columns - join_cols(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> requires (L::Cols == R::Cols) constexpr auto join_cols(L const& l, R const& r);

Parameters

  • l - Viewable
  • r - Viewable

Return

Matrix of joined columns of l and r
l and r must have same number of columns.

Example

auto a = SiLi::Matrix{{{ 1, 2, 3}, {11, 12, 13}}}; auto b = SiLi::Matrix{{{ 4, 5, 6}, {14, 15, 16}}}; auto v = join_cols(a, b); std::cout << rows(v) << "x" << cols(v) << "\n"; // prints 4x3 std::cout << v << "\n"; // prints {{ 1, 2, 3}, {11, 12, 13}, { 4, 5, 6}, {14, 15, 16}}

Compute determinant - det(M const& m)

template <_concept::Matrix M> requires (M::Rows == M::Cols and M::Rows > 3) constexpr auto det(M const& m);

Parameters

Return

Determinant of m

Example

auto a = SiLi::Matrix{{{ 1, 2}, {11, 12}}}; auto v = det(a); std::cout << v << "\n"; // prints -10

Cross product of two vectors. - cross(L const& l, R const& r)

template <_concept::Vector L, _concept::Vector R> requires (length_v<R> == 3 and length_v<L> == 3) constexpr auto cross(L const& l, R const& r);

Parameters

Return

Matrix of size 3×1 with cross product of l and r.

Example

auto a = SiLi::Matrix{{{ 1}, { 2}, { 3}}}; auto b = SiLi::Matrix{{{ 4}, { 5}, { 6}}}; auto v = cross(a, b); std::cout << v << "\n"; // prints {{-3}, { 6}, {-3}}

Sum of all elements. - sum(M const& m)

template <_concept::Matrix M> constexpr auto sum(M const& m);

Parameters

Return

Sum off all elements of m.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto c = sum(a) std::cout << c << "\n"; // prints 33

Sum of each row. - sum_rows(M const& m)

template <_concept::Matrix M> constexpr auto sum_rows(M const& m);

Parameters

Return

Column vector with the sums of each row of m

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto c = sum_rows(a) std::cout << c << "\n"; // prints {{14} {19}}

Sum of each column. - sum_cols(M const& m)

template <_concept::Matrix M> constexpr auto sum_cols(M const& m);

Parameters

Return

Row vector with the sums of each column of m

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto c = sum_cols(a) std::cout << c << "\n"; // prints {{8, 10, 15}}

Compute inverse - inv(M m)

template <_concept::Matrix M> requires (M::Rows == M::Cols and M::Rows > 3) constexpr auto inv(M m) -> std::tuple<typename M::value_t, M>;

Parameters

Return

Returns tuple of determinant and inverse, if detereminant is zero the inverse is invalid.

Example

auto a = SiLi::Matrix{{{ 1., 2.}, {11., 12.}}}; auto [v, m] = inv(a); std::cout << v << "\n"; // prints -10 std::cout << m << "\n"; // prints {{-1.2, 0.2}, { 1.1, -0.1}}

Transposed view - view_trans(M&& m)

template <_concept::Matrix M> constexpr auto view_trans(M&& m);

Parameters

Return

Matrix of the transposed of m

Example

auto a = SiLi::Matrix{{{ 1, 2, 3}, {11, 12, 4}}}; auto v = view_trans(a); std::cout << m << "\n"; // prints {{1, 11}, {2, 12}, {3, 13}}

Transposed - trans(M&& m)

template <_concept::Matrix M> constexpr auto trans(M&& m);

Parameters

Return

Matrix of the transposed of m

Example

auto a = SiLi::Matrix{{{ 1, 2, 3}, {11, 12, 4}}}; auto v = trans(a); std::cout << m << "\n"; // prints {{1, 11}, {2, 12}, {3, 13}}

Identity matrix - makeI()

template <int Rows, int Cols, typename T> constexpr auto makeI();

Parameters

  • Rows - number of rows
  • Cols - number of columns
  • T - type of each element

Return

matrix with ones on the diagonal

Example

auto a = SiLi::makeI<3, 4, int>(); std::cout << a << "\n"; // prints {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}

Identity matrix - makeI()

template <int N, typename T> constexpr auto makeI();

Parameters

  • N - size of the matrix
  • T - type of each element

Return

matrix with ones on the diagonal

Example

auto a = SiLi::makeI<3, int>(); std::cout << a << "\n"; // prints {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}

Compute abs - abs(M const& m)

template <_concept::Matrix M> constexpr auto abs(M const& m);

Parameters

Return

the absolute values

Example

auto a = SiLi::Matrix{{{ -3.}, { -4.}, { -5.}}}; auto v = abs(a); std::cout << v << "\n"; // prints {{3}, // {4}, // {5}}

Copy of m - operator+(M const& m)

template <_concept::Matrix M> constexpr auto operator+(M const& m);

Parameters

Return

copy of m with + applied to every element

Example

auto m = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto m2 = +m; std::cout << m << "\n"; std::cout << m2 << "\n";

Elementwise addition - operator+(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> constexpr auto operator+(L const& l, R const& r);

Parameters

Return

Matrix with l and r
l and r must have the same dimension.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2, 3}, {4, 5, 6}}}; auto c = a + b; std::cout << c << "\n"; // prints {{4, 6, 10}, {9, 11, 14}}

Elementwise addition - operator+=(L&& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> constexpr auto operator+=(L&& l, R const& r) -> auto&;

Parameters

Return

reference to l
l and r must have the same dimension.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2, 3}, {4, 5, 6}}}; auto a += b; std::cout << a << "\n"; // prints {{4, 6, 10}, {9, 11, 14}}

Elementwise negation - operator-(M const& m)

template <_concept::Matrix M> constexpr auto operator-(M const& m);

Parameters

Return

Matrix with negated elements

Example

auto m = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto m2 = -m; std::cout << m << "\n"; std::cout << m2 << "\n";

Elementwise subtraction - operator-(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> constexpr auto operator-(L const& l, R const& r);

Parameters

Return

matrix with (l - r)
l and r must have the same dimension.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2, 3}, {4, 5, 6}}}; auto c = a - b; std::cout << c << "\n"; // prints {{2, 2, 4}, {1, 1, 2}}

Elementwise subtraction - operator-=(L& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> constexpr auto operator-=(L& l, R const& r) -> auto&;

Parameters

Return

reference to l
l and r must have the same dimension.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2, 3}, {4, 5, 6}}}; auto a += b; std::cout << a << "\n"; // prints {{2, 2, 4}, {1, 1, 2}}

Matrix multiplication - operator*(L const& l, R const& r)

template <_concept::Matrix L, _concept::Matrix R> requires (L::Cols == R::Rows) constexpr auto operator*(L const& l, R const& r);

Parameters

Return

Matrix with result of l and r multiplication
l must have the same number of columns as r has rows.

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto b = SiLi::Matrix{{{1, 2}, {4, 5}, {3, 6}}; auto c = a * b; // c is of type SiLi::Matrix<2, 2, int> std::cout << c << "\n"; // prints {{40, 68}, {53, 88}}

Scalar multiplication - operator*(L const& l, value_t const& s)

template <_concept::Matrix L> constexpr auto operator*(L const& l, value_t<L> const& s);

Parameters

Return

Matrix with each element multiplied with s

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto c = a * 5; std::cout << c << "\n"; // prints {{15, 20, 35}, {25, 30, 40}}

Scalar multiplication - operator*(value_t const& s, R&& r)

template <_concept::Matrix R> constexpr auto operator*(value_t<R> const& s, R&& r);

Parameters

Return

Matrix with each element multiplied with s

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto c = 5 * a; std::cout << c << "\n"; // prints {{15, 20, 35}, {25, 30, 40}}

Scalar multiplication - operator*=(L&& l, value_t const& s)

template <_concept::Matrix L> constexpr auto operator*=(L&& l, value_t<L> const& s) -> auto&;

Parameters

Return

Referenc to l

Example

auto a = SiLi::Matrix{{{3, 4, 7}, {5, 6, 8}}}; auto a *= 5; std::cout << a << "\n"; // prints {{15, 20, 35}, {25, 30, 40}}

Divide elements by a scalar - operator/(L const& l, value_t const& s)

template <_concept::Matrix L> constexpr auto operator/(L const& l, value_t<L> const& s);

Parameters

Return

Matrix same size and type as l

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; auto c = a / 5; std::cout << c << "\n"; // prints {{3, 4, 7}, {5, 6, 8}}

Divide elementwise by a scalar - operator/=(L&& l, value_t const& s)

template <_concept::Matrix L> constexpr auto operator/=(L&& l, value_t<L> const& s) -> L&;

Parameters

Return

reference to l

Example

auto a = SiLi::Matrix{{{15, 20, 35}, {25, 30, 40}}}; a /= 5; std::cout << c << "\n"; // prints {{3, 4, 7}, {5, 6, 8}}

Access element - at(V&& v)

template <int Idx, _concept::Vector V> constexpr auto at(V&& v) -> auto&;
Checking accessibility at compile time.

Parameters

Return

returns reference to underlying element

Example

auto m = SiLi::Matrix{{{3}, {5}}}; at<0>(m) = 5; at<1>(m) = 6; std::cout << m << "\n"; // prints {{5}, {6}}

Compute norm - norm(V const& v)

template <_concept::Vector V> constexpr auto norm(V const& v);

Parameters

Return

the norm (also considered as the length).

Example

auto a = SiLi::Matrix{{{ 3.}, { 4.}, { 5.}}}; auto v = norm(a); std::cout << v << "\n"; // prints 7.0711

Dot product of two vectors - dot(L const& l, R const& r)

template <_concept::Vector L, _concept::Vector R> requires(length_v<L> == length_v<R>) constexpr auto dot(L const& l, R const& r);

Parameters

Return

the dot product (also called scalar product) of l and r. l and r must be vectors of the same length

Example

auto a = SiLi::Matrix{{{ 1}, { 2}, { 3}}}; auto b = SiLi::Matrix{{{ 4}, { 5}, { 6}}}; auto v = dot(a, b); std::cout << v << "\n"; // prints 32

Outer product - outerProd(L const& l, R const& r)

template <_concept::Vector L, _concept::Vector R> constexpr auto outerProd(L const& l, R const& r);

Parameters

Return

Matrix with the outer product of l and r

Example

auto a = SiLi::Matrix{{{ 1}, { 2}, { 3}}}; auto b = SiLi::Matrix{{{ 4}, { 5}, { 6}}}; auto v = outerProd(a, b); std::cout << v << "\n"; // prints {{ 4, 5, 6}, // { 8, 10, 12}, // {12, 15, 18}}