diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-10-12 12:50:40 +0200 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-10-12 12:50:40 +0200 |
| commit | af87db21b8c72d16e4f63aba9fcfecd48167e71d (patch) | |
| tree | 2d65d081ef99ff72cb7beaf7e606c83c3d355417 /modules/codec/c++/math.hpp | |
| parent | e39ee8cb7e664439db06e37fab89c7f570d4da2f (diff) | |
| download | forstio-forstio-af87db21b8c72d16e4f63aba9fcfecd48167e71d.tar.gz | |
Added multiplication for matrices and vectors
Diffstat (limited to 'modules/codec/c++/math.hpp')
| -rw-r--r-- | modules/codec/c++/math.hpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp index 095c8ca..a8bbba1 100644 --- a/modules/codec/c++/math.hpp +++ b/modules/codec/c++/math.hpp @@ -53,5 +53,33 @@ data<schema::Vector<T,D>, Encoding> vectorize_data(const data<schema::FixedArray return vec_data; } + +template<typename T, uint64_t M, uint64_t N, uint64_t K, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING> +data<schema::Matrix<T,M,N>, Encoding> multiply(const data<schema::Matrix<T,M,K>, Encoding>& left, const data<schema::Matrix<T,K,N>, Encoding>& right){ + data<schema::Matrix<T,M,N>, Encoding> lr; + + for(uint64_t i = 0u; i < M; ++i){ + for(uint64_t j = 0u; j < N; ++j){ + for(uint64_t k = 0u; k < K; ++k){ + lr.at({{i,j}}) = lr.at({{i,j}}) + left.at({{i,k}}) * right.at({{k,j}}); + } + } + } + + return lr; +} + +template<typename T, uint64_t M, uint64_t N, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING> +data<schema::Vector<T,M>, Encoding> multiply(const data<schema::Matrix<T,M,N>, Encoding>& left, const data<schema::Vector<T,N>, Encoding>& right){ + data<schema::Vector<T,M>, Encoding> lr; + + for(uint64_t i = 0u; i < M; ++i){ + for(uint64_t j = 0u; j < N; ++j){ + lr.at({{i}}) = lr.at({{i}}) + left.at({{i,j}}) * right.at({{j}}); + } + } + + return lr; +} } } |
