Skip to content

File arg2.h

File List > algebra > operators > arg2.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_COMPLEXALGEBRA_ARG2_H
#define TEMPLAT_LATTICE_ALGEBRA_COMPLEXALGEBRA_ARG2_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): Adrien Florio, Franz R. Sattler, Year: 2026

#include "TempLat/lattice/algebra/conditional/conditionalbinarygetter.h"
#include "TempLat/lattice/algebra/constants/onetype.h"
#include "TempLat/lattice/algebra/constants/zerotype.h"
#include "TempLat/lattice/algebra/helpers/getderiv.h"
#include "TempLat/lattice/algebra/operators/multiply.h"
#include "TempLat/lattice/algebra/operators/unaryoperator.h"
#include "TempLat/util/constants.h"
#include "TempLat/util/rangeiteration/tagliteral.h"

#include "TempLat/lattice/algebra/complexalgebra/helpers/complexfieldget.h"
#include "TempLat/lattice/algebra/complexalgebra/helpers/hascomplexfieldget.h"

namespace TempLat
{
  using device::atan2;
  namespace Operators
  {
    template <typename R, typename T> class Arg2 : public BinaryOperator<R, T>
    {
    public:
      // Put public methods here. These should change very little over time.
      using BinaryOperator<R, T>::mR;
      using BinaryOperator<R, T>::mT;

      Arg2(R r, T t) : BinaryOperator<R, T>(r, t) {}

      Arg2() : BinaryOperator<R, T>(R(), T()) {}

      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 res = atan2(DoEval::eval(mR, idx...), DoEval::eval(mT, idx...));
        using NT = std::decay_t<decltype(res)>;
        // atan2 returns [-pi, pi]; map the -pi endpoint to +pi so the phase lives in ]-pi, pi].
        return AlmostEqual(res, 0) ? 0 : (AlmostEqual(res, -Constants::pi<NT>) ? Constants::pi<NT> : res);
      }

      template <typename U> void d(const U &other) = delete;

      virtual std::string operatorString() const override { return "arg2"; }
    };
  } // namespace Operators

  template <typename R, typename T>
    requires ConditionalBinaryGetter<R, T>
  auto arg2(R r, T t)
  {
    return Operators::Arg2<R, T>{r, t};
  }
} // namespace TempLat

#endif