File divide.h
File List > algebra > operators > divide.h
Go to the documentation of this file
#ifndef TEMPLAT_LATTICE_ALGEBRA_OPERATORS_DIVIDE_H
#define TEMPLAT_LATTICE_ALGEBRA_OPERATORS_DIVIDE_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, Year: 2019
#include "TempLat/lattice/algebra/helpers/getderiv.h"
#include "TempLat/lattice/algebra/operators/binaryoperator.h"
#include "TempLat/lattice/algebra/operators/multiply.h"
#include "TempLat/lattice/algebra/operators/subtract.h"
#include "TempLat/lattice/algebra/constants/onetype.h"
#include "TempLat/lattice/algebra/constants/zerotype.h"
namespace TempLat
{
namespace Operators
{
template <typename R, typename T> class Division : public BinaryOperator<R, T>
{
public:
using BinaryOperator<R, T>::mR;
using BinaryOperator<R, T>::mT;
Division(const R &pR, const T &pT) : BinaryOperator<R, T>(pR, pT) {}
template <typename... IDX>
requires requires(std::decay_t<R> r, std::decay_t<T> t, IDX... idx) {
requires IsVariadicIndex<IDX...>;
DoEval::eval(r, idx...);
DoEval::eval(t, idx...);
}
DEVICE_INLINE_FUNCTION auto eval(const IDX &...idx) const
{
return DoEval::eval(mR, idx...) / DoEval::eval(mT, idx...);
}
virtual std::string operatorString() const override { return "/"; }
template <typename U> auto d(const U &other)
{
/* not using pow for mT * mT, because pow imports log which imports us, divide.h */
return GetDeriv::get(mR, other) / mT - GetDeriv::get(mT, other) * mR / (mT * mT);
}
};
template <typename R, typename T> struct SafeDivision : public BinaryOperator<R, T> {
public:
using BinaryOperator<R, T>::mR;
using BinaryOperator<R, T>::mT;
SafeDivision(const R &pR, const T &pT) : BinaryOperator<R, T>(pR, pT) {}
template <typename... IDX>
requires requires(std::decay_t<R> r, std::decay_t<T> t, IDX... idx) {
requires IsVariadicIndex<IDX...>;
DoEval::eval(r, idx...);
DoEval::eval(t, idx...);
}
DEVICE_INLINE_FUNCTION auto eval(const IDX &...idx) const
{
const auto a = DoEval::eval(mR, idx...);
const auto b = DoEval::eval(mT, idx...);
constexpr decltype(a / b) zero{};
return AlmostEqual(a, zero) ? zero : a / b;
}
virtual std::string operatorString() const override { return "/safe/"; }
template <typename U> auto d(const U &other)
{
/* not using pow for mT * mT, because pow imports log which imports us, divide.h */
return GetDeriv::get(mR, other) / mT - GetDeriv::get(mT, other) * mR / (mT * mT);
}
};
} // namespace Operators
template <typename R, typename T>
requires ConditionalBinaryGetter<R, T>
auto operator/(const R &r, const T &t)
{
return Operators::Division<R, T>(r, t);
}
template <typename R, typename T>
requires ConditionalBinaryGetter<R, T>
auto safeDivide(const R &r, const T &t)
{
return Operators::SafeDivision<R, T>(r, t);
}
template <typename T> constexpr T operator/(const T &a, OneType b) { return a; }
template <typename T>
requires std::is_same_v<T, ZeroType>
constexpr auto operator/(const ZeroType &a, const T &)
{
return ZeroType{};
}
} // namespace TempLat
#endif