Skip to content

File multiply.h

File List > algebra > operators > multiply.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_MULTIPLY_H
#define TEMPLAT_LATTICE_ALGEBRA_MULTIPLY_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/conditional/conditionalbinarygetter.h"
#include "TempLat/lattice/algebra/helpers/getderiv.h"
#include "TempLat/lattice/algebra/helpers/haseval.h"
#include "TempLat/lattice/algebra/helpers/isstdgettable.h"
#include "TempLat/lattice/algebra/helpers/istemplatgettable.h"
#include "TempLat/lattice/algebra/helpers/isarithmetic.h"
#include "TempLat/lattice/algebra/operators/binaryoperator.h"
#include "TempLat/lattice/algebra/operators/unaryoperator.h"
#include "TempLat/util/getcpptypename.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 Multiplication : public BinaryOperator<R, T>
    {
    public:
      using BinaryOperator<R, T>::mR;
      using BinaryOperator<R, T>::mT;

      Multiplication(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)
      {
        return GetDeriv::get(mT, other) * mR + mT * GetDeriv::get(mR, other);
      }
    };

    template <typename R, int N> class MultiplicationN : public UnaryOperator<R>
    {
    public:
      using UnaryOperator<R>::mR;

      MultiplicationN(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_FORCEINLINE_FUNCTION auto eval(const IDX &...idx) const
      {
        return N * DoEval::eval(mR, idx...);
      }

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

      template <typename U> auto d(const U &other) { return N * 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::Multiplication<R, T>(r, t);
  }

  template <typename R, int N>
    requires(HasEvalMethod<R> && !(IsTempLatGettable<0, R> || IsSTDGettable<0, R>))
  auto operator*(const R &r, Tag<N> n)
  {
    return Operators::MultiplicationN<R, N>(r);
  }

  template <typename R, int N>
    requires(HasEvalMethod<R> && !(IsTempLatGettable<0, R> || IsSTDGettable<0, R>))
  auto operator*(Tag<N> n, const R &r)
  {
    return Operators::MultiplicationN<R, N>(r);
  }

  template <int M, int N> constexpr auto operator*(Tag<N> n, Tag<M> m) { return Tag<N * M>(); }

  constexpr inline ZeroType operator*(ZeroType, ZeroType) { return {}; }

  template <typename T>
    requires(!std::is_same_v<T, ZeroType>)
  ZeroType operator*(const T &, ZeroType b)
  {
    return {};
  }
  template <typename T>
    requires(!std::is_same_v<T, ZeroType>)
  constexpr ZeroType operator*(ZeroType a, const T &)
  {
    return {};
  }

  template <typename T>
    requires(!std::is_same_v<T, OneType> && !std::is_same_v<T, ZeroType>)
  constexpr auto operator*(const T &a, const OneType b)
  {
    return a;
  }
  template <typename T>
    requires(!std::is_same_v<T, OneType> && !std::is_same_v<T, ZeroType>)
  constexpr auto operator*(const OneType &a, const T &b)
  {
    return b;
  }

  constexpr inline OneType operator*(OneType a, OneType b) { return a; }
} // namespace TempLat

#endif