88 std::cout <<
"Vector size: " <<
size() <<
"\n";
90 std::cout <<
"[" <<
f <<
"]\n";
104 throw std::invalid_argument(
"Index must be 0, 1, or 2");
114 size_t m = std::min(
data.size(), other.
data.size());
115 for (
size_t i = 0;
i <
m; ++
i)
117 for (
size_t i =
m;
i <
data.size(); ++
i)
127 if (
v.size() !=
size())
128 throw std::invalid_argument(
"Vector sizes do not match");
130 using reg =
typename Simd::reg;
137 for (;
i + 15 <
n;
i += 16) {
157 if (
v.size() !=
size())
158 throw std::invalid_argument(
"Vector sizes do not match");
160 using reg =
typename Simd::reg;
166 for (;
i + 15 <
n;
i += 16) {
189 using reg =
typename Simd::reg;
195 for (;
i + 15 <
n;
i += 16) {
219 using reg =
typename Simd::reg;
222 if (
u.size() !=
coefs.size())
223 throw std::invalid_argument(
"Mismatched number of vectors and coefficients");
227 const size_t n =
u[0].size();
228 for (
const auto &
v :
u)
230 throw std::invalid_argument(
"Vector sizes do not match");
236 for (;
i + W - 1 <
n;
i += W) {
237 reg acc = Simd::zero();
238 for (
size_t j = 0;
j <
u.size(); ++
j) {
241 acc = Simd::fmadd(
v,
c, acc);
248 for (
size_t j = 0;
j <
u.size(); ++
j)
265 if (a.size() !=
b.size())
266 throw std::invalid_argument(
"Vector sizes do not match");
268 using reg =
typename Simd::reg;
271 const size_t n = a.size();
305 using reg =
typename Simd::reg;
307 if (
v.size() !=
size())
308 throw std::invalid_argument(
"Vector sizes do not match");
309 const size_t n =
size();
311 reg acc = Simd::zero();
319 acc = Simd::fmadd(a,
b, acc);
322 float result = detail::reduce_sum(acc);
338 using reg =
typename Simd::reg;
340 reg acc = Simd::zero();
347 acc = Simd::add(acc,
abs_v);
350 float result = detail::reduce_sum(acc);
365 using reg =
typename Simd::reg;
367 reg acc = Simd::zero();
372 acc = Simd::fmadd(
v,
v, acc);
375 float result = detail::reduce_sum(acc);
380 return std::pow(
result, 0.5f);
390 using reg =
typename Simd::reg;
418 if (
u.size() !=
v.size())
419 throw std::invalid_argument(
"Vector sizes do not match");
436 if (
u.size() != 3 ||
v.size() != 3)
437 throw std::invalid_argument(
"Cross product is only defined for 3D vectors.");
455 size_t m = std::min(n, b.size());
456 for (
size_t i = 0; i <
m; ++i)
457 result[i] = a[i] + b[i];
458 for (
size_t i =
m; i < n; ++i)
464 assert(a.size() == b.size());
466 for (
size_t i = 0; i < a.size(); ++i)
467 result[i] = a[i] - b[i];
static double _max(double a, double b)
Definition MathsUtils.hpp:21
static float _fabs(float a)
Definition MathsUtils.hpp:13
Multi-dimensional tensor class with fixed rank and SIMD support.
Definition Tensor.hpp:25
void resize(const std::array< size_t, Rank > &dims)
Resize 2D tensor.
Definition Tensor.hpp:70
aligned_vector< K > data
Definition Tensor.hpp:31
Aligned, SIMD-optimized mathematical vector class for scientific computing.
Definition Vector.hpp:26
__attribute__((always_inline, hot, flatten)) inline float dot(const Vector< float > &v) const
Compute the dot product with another vector.
Definition Vector.hpp:303
Vector(size_t n, K value)
Construct a constant vector.
Definition Vector.hpp:62
size_t i
Definition Vector.hpp:277
__attribute__((always_inline, hot, flatten)) static inline Vector< float > cross_product(const Vector< float > &u
Compute the cross product between two 3D vectors.
const K & operator()(size_t i) const
Definition Vector.hpp:45
__m128 uxy
Definition Vector.hpp:441
__attribute__((always_inline, hot, flatten)) Vector< K > operator-(const Vector< K > &other) const
Subtract two vectors.
Definition Vector.hpp:112
const size_t simd_width
Definition Vector.hpp:269
Vector< float > result(n)
const float norm_v
Definition Vector.hpp:423
_mm_prefetch((const char *)&a.data[0], _MM_HINT_T0)
const K & operator[](size_t i) const
Definition Vector.hpp:44
Vector(std::initializer_list< K > init)
Construct from an initializer list.
Definition Vector.hpp:56
auto end()
Definition Vector.hpp:68
const reg vt
Definition Vector.hpp:274
__attribute__((always_inline, hot, flatten)) inline void scl(float a)
Scale this vector by a scalar (in-place).
Definition Vector.hpp:185
auto end() const
Definition Vector.hpp:70
__attribute__((always_inline, hot, flatten)) static inline Vector< float > lerp(const Vector< float > &a
Linearly interpolate between two vectors.
__attribute__((always_inline, hot, flatten)) inline void sub(const Vector &v)
Subtract another vector from this one (in-place).
Definition Vector.hpp:156
void resize(size_t n)
Definition Vector.hpp:77
return r
Definition Vector.hpp:448
typename Simd::reg reg
Definition Vector.hpp:268
Vector(const std::vector< K > &vec)
Construct from a standard vector.
Definition Vector.hpp:37
const size_t n
Definition Vector.hpp:271
const float dot
Definition Vector.hpp:421
void print() const
Print the vector to stdout.
Definition Vector.hpp:87
Vector(size_t n)
Construct an empty vector of size n.
Definition Vector.hpp:51
auto begin() const
Definition Vector.hpp:69
__attribute__((always_inline, hot, flatten)) inline float norm_2() const
Compute the 2-norm (Euclidean norm).
Definition Vector.hpp:361
const Vector< float > & b
Definition Vector.hpp:264
const Vector< float > & v
Definition Vector.hpp:417
aligned_vector< K > data
Underlying aligned data storage (SIMD-friendly).
Definition Vector.hpp:29
size_t size() const
Definition Vector.hpp:76
__attribute__((always_inline, hot, flatten)) static inline float angle_cos(const Vector< float > &u
Compute the cosine of the angle between two vectors.
return result
Definition Vector.hpp:291
__attribute__((always_inline, hot, flatten)) inline float norm_inf() const
Compute the infinity norm (maximum absolute value).
Definition Vector.hpp:386
auto begin()
Definition Vector.hpp:67
K & operator()(size_t i)
Definition Vector.hpp:46
const reg vt1
Definition Vector.hpp:275
K & operator[](size_t i)
Definition Vector.hpp:43
__m128 vxy
Definition Vector.hpp:442
const float norm_u
Definition Vector.hpp:422
static Vector< K > canonical(int index, K dx, K dy, K dz)
Definition Vector.hpp:95
__attribute__((always_inline, hot, flatten)) static inline Vector< float > linear_combination(const std
Compute the linear combination of vectors with coefficients.
Definition Vector.hpp:216
__attribute__((always_inline, hot, flatten)) inline void add(const Vector &v)
Add another vector to this one (in-place).
Definition Vector.hpp:126
const Vector< float > float t
Definition Vector.hpp:264
__attribute__((always_inline, hot, flatten)) inline float norm_1() const
Compute the 1-norm (sum of absolute values).
Definition Vector.hpp:334
Definition Derivate.hpp:24
Vector< K > operator-(const Vector< K > &a, const Vector< K > &b)
Definition Vector.hpp:463
Vector< K > operator+(const Vector< K > &a, const Vector< K > &b)
Definition Vector.hpp:452