Skip to content

File su2tracedeficit.h

File List > algebra > su2algebra > su2tracedeficit.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_SU2ALGEBRA_SU2TRACEDEFICIT_H
#define TEMPLAT_LATTICE_ALGEBRA_SU2ALGEBRA_SU2TRACEDEFICIT_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): Franz R. Sattler, Year: 2026

#include "TempLat/lattice/algebra/operators/unaryoperator.h"
#include "TempLat/lattice/algebra/helpers/doeval.h"
#include "TempLat/lattice/algebra/helpers/isvariadicindex.h"
#include "TempLat/lattice/algebra/su2algebra/helpers/hassu2get.h"
#include "TempLat/parallel/device.h"

#include <type_traits>

namespace TempLat
{

  template <typename R> class SU2TraceDeficit : public UnaryOperator<R>
  {
  public:
    using UnaryOperator<R>::mR;

    SU2TraceDeficit(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 U = DoEval::eval(mR, idx...);
      using E = std::decay_t<decltype(U[0])>;
      const E c0 = U[0];
      // Use direct comparison rather than a tolerance (device-compatible, cf. PauliVectorsAlgebra).
      // Either branch is well conditioned on its half: 1 + c0 >= 1 on the first, |2 - 2 c0| >= 2 on the second.
      if (c0 > E(0)) return E(2) * (U[1] * U[1] + U[2] * U[2] + U[3] * U[3]) / (E(1) + c0);
      return E(2) - E(2) * c0;
    }

    virtual std::string operatorString() const override { return "2-tr"; }
  };

  template <class R>
    requires HasSU2Get<R>
  auto traceDeficit(const R &r)
  {
    return SU2TraceDeficit<R>(r);
  }

} // namespace TempLat

#endif