From 46de13b36367e37ce2f8bec847e7357db33d7f7e Mon Sep 17 00:00:00 2001 From: conrad Date: Tue, 8 Jul 2025 23:32:03 +1000 Subject: [PATCH] use std::uniform_real_distribution with promoted type --- include/armadillo_bits/op_chi2rnd_meat.hpp | 1 + include/armadillo_bits/op_norm2est_bones.hpp | 12 ++++++---- include/armadillo_bits/op_norm2est_meat.hpp | 24 +++++++------------- include/armadillo_bits/op_norm_meat.hpp | 6 ++--- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/armadillo_bits/op_chi2rnd_meat.hpp b/include/armadillo_bits/op_chi2rnd_meat.hpp index a6fcec3d..cd23ca8b 100644 --- a/include/armadillo_bits/op_chi2rnd_meat.hpp +++ b/include/armadillo_bits/op_chi2rnd_meat.hpp @@ -56,6 +56,7 @@ op_chi2rnd::apply_noalias(Mat& out, const Proxy& P) arma_debug_sigprint(); typedef typename T1::elem_type eT; + // we can only make a generator for float/double/long double types typedef typename promote_type::result gT; diff --git a/include/armadillo_bits/op_norm2est_bones.hpp b/include/armadillo_bits/op_norm2est_bones.hpp index 7ac004bf..2c633772 100644 --- a/include/armadillo_bits/op_norm2est_bones.hpp +++ b/include/armadillo_bits/op_norm2est_bones.hpp @@ -24,8 +24,10 @@ template struct norm2est_randu_filler { - std::mt19937_64 local_engine; - std::uniform_real_distribution local_u_distr; + typedef typename promote_type::result eTp; + + std::mt19937_64 local_engine; + std::uniform_real_distribution local_u_distr; inline norm2est_randu_filler(); @@ -36,8 +38,10 @@ struct norm2est_randu_filler template struct norm2est_randu_filler< std::complex > { - std::mt19937_64 local_engine; - std::uniform_real_distribution local_u_distr; + typedef typename promote_type::result Tp; + + std::mt19937_64 local_engine; + std::uniform_real_distribution local_u_distr; inline norm2est_randu_filler(); diff --git a/include/armadillo_bits/op_norm2est_meat.hpp b/include/armadillo_bits/op_norm2est_meat.hpp index 3199f6ea..67b75142 100644 --- a/include/armadillo_bits/op_norm2est_meat.hpp +++ b/include/armadillo_bits/op_norm2est_meat.hpp @@ -27,11 +27,13 @@ norm2est_randu_filler::norm2est_randu_filler() { arma_debug_sigprint(); + typedef typename promote_type::result eTp; + typedef typename std::mt19937_64::result_type local_seed_type; local_engine.seed(local_seed_type(123)); - typedef typename std::uniform_real_distribution::param_type local_param_type; + typedef typename std::uniform_real_distribution::param_type local_param_type; local_u_distr.param(local_param_type(-1.0, +1.0)); } @@ -57,11 +59,13 @@ norm2est_randu_filler< std::complex >::norm2est_randu_filler() { arma_debug_sigprint(); + typedef typename promote_type::result Tp; + typedef typename std::mt19937_64::result_type local_seed_type; local_engine.seed(local_seed_type(123)); - typedef typename std::uniform_real_distribution::param_type local_param_type; + typedef typename std::uniform_real_distribution::param_type local_param_type; local_u_distr.param(local_param_type(-1.0, +1.0)); } @@ -120,24 +124,12 @@ op_norm2est::norm2est if((A.n_rows == 1) || (A.n_cols == 1)) { return op_norm::vec_norm_2( Proxy< Mat >(A) ); } - // low-precision types cannot be used for norm2est_randu_filler - // (std::uniform_real_distribution is undefined for types not float/double/long double) - norm2est_randu_filler< typename promote_type::result > randu_filler; + norm2est_randu_filler randu_filler; Col x(A.n_rows, fill::none); Col y(A.n_cols, fill::none); - if(is_fp16::yes) - { - // randu_filler can only fill floats, so do that and then convert - Col tmp(y.n_elem); - randu_filler.fill(tmp.memptr(), tmp.n_elem); - arrayops::convert(y.memptr(), tmp.memptr(), tmp.n_elem); - } - else - { - randu_filler.fill(y.memptr(), y.n_elem); - } + randu_filler.fill(y.memptr(), y.n_elem); T est_old = 0; T est_cur = 0; diff --git a/include/armadillo_bits/op_norm_meat.hpp b/include/armadillo_bits/op_norm_meat.hpp index d64b5942..f26cb462 100644 --- a/include/armadillo_bits/op_norm_meat.hpp +++ b/include/armadillo_bits/op_norm_meat.hpp @@ -956,9 +956,9 @@ typename get_pod_type::result op_norm::mat_norm_2(const Mat& X, typename arma_fp16_only::result* junk) { arma_debug_sigprint(); - - arma_stop_logic_error("norm(): matrix 2-norm currently not supported for fp16 type"); - + + arma_stop_logic_error("norm(): matrix 2-norm currently not supported for fp16; try norm2est() instead"); + return typename get_pod_type::result(0); }