sort out the eps() mess

This commit is contained in:
conrad
2025-08-20 00:44:22 +10:00
parent e22c9e7cff
commit fcd771fda7
8 changed files with 128 additions and 160 deletions
-1
View File
@@ -549,7 +549,6 @@ namespace arma
#include "armadillo_bits/fn_circshift.hpp"
#include "armadillo_bits/fn_shuffle.hpp"
#include "armadillo_bits/fn_prod.hpp"
#include "armadillo_bits/fn_eps.hpp"
#include "armadillo_bits/fn_pinv.hpp"
#include "armadillo_bits/fn_rank.hpp"
#include "armadillo_bits/fn_kron.hpp"
-49
View File
@@ -136,55 +136,6 @@ struct eop_aux
template<typename T1, typename T2> arma_inline static typename arma_integral_only<T1>::result pow (const T1 base, const T2 exponent) { return T1( std::pow( double(base), double(exponent) ) ); }
template<typename T1, typename T2> arma_inline static typename arma_real_or_cx_only<T1>::result pow (const T1 base, const T2 exponent) { return T1( std::pow( base, exponent ) ); }
template<typename eT>
arma_inline
static
typename arma_integral_only<eT>::result
direct_eps(const eT)
{
return eT(0);
}
template<typename eT>
inline
static
typename arma_real_only<eT>::result
direct_eps(const eT x)
{
//arma_debug_sigprint();
// according to IEEE Standard for Floating-Point Arithmetic (IEEE 754)
// the mantissa length for double is 53 bits = std::numeric_limits<double>::digits
// the mantissa length for float is 24 bits = std::numeric_limits<float >::digits
//return std::pow( std::numeric_limits<eT>::radix, (std::floor(std::log10(std::abs(x))/std::log10(std::numeric_limits<eT>::radix))-(std::numeric_limits<eT>::digits-1)) );
const eT radix_eT = eT(std::numeric_limits<eT>::radix);
const eT digits_m1_eT = eT(std::numeric_limits<eT>::digits - 1);
// return std::pow( radix_eT, eT(std::floor(std::log10(std::abs(x))/std::log10(radix_eT)) - digits_m1_eT) );
return eop_aux::pow( radix_eT, eT(std::floor(std::log10(std::abs(x))/std::log10(radix_eT)) - digits_m1_eT) );
}
template<typename T>
inline
static
typename arma_real_only<T>::result
direct_eps(const std::complex<T>& x)
{
//arma_debug_sigprint();
//return std::pow( std::numeric_limits<T>::radix, (std::floor(std::log10(std::abs(x))/std::log10(std::numeric_limits<T>::radix))-(std::numeric_limits<T>::digits-1)) );
const T radix_T = T(std::numeric_limits<T>::radix);
const T digits_m1_T = T(std::numeric_limits<T>::digits - 1);
return std::pow( radix_T, T(std::floor(std::log10(std::abs(x))/std::log10(radix_T)) - digits_m1_T) );
}
};
@@ -87,7 +87,6 @@ struct eop_acosh : public eop_core<eop_acosh> , public e
struct eop_asinh : public eop_core<eop_asinh> , public eop_use_mp_true {};
struct eop_atanh : public eop_core<eop_atanh> , public eop_use_mp_true {};
struct eop_sinc : public eop_core<eop_sinc> , public eop_use_mp_true {};
struct eop_eps : public eop_core<eop_eps> , public eop_use_mp_true {};
struct eop_abs : public eop_core<eop_abs> , public eop_use_mp_false {};
struct eop_arg : public eop_core<eop_arg> , public eop_use_mp_false {};
struct eop_conj : public eop_core<eop_conj> , public eop_use_mp_false {};
-3
View File
@@ -1104,9 +1104,6 @@ eop_core<eop_atanh >::process(const eT val, const eT ) { return eop_
template<> template<typename eT> arma_inline eT
eop_core<eop_sinc >::process(const eT val, const eT ) { return arma_sinc(val); }
template<> template<typename eT> arma_inline eT
eop_core<eop_eps >::process(const eT val, const eT ) { return eop_aux::direct_eps(val); }
template<> template<typename eT> arma_inline eT
eop_core<eop_abs >::process(const eT val, const eT ) { return eop_aux::arma_abs(val); }
+29
View File
@@ -1190,6 +1190,35 @@ tgamma(const BaseCube<typename T1::elem_type,T1>& A)
//
// eps
template<typename T1>
arma_warn_unused
inline
typename enable_if2< (is_arma_type<T1>::value && is_real_or_cx<typename T1::elem_type>::value), const mtOp<typename T1::pod_type, T1, op_eps> >::result
eps(const T1& X)
{
arma_debug_sigprint();
return mtOp<typename T1::pod_type, T1, op_eps>(X);
}
template<typename eT>
arma_warn_unused
inline
typename arma_real_or_cx_only<eT>::result
eps(const eT& x)
{
arma_debug_sigprint();
return op_eps::direct_eps(x);
}
// the functions below are currently unused; reserved for potential future use
template<typename T1> void exp_approx(const T1&) { arma_stop_logic_error("unimplemented"); }
-106
View File
@@ -1,106 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup fn_eps
//! @{
template<typename T1>
arma_warn_unused
inline
const eOp<T1, eop_eps>
eps(const Base<typename T1::elem_type, T1>& X, const typename arma_not_cx<typename T1::elem_type>::result* junk = nullptr)
{
arma_debug_sigprint();
arma_ignore(junk);
return eOp<T1, eop_eps>(X.get_ref());
}
template<typename T1>
arma_warn_unused
inline
Mat< typename T1::pod_type >
eps(const Base< std::complex<typename T1::pod_type>, T1>& X, const typename arma_cx_only<typename T1::elem_type>::result* junk = nullptr)
{
arma_debug_sigprint();
arma_ignore(junk);
typedef typename T1::pod_type T;
typedef typename T1::elem_type eT;
const unwrap<T1> tmp(X.get_ref());
const Mat<eT>& A = tmp.M;
Mat<T> out(A.n_rows, A.n_cols, arma_nozeros_indicator());
T* out_mem = out.memptr();
const eT* A_mem = A.memptr();
const uword n_elem = A.n_elem;
for(uword i=0; i<n_elem; ++i)
{
out_mem[i] = eop_aux::direct_eps( A_mem[i] );
}
return out;
}
template<typename eT>
arma_warn_unused
arma_inline
typename arma_integral_only<eT>::result
eps(const eT& x)
{
arma_ignore(x);
return eT(0);
}
template<typename eT>
arma_warn_unused
arma_inline
typename arma_real_only<eT>::result
eps(const eT& x)
{
return eop_aux::direct_eps(x);
}
template<typename T>
arma_warn_unused
arma_inline
typename arma_real_only<T>::result
eps(const std::complex<T>& x)
{
return eop_aux::direct_eps(x);
}
//! @}
+14
View File
@@ -79,4 +79,18 @@ struct op_replace
struct op_eps
: public traits_op_passthru
{
template<typename eT> inline static typename get_pod_type<eT>::result direct_eps(const eT& x);
template<typename T1>
inline static void apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_eps>& in);
template<typename T, typename eT>
inline static void apply_noalias(Mat<T>& out, const Mat<eT>& X);
};
//! @}
+85
View File
@@ -109,6 +109,10 @@ op_real::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::po
//
template<typename T1>
inline
void
@@ -207,6 +211,10 @@ op_imag::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::po
//
template<typename T1>
inline
void
@@ -321,6 +329,10 @@ op_abs::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod
//
template<typename T1>
inline
void
@@ -411,6 +423,10 @@ op_arg::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod
//
template<typename eT, typename T1>
inline
void
@@ -445,4 +461,73 @@ op_replace::apply(Cube<eT>& out, const mtOpCube<eT,T1,op_replace>& in)
//
template<typename eT>
inline
typename get_pod_type<eT>::result
op_eps::direct_eps(const eT& x)
{
typedef typename get_pod_type<eT>::result T;
const T xx = std::abs(x);
const T yy = std::nextafter(xx, std::numeric_limits<T>::infinity());
return (yy - xx);
}
template<typename T1>
inline
void
op_eps::apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_eps>& in)
{
arma_debug_sigprint();
typedef typename T1::pod_type T;
const quasi_unwrap<T1> U(in.m);
if(U.is_alias(out))
{
Mat<T> tmp;
op_eps::apply_noalias(tmp, U.M);
out.steal_mem(tmp);
}
else
{
op_eps::apply_noalias(out, U.M);
}
}
template<typename T, typename eT>
inline
void
op_eps::apply_noalias(Mat<T>& out, const Mat<eT>& X)
{
arma_debug_sigprint();
out.set_size(X.n_rows, X.n_cols);
T* out_mem = out.memptr();
const eT* X_mem = X.memptr();
const uword n_elem = X.n_elem;
for(uword i=0; i<n_elem; ++i)
{
out_mem[i] = op_eps::direct_eps( X_mem[i] );
}
}
//! @}