Description Usage Arguments Details Value See Also Examples

Taking advantage of matrix sparseness, the function only evaluates
`fun`

between pairs of elements of `A`

and `B`

where
either `A`

or `B`

have non-zero values. This can be helpful
to implement other binary operators between sparse matrices that may
not be implemented in the Matrix package.

1 2 3 | ```
matrix_compare(A, B, fun)
compare_matrix(A, B, fun)
``` |

`A` |
A matrix of size |

`B` |
A matrix of size |

`fun` |
A function that receives 2 arguments and returns a scalar. |

Instead of comparing element by element, the function loops through each matrix non-zero elements to make the comparisons, which in the case of sparse matrices can be more efficient (faster). Algorithmically it can be described as follows:

1 2 3 4 5 6 7 8 9 10 11 12 | ```
# Matrix initialization
init ans[n,m];
# Looping through non-zero elements of A
for e_A in E_A:
ans[e_A] = fun(A[e_A], B[e_A])
# Looping through non-zero elements of B and applying the function
# in e_B only if it was not applied while looping in E_A.
for e_B in E_B:
if (ans[e_B] == Empty)
ans[e_B] = fun(A[e_B], B[e_B])
``` |

`compare_matrix`

is just an alias for `matrix_compare`

.

An object of class `dgCMatrix`

of size `n*m`

.

Other dyadic-level comparison functions: `vertex_covariate_compare`

,
`vertex_covariate_dist`

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 | ```
# These two should yield the same results -----------------------------------
# Creating two random matrices
set.seed(89)
A <- rgraph_ba(t = 9, m = 4)
B <- rgraph_ba(t = 9, m = 4)
A;B
# Comparing
ans0 <- matrix_compare(A,B, function(a,b) (a+b)/2)
ans1 <- matrix(0, ncol=10, nrow=10)
for (i in 1:10)
for (j in 1:10)
ans1[i,j] <- mean(c(A[i,j], B[i,j]))
# Are these equal?
all(ans0[] == ans1[]) # Should yield TRUE
# More elaborated example (speed) -------------------------------------------
## Not run:
set.seed(123123123)
A <- rgraph_ba(t = 5e3, m = 2)
B <- rgraph_ba(t = 5e3, m = 2)
Am <- as.matrix(A)
Bm <- as.matrix(B)
compfun <- function(a,b)
ifelse(a > b, a, b)
microbenchmark::microbenchmark(
diffnet = matrix_compare(A, B, compfun),
R = matrix(ifelse(Am > Bm, Am, Bm), ncol=ncol(Am)),
times = 10
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# diffnet 352.7989 355.0193 583.5366 357.7138 364.7604 2493.914 10
# R 1648.9607 1744.6762 2491.2435 1947.4344 2729.1274 6260.011 10
## End(Not run)
``` |

srdyal/diffusiontest documentation built on July 31, 2018, 2:14 a.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.