Skip to content

File number.h

File List > algebra > constants > number.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_ALGEBRA_CONSTANTS_NUMBER_H
#define TEMPLAT_LATTICE_ALGEBRA_CONSTANTS_NUMBER_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, Year: 2026

#include "TempLat/lattice/algebra/helpers/isvariadicindex.h"
#include "TempLat/lattice/algebra/helpers/haseval.h"
#include "TempLat/lattice/algebra/helpers/getndim.h"
#include "TempLat/lattice/algebra/helpers/doeval.h"
#include "TempLat/lattice/algebra/constants/zerotype.h"
#include "TempLat/lattice/measuringtools/averager.h"

#include "TempLat/parallel/device.h"

namespace TempLat
{
  template <typename T> struct Number {
    T value;

    template <typename... IDX>
      requires IsVariadicIndex<IDX...>
    DEVICE_FORCEINLINE_FUNCTION constexpr auto eval(const IDX &...) const
    {
      return value;
    }

    // 1. Lattice expression (NDim > 0): spatially average, then add
    template <class Expr>
      requires(HasEvalMethod<std::decay_t<Expr>> && GetNDim::get<std::decay_t<Expr>>() > 0)
    Number &operator+=(Expr &&e)
    {
      value += average(std::forward<Expr>(e));
      return *this;
    }

    // 2. 0-dim expression (another Number, scalar*Number, etc): eval directly
    template <class Expr>
      requires(HasEvalMethod<std::decay_t<Expr>> && GetNDim::get<std::decay_t<Expr>>() == 0 &&
               !std::is_arithmetic_v<std::decay_t<Expr>>)
    Number &operator+=(Expr &&e)
    {
      value += DoEval::eval(std::forward<Expr>(e), size_t{0});
      return *this;
    }

    // 3. Arithmetic (plain double/float)
    Number &operator+=(T v)
    {
      value += v;
      return *this;
    }

    // 4. ZeroType: no-op
    Number &operator+=(ZeroType) { return *this; }

    Number &operator=(T v)
    {
      value = v;
      return *this;
    }
  };
} // namespace TempLat

#endif