Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented 'array' for Tensor objects, added 'sweep' method for appl…
…ying operations across dimensions and associated unit tests, this also led to requiring additional paste0 call when dealing with multiple arguments
- Loading branch information
Determan
authored and
Determan
committed
Dec 19, 2017
1 parent
fdac4d0
commit 3ca0aef
Showing
2 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
library(lazytensor) | ||
context("Apply Operations") | ||
|
||
# basic matrix - 2D | ||
mat <- matrix(1:12, nrow = 4) | ||
|
||
## array - 3D | ||
A <- array(1:24, dim = 4:2) | ||
|
||
|
||
test_that("Sweep Matrix", { | ||
|
||
# columns | ||
baseC <- sweep(mat, 1, seq(4), FUN = '-') | ||
# rows | ||
baseR <- sweep(mat, 2, seq(3), FUN = '-') | ||
|
||
# Tensor computations | ||
A_tensor <- Tensor$new(mat) | ||
tensorC <- A_tensor$sweep(1, seq(4), FUN = '-')$compute() | ||
A_tensor$drop() | ||
tensorR <- A_tensor$sweep(2, seq(3), FUN = '-')$compute() | ||
|
||
expect_equal(tensorC, baseC, tolerance=.Machine$double.eps ^ 0.5, | ||
info="columnwise sweep elements not equivalent", | ||
check.attributes=FALSE) | ||
expect_equal(tensorR, baseR, tolerance=.Machine$double.eps ^ 0.5, | ||
info="rowwise sweep elements not equivalent", | ||
check.attributes=FALSE) | ||
}) | ||
|
||
test_that("Sweep Array (3D)", { | ||
|
||
# columns | ||
baseC <- sweep(A, 1, seq(4), FUN = '-') | ||
# rows | ||
baseR <- sweep(A, 2, seq(3), FUN = '-') | ||
# N dimension | ||
baseN <- sweep(A, 3, seq(2), FUN = '-') | ||
|
||
# Tensor computations | ||
A_tensor <- Tensor$new(A) | ||
tensorC <- A_tensor$sweep(1, seq(4), FUN = '-')$compute() | ||
A_tensor$drop() | ||
tensorR <- A_tensor$sweep(2, seq(3), FUN = '-')$compute() | ||
A_tensor$drop() | ||
tensorN <- A_tensor$sweep(3, seq(2), FUN = '-')$compute() | ||
|
||
expect_equal(tensorC, baseC, tolerance=.Machine$double.eps ^ 0.5, | ||
info="columnwise sweep elements not equivalent", | ||
check.attributes=FALSE) | ||
expect_equal(tensorR, baseR, tolerance=.Machine$double.eps ^ 0.5, | ||
info="rowwise sweep elements not equivalent", | ||
check.attributes=FALSE) | ||
expect_equal(tensorN, baseN, tolerance=.Machine$double.eps ^ 0.5, | ||
info="N-wise sweep elements not equivalent", | ||
check.attributes=FALSE) | ||
}) | ||
|
||
|
||
|