Commit cb01fd46 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'issue/operations_constexpr' into 'master'

make some operations constexpr and add missing inline

See merge request !9
parents 0215302f 14713b9c
......@@ -43,13 +43,13 @@ namespace AMDiS
#endif
template <class... Int>
int order(Plus, Int... orders)
constexpr int order(Plus const&, Int... orders)
{
return Math::max(int(orders)...);
}
template <std::size_t I>
auto partial(Plus, index_t<I>)
constexpr auto partial(Plus const&, index_t<I>)
{
static_assert((I < 2), "Derivatives of `Plus` only defined for the binary case.");
return One{};
......@@ -66,17 +66,17 @@ namespace AMDiS
return lhs - rhs;
}
friend int order(Minus, int lhs, int rhs)
friend constexpr int order(Minus const&, int lhs, int rhs)
{
return Math::max(lhs, rhs);
}
friend auto partial(Minus, index_t<0>)
friend constexpr auto partial(Minus const&, index_t<0>)
{
return One{};
}
friend auto partial(Minus, index_t<1>)
friend constexpr auto partial(Minus const&, index_t<1>)
{
return StaticConstant<int,-1>{};
}
......@@ -134,15 +134,15 @@ namespace AMDiS
template <class... Int>
int order(Multiplies, Int... orders)
constexpr int order(Multiplies const&, Int... orders)
{
return Plus{}(int(orders)...);
return Math::sum(int(orders)...);
}
// only for binary *
// d_0 (x * y) = y, d_1 (x * y) = x
template <std::size_t I>
auto partial(Multiplies, index_t<I>)
constexpr auto partial(Multiplies const&, index_t<I>)
{
static_assert((I < 2), "Derivatives of `Multiplies` only defined for the binary case.");
return Arg<1-I>{};
......@@ -160,13 +160,13 @@ namespace AMDiS
}
// d_0 f(x,y) = 1 / y
friend auto partial(Divides, index_t<0>)
friend constexpr auto partial(Divides const&, index_t<0>)
{
return compose(Divides{}, One{}, Arg<1>{});
}
// d_1 f(x,y) = (y - x)/y^2
friend auto partial(Divides, index_t<1>);
friend constexpr auto partial(Divides const&, index_t<1>);
};
// -------------------------------------------------------------------------
......@@ -180,12 +180,12 @@ namespace AMDiS
return -x;
}
friend int order(Negate, int d)
friend constexpr int order(Negate const&, int d)
{
return d;
}
friend auto partial(Negate, index_t<0>)
friend constexpr auto partial(Negate const&, index_t<0>)
{
return StaticConstant<int,-1>{};
}
......@@ -238,12 +238,12 @@ namespace AMDiS
return Math::pow<p>(x);
}
friend int order(PowImpl, int d)
friend constexpr int order(PowImpl const&, int d)
{
return p*d;
}
friend auto partial(PowImpl, index_t<0>)
friend constexpr auto partial(PowImpl const&, index_t<0>)
{
return compose(Multiplies{}, StaticConstant<int,p>{}, Pow<p-1>{});
}
......@@ -278,7 +278,7 @@ namespace AMDiS
#endif
// d_1 f(x,y) = (y - x)/y^2
auto partial(Divides, index_t<1>)
inline constexpr auto partial(Divides const&, index_t<1>)
{
return compose(Divides{}, compose(Minus{}, Arg<1>{}, Arg<0>{}),
compose(Pow<2>{}, Arg<1>{}));
......@@ -287,7 +287,7 @@ namespace AMDiS
/// Functor that represents x^p, \see \ref Pow
struct Pow_
{
Pow_(int p)
constexpr Pow_(int p)
: p_(p)
{}
......@@ -297,12 +297,12 @@ namespace AMDiS
return std::pow(x, p_);
}
friend int order(Pow_ P, int d)
friend constexpr int order(Pow_ const& P, int d)
{
return P.p_ * d;
}
friend auto partial(Pow_ P, index_t<0>)
friend constexpr auto partial(Pow_ const& P, index_t<0>)
{
return compose(Multiplies{}, Constant<int>{P.p_}, Pow_{P.p_-1});
}
......
......@@ -50,7 +50,7 @@ namespace AMDiS
}
template <class T, T value, std::size_t J>
constexpr auto partial(StaticConstant<T,value>, index_t<J>)
constexpr auto partial(StaticConstant<T,value> const&, index_t<J>)
{
return Zero{};
}
......@@ -66,12 +66,12 @@ namespace AMDiS
return x;
}
friend constexpr int order(Id, int d)
friend constexpr int order(Id const&, int d)
{
return d;
}
friend auto partial(Id, index_t<0>)
friend auto partial(Id const&, index_t<0>)
{
return One{};
}
......@@ -111,24 +111,38 @@ namespace AMDiS
// -------------------------------------------------------------------------
template <class T0, class... Ts>
inline constexpr decltype(auto) get_element(index_t<0>, T0&& t0, Ts&&... /*ts*/)
{
return FWD(t0);
}
template <std::size_t J, class T0, class... Ts>
inline constexpr decltype(auto) get_element(index_t<J>, T0&& /*t0*/, Ts&&... ts)
{
static_assert(J <= sizeof...(Ts), "");
return get_element(index_t<J-1>{}, FWD(ts)...);
}
template <std::size_t I>
struct Arg
{
template <class... Ts>
constexpr auto&& operator()(Ts&&... args) const
{
return std::get<I>(std::forward_as_tuple(FWD(args)...));
return get_element(index_t<I>{}, FWD(args)...);
}
};
template <std::size_t I, class... Int>
constexpr int order(Arg<I> const&, Int... orders)
{
return std::get<I>(std::forward_as_tuple(orders...));
return get_element(index_t<I>{}, orders...);
}
template <std::size_t I, std::size_t J>
constexpr auto partial(Arg<I>, index_t<J>)
constexpr auto partial(Arg<I> const&, index_t<J>)
{
return StaticConstant<int,(I==J ? 1 : 0)>{};
}
......@@ -144,7 +158,7 @@ namespace AMDiS
return vec[I];
}
friend constexpr int order(Get, int d)
friend constexpr int order(Get const&, int d)
{
return d;
}
......@@ -162,7 +176,7 @@ namespace AMDiS
return vec[i_];
}
friend constexpr int order(Get_, int d)
friend constexpr int order(Get_ const&, int d)
{
return d;
}
......
......@@ -27,7 +27,7 @@
return DERIV ; \
} \
}; \
friend constexpr auto partial(NAME, index_t<0>) \
friend constexpr auto partial(NAME const&, index_t<0>) \
{ \
return Derivative{} ; \
} \
......@@ -52,7 +52,7 @@ namespace AMDiS
return (x > T{0} ? T{1} : T{-1});
}
constexpr friend auto partial(Signum, index_t<0>)
friend constexpr auto partial(Signum const&, index_t<0>)
{
return Zero{};
}
......@@ -91,14 +91,14 @@ namespace AMDiS
}
// -y/(Math::sqr(x) + Math::sqr(y))
constexpr friend auto partial(Atan2, index_t<0>)
constexpr friend auto partial(Atan2 const&, index_t<0>)
{
return compose(Divides{}, compose(Negate{}, Arg<1>{}),
compose(Plus{}, compose(Pow<2>{}, Arg<0>{}), compose(Pow<2>{}, Arg<1>{})));
}
// x/(Math::sqr(x) + Math::sqr(y));
constexpr friend auto partial(Atan2, index_t<1>)
constexpr friend auto partial(Atan2 const&, index_t<1>)
{
return compose(Divides{}, Arg<0>{},
compose(Plus{}, compose(Pow<2>{}, Arg<0>{}), compose(Pow<2>{}, Arg<1>{})));
......@@ -131,7 +131,7 @@ namespace AMDiS
T lo_, hi_;
};
constexpr friend auto partial(Clamp c, index_t<0>)
constexpr friend auto partial(Clamp const& c, index_t<0>)
{
return DClamp{c.lo_,c.hi_};
}
......
......@@ -23,17 +23,17 @@ namespace AMDiS
return lhs.dot(rhs);
}
friend constexpr int order(Dot, int d1, int d2)
friend constexpr int order(Dot const&, int d1, int d2)
{
return d1 + d2;
}
friend constexpr auto partial(Dot, index_t<0>)
friend constexpr auto partial(Dot const&, index_t<0>)
{
return Arg<1>{};
}
friend constexpr auto partial(Dot, index_t<1>)
friend constexpr auto partial(Dot const&, index_t<1>)
{
return Arg<0>{};
}
......@@ -50,12 +50,12 @@ namespace AMDiS
return unary_dot(vec);
}
friend constexpr int order(UnaryDot, int d)
friend constexpr int order(UnaryDot const&, int d)
{
return 2*d;
}
friend auto partial(UnaryDot, index_t<0>)
friend auto partial(UnaryDot const&, index_t<0>)
{
return compose(Multiplies{}, StaticConstant<int,2>{}, Id{});
}
......@@ -88,7 +88,7 @@ namespace AMDiS
return trans(mat);
}
friend constexpr int order(Trans, int d)
friend constexpr int order(Trans const&, int d)
{
return d;
}
......
......@@ -34,7 +34,8 @@ namespace AMDiS
template <class T, class S>
constexpr auto operator()(T const& lhs, S const& rhs) const
{
return Math::max(std::abs(lhs), std::abs(rhs));
using std::abs;
return Math::max(abs(lhs), abs(rhs));
}
};
......@@ -46,7 +47,8 @@ namespace AMDiS
template <class T, class S>
constexpr auto operator()(T const& lhs, S const& rhs) const
{
return Math::min(std::abs(lhs), std::abs(rhs));
using std::abs;
return Math::min(abs(lhs), abs(rhs));
}
};
......
......@@ -43,6 +43,9 @@ dune_add_test(SOURCES MultiTypeVectorTest.cpp
dune_add_test(SOURCES MultiTypeMatrixTest.cpp
LINK_LIBRARIES amdis)
dune_add_test(SOURCES OperationsTest.cpp
LINK_LIBRARIES amdis)
dune_add_test(SOURCES OperatorsTest.cpp
LINK_LIBRARIES amdis)
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <iostream>
#include <amdis/AMDiS.hpp>
#include <amdis/Operations.hpp>
#include "Tests.hpp"
using namespace AMDiS;
int main(int argc, char** argv)
{
AMDiS::init(argc, argv);
constexpr Operation::StaticConstant<int,0> op0a;
constexpr Operation::Zero op0b;
AMDIS_TEST_EQ(order(op0a,4,2,6,3,4), 0);
AMDIS_TEST_EQ(order(op0b,4,2,6,3,4), 0);
constexpr int zero = op0a(1,2,3,4,5);
AMDIS_TEST_EQ(zero, 0);
constexpr Operation::StaticConstant<int,1> op1a;
constexpr Operation::One op1b;
AMDIS_TEST_EQ(order(op1a,4,2,6,3,4), 0);
AMDIS_TEST_EQ(order(op1b,4,2,6,3,4), 0);
constexpr int one = op1a(1,2,3,4,5);
AMDIS_TEST_EQ(one, 1);
constexpr auto op1a_0 = partial(op1a, index_t<0>{});
AMDIS_TEST((std::is_same<decltype(op1a_0), decltype(op0a)>::value));
constexpr Operation::Id op2;
AMDIS_TEST_EQ(op2(7), 7);
AMDIS_TEST_EQ(order(op2,7), 7);
constexpr Operation::Constant<int> op3(42);
AMDIS_TEST_EQ(op3(1,2,3,4,5), 42);
AMDIS_TEST_EQ(order(op3,4,2,6,3,4), 0);
constexpr Operation::Arg<0> op4a;
constexpr Operation::Arg<1> op4b;
AMDIS_TEST_EQ(op4a(1,2,3,4,5), 1);
AMDIS_TEST_EQ(op4b(1,2,3,4,5), 2);
AMDIS_TEST_EQ(order(op4a,4,2,6,3,4), 4);
AMDIS_TEST_EQ(order(op4b,4,2,6,3,4), 2);
constexpr Operation::Plus op5;
constexpr auto erg5 = op5(7,8);
constexpr int order5 = order(op5,2,3);
AMDIS_TEST_EQ(erg5, 15);
AMDIS_TEST_EQ(order5, 3);
constexpr Operation::Minus op6;
constexpr auto erg6 = op6(15,8);
constexpr int order6 = order(op6,2,3);
AMDIS_TEST_EQ(erg6, 7);
AMDIS_TEST_EQ(order6, 3);
constexpr Operation::Multiplies op7;
constexpr auto erg7 = op7(2,4);
constexpr int order7 = order(op7,2,3);
AMDIS_TEST_EQ(erg7, 8);
AMDIS_TEST_EQ(order7, 5);
constexpr auto op7_0 = partial(op7, index_t<0>{});
constexpr auto op7_1 = partial(op7, index_t<1>{});
constexpr auto erg7_0 = op7_0(2,4);
constexpr auto erg7_1 = op7_1(2,4);
AMDIS_TEST_EQ(erg7_0, 4);
AMDIS_TEST_EQ(erg7_1, 2);
constexpr Operation::Divides op8;
constexpr auto erg8 = op8(16,8);
AMDIS_TEST_EQ(erg8, 2);
// constexpr int order8 = order(op8,2,3); // no order() for divides
AMDiS::finalize();
return report_errors();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment