Commit 14713b9c authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

proper constexpr functions for older compilers in operations

parent 562973be
......@@ -43,13 +43,13 @@ namespace AMDiS
#endif
template <class... Int>
constexpr int order(Plus, Int... orders)
constexpr int order(Plus const&, Int... orders)
{
return Math::max(int(orders)...);
}
template <std::size_t I>
constexpr 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 constexpr int order(Minus, int lhs, int rhs)
friend constexpr int order(Minus const&, int lhs, int rhs)
{
return Math::max(lhs, rhs);
}
friend constexpr auto partial(Minus, index_t<0>)
friend constexpr auto partial(Minus const&, index_t<0>)
{
return One{};
}
friend constexpr auto partial(Minus, index_t<1>)
friend constexpr auto partial(Minus const&, index_t<1>)
{
return StaticConstant<int,-1>{};
}
......@@ -134,7 +134,7 @@ namespace AMDiS
template <class... Int>
constexpr int order(Multiplies, Int... orders)
constexpr int order(Multiplies const&, Int... orders)
{
return Math::sum(int(orders)...);
}
......@@ -142,7 +142,7 @@ namespace AMDiS
// only for binary *
// d_0 (x * y) = y, d_1 (x * y) = x
template <std::size_t I>
constexpr 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 constexpr 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 constexpr auto partial(Divides, index_t<1>);
friend constexpr auto partial(Divides const&, index_t<1>);
};
// -------------------------------------------------------------------------
......@@ -180,12 +180,12 @@ namespace AMDiS
return -x;
}
friend constexpr int order(Negate, int d)
friend constexpr int order(Negate const&, int d)
{
return d;
}
friend constexpr 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 constexpr int order(PowImpl, int d)
friend constexpr int order(PowImpl const&, int d)
{
return p*d;
}
friend constexpr 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
inline constexpr 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>{}));
......@@ -297,12 +297,12 @@ namespace AMDiS
return std::pow(x, p_);
}
friend constexpr int order(Pow_ P, int d)
friend constexpr int order(Pow_ const& P, int d)
{
return P.p_ * d;
}
friend constexpr 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});
}
friend constexpr 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));
}
};
......
Supports Markdown
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