File squareroot.h
File List > algebra > operators > squareroot.h
Go to the documentation of this file
#ifndef TEMPLAT_LATTICE_ALGEBRA_OPERATORS_SQUAREROOT_H
#define TEMPLAT_LATTICE_ALGEBRA_OPERATORS_SQUAREROOT_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
#include "TempLat/lattice/algebra/operators/power.h"
#include "TempLat/lattice/algebra/constants/halftype.h"
namespace TempLat
{
using device::sqrt;
namespace Operators
{
template <typename R> struct SafeSqrt : public UnaryOperator<R> {
public:
using UnaryOperator<R>::mR;
SafeSqrt(const R &pR) : UnaryOperator<R>(pR) {}
template <typename... IDX>
requires requires(std::decay_t<R> r, IDX... idx) {
requires IsVariadicIndex<IDX...>;
DoEval::eval(r, idx...);
}
DEVICE_INLINE_FUNCTION auto eval(const IDX &...idx) const
{
const auto a = DoEval::eval(mR, idx...);
constexpr decltype(a) zero{};
return (a < zero) ? zero : sqrt(a);
}
virtual std::string operatorString() const override { return "safe_sqrt"; }
template <typename U> void d(const U &other) = delete;
};
} // namespace Operators
template <typename R>
requires ConditionalUnaryGetter<R>
auto safeSqrt(const R &r)
{
return Operators::SafeSqrt<R>(r);
}
template <typename T>
requires(ConditionalBinaryGetter<T, HalfType> && !std::is_arithmetic_v<T>)
auto sqrt(T a)
{
return Operators::Power<T, HalfType>(a, HalfType());
}
constexpr inline ZeroType sqrt(ZeroType a) { return a; }
constexpr inline OneType sqrt(OneType a) { return a; }
} // namespace TempLat
#endif