15 #include "etl/temporary.hpp" 31 return is_2d<E> && etl::dim<0>(expr) == etl::dim<1>(expr);
41 return !is_complex<E>;
61 return is_2d<E> && etl::dim<0>(expr) != etl::dim<1>(expr);
71 return is_3d<E> && etl::dim<1>(expr) == etl::dim<2>(expr);
81 return is_3d<E> && etl::dim<1>(expr) != etl::dim<2>(expr);
95 auto first = *expr.begin();
111 template <
typename E>
122 for (
size_t i = 0; i < etl::dim<0>(expr); ++i) {
124 for (
size_t j = 0; j < etl::dim<0>(expr); ++j) {
137 for (
size_t j = 0; j < etl::dim<0>(expr); ++j) {
139 for (
size_t i = 0; i < etl::dim<0>(expr); ++i) {
156 template <etl_expr L, etl_expr R>
177 for (
size_t i = 0; i <
etl::size(lhs); ++i) {
178 if (lhs[i] != rhs[i]) {
191 template <etl_expr L, etl_expr R>
193 return !(lhs == rhs);
205 template <std::
floating_po
int T, std::
floating_po
int TE = T>
209 const auto abs_a = fabs(a);
210 const auto abs_b = fabs(b);
211 const auto abs_diff = fabs(a - b);
220 }
else if (a == 0 || b == 0 || abs_diff < min_normal) {
223 return abs_diff < epsilon;
225 return (abs_diff /
std::min(abs_a + abs_b, max)) < epsilon;
236 template <etl_expr L, etl_expr E>
239 if constexpr (etl::dimensions<L>() != etl::dimensions<E>()) {
253 for (
size_t i = 0; i <
etl::size(lhs); ++i) {
271 template <etl_expr E>
277 for (
size_t i = 0; i < etl::dim<0>(expr); ++i) {
292 template <etl_expr E>
307 template <etl_expr AT, etl_expr LT, etl_expr UT, etl_expr PT>
308 bool lu(
const AT& A, LT& L, UT& U, PT& P) {
331 template <etl_expr AT, etl_expr QT, etl_expr RT>
332 bool qr(AT& A, QT& Q, RT& R) {
358 template <etl_expr T,
typename G>
366 if constexpr (impl::egblas::has_shuffle_seed) {
367 std::uniform_int_distribution<size_t> seed_dist;
369 vector.ensure_gpu_up_to_date();
371 impl::egblas::shuffle_seed(n, vector.gpu_memory(),
sizeof(
etl::value_t<T>), seed_dist(g));
373 vector.invalidate_cpu();
375 using distribution_t =
typename std::uniform_int_distribution<size_t>;
376 using param_t =
typename distribution_t::param_type;
380 for (
auto i = n - 1; i > 0; --i) {
381 auto new_i = dist(g, param_t(0, i));
384 swap(vector[i], vector[new_i]);
398 template <etl_expr T>
400 static std::random_device rd;
418 template <etl_expr T,
typename G>
420 const auto n = etl::dim<0>(matrix);
426 if constexpr (impl::egblas::has_shuffle_seed) {
427 std::uniform_int_distribution<size_t> seed_dist;
429 matrix.ensure_gpu_up_to_date();
431 impl::egblas::shuffle_seed(n, matrix.gpu_memory(),
sizeof(
etl::value_t<T>) * (
etl::size(matrix) / n), seed_dist(g));
433 matrix.invalidate_cpu();
435 using distribution_t =
typename std::uniform_int_distribution<size_t>;
436 using param_t =
typename distribution_t::param_type;
442 for (
auto i = n - 1; i > 0; --i) {
443 auto new_i = dist(g, param_t(0, i));
446 matrix(i) = matrix(new_i);
447 matrix(new_i) = temp;
463 template <etl_expr T>
465 static std::random_device rd;
479 template <etl_expr T>
481 if constexpr (is_1d<T>) {
497 template <etl_expr T,
typename G>
499 if constexpr (is_1d<T>) {
515 template <etl_expr T1, same_dimensions<T1> T2,
typename G>
517 cpp_assert(
etl::size(v1) ==
etl::size(v2),
"Impossible to shuffle vector of different dimensions");
525 if constexpr (impl::egblas::has_par_shuffle_seed) {
526 std::uniform_int_distribution<size_t> seed_dist;
528 v1.ensure_gpu_up_to_date();
529 v2.ensure_gpu_up_to_date();
536 using distribution_t =
typename std::uniform_int_distribution<size_t>;
537 using param_t =
typename distribution_t::param_type;
541 for (
auto i = n - 1; i > 0; --i) {
542 auto new_i = dist(g, param_t(0, i));
545 swap(v1[i], v1[new_i]);
546 swap(v2[i], v2[new_i]);
560 template <etl_expr T1, etl_expr T2>
562 static std::random_device rd;
580 template <etl_expr T1, etl_expr T2,
typename G>
582 cpp_assert(etl::dim<0>(m1) == etl::dim<0>(m2),
"Impossible to shuffle together matrices of different first dimension");
584 const auto n = etl::dim<0>(m1);
590 if constexpr (impl::egblas::has_par_shuffle_seed) {
591 std::uniform_int_distribution<size_t> seed_dist;
593 m1.ensure_gpu_up_to_date();
594 m2.ensure_gpu_up_to_date();
602 using distribution_t =
typename std::uniform_int_distribution<size_t>;
603 using param_t =
typename distribution_t::param_type;
610 for (
auto i = n - 1; i > 0; --i) {
611 auto new_i = dist(g, param_t(0, i));
636 template <etl_expr T1, etl_expr T2>
638 static std::random_device rd;
653 template <etl_expr T1, etl_expr T2>
655 static std::random_device rd;
661 template <etl_expr T>
662 void shuffle_swap(T& v1,
size_t i,
size_t new_i) {
663 if constexpr (is_1d<T>) {
671 for (
size_t index = 0 ; index <
etl::subsize(v1); ++index) {
673 s1[index] = s2[index];
688 template <etl_expr T1, etl_expr T2,
typename G>
690 cpp_assert(etl::dim<0>(v1) == etl::dim<0>(v2),
"Impossible to shuffle together matrices of different first dimension");
692 const auto n = etl::dim<0>(v1);
698 if constexpr (impl::egblas::has_par_shuffle_seed) {
699 std::uniform_int_distribution<size_t> seed_dist;
701 v1.ensure_gpu_up_to_date();
702 v2.ensure_gpu_up_to_date();
710 using distribution_t =
typename std::uniform_int_distribution<size_t>;
711 using param_t =
typename distribution_t::param_type;
718 for (
auto i = n - 1; i > 0; --i) {
719 auto new_i = dist(g, param_t(0, i));
721 shuffle_swap(v1, i, new_i);
722 shuffle_swap(v2, i, new_i);
736 template <etl_expr M, etl_expr N>
737 M&
merge(M& merged,
const N& sub,
size_t index) {
739 const size_t n = index *
s;
755 template <etl_expr M, etl_expr N>
758 const size_t n = index *
s;
760 for (
size_t b = 0; b < etl::dim<0>(merged); ++b) {
776 template <etl_expr M, etl_expr N>
777 M&
dispatch(M& dispatched,
const N& merged,
size_t index) {
779 const size_t n = index *
s;
795 template <etl_expr M, etl_expr N>
798 const size_t n = index *
s;
800 for (
size_t b = 0; b < etl::dim<0>(merged); ++b) {
812 template <etl_expr M,
typename T>
816 for (
auto& value : matrix) {
817 value = value > b ? VT(1) : VT(0);
825 template <etl_expr M>
829 auto m =
mean(matrix);
846 template <etl_expr M>
856 auto mm = matrix.memory_start();
857 const auto n = etl::dim<0>(matrix);
858 const auto sub_n =
etl::size(matrix) / n;
860 for (
size_t i = 0; i < n; ++i) {
863 for (
size_t j = 0; j < sub_n; ++j) {
864 m += mm[i * sub_n + j];
871 for (
size_t j = 0; j < sub_n; ++j) {
872 s += (mm[i * sub_n + j] - m) * (mm[i * sub_n + j] - m);
877 for (
size_t j = 0; j < sub_n; ++j) {
878 mm[i * sub_n + j] = (mm[i * sub_n + j] - m) / s;
882 for (
size_t i = 0; i < etl::dim<0>(matrix); ++i) {
883 auto m =
mean(matrix(i));
885 matrix(i) = matrix(i) - m;
887 auto s =
stddev(matrix(i), 0.0);
890 matrix(i) = matrix(i) /
s;
void parallel_shuffle(T1 &v1, T2 &v2)
Shuffle all the elements of two vectors or matrices, using the same permutation.
Definition: globals.hpp:654
value_t< E > mean(E &&values)
Returns the mean of all the values contained in the given expression.
Definition: expression_builder.hpp:650
M & batch_dispatch(M &dispatched, const N &merged, size_t index)
Dispatch a part of merged to dispatched from the given position, for each batch.
Definition: globals.hpp:796
Selector for the decompositions implementation.
bool is_sub_square(E &&expr)
Indicates if the given expression contains sub matrices that are square.
Definition: globals.hpp:70
EGBLAS wrappers for the shuffle operations.
auto s(T &&value)
Force the evaluation of the given expression.
Definition: stop.hpp:18
bool approx_equals(L &&lhs, E &&rhs, value_t< L > eps)
Test if two ETL expression are approximately equals.
Definition: globals.hpp:237
bool qr(AT &A, QT &Q, RT &R)
Decomposition the matrix so that A = Q * R.
Definition: globals.hpp:332
void shuffle(T &vector)
Shuffle all the elements of an ETL vector.
Definition: globals.hpp:480
auto max(L &&lhs, R &&rhs)
Create an expression with the max value of lhs or rhs.
Definition: expression_builder.hpp:65
bool is_uniform(E &&expr)
Indicates if the given expression is uniform (all elements of the same value)
Definition: globals.hpp:90
M & batch_merge(M &merged, const N &sub, size_t index)
Merge sub inside merged at the given position, for each batch.
Definition: globals.hpp:756
bool is_permutation_matrix(E &&expr)
Indicates if the given expression represents a permutation matrix.
Definition: globals.hpp:112
void parallel_shuffle_flat(T1 &v1, T2 &v2, G &&g)
Shuffle all the elements of two vectors, using the same permutation.
Definition: globals.hpp:516
auto sqrt(E &&value) -> detail::unary_helper< E, sqrt_unary_op >
Apply square root on each value of the given expression.
Definition: function_expression_builder.hpp:24
void normalize_flat(M &matrix)
Normalize the given ETL contrainer to zero-mean and unit-variance.
Definition: globals.hpp:826
bool is_real_matrix([[maybe_unused]] E &&expr)
Indicates if the given expression is a real matrix or not.
Definition: globals.hpp:40
static void apply(AT &A, QT &Q, RT &R)
Apply the functor to A,Q,R.
Definition: decomposition.hpp:48
bool operator!=(const complex< T > &lhs, const complex< T > &rhs)
Test two complex numbers for inequality.
Definition: complex.hpp:179
value_t< E > trace(E &&expr)
Returns the trace of the given square matrix.
Definition: globals.hpp:272
M & merge(M &merged, const N &sub, size_t index)
Merge sub inside merged at the given position.
Definition: globals.hpp:737
void shuffle_flat(T &vector, G &&g)
Shuffle all the elements of an ETL vector or matrix (considered as array).
Definition: globals.hpp:359
void force(Expr &&expr)
Force the internal evaluation of an expression.
Definition: evaluator.hpp:1292
constexpr size_t dimensions() noexcept
Return the number of dimensions of the given ETL type.
Definition: helpers.hpp:28
void binarize(M &matrix, T b)
Binarize the given ETL contrainer.
Definition: globals.hpp:813
Root namespace for the ETL library.
Definition: adapter.hpp:15
auto dim(E &&value, size_t i) -> detail::identity_helper< E, dim_view< detail::build_identity_type< E >, D >>
Return a view representing the ith Dth dimension.
Definition: view_expression_builder.hpp:25
static value_t< AT > apply(const AT &A)
Apply the functor to A.
Definition: det.hpp:30
value_t< E > determinant(E &&expr)
Returns the determinant of the given square matrix.
Definition: globals.hpp:293
value_t< E > stddev(E &&values)
Returns the standard deviation of all the values contained in the given expression.
Definition: expression_builder.hpp:670
bool is_square(E &&expr)
Indicates if the given expression is a square matrix or not.
Definition: globals.hpp:30
void parallel_shuffle_first(T1 &m1, T2 &m2, G &&g)
Shuffle all the elements of two matrices, using the same permutation.
Definition: globals.hpp:581
bool is_sub_rectangular(E &&expr)
Indicates if the given expression contains sub matrices that are rectangular.
Definition: globals.hpp:80
M & dispatch(M &dispatched, const N &merged, size_t index)
Dispatch a part of merged to dispatched from the given position.
Definition: globals.hpp:777
bool lu(const AT &A, LT &L, UT &U, PT &P)
Decomposition the matrix so that P * A = L * U.
Definition: globals.hpp:308
static void apply(const AT &A, LT &L, UT &U, PT &P)
Apply the functor to A, L, U, P.
Definition: decomposition.hpp:32
bool is_complex_matrix([[maybe_unused]] E &&expr)
Indicates if the given expression is a complex matrix or not.
Definition: globals.hpp:50
void swap(custom_dyn_matrix_impl< T, SO, D > &lhs, custom_dyn_matrix_impl< T, SO, D > &rhs)
Swap two dyn matrix.
Definition: custom_dyn.hpp:403
auto min(L &&lhs, R &&rhs)
Create an expression with the min value of lhs or rhs.
Definition: expression_builder.hpp:77
value_t< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
Selector for the determinant implementation.
bool is_rectangular(E &&expr)
Indicates if the given expression is a rectangular matrix or not.
Definition: globals.hpp:60
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
void normalize_sub(M &matrix)
Normalize each sub container of the given ETL contrainer to zero-mean and unit-variance.
Definition: globals.hpp:847
auto memory_slice(E &&value, size_t first, size_t last) -> detail::identity_helper< E, memory_slice_view< detail::build_identity_type< E >, Aligned >>
Returns view representing a memory slice view of the given expression.
Definition: memory_slice_view.hpp:351
bool approx_equals_float(T a, T b, TE epsilon)
Test if two floating point numbers are approximately equals.
Definition: globals.hpp:206
decltype(auto) force_temporary(E &&expr)
Force a temporary out of the expression.
Definition: temporary.hpp:91
bool operator==(const complex< T > &lhs, const complex< T > &rhs)
Test two complex numbers for equality.
Definition: complex.hpp:168
std::mt19937_64 random_engine
The random engine used by the library.
Definition: random.hpp:22
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
void shuffle_first(T &matrix, G &&g)
Shuffle all the elements of a matrix.
Definition: globals.hpp:419
size_t subsize(const E &expr)
Returns the sub-size of the given ETL expression, i.e. the size not considering the first dimension...
Definition: helpers.hpp:118
void assert_square([[maybe_unused]] E &&expr)
Make sure that the expression is square.
Definition: checks.hpp:73