File absolutevalue.h
File List > algebra > operators > absolutevalue.h
Go to the documentation of this file
#ifndef TEMPLAT_LATTICE_ALGEBRA_OPERATORS_ABSOLUTEVALUE_H
#define TEMPLAT_LATTICE_ALGEBRA_OPERATORS_ABSOLUTEVALUE_H
/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
Released under the MIT license, see LICENSE.md. */
// File info: Main contributor(s): Wessel Valkenburg, Franz R. Sattler, Year: 2025
// imperative to include this:
// otherwise abs might be defined for integers only, with possibly desastrous consequences.
#include "TempLat/lattice/algebra/helpers/getfloattype.h"
#include "TempLat/lattice/algebra/helpers/getgetreturntype.h"
#include "TempLat/lattice/algebra/operators/unaryoperator.h"
#include "TempLat/lattice/algebra/helpers/getderiv.h"
#include "TempLat/lattice/algebra/operators/heavisidestepfunction.h"
#include "TempLat/lattice/algebra/constants/halftype.h"
#include "TempLat/lattice/algebra/constants/zerotype.h"
#include "TempLat/lattice/algebra/constants/onetype.h"
namespace TempLat
{
using device::abs;
namespace Operators
{
template <typename R> class AbsoluteValue : public UnaryOperator<R>
{
public:
// Put public methods here. These should change very little over time.
using UnaryOperator<R>::mR;
AbsoluteValue(const R &a) : UnaryOperator<R>(a) {}
template <typename... IDX>
requires requires(std::decay_t<R> r, IDX... idx) {
requires IsVariadicIndex<IDX...>;
DoEval::eval(r, idx...);
}
DEVICE_FORCEINLINE_FUNCTION auto eval(const IDX &...idx) const
{
return abs(DoEval::eval(mR, idx...));
}
virtual std::string operatorString() const override { return "abs"; }
template <typename U> auto d(const U &other)
{
return GetDeriv::get(mR, other) * (-heaviside(-mR) + heaviside(mR));
}
};
} // namespace Operators
template <typename T>
requires ConditionalUnaryGetter<T>
auto abs(const T &a)
{
return Operators::AbsoluteValue<T>(a);
}
constexpr inline ZeroType abs(ZeroType a) { return ZeroType(); }
constexpr inline OneType abs(OneType a) { return OneType(); }
constexpr inline HalfType abs(HalfType a) { return HalfType(); }
} // namespace TempLat
#endif