From 2e836ead5c10f00917560e9a98d46b59b2f977d4 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sat, 7 Nov 2026 18:05:26 +0100 Subject: Fixing double bracketing --- modules/codec/c++/data.hpp | 27 ++++++++++++++---- modules/codec/c++/data_math.hpp | 21 ++++++++++++++ modules/codec/c++/math.hpp | 62 +++++++++++++++++++++++++++++++++++++---- modules/codec/tests/math.cpp | 25 +++++++++++++++-- 4 files changed, 122 insertions(+), 13 deletions(-) diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index c43402e..a8b7836 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -330,13 +330,28 @@ public: { } - /* + /** + * Forwarding constructor, so the elements can be written without + * spelling out the inner array. + * + * data>{0,1,2} instead of {{0,1,2}} + * + * The guards keep it out of the way of the copy and the meta schema + * constructor in the single element case. + * + * Explicit for a single element, because a one dimensional index would + * otherwise be convertible to both data and this, which makes + * the two at overloads of data> ambiguous. + */ template - requires (sizeof...(Args) == ct_multiply::value) && - (std::constructible_from, Args> && ...) - constexpr data(Args&&... args): - value_{ { std::forward(args)... } } {} - */ + requires (sizeof...(Args) == ct_multiply::value) + && (sizeof...(Args) > 1u + || (!std::same_as, data> && ...)) + && (!std::same_as, data> && ...) + && (std::constructible_from, Args> && ...) + explicit(sizeof...(Args) == 1u) constexpr data(Args&&... args): + value_{ data{std::forward(args)}... } + {} SAW_DEFAULT_COPY(data); SAW_DEFAULT_MOVE(data); diff --git a/modules/codec/c++/data_math.hpp b/modules/codec/c++/data_math.hpp index 9d07eb3..7caedd9 100644 --- a/modules/codec/c++/data_math.hpp +++ b/modules/codec/c++/data_math.hpp @@ -93,6 +93,27 @@ public: values_{inner_values__} {} + /** + * Forwarding constructor, so a tensor can be written as its elements + * in row major order. + * + * data>{1.0, 2.0, 3.0, 4.0} + * data>{5.1f} + * + * The guards keep it out of the way of the copy constructor and of the + * two constructors above in the single element case. + */ + template + requires (sizeof...(Args) == ct_multiply::value) + && (sizeof...(Args) > 1u + || (!std::same_as, data> && ...)) + && (!std::same_as, data> && ...) + && (!std::same_as, data, encode::Native>> && ...) + && (std::constructible_from, Args> && ...) + data(Args&&... args): + values_{std::forward(args)...} + {} + data& at(const data, encode::Native>& index){ return values_.at(index); } diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp index 1d627e6..f831256 100644 --- a/modules/codec/c++/math.hpp +++ b/modules/codec/c++/math.hpp @@ -36,12 +36,30 @@ public: }; namespace math { -/* +/** + * Builds a scalar from its native value. Shorthand for the two step + * data{value} -> data>{...} conversion, which the language won't + * do implicitly. + */ template -data norm_2(const data& d){ - return {}; +constexpr data, Encoding> make_scalar(typename native_data_type::type value){ + return data, Encoding>{data{value}}; +} + +/** + * Builds a vector from its native component values. The dimension is deduced + * from the argument count. + */ +template +constexpr data, Encoding> make_vector(Args... args){ + return data, Encoding>{ + data, Encoding>{ + std::array, sizeof...(Args)>{ + data{static_cast::type>(args)}... + } + } + }; } -*/ template data, Encoding> dot(const data, Encoding>& left, const data, Encoding>& right){ @@ -61,6 +79,14 @@ data,Encoding> sqrt(const data,Encoding>& in return out; } +/** + * Euclidean length of a vector. + */ +template +data, Encoding> norm(const data, Encoding>& input){ + return sqrt(dot(input,input)); +} + template data, Encoding> normalize(const data>& input ) { auto inp_dot = dot(input,input); @@ -174,6 +200,32 @@ data,Encoding> sin( } +/** + * Angle of a 2d vector measured counter clockwise from the positive x axis. + * Inverse of unit_vector(). + */ +template +data,Encoding> angle( + const data, Encoding>& vec +){ + data,Encoding> ret; + ret.at({}).set(std::atan2(vec.at({{1u}}).get(), vec.at({{0u}}).get())); + return ret; +} + +/** + * Unit length 2d vector pointing along the given angle. Inverse of angle(). + */ +template +data,Encoding> unit_vector( + const data, Encoding>& rot +){ + data,Encoding> ret; + ret.at({{0u}}) = cos(rot).at({}); + ret.at({{1u}}) = sin(rot).at({}); + return ret; +} + template data,Encoding> rotate( const data, Encoding> vec, @@ -192,7 +244,7 @@ data,Encoding> scale( ){ data, Encoding> sc_vec; for(uint64_t i = 0u; i< D; ++i){ - sc_vec.at({{i}}) = vec.at({{i}}) * scale; + sc_vec.at({{i}}) = vec.at({{i}}) * scale.at({}); } return sc_vec; } diff --git a/modules/codec/tests/math.cpp b/modules/codec/tests/math.cpp index 18b6c41..ad79c95 100644 --- a/modules/codec/tests/math.cpp +++ b/modules/codec/tests/math.cpp @@ -24,8 +24,29 @@ SAW_TEST("Math/Basic"){ SAW_TEST("Math/Tensor Construction"){ using namespace saw; - // data> a{{5.1f}}; - // data> b{{{1,3,5}}}; + data> b{1,3,5}; + + /** + * A one dimensional index keeps the inner braces, since a single + * element has to stay explicit to not clash with data. + */ + SAW_EXPECT(b.at({{0u}}).get() == 1, "Wrong value at 0"); + SAW_EXPECT(b.at({{1u}}).get() == 3, "Wrong value at 1"); + SAW_EXPECT(b.at({{2u}}).get() == 5, "Wrong value at 2"); + + /** + * From two dimensions up the index forwards, since it is a FixedArray + * itself. + * + * The elements are given in flat order, and get_flat_index runs the + * stride from dimension zero up, so the first index varies fastest. + */ + data> c{1.0, 2.0, 3.0, 4.0}; + + SAW_EXPECT(c.at({0u,0u}).get() == 1.0, "Wrong value at 0,0"); + SAW_EXPECT(c.at({1u,0u}).get() == 2.0, "Wrong value at 1,0"); + SAW_EXPECT(c.at({0u,1u}).get() == 3.0, "Wrong value at 0,1"); + SAW_EXPECT(c.at({1u,1u}).get() == 4.0, "Wrong value at 1,1"); } SAW_TEST("Math/Tensor Fill"){ -- cgit v1.3.1