Skip to content

Instantly share code, notes, and snippets.

@JohanAR
Created April 3, 2024 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohanAR/1ecdf60e1def764657d87366e7c783dc to your computer and use it in GitHub Desktop.
Save JohanAR/1ecdf60e1def764657d87366e7c783dc to your computer and use it in GitHub Desktop.
Fast coefficient lookup / reference for Eigen3 arrays
// Fast coefficient reference for rowmajor array with fixed number of columns
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<(Flags_ & Eigen::RowMajor) && (Cols_ != Eigen::Dynamic), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> &arr,
Eigen::Index row, Eigen::Index col) -> Scalar_ & {
return *(arr.data() + Cols_ * row + col);
}
// Fast coefficient const reference for rowmajor array with fixed number of columns
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<(Flags_ & Eigen::RowMajor) && (Cols_ != Eigen::Dynamic), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> const &arr,
Eigen::Index row, Eigen::Index col) -> Scalar_ const & {
return *(arr.data() + Cols_ * row + col);
}
// Fast coefficient reference for colmajor array with fixed number of rows
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<!(Flags_ & Eigen::RowMajor) && (Rows_ != Eigen::Dynamic), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> &arr,
Eigen::Index row, Eigen::Index col) -> Scalar_ & {
return *(arr.data() + row + Rows_ * col);
}
// Fast coefficient const reference for colmajor array with fixed number of rows
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<!(Flags_ & Eigen::RowMajor) && (Rows_ != Eigen::Dynamic), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> const &arr,
Eigen::Index row, Eigen::Index col) -> Scalar_ const & {
return *(arr.data() + row + Rows_ * col + row);
}
// Fast coefficient reference for 1d arrays
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<(Rows_ == 1) || (Cols_ == 1), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> &arr,
Eigen::Index idx) -> Scalar_ & {
return *(arr.data() + idx);
}
// Fast coefficient const reference for 1d arrays
template <class Scalar_, int Rows_, int Cols_, int Flags_, int MaxRows_, int MaxCols_,
std::enable_if_t<(Rows_ == 1) || (Cols_ == 1), bool> = true>
auto CO(Eigen::Array<Scalar_, Rows_, Cols_, Flags_, MaxRows_, MaxCols_> const &arr,
Eigen::Index idx) -> Scalar_ const & {
return *(arr.data() + idx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment