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 1d3b1c6
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 966 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
102 changes: 98 additions & 4 deletions common/include/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, int N> \
struct matrix<A::vector_type<T, N>> { \
template <typename other_T, int other_ROWS, int 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
1 change: 0 additions & 1 deletion math/smatrix/include/algebra/math/smatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

// Project include(s).
#include "algebra/math/impl/smatrix_getter.hpp"
#include "algebra/math/impl/smatrix_matrix.hpp"
#include "algebra/math/impl/smatrix_transform3.hpp"
#include "algebra/math/impl/smatrix_vector.hpp"
119 changes: 6 additions & 113 deletions storage/array/include/algebra/storage/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,120 +44,13 @@ using vector2 = storage_type<T, 2>;
template <typename T>
using point2 = vector2<T>;

} // namespace array

namespace trait {

/// Type trait specializations
/// @{

/// Index
/// @{
template <typename T, std::size_t N>
struct index<std::array<T, N>> {
using type = algebra::array::size_type;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct index<std::array<std::array<T, ROWS>, COLS>> {
using type = algebra::array::size_type;
};
/// @}

/// Dimension
/// @{
template <typename T, std::size_t N>
struct dimensions<std::array<T, N>> {

using size_type = index_t<std::array<T, N>>;

static constexpr size_type dim{1};
static constexpr size_type rows{N};
static constexpr size_type columns{1};
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct dimensions<std::array<std::array<T, ROWS>, COLS>> {

using size_type = index_t<std::array<std::array<T, ROWS>, COLS>>;

static constexpr size_type dim{2};
static constexpr size_type rows{ROWS};
static constexpr size_type columns{COLS};
};
/// @}

/// Value
/// @{
template <typename T, std::size_t N>
struct value<std::array<T, N>> {
using type = T;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct value<std::array<std::array<T, ROWS>, COLS>> {
using type = T;
};
/// @}
/// Element Getter
using element_getter = cmath::storage::element_getter;
/// Block Getter
using block_getter = cmath::storage::block_getter;

/// Vector
/// @{
template <typename T, std::size_t N>
struct vector<std::array<T, N>> {

template <typename other_T, std::size_t other_N>
using other_type = std::array<other_T, other_N>;

using type = other_type<T, N>;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct vector<std::array<std::array<T, ROWS>, COLS>> {

template <typename other_T, std::size_t other_N>
using other_type = std::array<other_T, other_N>;

using type = other_type<T, ROWS>;
};
/// @}

/// Matrix
/// @{
template <typename T, std::size_t ROWS, std::size_t COLS>
struct matrix<std::array<std::array<T, ROWS>, COLS>> {
template <typename other_T, std::size_t other_ROWS, std::size_t other_COLS>
using other_type = std::array<std::array<other_T, other_ROWS>, other_COLS>;

using type = std::array<std::array<T, ROWS>, COLS>;
};

template <typename T, int N>
struct matrix<std::array<T, N>> {
template <typename other_T, int other_ROWS, int other_COLS>
using other_type = std::array<std::array<other_T, other_ROWS>, other_COLS>;

using type = other_type<T, N, 1>;
};
/// @}

/// Elemet/Block Getter
/// @{
template <typename T, std::size_t N>
struct element_getter<std::array<T, N>> {
using type = cmath::storage::element_getter;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct element_getter<std::array<std::array<T, ROWS>, COLS>> {
using type = cmath::storage::element_getter;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct block_getter<std::array<std::array<T, ROWS>, COLS>> {
using type = cmath::storage::block_getter;
};
/// @}
} // namespace array

} // namespace trait
ALGEBRA_PLUGINS_DEFINE_TYPE_TRAITS(array)

} // namespace algebra
Loading

0 comments on commit 1d3b1c6

Please sign in to comment.