Skip to content

Commit

Permalink
deduplicate type traits
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Nov 16, 2024
1 parent 86cda05 commit 65850f1
Show file tree
Hide file tree
Showing 28 changed files with 405 additions and 1,067 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
#include "benchmark_vector.hpp"
#include "register_benchmark.hpp"

namespace algebra::bench_op {} // namespace algebra::bench_op
namespace algebra::bench_op { /* TODO: Implement */
} // namespace algebra::bench_op
104 changes: 99 additions & 5 deletions common/include/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ template <class M>
struct matrix {};

template <class M, index_t<M> ROWS, index_t<M> COLS, typename T>
using other_matrix_t = typename matrix<M>::template other_type<T, ROWS, COLS>;
using get_matrix_t = typename matrix<M>::template other_type<T, ROWS, COLS>;
/// @}

/// Matrix dimensions
Expand All @@ -71,8 +71,7 @@ struct dimensions {};

/// Specilization for scalar types
template <typename M>
requires std::is_fundamental_v<M>
struct dimensions<M> {
requires std::is_fundamental_v<M> struct dimensions<M> {

using size_type = std::size_t;

Expand Down Expand Up @@ -121,6 +120,101 @@ template <class M>
using block_getter_t = typename block_getter<M>::type;
/// @}

/// @}

} // namespace algebra::trait

/// Default type trait specializations
/// @{
#define ALGEBRA_PLUGINS_DEFINE_TYPE_TRAITS(A) \
\
namespace trait { \
\
template <typename T, auto N> \
struct index<A::vector_type<T, N>> { \
using type = algebra::A::size_type; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct index<A::matrix_type<T, ROWS, COLS>> { \
using type = algebra::A::size_type; \
}; \
\
template <typename T, auto N> \
struct dimensions<A::vector_type<T, N>> { \
\
using size_type = index_t<A::vector_type<T, N>>; \
\
static constexpr size_type dim{1}; \
static constexpr size_type rows{N}; \
static constexpr size_type columns{1}; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct dimensions<A::matrix_type<T, ROWS, COLS>> { \
\
using size_type = index_t<A::matrix_type<T, ROWS, COLS>>; \
\
static constexpr size_type dim{2}; \
static constexpr size_type rows{ROWS}; \
static constexpr size_type columns{COLS}; \
}; \
\
template <typename T, auto N> \
struct value<A::vector_type<T, N>> { \
using type = T; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct value<A::matrix_type<T, ROWS, COLS>> { \
using type = T; \
}; \
\
template <typename T, auto N> \
struct vector<A::vector_type<T, N>> { \
\
template <typename other_T, auto other_N> \
using other_type = A::vector_type<other_T, other_N>; \
\
using type = other_type<T, N>; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct vector<A::matrix_type<T, ROWS, COLS>> { \
\
template <typename other_T, auto other_N> \
using other_type = A::vector_type<other_T, other_N>; \
\
using type = other_type<T, ROWS>; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct matrix<A::matrix_type<T, ROWS, COLS>> { \
template <typename other_T, auto other_ROWS, auto other_COLS> \
using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
\
using type = A::matrix_type<T, ROWS, COLS>; \
}; \
\
template <typename T, auto N> \
struct matrix<A::vector_type<T, N>> { \
template <typename other_T, auto other_ROWS, auto other_COLS> \
using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
\
using type = other_type<T, N, 1>; \
}; \
\
template <typename T, auto N> \
struct element_getter<A::vector_type<T, N>> { \
using type = A::element_getter; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct element_getter<A::matrix_type<T, ROWS, COLS>> { \
using type = A::element_getter; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct block_getter<A::matrix_type<T, ROWS, COLS>> { \
using type = A::block_getter; \
}; \
\
} // namespace algebra::trait
4 changes: 2 additions & 2 deletions math/cmath/include/algebra/math/impl/cmath_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ requires(std::is_scalar_v<typename matrix_t::value_type::value_type>)
/// Set @param m as zero matrix
template <std::size_t ROWS, std::size_t COLS, typename scalar_t,
template <typename, std::size_t> class array_t>
ALGEBRA_HOST_DEVICE inline constexpr void set_zero(
ALGEBRA_HOST_DEVICE constexpr void set_zero(
array_t<array_t<scalar_t, ROWS>, COLS> &m) {
m = zero<array_t<array_t<scalar_t, ROWS>, COLS>>();
}

/// Set @param m as identity matrix
template <std::size_t ROWS, std::size_t COLS, typename scalar_t,
template <typename, std::size_t> class array_t>
ALGEBRA_HOST_DEVICE inline constexpr void set_identity(
ALGEBRA_HOST_DEVICE constexpr void set_identity(
array_t<array_t<scalar_t, ROWS>, COLS> &m) {
m = identity<array_t<array_t<scalar_t, ROWS>, COLS>>();
}
Expand Down
64 changes: 28 additions & 36 deletions math/cmath/include/algebra/math/impl/cmath_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ namespace algebra::cmath {
///
/// @return the scalar dot product value
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N,
std::enable_if_t<std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t dot(const array_t<scalar_t, N> &a,
const array_t<scalar_t, N> &b) {
typename scalar_t, size_type N>
requires std::is_scalar_v<scalar_t> ALGEBRA_HOST_DEVICE inline scalar_t dot(
const array_t<scalar_t, N> &a, const array_t<scalar_t, N> &b) {
array_t<scalar_t, N> tmp;
for (size_type i = 0; i < N; i++) {
tmp[i] = a[i] * b[i];
Expand All @@ -41,13 +40,11 @@ ALGEBRA_HOST_DEVICE inline scalar_t dot(const array_t<scalar_t, N> &a,
/// @param b the second input matrix with single column
///
/// @return the scalar dot product value
template <
typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS,
std::enable_if_t<COLS == 1 && std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t dot(
const array_t<scalar_t, N> &a,
const array_t<array_t<scalar_t, N>, COLS> &b) {
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS>
requires(COLS == 1 && std::is_scalar_v<scalar_t>) ALGEBRA_HOST_DEVICE
inline scalar_t dot(const array_t<scalar_t, N> &a,
const array_t<array_t<scalar_t, N>, COLS> &b) {
array_t<scalar_t, N> tmp;
for (size_type i = 0; i < N; i++) {
tmp[i] = a[i] * b[0][i];
Expand All @@ -65,13 +62,11 @@ ALGEBRA_HOST_DEVICE inline scalar_t dot(
/// @param b the second input vector
///
/// @return the scalar dot product value
template <
typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS,
std::enable_if_t<COLS == 1 && std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t dot(
const array_t<array_t<scalar_t, N>, COLS> &a,
const array_t<scalar_t, N> &b) {
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS>
requires(COLS == 1 && std::is_scalar_v<scalar_t>) ALGEBRA_HOST_DEVICE
inline scalar_t dot(const array_t<array_t<scalar_t, N>, COLS> &a,
const array_t<scalar_t, N> &b) {
array_t<scalar_t, N> tmp;
for (size_type i = 0; i < N; i++) {
tmp[i] = a[0][i] * b[i];
Expand All @@ -89,13 +84,11 @@ ALGEBRA_HOST_DEVICE inline scalar_t dot(
/// @param b the second input matrix with single column
///
/// @return the scalar dot product value
template <
typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS,
std::enable_if_t<COLS == 1 && std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t dot(
const array_t<array_t<scalar_t, N>, COLS> &a,
const array_t<array_t<scalar_t, N>, COLS> &b) {
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N, size_type COLS>
requires(COLS == 1 && std::is_scalar_v<scalar_t>) ALGEBRA_HOST_DEVICE
inline scalar_t dot(const array_t<array_t<scalar_t, N>, COLS> &a,
const array_t<array_t<scalar_t, N>, COLS> &b) {
array_t<scalar_t, N> tmp;
for (size_type i = 0; i < N; i++) {
tmp[i] = a[0][i] * b[0][i];
Expand All @@ -111,9 +104,9 @@ ALGEBRA_HOST_DEVICE inline scalar_t dot(
///
/// @param v the input vector
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N,
std::enable_if_t<(N >= 2) && std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t norm(const array_t<scalar_t, N> &v) {
typename scalar_t, size_type N>
requires(N >= 2 && std::is_scalar_v<scalar_t>) ALGEBRA_HOST_DEVICE
inline scalar_t norm(const array_t<scalar_t, N> &v) {

return algebra::math::sqrt(dot(v, v));
}
Expand All @@ -123,10 +116,9 @@ ALGEBRA_HOST_DEVICE inline scalar_t norm(const array_t<scalar_t, N> &v) {
///
/// @param v the input vector
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N,
std::enable_if_t<(N >= 3) && std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline scalar_t eta(
const array_t<scalar_t, N> &v) noexcept {
typename scalar_t, size_type N>
requires(N >= 3 && std::is_scalar_v<scalar_t>) ALGEBRA_HOST_DEVICE
inline scalar_t eta(const array_t<scalar_t, N> &v) noexcept {

return algebra::math::atanh(v[2] / norm(v));
}
Expand All @@ -135,10 +127,10 @@ ALGEBRA_HOST_DEVICE inline scalar_t eta(
///
/// @param v the input vector
template <typename size_type, template <typename, size_type> class array_t,
typename scalar_t, size_type N,
std::enable_if_t<std::is_scalar_v<scalar_t>, bool> = true>
ALGEBRA_HOST_DEVICE inline array_t<scalar_t, N> normalize(
const array_t<scalar_t, N> &v) {
typename scalar_t, size_type N>
requires std::is_scalar_v<scalar_t>
ALGEBRA_HOST_DEVICE inline array_t<scalar_t, N> normalize(
const array_t<scalar_t, N> &v) {

return (static_cast<scalar_t>(1.) / norm(v)) * v;
}
Expand Down
6 changes: 2 additions & 4 deletions math/eigen/include/algebra/math/impl/eigen_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ ALGEBRA_HOST_DEVICE inline auto norm(const Eigen::MatrixBase<derived_type> &v) {
/// rows >= 3
///
/// @param v the input vector
template <
typename derived_type,
std::enable_if_t<Eigen::MatrixBase<derived_type>::RowsAtCompileTime >= 3,
bool> = true>
template <typename derived_type>
requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime >= 3)
ALGEBRA_HOST_DEVICE inline auto eta(
const Eigen::MatrixBase<derived_type> &v) noexcept {

Expand Down
9 changes: 6 additions & 3 deletions math/fastor/include/algebra/math/impl/fastor_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace algebra::fastor::math {
/// This method retrieves theta from a vector, vector base with rows >= 3
///
/// @param v the input vector
template <typename scalar_t, auto N, std::enable_if_t<N >= 3, bool> = true>
template <typename scalar_t, auto N>
requires(N >= 3)
ALGEBRA_HOST inline scalar_t theta(
const Fastor::Tensor<scalar_t, N> &v) noexcept {

Expand All @@ -34,7 +35,8 @@ ALGEBRA_HOST inline scalar_t theta(
/// This method retrieves the perpenticular magnitude of a vector with rows >= 2
///
/// @param v the input vector
template <typename scalar_t, auto N, std::enable_if_t<N >= 2, bool> = true>
template <typename scalar_t, auto N>
requires(N >= 2)
ALGEBRA_HOST inline scalar_t perp(
const Fastor::Tensor<scalar_t, N> &v) noexcept {

Expand All @@ -55,7 +57,8 @@ ALGEBRA_HOST inline scalar_t norm(const Fastor::Tensor<scalar_t, N> &v) {
/// rows >= 3
///
/// @param v the input vector
template <typename scalar_t, auto N, std::enable_if_t<N >= 3, bool> = true>
template <typename scalar_t, auto N>
requires(N >= 3)
ALGEBRA_HOST inline scalar_t eta(
const Fastor::Tensor<scalar_t, N> &v) noexcept {

Expand Down
2 changes: 1 addition & 1 deletion math/generic/include/algebra/math/impl/generic_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ALGEBRA_HOST_DEVICE inline auto transpose(const matrix_t &m) {
constexpr index_t rows{algebra::trait::rows<matrix_t>};
constexpr index_t columns{algebra::trait::columns<matrix_t>};

algebra::trait::other_matrix_t<matrix_t, columns, rows, value_t> ret;
algebra::trait::get_matrix_t<matrix_t, columns, rows, value_t> ret;

for (index_t i = 0; i < rows; ++i) {
for (index_t j = 0; j < columns; ++j) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ struct transform3 {
///
/// @param m is the full 4x4 matrix
ALGEBRA_HOST_DEVICE
explicit transform3(const matrix44 &m) {

_data = m;
explicit transform3(const matrix44 &m) : _data{m} {
_data_inv = matrix_inversion{}(_data);
}

Expand Down
Loading

0 comments on commit 65850f1

Please sign in to comment.