Test if Two Sparse Matrices are (Nearly) Equal
allequal.Rd
Utility to compare two spam
objects
testing 'near equality'. Depending on the type of difference, comparison is
still made to some extent, and a report of the differences is
returned.
Usage
# S3 method for class 'spam'
all.equal(target, current, tolerance = .Machine$double.eps^0.5,
scale = NULL, check.attributes = FALSE,...)
Arguments
- target
a
spam
object.- current
another
spam
object to be compared withtarget
.- tolerance
numeric >= 0. Differences smaller than
tolerance
are not considered.- scale
numeric scalar > 0 (or
NULL
). See ‘Details’.- check.attributes
currently not yet implemented.
- ...
Further arguments for different methods.
Value
Either TRUE
or a vector of 'mode' "character"
describing the
differences between target
and current
.
Details
Numerical comparisons for scale = NULL
(the default) are
typically on a relative difference scale unless the
target
values are close to zero or infinite. Specifically,
the scale is computed as the average absolute value of target
.
If this scale is finite and exceeds tolerance
, differences
are expressed relative to it; otherwise, absolute differences are used.
If scale
is numeric (and positive), absolute comparisons are
made after scaling (dividing) by scale
. Note that if all of
scale is sufficiently close to 1 (specifically, within tolerance
),
the difference is still reported as being on an absolute scale.
Do not use all.equal.spam
directly in if
expressions: either use isTRUE( all.equal.spam(...))
or
identical
if appropriate.
Cholesky decomposition routines use this function to test for symmetry.
A method for matrix-spam
objects is defined as well.
There is the additional catch of a zero matrix being represented by one zero element, see ‘Examples’ below.
See also
isSymmetric.spam
and cleanup
.
Examples
obj <- diag.spam(2)
obj[1,2] <- .Machine$double.eps
all.equal( diag.spam(2), obj)
#> [1] "Lengths (2, 3) differ"
all.equal( t(obj), obj)
#> [1] "Column-sparsity structure differ (at least 1 instance(s))"
#> [2] "Row-sparsity structure differ (at least 1 instance(s))"
all.equal( t(obj), obj*1.1)
#> [1] "Column-sparsity structure differ (at least 1 instance(s))"
#> [2] "Row-sparsity structure differ (at least 1 instance(s))"
#> [3] "Mean relative difference: 0.1"
# We can compare a spam to a matrix
all.equal(diag(2),diag.spam(2))
#> [1] TRUE
# the opposite does often not make sense,
# hence, it is not implemented.
all.equal(diag.spam(2),diag(2))
#> [1] TRUE
# A zero matrix contains one element:
str(spam(0))
#> Formal class 'spam' [package "spam"] with 4 slots
#> ..@ entries : num 0
#> ..@ colindices : int 1
#> ..@ rowpointers: int [1:2] 1 2
#> ..@ dimension : int [1:2] 1 1
# hence
all.equal.spam(spam(0,3,3), diag.spam(0,3) )
#> [1] "Lengths (1, 3) differ"
norm(spam(0,3,3) - diag.spam(0,3) )
#> [1] 0