diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-09-23 22:29:01 +0200 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-09-23 22:29:01 +0200 |
| commit | 5b822e1422ed808de9144da8ed38037b17dd0b65 (patch) | |
| tree | 07c258a6e9f050cf5c62070e04d991c0b46764dc /modules/codec | |
| parent | 62500475dfdc59040e4e3da508d61c4a3b50a773 (diff) | |
| download | forstio-forstio-5b822e1422ed808de9144da8ed38037b17dd0b65.tar.gz | |
Added tests for dot product
Diffstat (limited to 'modules/codec')
| -rw-r--r-- | modules/codec/c++/math.hpp | 2 | ||||
| -rw-r--r-- | modules/codec/tests/math.cpp | 38 |
2 files changed, 33 insertions, 7 deletions
diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp index d33482d..ec051df 100644 --- a/modules/codec/c++/math.hpp +++ b/modules/codec/c++/math.hpp @@ -36,7 +36,7 @@ data<schema::Vector<T,D>, Encoding> normalize(const data<schema::Vector<T,D>>& i saw::data<schema::Vector<T,D>, Encoding> out; for(uint64_t i = 0u; i < D; ++i){ - out.at({{i}}).set(input.at({{i}}).get() / sqrt_inp_dot.at({}).get()); + out.at({{i}}) = (input.at({{i}}) / sqrt_inp_dot.at({})); } return out; } diff --git a/modules/codec/tests/math.cpp b/modules/codec/tests/math.cpp index cc371dd..6f60e9c 100644 --- a/modules/codec/tests/math.cpp +++ b/modules/codec/tests/math.cpp @@ -76,7 +76,7 @@ SAW_TEST("Math/Dot"){ SAW_EXPECT(c.at({}).get() == 3.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) ); } -SAW_TEST("Math/Tensor Mult Scalar"){ +SAW_TEST("Math/Dot Self"){ using namespace saw; data<sch::Vector<sch::Float64, 2u>> a; @@ -84,14 +84,40 @@ SAW_TEST("Math/Tensor Mult Scalar"){ a.at({{0u}}) = 2.0; a.at({{1u}}) = 1.0; } - data<sch::Vector<sch::Float64, 2u>> b; + + auto c = math::dot(a,a); + + SAW_EXPECT(c.at({}).get() == 5.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) ); +} + +SAW_TEST("Math/Dot and Sqrt"){ + using namespace saw; + + data<sch::Vector<sch::Float64, 2u>> a; { - b.at({{0u}}) = -1.0; - b.at({{1u}}) = 5.0; + a.at({{0u}}) = 3.0; + a.at({{1u}}) = 4.0; } - auto c = math::dot(a,b); + auto c = math::dot(a,a); - SAW_EXPECT(c.at({}).get() == 3.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) ); + auto d = math::sqrt(c); + + SAW_EXPECT(d.at({}).get() == 5.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(d.at({}).get()) ); +} + +SAW_TEST("Math/Normalize"){ + using namespace saw; + + data<sch::Vector<sch::Float64, 2u>> a; + { + a.at({{0u}}) = 5.0; + a.at({{1u}}) = 0.0; + } + + auto c = math::normalize(a); + + SAW_EXPECT(c.at({{0u}}).get() == 1.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(c.at({{0u}}).get()) ); + SAW_EXPECT(c.at({{1u}}).get() == 0.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(c.at({{1u}}).get()) ); } } |
