diff --git a/R/zeros_ones.R b/R/zeros_ones.R index 2bc6f00..435cbe2 100644 --- a/R/zeros_ones.R +++ b/R/zeros_ones.R @@ -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)) } \ No newline at end of file diff --git a/tests/testthat/test-convertedBaseFunctions.R b/tests/testthat/test-convertedBaseFunctions.R index 94bb5a5..1800e15 100644 --- a/tests/testthat/test-convertedBaseFunctions.R +++ b/tests/testthat/test-convertedBaseFunctions.R @@ -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]), ]) }) \ No newline at end of file