Tensorium
Loading...
Searching...
No Matches
Functional.hpp
Go to the documentation of this file.
1#pragma once
2#include "../Core/Vector.hpp"
3#include "../Core/Matrix.hpp"
4#include "../Core/Tensor.hpp"
9
10namespace tensorium {
11 // === VECTOR OPS ===
12
13/*
14 * @brief Add two vectors
15 * @param a First vector
16 * @param b Second vector
17 * @return Resulting vector
18 */
19
20 template <typename T>
21 Vector<T> add_vec(const Vector<T>& a, const Vector<T>& b) {
22 Vector<T> result = a;
23 result.add(b);
24 return result;
25 }
26
27/*
28 * @brief Subtract two vectors
29 * @param a First vector
30 * @param b Second vector
31 * @return Resulting vector
32 */
33
34 template <typename T>
35 Vector<T> sub_vec(const Vector<T>& a, const Vector<T>& b) {
36 Vector<T> result = a;
37 result.sub(b);
38 return result;
39 }
40
41/*
42 * @brief Scale a vector by a scalar
43 * @param a Vector to scale
44 * @param scalar Scalar value
45 * @return Resulting vector
46 */
47
48 template <typename T>
50 Vector<T> result = a;
51 result.scl(scalar);
52 return result;
53 }
54
55/*
56 * @brief Normalize a vector
57 * @param a Vector to normalize
58 * @return Normalized vector
59 */
60
61 template <typename T> T norm1_vec(const Vector<T>& a) { return a.norm_1(); }
62
63/*
64 * @brief Normalize 2 a vector
65 * @param a Vector to normalize
66 * @return Normalized vector
67 */
68
69 template <typename T> T norm2_vec(const Vector<T>& a) { return a.norm_2(); }
70
71/*
72 * @brief Normalize inf a vector
73 * @param a Vector to normalize
74 * @return Normalized vector
75 */
76
77 template <typename T> T normInf_vec(const Vector<T>& a) { return a.norm_inf(); }
78
79/*
80 * @brief Dot product of two vectors
81 * @param a First vector
82 * @param b Second vector
83 * @return Dot product result
84 */
85
86 template <typename T> T dot_vec(const Vector<T>& a, const Vector<T>& b) { return a.dot(b); }
87/*
88 * Cosine of the angle between two vectors
89 * @param a First vector
90 * @param b Second vector
91 * @return Cosine of the angle
92 */
93
94 template <typename T> T cosine_vec(const Vector<T>& a, const Vector<T>& b) { return Vector<T>::angle_cos(a, b); }
95/*
96 * Lerp between two vectors
97 * @param a First vector
98 * @param b Second vector
99 * @param t Interpolation factor
100 * @return Interpolated vector
101 */
102
103 template <typename T>
104 Vector<T> lerp_vec(const Vector<T>& a, const Vector<T>& b, T t) {
105 return Vector<T>::lerp(a, b, t);
106 }
107
108/*
109 * Linear combination of multiple vectors
110 * @param u Vector of vectors
111 * @param coef Coefficients for the linear combination
112 * @return Resulting vector
113 */
114
115 template <typename T>
116 Vector<T> linear_combination_vec(const std::vector<Vector<T>>& u, const std::vector<T>& coef) {
118 }
119
120/*
121 * Cross product of two vectors
122 * @param a First vector
123 * @param b Second vector
124 * @return Cross product result
125 */
126
127 template <typename T>
129 return Vector<T>::cross_product(a, b);
130 }
131
132 // === MATRIX OPS ===
133
134/*
135 * @brief Add two matrices
136 * @param A First matrix
137 * @param B Second matrix
138 * @return Resulting matrix
139 */
140
141 template <typename T>
143 Matrix<T> result = A;
144 result.add(B);
145 return result;
146 }
147
148/*
149 * @brief Subtract two matrices
150 * @param A First matrix
151 * @param B Second matrix
152 * @return Resulting matrix
153 */
154
155 template <typename T>
157 Matrix<T> result = A;
158 result.sub(B);
159 return result;
160 }
161
162/*
163 * @brief Scale a matrix by a scalar
164 * @param A Matrix to scale
165 * @param scalar Scalar value
166 * @return Resulting matrix
167 */
168
169 template <typename T>
171 Matrix<T> result = A;
172 result.scl(scalar);
173 return result;
174 }
175
176 template <typename T>
177 Matrix<T> lerp_mat(const Matrix<T>& A, const Matrix<T>& B, T t) {
178 Matrix<T> result(A.rows, A.cols);
179 result.lerp(A, B, t);
180 return result;
181 }
182
183
184/*
185 * @brief Matrix multiplication
186 * @param A First matrix
187 * @param B Second matrix
188 * @return Resulting matrix
189 * @note Assumes A.cols() == B.rows()
190 * @note Uses SIMD for optimization
191 * @note OpenMP parallelization for large matrices, for small matrices, it is not worth the overhead
192 * @note (For matrix 4x4, 8x8 and 16x16, there's a SIMD fallback to ensure L1 and L2 cache storage)
193 */
194
195
196
197 template <typename T>
199 // if (A.rows == 2 && A.cols == 2 && B.rows == 2 && B.cols == 2) {
200 // const MatrixKernel<T> kernelA(A);
201 // const MatrixKernel<T> kernelB(B);
202 // return kernelA.mul_mat2x2(kernelB);
203 // }
204
205 if (A.rows == 3 && A.cols == 3 && B.rows == 3 && B.cols == 3) {
207 const MatrixKernel<T> kernelB(B);
208 return kernelA.mul_mat3x3(kernelB);
209 }
210
211 if (A.rows == 4 && A.cols == 4 && B.rows == 4 && B.cols == 4) {
213 const MatrixKernel<T> kernelB(B);
214 return kernelA.mul_mat4x4(kernelB);
215 }
216
217 if (A.rows == 8 && A.cols == 8 && B.rows == 8 && B.cols == 8) {
219 const MatrixKernel<T> kernelB(B);
220 return kernelA.mul_mat8x8(kernelB);
221 }
222
223 if (A.rows == 16 && A.cols == 16 && B.rows == 16 && B.cols == 16) {
225 const MatrixKernel<T> kernelB(B);
226 return kernelA.mul_mat16x16(kernelB);
227 }
228
229 if (A.rows == 32 && A.cols == 32 && B.rows == 32 && B.cols == 32) {
231 const MatrixKernel<T> kernelB(B);
232 return kernelA.mul_mat32x32(kernelB);
233 }
234 //
235 //
236 // if (A.rows == 64 && A.cols == 64 && B.rows == 64 && B.cols == 64) {
237 // const MatrixKernel<T> kernelA(A);
238 // const MatrixKernel<T> kernelB(B);
239 // return kernelA.mul_mat64x64(kernelB);
240 // }
241 //
242 //
243
244
245 Matrix<T> Acol = A;
246 return Acol._mul_mat(B);
247 }
248
249
250
251/*
252 * @brief Matrix transpose
253 * @param A Matrix to transpose
254 */
255
256 template <typename T>
258 return A.transpose();
259 }
260
261/*
262 * @brief Trace of a matrix
263 * @param A Matrix to trace
264 */
265
266 template <typename T>
268 return A.trace();
269 }
270
271/*
272 * @brief Multiply a matrix by a vector
273 * @param A Matrix
274 * @param x Vector
275 * @return Resulting vector
276 * @note Assumes A.cols() == x.size()
277 */
278
279 template <typename T>
281 return A.mul_vec(x);
282 }
283
284/*
285 * @brief Inverse of a matrix
286 * @param A Matrix to invert
287 * @return Inverted matrix
288 * @note Assumes A is square
289 * @note Uses LU decomposition for inversion
290 */
291
292 template <typename T>
294 return A.inverse();
295 }
296
297 template <typename T>
299 return A.det();
300 }
301
302 template <typename T>
303 size_t rank_mat(const Matrix<T>& A) {
304 return A.rank();
305 }
306
307 // === TENSOR OPS ===
308 template<typename K, std::size_t Rank>
309 template <size_t I, size_t J>
310 Tensor<K, Rank - 2> Tensor<K, Rank>::contract_tensor() const {
311 static_assert(I < Rank && J < Rank && I != J, "Invalid contraction indices");
312 return contract_simd(*this, I, J);
313 }
314
315 template <typename K, std::size_t Rank>
319
320
321 template<typename K, size_t R1, size_t R2>
325
326 template <size_t I, size_t J, typename K, std::size_t Rank>
328 static_assert(I < Rank && J < Rank && I != J, "Invalid contraction indices");
329 return T.template contract_tensor<I, J>();
330 }
331 // === LINEAR SOLVERS ===
332 template <typename T>
334 return solver::Gauss<T>::solve(A, b);
335 }
336
337 template <typename T>
338 Vector<T> jacobi_solve(const Matrix<T>& A, const Vector<T>& b, T tol = 1e-6, int max_iter = 1000) {
340 }
341
342 template <typename T>
343 inline void row_echelon(Matrix<T>& A, Vector<T>* b = nullptr, T eps = T(1e-12)) {
345 }
346
347 // === DERIVATIVES ===
348 template<typename K>
349 inline void centered_derivative(const Derivate<K>& input, Derivate<K>& output, size_t axis, K dx) {
350 input.centered_derivative(input, output, axis, dx);
351 }
352
353
354 template<typename K>
355 inline void centered_derivative_order4(const Derivate<K>& input, Derivate<K>& output, size_t axis, K dx) {
356 centered_derivative_order4(input, output, axis, dx);
357 }
358
359 template<typename K, size_t Rank>
360 inline void centered_derivative(const DerivateND<K, Rank>& input, DerivateND<K, Rank>& output, size_t axis, K dx) {
361 input.centered_derivative(input, output, axis, dx);
362 }
363
364
365 template<typename K, size_t Rank>
366 inline void centered_derivative_order4(const DerivateND<K, Rank>& input, DerivateND<K, Rank>& output, size_t axis, K dx) {
367 input.centered_derivative_order4_rank(input, output, axis, dx);
368 }
369
370
371 //=== SPECTRAL METHODS ===
372
373 template<typename T>
374 inline void forwardFFT(tensorium::Vector<std::complex<T>>& data)
375 {
377 }
378
379 template<typename T>
380 inline void backwardFFT(tensorium::Vector<std::complex<T>>& data)
381 {
383 }
384
385 template<typename T>
386 inline void backwardFFP(tensorium::Vector<std::complex<T>>& data)
387 {
389 }
390
391}
Numerical differentiation operators using SIMD.
static void backward(CVectorT &a)
Perform inverse FFT (in-place)
Definition Spectral.hpp:93
static void forward(CVectorT &a)
Perform forward FFT (in-place)
Definition Spectral.hpp:38
Multi-dimensional tensor class with fixed rank and SIMD support.
Definition Tensor.hpp:25
__attribute__((always_inline, hot, flatten)) inline size_t flatten_index(const std transpose_simd() const
Definition Tensor.hpp:287
Tensor()
Default constructor.
Definition Tensor.hpp:35
Aligned, SIMD-optimized mathematical vector class for scientific computing.
Definition Vector.hpp:26
Direct Gaussian elimination solver with SIMD acceleration.
Definition LinearSolver.hpp:29
static void raw_row_echelon(Matrix< K > &A, Vector< K > *b=nullptr, K eps=1e-12)
Definition LinearSolver.hpp:141
static Vector< K > solve(const Matrix< K > &A, const Vector< K > &b, K tol=1e-10, int max_iter=2000)
Solve the system using the Jacobi method.
Definition LinearSolver.hpp:212
Definition Derivate.hpp:24
Vector< T > cross_vec(const Vector< T > &a, const Vector< T > &b)
Definition Functional.hpp:128
Vector< T > lerp_vec(const Vector< T > &a, const Vector< T > &b, T t)
Definition Functional.hpp:104
Vector< T > mul_vec(const Matrix< T > &A, const Vector< T > &x)
Definition Functional.hpp:280
Matrix< T > lerp_mat(const Matrix< T > &A, const Matrix< T > &B, T t)
Definition Functional.hpp:177
Matrix< T > scl_mat(const Matrix< T > &A, T scalar)
Definition Functional.hpp:170
void forwardFFT(tensorium::Vector< std::complex< T > > &data)
Definition Functional.hpp:374
Matrix< T > trace_mat(const Matrix< T > &A)
Definition Functional.hpp:267
Tensor< K, Rank - 2 > contract_tensor(const Tensor< K, Rank > &T)
Definition Functional.hpp:327
Vector< T > scl_vec(const Vector< T > &a, T scalar)
Definition Functional.hpp:49
void row_echelon(Matrix< T > &A, Vector< T > *b=nullptr, T eps=T(1e-12))
Definition Functional.hpp:343
Vector< T > linear_combination_vec(const std::vector< Vector< T > > &u, const std::vector< T > &coef)
Definition Functional.hpp:116
Vector< T > add_vec(const Vector< T > &a, const Vector< T > &b)
Definition Functional.hpp:21
T norm2_vec(const Vector< T > &a)
Definition Functional.hpp:69
T dot_vec(const Vector< T > &a, const Vector< T > &b)
Definition Functional.hpp:86
Matrix< T > inverse_mat(const Matrix< T > &A)
Definition Functional.hpp:293
Matrix< T > sub_mat(const Matrix< T > &A, const Matrix< T > &B)
Definition Functional.hpp:156
Vector< T > jacobi_solve(const Matrix< T > &A, const Vector< T > &b, T tol=1e-6, int max_iter=1000)
Definition Functional.hpp:338
Matrix< T > add_mat(const Matrix< T > &A, const Matrix< T > &B)
Definition Functional.hpp:142
T norm1_vec(const Vector< T > &a)
Definition Functional.hpp:61
Tensor< K, Rank > transpose_tensor(const Tensor< K, Rank > &T)
Definition Functional.hpp:316
void backwardFFT(tensorium::Vector< std::complex< T > > &data)
Definition Functional.hpp:380
void centered_derivative(const Derivate< K > &input, Derivate< K > &output, size_t axis, K dx)
Definition Functional.hpp:349
Matrix< T > mul_mat(const Matrix< T > &A, const Matrix< T > &B)
Definition Functional.hpp:198
void centered_derivative_order4(const Derivate< K > &input, Derivate< K > &output, size_t axis, K dx)
Definition Functional.hpp:355
size_t rank_mat(const Matrix< T > &A)
Definition Functional.hpp:303
Vector< T > gauss_solve(const Matrix< T > &A, const Vector< T > &b)
Definition Functional.hpp:333
Tensor< K, R1+R2 > mul_tensor(const Tensor< K, R1 > &A, const Tensor< K, R2 > &B)
Definition Functional.hpp:322
T normInf_vec(const Vector< T > &a)
Definition Functional.hpp:77
T det_mat(const Matrix< T > &A)
Definition Functional.hpp:298
Matrix< T > transpose_mat(const Matrix< T > &A)
Definition Functional.hpp:257
Vector< T > sub_vec(const Vector< T > &a, const Vector< T > &b)
Definition Functional.hpp:35
void backwardFFP(tensorium::Vector< std::complex< T > > &data)
Definition Functional.hpp:386
T cosine_vec(const Vector< T > &a, const Vector< T > &b)
Definition Functional.hpp:94