Skip to content
Snippets Groups Projects
Commit cdf11377 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add dot functions for R^d vectors

parent 1eb4442a
No related branches found
No related tags found
1 merge request!90Add access to cells' volume of a mesh in the script file
...@@ -69,6 +69,19 @@ MathModule::MathModule() ...@@ -69,6 +69,19 @@ MathModule::MathModule()
this->_addBuiltinFunction("round", std::make_shared<BuiltinFunctionEmbedder<int64_t(double)>>( this->_addBuiltinFunction("round", std::make_shared<BuiltinFunctionEmbedder<int64_t(double)>>(
[](double x) -> int64_t { return std::lround(x); })); [](double x) -> int64_t { return std::lround(x); }));
this->_addBuiltinFunction("dot",
std::make_shared<BuiltinFunctionEmbedder<double(const TinyVector<1>, const TinyVector<1>)>>(
[](const TinyVector<1> x, const TinyVector<1> y) -> double { return (x, y); }));
this->_addBuiltinFunction("dot",
std::make_shared<BuiltinFunctionEmbedder<double(const TinyVector<2>, const TinyVector<2>)>>(
[](const TinyVector<2> x, const TinyVector<2> y) -> double { return (x, y); }));
this
->_addBuiltinFunction("dot",
std::make_shared<BuiltinFunctionEmbedder<double(const TinyVector<3>&, const TinyVector<3>&)>>(
[](const TinyVector<3>& x, const TinyVector<3>& y) -> double { return (x, y); }));
} }
void void
......
...@@ -278,6 +278,39 @@ let z:Z, z = round(-1.2); ...@@ -278,6 +278,39 @@ let z:Z, z = round(-1.2);
CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1}); CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1});
} }
{ // dot
tested_function_set.insert("dot:R^1*R^1");
std::string_view data = R"(
import math;
let x:R^1, x = -2;
let y:R^1, y = 4;
let s:R, s = dot(x,y);
)";
CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "s", double{-2 * 4});
}
{ // dot
tested_function_set.insert("dot:R^2*R^2");
std::string_view data = R"(
import math;
let x:R^2, x = (-2, 3);
let y:R^2, y = (4, 3);
let s:R, s = dot(x,y);
)";
CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "s", (TinyVector<2>{-2, 3}, TinyVector<2>{4, 3}));
}
{ // dot
tested_function_set.insert("dot:R^3*R^3");
std::string_view data = R"(
import math;
let x:R^3, x = (-2, 3, 4);
let y:R^3, y = (4, 3, 5);
let s:R, s = dot(x,y);
)";
CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "s", (TinyVector<3>{-2, 3, 4}, TinyVector<3>{4, 3, 5}));
}
MathModule math_module; MathModule math_module;
bool missing_test = false; bool missing_test = false;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <language/modules/MathModule.hpp> #include <language/modules/MathModule.hpp>
#include <language/utils/BuiltinFunctionEmbedder.hpp> #include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <set>
// clazy:excludeall=non-pod-global-static // clazy:excludeall=non-pod-global-static
TEST_CASE("MathModule", "[language]") TEST_CASE("MathModule", "[language]")
...@@ -13,7 +15,7 @@ TEST_CASE("MathModule", "[language]") ...@@ -13,7 +15,7 @@ TEST_CASE("MathModule", "[language]")
MathModule math_module; MathModule math_module;
const auto& name_builtin_function = math_module.getNameBuiltinFunctionMap(); const auto& name_builtin_function = math_module.getNameBuiltinFunctionMap();
REQUIRE(name_builtin_function.size() == 22); REQUIRE(name_builtin_function.size() == 25);
SECTION("double -> double") SECTION("double -> double")
{ {
...@@ -322,4 +324,61 @@ TEST_CASE("MathModule", "[language]") ...@@ -322,4 +324,61 @@ TEST_CASE("MathModule", "[language]")
REQUIRE(std::get<decltype(result)>(result_variant) == Catch::Approx(result)); REQUIRE(std::get<decltype(result)>(result_variant) == Catch::Approx(result));
} }
} }
SECTION("(R^d, R^d) -> double")
{
SECTION("dot:R^1*R^1")
{
TinyVector<1> arg0 = 3;
TinyVector<1> arg1 = 2;
DataVariant arg0_variant = arg0;
DataVariant arg1_variant = arg1;
auto i_function = name_builtin_function.find("dot:R^1*R^1");
REQUIRE(i_function != name_builtin_function.end());
IBuiltinFunctionEmbedder& function_embedder = *i_function->second;
DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant});
auto result = (arg0, arg1);
REQUIRE(std::get<decltype(result)>(result_variant) == result);
}
SECTION("dot:R^2*R^2")
{
TinyVector<2> arg0{3, 2};
TinyVector<2> arg1{-2, 5};
DataVariant arg0_variant = arg0;
DataVariant arg1_variant = arg1;
auto i_function = name_builtin_function.find("dot:R^2*R^2");
REQUIRE(i_function != name_builtin_function.end());
IBuiltinFunctionEmbedder& function_embedder = *i_function->second;
DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant});
auto result = (arg0, arg1);
REQUIRE(std::get<decltype(result)>(result_variant) == result);
}
SECTION("dot:R^3*R^3")
{
TinyVector<3> arg0{3, 2, 4};
TinyVector<3> arg1{-2, 5, 2};
DataVariant arg0_variant = arg0;
DataVariant arg1_variant = arg1;
auto i_function = name_builtin_function.find("dot:R^3*R^3");
REQUIRE(i_function != name_builtin_function.end());
IBuiltinFunctionEmbedder& function_embedder = *i_function->second;
DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant});
auto result = (arg0, arg1);
REQUIRE(std::get<decltype(result)>(result_variant) == result);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment