Skip to content

File add.h

File List > algebra > operators > add.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_ADD_H
#define TEMPLAT_LATTICE_ALGEBRA_ADD_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/constants/halftype.h"
#include "TempLat/lattice/algebra/constants/onetype.h"
#include "TempLat/lattice/algebra/constants/zerotype.h"
#include "TempLat/lattice/algebra/conditional/conditionalbinarygetter.h"
#include "TempLat/lattice/algebra/helpers/getderiv.h"
#include "TempLat/lattice/algebra/operators/binaryoperator.h"

namespace TempLat
{
  namespace Operators
  {
    template <typename R, typename T> class Addition : public TempLat::BinaryOperator<R, T>
    {
    public:
      using BinaryOperator<R, T>::mR;
      using BinaryOperator<R, T>::mT;

      Addition(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(mT, idx...) + DoEval::eval(mR, idx...);
      }

      virtual std::string operatorString() const override { return "+"; }

      template <typename U> auto d(const U &other) { return GetDeriv::get(mT, other) + GetDeriv::get(mR, other); }
    };
  } // namespace Operators

  template <typename R, typename T>
    requires ConditionalBinaryGetter<R, T>
  auto operator+(const R &r, const T &t)
  {
    return Operators::Addition<R, T>(r, t);
  }

  constexpr inline auto operator+(const ZeroType a, const ZeroType b) { return ZeroType(); }

  constexpr inline OneType operator+(const HalfType a, const HalfType b) { return {}; }

  template <typename T>
    requires(!std::is_same<T, ZeroType>::value)
  T operator+(const ZeroType a, const T b)
  {
    return b;
  }
  template <typename T>
    requires(!std::is_same<T, ZeroType>::value)
  T operator+(const T b, const ZeroType a)
  {
    return b;
  }
} // namespace TempLat

#endif