From f398d046ed5ff6e3bf803c3f16d1fb470b464bdb Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 22 Sep 2025 15:53:45 +0200 Subject: Add sqrt and normalize --- modules/codec/c++/data_math.hpp | 11 +++++++++++ modules/codec/c++/math.hpp | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'modules') diff --git a/modules/codec/c++/data_math.hpp b/modules/codec/c++/data_math.hpp index efdca7d..51657c7 100644 --- a/modules/codec/c++/data_math.hpp +++ b/modules/codec/c++/data_math.hpp @@ -120,6 +120,17 @@ public: return c; } + + template + data, encode::Native> cast_to(){ + data, encode::Native> native_change; + rank_iterator::in_fixed_bounds([&](const data, encode::Native>& index) -> error_or{ + native_change.at(index) = at(index).template cast_to(); + return make_void(); + }); + + return inner_change; + } }; } diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp index ddeea3f..67d8ed3 100644 --- a/modules/codec/c++/math.hpp +++ b/modules/codec/c++/math.hpp @@ -21,5 +21,23 @@ data, Encoding> dot(const data, Encoding>& return val; } + +template +data,Encoding> sqrt(const data,Encoding>& inp){ + data,Encoding> out; + out.at({}).set(std::sqrt(inp.at({}).get())); + return out; +} + +template +data, Encoding> normalize(const data>& input ) { + auto inp_dot = dot(input,input); + auto sqrt_inp_dot = sqrt(inp_dot); + + saw::data, Encoding> out; + out.at({{0u}}).set(out.at({{0u}}).get() / sqrt_inp_dot.at({}).get()); + out.at({{1u}}).set(out.at({{1u}}).get() / sqrt_inp_dot.at({}).get()); + return out; +} } } -- cgit v1.2.3