Loading...
Searching...
No Matches
horizontal.h
Go to the documentation of this file.
1#ifndef _BBM_NATIVE_HORIZONTAL_H_
2#define _BBM_NATIVE_HORIZONTAL_H_
3
4#include <cmath>
5#include <numeric>
6#include <concepts>
7#include <algorithm>
8#include <functional>
9#include <type_traits>
10
11#include "backbone/math.h"
12
13/************************************************************************/
14/*! \file horizonal.h
15
16 \brief Emulate horizontal operations on scalar and array types
17
18*************************************************************************/
19
20namespace backbone {
21
22 //! @{ \name Horizontal operations on scalars
23 template<typename T> requires std::is_scalar_v<T>
24 inline T hsum(const T& t) { return t; }
25
26 template<typename T> requires std::is_scalar_v<T>
27 inline T hprod(const T& t) { return t; }
28
29 template<typename T> requires std::is_scalar_v<T>
30 inline T hmax(const T& t) { return t; }
31
32 template<typename T> requires std::is_scalar_v<T>
33 inline T hmin(const T& t) { return t; }
34
35 template<typename T> requires std::is_scalar_v<T>
36 inline T dot(const T& a, const T& b) { return a*b; }
37
38 template<typename T> requires std::is_scalar_v<T>
39 inline T norm(const T& t) { return std::abs(t); }
40
41 template<typename T> requires std::is_scalar_v<T>
42 inline T squared_norm(const T& t) { return t*t; }
43
44 template<typename T> requires std::is_scalar_v<T>
45 inline T normalize(const T& ) { return 1; }
46 //! @}
47
48 //! @{ \name Mask operations on scalars
49 template<typename T> requires std::convertible_to<T, bool>
50 inline bool all(const T& t) { return t; }
51
52 template<typename T> requires std::convertible_to<T, bool>
53 inline bool any(const T& t) { return t; }
54
55 template<typename T> requires std::convertible_to<T, bool>
56 inline bool none(const T& t) { return !t; }
57
58 template<typename T> requires std::convertible_to<T, bool>
59 inline size_t count(const T& t) { return t; }
60 //! @}
61
62 //! @{ \name Horizontal operations on arrays
63 template<typename T, size_t N>
64 inline auto hsum(const array<T,N>& t)
65 {
66 return std::accumulate(std::begin(t), std::end(t), std::decay_t<T>(0));
67 }
68
69 template<typename T, size_t N>
70 inline auto hprod(const array<T,N>& t)
71 {
72 return std::accumulate(std::begin(t), std::end(t), std::decay_t<T>(1), std::multiplies<value_t<T>>());
73 }
74
75 template<typename T, size_t N>
76 inline auto hmax(const array<T,N>& t)
77 {
78 return *std::max_element(std::begin(t), std::end(t));
79 }
80
81 template<typename T, size_t N>
82 inline auto hmin(const array<T,N>& t)
83 {
84 return *std::min_element(std::begin(t), std::end(t));
85 }
86
87 template<typename T, typename U, size_t N>
88 inline auto dot(const array<T,N>& a, const array<U,N>& b)
89 {
90 return std::inner_product(std::begin(a), std::end(a), std::begin(b), std::decay_t<T>(0));
91 }
92
93 template<typename T, size_t N>
94 inline auto squared_norm(const array<T,N>& t)
95 {
96 return dot(t, t);
97 }
98
99 template<typename T, size_t N>
100 inline auto norm(const array<T,N>& t)
101 {
102 return sqrt(squared_norm(t));
103 }
104
105 template<typename T, size_t N>
106 inline auto normalize(const array<T,N>& t)
107 {
108 return t * rsqrt(squared_norm(t));
109 }
110 //! @}
111
112 //! @{ \Name Mask operations on arrays
113 template<size_t N>
114 inline bool all(const array<bool,N>& t)
115 {
116 return std::all_of(std::begin(t), std::end(t), std::identity());
117 }
118
119 template<size_t N>
120 inline bool any(const array<bool,N>& t)
121 {
122 return std::any_of(std::begin(t), std::end(t), std::identity());
123 }
124
125 template<size_t N>
126 inline bool none(const array<bool,N>& t)
127 {
128 return std::none_of(std::begin(t), std::end(t), std::identity());
129 }
130
131 template<size_t N>
132 inline size_t count(const array<bool,N>& t)
133 {
134 return hsum(t);
135 }
136 //! @}
137
138} // end backbone namespace
139
140#endif /* _BBM_NATIVE_HORIZONTAL_H_ */
Random number generator; built on top of Drjit.
Definition: backbone.h:53
size_t count(const T &t)
Definition: horizontal.h:59
auto hprod(const T &t)
Definition: horizontal.h:54
typename backbone::detail::value< std::decay_t< T > >::type value_t
Value trait.
Definition: type_traits.h:35
bool none(const T &t)
Definition: horizontal.h:56
bool all(const T &t)
Definition: horizontal.h:50
constexpr auto rsqrt(T a)
Definition: math.h:112
auto hsum(const T &t)
Definition: horizontal.h:53
auto dot(const T &a, const T &b)
Definition: horizontal.h:17
auto norm(const T &a)
Definition: horizontal.h:24
auto squared_norm(const T &a)
Definition: horizontal.h:31
bool any(const T &t)
Definition: horizontal.h:53
auto hmax(const T &t)
Definition: horizontal.h:55
auto normalize(const T &a)
Definition: horizontal.h:38
auto hmin(const T &t)
Definition: horizontal.h:56
Definition: array.h:21