summaryrefslogtreecommitdiff
path: root/modules/codec
diff options
context:
space:
mode:
Diffstat (limited to 'modules/codec')
-rw-r--r--modules/codec/c++/math.hpp13
-rw-r--r--modules/codec/tests/math.cpp20
2 files changed, 33 insertions, 0 deletions
diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp
index dd9e49b..ddeea3f 100644
--- a/modules/codec/c++/math.hpp
+++ b/modules/codec/c++/math.hpp
@@ -4,9 +4,22 @@
namespace saw {
namespace math {
+/*
template<typename T, typename Encoding = encode::Native>
data<typename T::InnerType,Encoding> norm_2(const data<T,Encoding>& d){
return {};
}
+*/
+
+template<typename T, uint64_t D, typename Encoding = encode::Native>
+data<schema::Scalar<T>, Encoding> dot(const data<schema::Vector<T,D>, Encoding>& left, const data<schema::Vector<T,D>, Encoding>& right){
+ data<schema::Scalar<T>,Encoding> val;
+ auto& inner = val({});
+ for(uint64_t i = 0u; i < D; ++i){
+ inner = inner + left({{i}}) * right({{i}});
+ }
+
+ return val;
+}
}
}
diff --git a/modules/codec/tests/math.cpp b/modules/codec/tests/math.cpp
index ad2d9a6..4fb012f 100644
--- a/modules/codec/tests/math.cpp
+++ b/modules/codec/tests/math.cpp
@@ -1,6 +1,7 @@
#include <forstio/test/suite.hpp>
#include "../c++/data.hpp"
#include "../c++/data_math.hpp"
+#include "../c++/math.hpp"
#include "../c++/csv.hpp"
#include <iostream>
@@ -55,4 +56,23 @@ SAW_TEST("Math/Tensor"){
SAW_EXPECT(d.at({{1u,1u}}).get() == 1.0, std::string{"Unexpected value at (1,1): "} + std::to_string(d.at({{1u,1u}}).get()));
}
}
+
+SAW_TEST("Math/Dot"){
+ using namespace saw;
+
+ data<sch::Vector<sch::Float64, 2u>> a;
+ {
+ a.at({{0u}}) = 2.0;
+ a.at({{1u}}) = 1.0;
+ }
+ data<sch::Vector<sch::Float64, 2u>> b;
+ {
+ b.at({{0u}}) = -1.0;
+ b.at({{1u}}) = 5.0;
+ }
+
+ auto c = math::dot(a,b);
+
+ SAW_EXPECT(c.at({}).get() == 3.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) );
+}
}