From eda5bc26f44ee9a6f83dcf8c91f17296d7fc509d Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 12 Feb 2024 14:52:43 +0100 Subject: Move into version control --- .../include/armadillo_bits/fn_approx_equal.hpp | 471 +++++++++++++++++++++ 1 file changed, 471 insertions(+) create mode 100644 src/armadillo/include/armadillo_bits/fn_approx_equal.hpp (limited to 'src/armadillo/include/armadillo_bits/fn_approx_equal.hpp') diff --git a/src/armadillo/include/armadillo_bits/fn_approx_equal.hpp b/src/armadillo/include/armadillo_bits/fn_approx_equal.hpp new file mode 100644 index 0000000..e92b94f --- /dev/null +++ b/src/armadillo/include/armadillo_bits/fn_approx_equal.hpp @@ -0,0 +1,471 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (http://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 +// http://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_approx_equal +//! @{ + + + +template +arma_inline +bool +internal_approx_equal_abs_diff(const eT& x, const eT& y, const typename get_pod_type::result tol) + { + typedef typename get_pod_type::result T; + + if(x != y) + { + if(is_real::value) // also true for eT = std::complex or eT = std::complex + { + if( arma_isnan(x) || arma_isnan(y) || (eop_aux::arma_abs(x - y) > tol) ) { return false; } + } + else + { + if( eop_aux::arma_abs( ( cond_rel< is_cx::no >::gt(x, y) ) ? (x-y) : (y-x) ) > tol ) { return false; } + } + } + + return true; + } + + + +template +arma_inline +bool +internal_approx_equal_rel_diff(const eT& a, const eT& b, const typename get_pod_type::result tol) + { + typedef typename get_pod_type::result T; + + if(a != b) + { + if(is_real::value) // also true for eT = std::complex or eT = std::complex + { + if( arma_isnan(a) || arma_isnan(b) ) { return false; } + + const T abs_a = eop_aux::arma_abs(a); + const T abs_b = eop_aux::arma_abs(b); + + const T max_c = (std::max)(abs_a,abs_b); + + const T abs_d = eop_aux::arma_abs(a - b); + + if(max_c >= T(1)) + { + if( abs_d > (tol * max_c) ) { return false; } + } + else + { + if( (abs_d / max_c) > tol ) { return false; } + } + } + else + { + const T abs_a = eop_aux::arma_abs(a); + const T abs_b = eop_aux::arma_abs(b); + + const T max_c = (std::max)(abs_a,abs_b); + + const T abs_d = eop_aux::arma_abs( ( cond_rel< is_cx::no >::gt(a, b) ) ? (a-b) : (b-a) ); + + if( abs_d > (tol * max_c) ) { return false; } + } + } + + return true; + } + + + +template +inline +bool +internal_approx_equal_worker + ( + const Base& A, + const Base& B, + const typename T1::pod_type abs_tol, + const typename T1::pod_type rel_tol + ) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename T1::pod_type T; + + arma_debug_check( ((use_abs_diff == false) && (use_rel_diff == false)), "internal_approx_equal_worker(): both 'use_abs_diff' and 'use_rel_diff' are false" ); + + if(use_abs_diff) { arma_debug_check( cond_rel< is_signed::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" ); } + if(use_rel_diff) { arma_debug_check( cond_rel< is_signed::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" ); } + + const Proxy PA(A.get_ref()); + const Proxy PB(B.get_ref()); + + if( (PA.get_n_rows() != PB.get_n_rows()) || (PA.get_n_cols() != PB.get_n_cols()) ) { return false; } + + if( (Proxy::use_at == false) && (Proxy::use_at == false) ) + { + const uword N = PA.get_n_elem(); + + const typename Proxy::ea_type PA_ea = PA.get_ea(); + const typename Proxy::ea_type PB_ea = PB.get_ea(); + + for(uword i=0; i +inline +bool +internal_approx_equal_worker + ( + const BaseCube& A, + const BaseCube& B, + const typename T1::pod_type abs_tol, + const typename T1::pod_type rel_tol + ) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename T1::pod_type T; + + arma_debug_check( ((use_abs_diff == false) && (use_rel_diff == false)), "internal_approx_equal_worker(): both 'use_abs_diff' and 'use_rel_diff' are false" ); + + if(use_abs_diff) { arma_debug_check( cond_rel< is_signed::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" ); } + if(use_rel_diff) { arma_debug_check( cond_rel< is_signed::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" ); } + + const ProxyCube PA(A.get_ref()); + const ProxyCube PB(B.get_ref()); + + if( (PA.get_n_rows() != PB.get_n_rows()) || (PA.get_n_cols() != PB.get_n_cols()) || (PA.get_n_slices() != PB.get_n_slices()) ) { return false; } + + if( (ProxyCube::use_at == false) && (ProxyCube::use_at == false) ) + { + const uword N = PA.get_n_elem(); + + const typename ProxyCube::ea_type PA_ea = PA.get_ea(); + const typename ProxyCube::ea_type PB_ea = PB.get_ea(); + + for(uword i=0; i +inline +bool +internal_approx_equal_handler(const T1& A, const T2& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol) + { + arma_extra_debug_sigprint(); + + typedef typename T1::pod_type T; + + const char sig = (method != nullptr) ? method[0] : char(0); + + arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" ); + + bool status = false; + + if(sig == 'a') + { + status = internal_approx_equal_worker(A, B, abs_tol, T(0)); + } + else + if(sig == 'r') + { + status = internal_approx_equal_worker(A, B, T(0), rel_tol); + } + else + if(sig == 'b') + { + status = internal_approx_equal_worker(A, B, abs_tol, rel_tol); + } + + return status; + } + + + +template +inline +bool +internal_approx_equal_handler(const T1& A, const T2& B, const char* method, const typename T1::pod_type tol) + { + arma_extra_debug_sigprint(); + + typedef typename T1::pod_type T; + + const char sig = (method != nullptr) ? method[0] : char(0); + + arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" ); + + arma_debug_check( (sig == 'b'), "approx_equal(): argument 'method' is \"both\", but only one 'tol' argument has been given" ); + + bool status = false; + + if(sig == 'a') + { + status = internal_approx_equal_worker(A, B, tol, T(0)); + } + else + if(sig == 'r') + { + status = internal_approx_equal_worker(A, B, T(0), tol); + } + + return status; + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const Base& A, const Base& B, const char* method, const typename T1::pod_type tol) + { + arma_extra_debug_sigprint(); + + return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, tol); + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const BaseCube& A, const BaseCube& B, const char* method, const typename T1::pod_type tol) + { + arma_extra_debug_sigprint(); + + return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, tol); + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const Base& A, const Base& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol) + { + arma_extra_debug_sigprint(); + + return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, abs_tol, rel_tol); + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const BaseCube& A, const BaseCube& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol) + { + arma_extra_debug_sigprint(); + + return internal_approx_equal_handler(A.get_ref(), B.get_ref(), method, abs_tol, rel_tol); + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const SpBase& A, const SpBase& B, const char* method, const typename T1::pod_type tol) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename T1::pod_type T; + + const char sig = (method != nullptr) ? method[0] : char(0); + + arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" ); + + arma_debug_check( (sig == 'b'), "approx_equal(): argument 'method' is \"both\", but only one 'tol' argument has been given" ); + + arma_debug_check( (sig == 'r'), "approx_equal(): only the \"absdiff\" method is currently implemented for sparse matrices" ); + + arma_debug_check( cond_rel< is_signed::value >::lt(tol, T(0)), "approx_equal(): argument 'tol' must be >= 0" ); + + const unwrap_spmat UA(A.get_ref()); + const unwrap_spmat UB(B.get_ref()); + + if( (UA.M.n_rows != UB.M.n_rows) || (UA.M.n_cols != UB.M.n_cols) ) { return false; } + + const SpMat C = UA.M - UB.M; + + typename SpMat::const_iterator it = C.begin(); + typename SpMat::const_iterator it_end = C.end(); + + while(it != it_end) + { + const eT val = (*it); + + if( arma_isnan(val) || (eop_aux::arma_abs(val) > tol) ) { return false; } + + ++it; + } + + return true; + } + + + +template +arma_warn_unused +inline +bool +approx_equal(const SpBase& A, const SpBase& B, const char* method, const typename T1::pod_type abs_tol, const typename T1::pod_type rel_tol) + { + arma_extra_debug_sigprint(); + + typedef typename T1::pod_type T; + + const char sig = (method != nullptr) ? method[0] : char(0); + + arma_debug_check( ((sig != 'a') && (sig != 'r') && (sig != 'b')), "approx_equal(): argument 'method' must be \"absdiff\" or \"reldiff\" or \"both\"" ); + + arma_debug_check( ((sig == 'r') || (sig == 'b')), "approx_equal(): only the \"absdiff\" method is currently implemented for sparse matrices" ); + + arma_debug_check( cond_rel< is_signed::value >::lt(abs_tol, T(0)), "approx_equal(): argument 'abs_tol' must be >= 0" ); + arma_debug_check( cond_rel< is_signed::value >::lt(rel_tol, T(0)), "approx_equal(): argument 'rel_tol' must be >= 0" ); + + return approx_equal(A.get_ref(), B.get_ref(), "abs", abs_tol); + } + + + +//! @} -- cgit v1.2.1