Skip to content

File log.h

File List > algebra > operators > log.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_OPERATORS_LOG_H
#define TEMPLAT_LATTICE_ALGEBRA_OPERATORS_LOG_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/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/divide.h"
#include "TempLat/lattice/algebra/operators/unaryoperator.h"

namespace TempLat
{
  using device::log;

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

      Log(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 log(DoEval::eval(mR, idx...));
      }

      template <typename U> auto d(const U &other)
      {
        /* not using pow for 1/mInstanceT because pow imports us, log.h */
        return GetDeriv::get(mR, other) / mR;
      }

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

  template <typename T>
    requires ConditionalUnaryGetter<T>
  auto log(T a)
  {
    return Operators::Log<T>(a);
  }

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

#endif