Program Listing for File vector_macros.h

Return to documentation for file (numerics/vector_macros.h)

#pragma once

#include <functional>
#include <map>

#include "lupnt/core/constants.h"

// Function:
// Real = func(Real)
// New definitions:
// Vec = func(Vec)
#define VEC_DEF_REAL(func) VecX func(const VecX& x);

#define VEC_IMP_REAL(func)                                                                  \
  VecX func(const VecX& x) {                                                                \
    VecX out(x.size());                                                                     \
    _Pragma("omp parallel for") for (int i = 0; i < x.size(); i++) { out(i) = func(x(i)); } \
    return out;                                                                             \
  }

// Function:
// Vec<size> = func(Vec<size>)
// New definitions:
// Mat<-1,size> = (Mat<-1,size>)
#define VEC_DEF_VECTOR(func, size)    \
  Vec<size> func(const Vec<size>& x); \
  Mat<-1, size> func(const Mat<-1, size>& x);

#define VEC_IMP_VECTOR(func, size)                                                  \
  Vec<size> func(const Vec<size>& x) { return func(static_cast<const State&>(x)); } \
  Mat<-1, size> func(const Mat<-1, size>& x) {                                      \
    Mat<-1, size> out(x.rows(), size);                                              \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {                \
      Vec<size> x_ = x.row(i);                                                      \
      out.row(i) = func(x_);                                                        \
    }                                                                               \
    return out;                                                                     \
  }

// Function:
// Vec<size> = func(Vec<size>)
// New definitions:
// Mat<-1,size> = (Mat<-1,size>)
#define VEC_DEF_VECTOR_SIZE(func, size1, size2) \
  Vec<size2> func(const Vec<size1>& x);         \
  Mat<-1, size2> func(const Mat<-1, size1>& x);

#define VEC_IMP_VECTOR_SIZE(func, size1, size2)                                       \
  Vec<size2> func(const Vec<size1>& x) { return func(static_cast<const State&>(x)); } \
  Mat<-1, size2> func(const Mat<-1, size1>& x) {                                      \
    Mat<-1, size2> out(x.rows(), size2);                                              \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {                  \
      Vec<size1> x_ = x.row(i);                                                       \
      out.row(i) = func(x_);                                                          \
    }                                                                                 \
    return out;                                                                       \
  }

// Function:
// Vec<size> = func(Vec<size>, Real)
// New definitions:
// Mat<-1,size> = func(Vec<size>, VecX)
// Mat<-1,size> = func(Mat<-1,size>, Real)
// Mat<-1,size> = func(Mat<-1,size>, VecX)
#define VEC_DEF_VECTOR_REAL(func, size)                  \
  Vec<size> func(const Vec<size>& x, Real y);            \
  Mat<-1, size> func(const Vec<size>& x, const VecX& y); \
  Mat<-1, size> func(const Mat<-1, size>& x, Real y);    \
  Mat<-1, size> func(const Mat<-1, size>& x, const VecX& y);

#define VEC_IMP_VECTOR_REAL(func, size)                                                        \
  Vec<size> func(const Vec<size>& x, Real y) { return func(static_cast<const State&>(x), y); } \
  Mat<-1, size> func(const Vec<size>& x, const VecX& y) {                                      \
    Mat<-1, size> out(y.rows(), size);                                                         \
    _Pragma("omp parallel for") for (int i = 0; i < y.rows(); i++) out.row(i) = func(x, y(i)); \
    return out;                                                                                \
  }                                                                                            \
  Mat<-1, size> func(const Mat<-1, size>& x, Real y) {                                         \
    Mat<-1, size> out(x.rows(), size);                                                         \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {                           \
      Vec<size> x_ = x.row(i);                                                                 \
      out.row(i) = func(x_, y);                                                                \
    }                                                                                          \
    return out;                                                                                \
  }                                                                                            \
  Mat<-1, size> func(const Mat<-1, size>& x, const VecX& y) {                                  \
    LUPNT_CHECK(x.rows() == y.rows(), "Size mismatch", "VectorMacros");                        \
    Mat<-1, size> out(x.rows(), size);                                                         \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {                           \
      Vec<size> x_ = x.row(i);                                                                 \
      out.row(i) = func(x_, y(i));                                                             \
    }                                                                                          \
    return out;                                                                                \
  }

// Function:
// Vec<size> = func(Vec<size>, Real, Real)
// New definitions:
// Mat<-1,size> = func(Mat<-1,size>, Real, Real)
#define VEC_DEF_VECTOR_REAL_REAL(func, size)          \
  Vec<size> func(const Vec<size>& x, Real y, Real z); \
  Mat<-1, size> func(const Mat<-1, size>& x, Real y, Real z);

#define VEC_IMP_VECTOR_REAL_REAL(func, size)                         \
  Vec<size> func(const Vec<size>& x, Real y, Real z) {               \
    return func(static_cast<const State&>(x), y, z);                 \
  }                                                                  \
  Mat<-1, size> func(const Mat<-1, size>& x, Real y, Real z) {       \
    Mat<-1, size> out(x.rows(), size);                               \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) { \
      Vec<size> x_ = x.row(i);                                       \
      out.row(i) = func(x_, y, z);                                   \
    }                                                                \
    return out;                                                      \
  }

// Function:
// Vec<size> = func(Vec<size>, Vec<size>, Real)
// New definitions:
// Mat<-1,size> = func(Mat<-1,size>, Vec<size>, Real)
// Mat<-1,size> = func(Vec<size>, Mat<-1,size>, Real)
// Mat<-1,size> = func(Mat<-1,size>, Mat<-1,size>, Real)
#define VEC_DEF_VECTOR_VECTOR_REAL(func, size)                            \
  Mat<-1, size> func(const Mat<-1, size>& x, const Vec<size>& y, Real z); \
  Mat<-1, size> func(const Vec<size>& x, const Mat<-1, size>& y, Real z); \
  Mat<-1, size> func(const Mat<-1, size>& x, const Mat<-1, size>& y, Real z);

#define VEC_IMP_VECTOR_VECTOR_REAL(func, size)                                 \
  Mat<-1, size> func(const Mat<-1, size>& x, const Vec<size>& y, Real z) {     \
    Mat<-1, size> out(x.rows(), size);                                         \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {           \
      Vec<size> x_ = x.row(i);                                                 \
      out.row(i) = func(x_, y, z);                                             \
    }                                                                          \
    return out;                                                                \
  }                                                                            \
  Mat<-1, size> func(const Vec<size>& x, const Mat<-1, size>& y, Real z) {     \
    Mat<-1, size> out(y.rows(), size);                                         \
    _Pragma("omp parallel for") for (int i = 0; i < y.rows(); i++) {           \
      Vec<size> y_ = y.row(i);                                                 \
      out.row(i) = func(x, y_, z);                                             \
    }                                                                          \
    return out;                                                                \
  }                                                                            \
  Mat<-1, size> func(const Mat<-1, size>& x, const Mat<-1, size>& y, Real z) { \
    LUPNT_CHECK(x.rows() == y.rows(), "Size mismatch", "VectorMacros");        \
    Mat<-1, size> out(x.rows(), size);                                         \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {           \
      Vec<size> x_ = x.row(i);                                                 \
      Vec<size> y_ = y.row(i);                                                 \
      out.row(i) = func(x_, y_, z);                                            \
    }                                                                          \
    return out;                                                                \
  }

// Function:
// Vec<size> = func(Vec<size>, Vec<size>
// New definitions:
// Mat<-1,size> = func(Mat<-1,size>, Mat<-1,size>)
// Mat<-1,size> = func(Mat<-1,size>, Vec<size>)
// Mat<-1,size> = func(Vec<size>, Mat<-1,size>)
#define VEC_DEF_VECTOR_VECTOR(func, size)                             \
  Vec<size> func(const Vec<size>& x, const Vec<size>& y);             \
  Mat<-1, size> func(const Mat<-1, size>& x, const Mat<-1, size>& y); \
  Mat<-1, size> func(const Mat<-1, size>& x, const Vec<size>& y);     \
  Mat<-1, size> func(const Vec<size>& x, const Mat<-1, size>& y);

#define VEC_IMP_VECTOR_VECTOR(func, size)                                    \
  Vec<size> func(const Vec<size>& x, const Vec<size>& y) {                   \
    return func(static_cast<const State&>(x), static_cast<const State&>(y)); \
  }                                                                          \
  Mat<-1, size> func(const Mat<-1, size>& x, const Mat<-1, size>& y) {       \
    LUPNT_CHECK(x.rows() == y.rows(), "Size mismatch", "VectorMacros");      \
    Mat<-1, size> out(x.rows(), size);                                       \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {         \
      Vec<size> x_ = x.row(i);                                               \
      Vec<size> y_ = y.row(i);                                               \
      out.row(i) = func(x_, y_);                                             \
    }                                                                        \
    return out;                                                              \
  }                                                                          \
  Mat<-1, size> func(const Mat<-1, size>& x, const Vec<size>& y) {           \
    Mat<-1, size> out(x.rows(), size);                                       \
    _Pragma("omp parallel for") for (int i = 0; i < x.rows(); i++) {         \
      Vec<size> x_ = x.row(i);                                               \
      out.row(i) = func(x_, y);                                              \
    }                                                                        \
    return out;                                                              \
  }                                                                          \
  Mat<-1, size> func(const Vec<size>& x, const Mat<-1, size>& y) {           \
    Mat<-1, size> out(y.rows(), size);                                       \
    _Pragma("omp parallel for") for (int i = 0; i < y.rows(); i++) {         \
      Vec<size> y_ = y.row(i);                                               \
      out.row(i) = func(x, y_);                                              \
    }                                                                        \
    return out;                                                              \
  }

// Function:
// Real = func(Real, Real)
// New definitions:
// vector = func(vector, Real)
// vector = func(Real, vector)
// vector = func(vector, vector)
#define VEC_DEF_REAL_REAL(func)     \
  VecX func(const VecX& x, Real y); \
  VecX func(Real x, const VecX& y); \
  VecX func(const VecX& x, const VecX& y);

#define VEC_IMP_REAL_REAL(func)                                                                   \
  VecX func(const VecX& x, Real y) {                                                              \
    VecX out(x.size());                                                                           \
    _Pragma("omp parallel for") for (int i = 0; i < x.size(); i++) { out(i) = func(x(i), y); }    \
    return out;                                                                                   \
  }                                                                                               \
  VecX func(Real x, const VecX& y) {                                                              \
    VecX out(y.size());                                                                           \
    _Pragma("omp parallel for") for (int i = 0; i < y.size(); i++) { out(i) = func(x, y(i)); }    \
    return out;                                                                                   \
  }                                                                                               \
  VecX func(const VecX& x, const VecX& y) {                                                       \
    if (x.size() != y.size()) throw std::runtime_error("Size mismatch");                          \
    VecX out(x.size());                                                                           \
    _Pragma("omp parallel for") for (int i = 0; i < x.size(); i++) { out(i) = func(x(i), y(i)); } \
    return out;                                                                                   \
  }