Skip to content

File tanh.h

File List > algebra > operators > tanh.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_OPERATORS_TANH_H
#define TEMPLAT_LATTICE_ALGEBRA_OPERATORS_TANH_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: 2025

#include "TempLat/lattice/algebra/conditional/conditionalunarygetter.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/cosh.h"
#include "TempLat/lattice/algebra/operators/multiply.h"
#include "TempLat/lattice/algebra/operators/sinh.h"
#include "TempLat/lattice/algebra/operators/unaryoperator.h"
#include <type_traits>

namespace TempLat
{
  using device::tanh;

  namespace Operators
  {
    template <typename T> class Tanh : public UnaryOperator<T>
    {
    public:
      // Put public methods here. These should change very little over time.
      using UnaryOperator<T>::mR;

      Tanh(const T &a) : UnaryOperator<T>(a) {}

      template <typename... IDX>
        requires requires(std::decay_t<T> t, IDX... idx) {
          requires IsVariadicIndex<IDX...>;
          DoEval::eval(t, idx...);
        }
      DEVICE_FORCEINLINE_FUNCTION auto eval(const IDX &...idx) const
      {
        return tanh(DoEval::eval(mR, idx...));
      }

      template <typename U> auto d(const U &other) { return GetDeriv::get(mR, other) / pow<2>(cosh(mR)); }

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

  template <typename T>
    requires(!std::is_arithmetic_v<T> && !IsComplexType<T>)
  auto tanh(T a)
  {
    return Operators::Tanh<T>(a);
  }

  constexpr inline ZeroType tanh(ZeroType a) { return ZeroType(); }
} // namespace TempLat

#endif