1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
function ok = isequal_roundoff (A,B,tol)
%ISEQUAL_ROUNDOFF compare two matrices, allowing for roundoff errors
%
% returns true if A == B to within relative tolerance tol.
% tol = 64*eps if not present. NaNs and Infs are ignored in the
% tol, but the NaN and +/-Inf pattern must be the same.
% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0
% if (~isequal (GB_spec_type (A), GB_spec_type (B)))
% ok = false ;
% return ;
% end
if (isequalwithequalnans (A, B))
ok = true ;
return
end
% floating-point NaN pattern must match exactly
if (~isequal (isnan (A), isnan (B)))
% pattern of NaNs is different
ok = false ;
return
end
% remove NaNs
A (isnan (A)) = 0 ;
B (isnan (B)) = 0 ;
% floating-point Inf pattern must match exactly;
% treat +Inf and -Inf differently
if (~isequal (sign (A) .* isinf (A), sign (B) .* isinf (B)))
% pattern of +Inf/-Inf is different
ok = false ;
return
end
% remove the Infs
A (isinf (A)) = 0 ;
B (isinf (B)) = 0 ;
% values must close, to within relative tol
err = norm (A - B, 1) ;
anorm = norm (A, 1) ;
if (nargin < 3)
tol = 64*eps ;
end
anorm = max (anorm, 1) ;
ok = (err == 0) || (err <= tol * anorm) ;
|