Added capacity to handle arrays

This commit is contained in:
Waldir Leoncio 2020-03-18 11:24:19 +01:00
parent e823b3a58b
commit 8a73fe4bc1
2 changed files with 21 additions and 5 deletions

View file

@ -8,8 +8,15 @@
#' @note Actually works for any `x`, but there's no need to bother imposing
#' validation controls here.
zeros_or_ones <- function(n, x) {
# Expanding n to length 2 if necessary
if (length(n) == 1) n <- c(n, n)
return(matrix(x, n[1], n[2]))
# Returning a matrix or an array
if (length(n) == 2) {
return(matrix(x, n[1], n[2]))
} else {
return(array(x, dim=n))
}
}
#' @title Matrix of zeros
@ -17,8 +24,8 @@ zeros_or_ones <- function(n, x) {
#' the `zeros()` function on Matlab
#' @param n1 number of rows
#' @param n2 number of columns
zeros <- function(n1, n2 = n1) {
return(zeros_or_ones(c(n1, n2), 0))
zeros <- function(n1, n2 = n1, ...) {
return(zeros_or_ones(c(n1, n2, ...), 0))
}
#' @title Matrix of ones
@ -26,6 +33,6 @@ zeros <- function(n1, n2 = n1) {
#' the `ones()` function on Matlab
#' @param n1 number of rows
#' @param n2 number of columns
ones <- function(n1, n2 = n1) {
return(zeros_or_ones(c(n1, n2), 1))
ones <- function(n1, n2 = n1, ...) {
return(zeros_or_ones(c(n1, n2, ...), 1))
}

View file

@ -39,9 +39,11 @@ test_that("zeros and ones work as expected", {
expect_equal(zeros(2), matrix(0, 2, 2))
expect_equal(zeros(2, 1), matrix(0, 2, 1))
expect_equal(zeros(1, 10), matrix(0, 1, 10))
expect_equal(zeros(3, 2, 4), array(0, c(3, 2, 4)))
expect_equal(ones(8), matrix(1, 8, 8))
expect_equal(ones(5, 2), matrix(1, 5, 2))
expect_equal(ones(2, 100), matrix(1, 2, 100))
expect_equal(ones(3, 2, 4, 2), array(1, c(3, 2, 4, 2)))
})
test_that("times works as expected", {
@ -151,4 +153,11 @@ test_that("find works as expected", {
expect_equal(find(X), c(1, 5, 7, 8, 9))
expect_equal(find(!X), c(2, 3, 4, 6))
expect_equal(find(Y == 13), 7)
})
test_that("sortrows works as expected", {
mx <- matrix(c(3, 2, 2, 1, 1, 10, 0, pi), 4)
expect_equal(sortrows(mx), matrix(c(1, 2, 2, 3, pi, 10, 0, 1), 4))
expect_equal(sortrows(mx, 2), matrix(c(2, 3, 1, 2, 0, 1, pi, 10), 4))
expect_equal(sortrows(mx, 1:2), mx[order(mx[, 1], mx[, 2]), ])
})