Skip to content

Commit

Permalink
Merge pull request #87 from niermann999/fix-eigen-indices
Browse files Browse the repository at this point in the history
fix: implicit conversions
  • Loading branch information
beomki-yeo authored Jan 30, 2023
2 parents f4c0bb6 + cc3bf88 commit f4e9c1c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 45 deletions.
3 changes: 2 additions & 1 deletion frontend/eigen_cmath/include/algebra/eigen_cmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ template <unsigned int SIZE, typename derived_type>
ALGEBRA_HOST_DEVICE inline auto vector(const Eigen::MatrixBase<derived_type>& m,
std::size_t row, std::size_t col) {

return m.template block<SIZE, 1>(row, col);
return m.template block<SIZE, 1>(static_cast<Eigen::Index>(row),
static_cast<Eigen::Index>(col));
}

/// @name Getter functions on @c algebra::eigen::matrix_type
Expand Down
3 changes: 2 additions & 1 deletion frontend/eigen_eigen/include/algebra/eigen_eigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ template <unsigned int SIZE, typename derived_type>
ALGEBRA_HOST_DEVICE inline auto vector(const Eigen::MatrixBase<derived_type>& m,
std::size_t row, std::size_t col) {

return m.template block<SIZE, 1>(row, col);
return m.template block<SIZE, 1>(static_cast<Eigen::Index>(row),
static_cast<Eigen::Index>(col));
}

/// @name Getter functions on @c algebra::eigen::matrix_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct partial_pivot_lud {

// Get the LU decomposition matrix equal to (L - I) + U
const auto& lu = decomp_res.lu;
const auto& n_pivot = decomp_res.n_pivot;
const size_type n_pivot = static_cast<size_type>(decomp_res.n_pivot);

scalar_t det = element_getter_t()(lu, 0, 0);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Algebra plugins library, part of the ACTS project
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -50,9 +50,10 @@ struct partial_pivot_lud {
// Calculate inv(A) = inv(U) * inv(L) * P;
for (size_type j = 0; j < N; j++) {
for (size_type i = 0; i < N; i++) {
element_getter_t()(inv, i, j) = element_getter_t()(P, 0, i) == j
? static_cast<scalar_t>(1.0)
: static_cast<scalar_t>(0.0);
element_getter_t()(inv, i, j) =
static_cast<size_type>(element_getter_t()(P, 0, i)) == j
? static_cast<scalar_t>(1.0)
: static_cast<scalar_t>(0.0);

for (size_type k = 0; k < i; k++) {
element_getter_t()(inv, i, j) -=
Expand Down
10 changes: 5 additions & 5 deletions math/cmath/include/algebra/math/impl/cmath_matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Algebra plugins library, part of the ACTS project
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -67,8 +67,8 @@ struct actor {
/// Operator setting a block with a vector matrix
template <size_type ROWS, size_type COLS, class input_matrix_type>
ALGEBRA_HOST_DEVICE void set_block(input_matrix_type &m,
const matrix_type<ROWS, COLS> &b, int row,
int col) {
const matrix_type<ROWS, COLS> &b,
size_type row, size_type col) {
for (size_type i = 0; i < ROWS; ++i) {
for (size_type j = 0; j < COLS; ++j) {
element_getter()(m, i + row, j + col) = element_getter()(b, i, j);
Expand All @@ -80,8 +80,8 @@ struct actor {
template <size_type ROWS, template <typename, size_type> class vector_t,
class input_matrix_type>
ALGEBRA_HOST_DEVICE void set_block(input_matrix_type &m,
const vector_t<scalar_t, ROWS> &b, int row,
int col) {
const vector_t<scalar_t, ROWS> &b,
size_type row, size_type col) {
for (size_type i = 0; i < ROWS; ++i) {
element_getter()(m, i + row, col) = b[i];
}
Expand Down
4 changes: 2 additions & 2 deletions math/cmath/include/algebra/math/impl/cmath_transform3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ struct transform3 {
ALGEBRA_HOST_DEVICE
inline bool operator==(const transform3 &rhs) const {

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (size_type i = 0; i < 4; i++) {
for (size_type j = 0; j < 4; j++) {
if (matrix_actor().element(_data, i, j) !=
matrix_actor().element(rhs._data, i, j)) {
return false;
Expand Down
28 changes: 17 additions & 11 deletions math/eigen/include/algebra/math/impl/eigen_getter.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Algebra plugins, part of the ACTS project
*
* (c) 2020-2022 CERN for the benefit of the ACTS project
* (c) 2020-2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -108,7 +108,7 @@ struct element_getter {
Eigen::MatrixBase<derived_type> &m, size_type_1 row,
size_type_2 col) const {

return m(row, col);
return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
}
/// Get const access to a matrix element
template <typename derived_type, typename size_type_1, typename size_type_2,
Expand All @@ -120,12 +120,16 @@ struct element_getter {
const Eigen::MatrixBase<derived_type> &m, size_type_1 row,
size_type_2 col) const {

return m(row, col);
return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
}
}; // struct element_getter

/// Function extracting an element from a matrix (const)
template <typename derived_type, typename size_type_1, typename size_type_2>
template <
typename derived_type, typename size_type_1, typename size_type_2,
std::enable_if_t<std::is_convertible<size_type_1, Eigen::Index>::value &&
std::is_convertible<size_type_2, Eigen::Index>::value,
bool> = true>
ALGEBRA_HOST_DEVICE inline auto element(
const Eigen::MatrixBase<derived_type> &m, size_type_1 row,
size_type_2 col) {
Expand All @@ -134,12 +138,14 @@ ALGEBRA_HOST_DEVICE inline auto element(
}

/// Function extracting an element from a matrix (non-const)
template <
typename derived_type, typename size_type_1, typename size_type_2,
std::enable_if_t<std::is_base_of<Eigen::DenseCoeffsBase<
derived_type, Eigen::WriteAccessors>,
Eigen::MatrixBase<derived_type> >::value,
bool> = true>
template <typename derived_type, typename size_type_1, typename size_type_2,
std::enable_if_t<
std::is_base_of<
Eigen::DenseCoeffsBase<derived_type, Eigen::WriteAccessors>,
Eigen::MatrixBase<derived_type> >::value &&
std::is_convertible<size_type_1, Eigen::Index>::value &&
std::is_convertible<size_type_2, Eigen::Index>::value,
bool> = true>
ALGEBRA_HOST_DEVICE inline auto &element(Eigen::MatrixBase<derived_type> &m,
size_type_1 row, size_type_2 col) {

Expand All @@ -161,4 +167,4 @@ struct block_getter {
}
}; // struct block_getter

} // namespace algebra::eigen::math
} // namespace algebra::eigen::math
14 changes: 8 additions & 6 deletions math/eigen/include/algebra/math/impl/eigen_matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Algebra plugins library, part of the ACTS project
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -52,7 +52,7 @@ struct actor {
ALGEBRA_HOST_DEVICE inline scalar_t &element(matrix_type<ROWS, COLS> &m,
size_type_1 row,
size_type_2 col) const {
return m(row, col);
return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
}

/// Operator getting one value of a const matrix
Expand All @@ -64,7 +64,7 @@ struct actor {
ALGEBRA_HOST_DEVICE inline scalar_t element(const matrix_type<ROWS, COLS> &m,
size_type_1 row,
size_type_2 col) const {
return m(row, col);
return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
}

/// Operator getting a block of a const matrix
Expand All @@ -77,7 +77,8 @@ struct actor {
ALGEBRA_HOST_DEVICE matrix_type<ROWS, COLS> block(const input_matrix_type &m,
size_type_1 row,
size_type_2 col) {
return m.template block<ROWS, COLS>(row, col);
return m.template block<ROWS, COLS>(static_cast<Eigen::Index>(row),
static_cast<Eigen::Index>(col));
}

/// Operator setting a block
Expand All @@ -90,7 +91,8 @@ struct actor {
ALGEBRA_HOST_DEVICE void set_block(input_matrix_type &m,
const matrix_type<ROWS, COLS> &b,
size_type_1 row, size_type_2 col) {
m.template block<ROWS, COLS>(row, col) = b;
m.template block<ROWS, COLS>(static_cast<Eigen::Index>(row),
static_cast<Eigen::Index>(col)) = b;
}

// Create zero matrix
Expand Down Expand Up @@ -139,4 +141,4 @@ struct actor {
}
};

} // namespace algebra::eigen::matrix
} // namespace algebra::eigen::matrix
24 changes: 13 additions & 11 deletions math/smatrix/include/algebra/math/impl/smatrix_getter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ template <typename scalar_t, auto N, std::enable_if_t<N >= 2, bool> = true>
ALGEBRA_HOST inline scalar_t phi(
const ROOT::Math::SVector<scalar_t, N> &v) noexcept {

return TMath::ATan2(v[1], v[0]);
return static_cast<scalar_t>(TMath::ATan2(v[1], v[0]));
}

template <typename scalar_t, class A, auto N,
std::enable_if_t<N >= 2, bool> = true>
ALGEBRA_HOST inline scalar_t phi(
const ROOT::Math::VecExpr<A, scalar_t, N> &v) noexcept {

return TMath::ATan2(v.apply(1), v.apply(0));
return static_cast<scalar_t>(TMath::ATan2(v.apply(1), v.apply(0)));
}

/** This method retrieves theta from a vector, vector base with rows >= 3
Expand All @@ -49,17 +49,18 @@ template <typename scalar_t, auto N, std::enable_if_t<N >= 3, bool> = true>
ALGEBRA_HOST inline scalar_t theta(
const ROOT::Math::SVector<scalar_t, N> &v) noexcept {

return TMath::ATan2(TMath::Sqrt(v[0] * v[0] + v[1] * v[1]), v[2]);
return static_cast<scalar_t>(
TMath::ATan2(TMath::Sqrt(v[0] * v[0] + v[1] * v[1]), v[2]));
}

template <typename scalar_t, class A, auto N,
std::enable_if_t<N >= 3, bool> = true>
ALGEBRA_HOST inline scalar_t theta(
const ROOT::Math::VecExpr<A, scalar_t, N> &v) noexcept {

return TMath::ATan2(
return static_cast<scalar_t>(TMath::ATan2(
TMath::Sqrt(v.apply(0) * v.apply(0) + v.apply(1) * v.apply(1)),
v.apply(2));
v.apply(2)));
}

/** This method retrieves the norm of a vector, no dimension restriction
Expand All @@ -69,14 +70,14 @@ ALGEBRA_HOST inline scalar_t theta(
template <typename scalar_t, auto N>
ALGEBRA_HOST inline scalar_t norm(const ROOT::Math::SVector<scalar_t, N> &v) {

return TMath::Sqrt(ROOT::Math::Dot(v, v));
return static_cast<scalar_t>(TMath::Sqrt(ROOT::Math::Dot(v, v)));
}

template <typename scalar_t, class A, auto N>
ALGEBRA_HOST inline scalar_t norm(
const ROOT::Math::VecExpr<A, scalar_t, N> &v) {

return TMath::Sqrt(ROOT::Math::Dot(v, v));
return static_cast<scalar_t>(TMath::Sqrt(ROOT::Math::Dot(v, v)));
}

/** This method retrieves the pseudo-rapidity from a vector or vector base with
Expand All @@ -88,15 +89,15 @@ template <typename scalar_t, auto N, std::enable_if_t<N >= 3, bool> = true>
ALGEBRA_HOST inline scalar_t eta(
const ROOT::Math::SVector<scalar_t, N> &v) noexcept {

return TMath::ATanH(v[2] / norm(v));
return static_cast<scalar_t>(TMath::ATanH(v[2] / norm(v)));
}

template <typename scalar_t, class A, auto N,
std::enable_if_t<N >= 3, bool> = true>
ALGEBRA_HOST inline scalar_t eta(
const ROOT::Math::VecExpr<A, scalar_t, N> &v) noexcept {

return TMath::ATanH(v.apply(2) / norm(v));
return static_cast<scalar_t>(TMath::ATanH(v.apply(2) / norm(v)));
}

/** This method retrieves the perpenticular magnitude of a vector with rows >= 2
Expand All @@ -107,15 +108,16 @@ template <typename scalar_t, auto N, std::enable_if_t<N >= 2, bool> = true>
ALGEBRA_HOST inline scalar_t perp(
const ROOT::Math::SVector<scalar_t, N> &v) noexcept {

return TMath::Sqrt(v[0] * v[0] + v[1] * v[1]);
return static_cast<scalar_t>(TMath::Sqrt(v[0] * v[0] + v[1] * v[1]));
}

template <typename scalar_t, class A, auto N,
std::enable_if_t<N >= 2, bool> = true>
ALGEBRA_HOST inline scalar_t perp(
const ROOT::Math::VecExpr<A, scalar_t, N> &v) noexcept {

return TMath::Sqrt(v.apply(0) * v.apply(0) + v.apply(1) * v.apply(1));
return static_cast<scalar_t>(
TMath::Sqrt(v.apply(0) * v.apply(0) + v.apply(1) * v.apply(1)));
}

/// Functor used to access elements of Vc matrices
Expand Down
6 changes: 3 additions & 3 deletions tests/common/test_host_basics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ TYPED_TEST_P(test_host_basics, matrix64) {
for (typename TypeParam::size_type i = 0; i < ROWS; ++i) {
for (typename TypeParam::size_type j = 0; j < COLS; ++j) {
algebra::getter::element(m, i, j) =
static_cast<typename TypeParam::scalar>(0.5 * i + j);
0.5f * static_cast<typename TypeParam::scalar>(i + j);
}
}

Expand All @@ -160,7 +160,7 @@ TYPED_TEST_P(test_host_basics, matrix64) {
for (typename TypeParam::size_type i = 0; i < ROWS; ++i) {
for (typename TypeParam::size_type j = 0; j < COLS; ++j) {
const typename TypeParam::scalar ref =
static_cast<typename TypeParam::scalar>(0.5 * i + j);
0.5f * static_cast<typename TypeParam::scalar>(i + j);
ASSERT_NEAR(algebra::getter::element(m, i, j), ref, this->m_epsilon);
ASSERT_NEAR(algebra::getter::element(m_const_ref, i, j), ref,
this->m_epsilon);
Expand Down Expand Up @@ -564,7 +564,7 @@ TYPED_TEST_P(test_host_basics, transform3) {
std::array<typename TypeParam::scalar, 16> matray_helper = {
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
typename TypeParam::transform3::template array_type<16> matray;
for (int i = 0; i < 16; ++i) {
for (unsigned int i = 0u; i < 16u; ++i) {
matray[i] = matray_helper[i];
}
typename TypeParam::transform3 trfma(matray);
Expand Down

0 comments on commit f4e9c1c

Please sign in to comment.