Merge branch 'zerosOnes3D+' into admix1
This commit is contained in:
commit
389975366d
4 changed files with 39 additions and 7 deletions
|
|
@ -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,14 @@ 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))
|
||||
#' @param ... extra dimensions
|
||||
zeros <- function(n1, n2 = n1, ...) {
|
||||
if (length(n1) == 1) {
|
||||
n <- c(n1, n2, ...)
|
||||
} else {
|
||||
n <- n1
|
||||
}
|
||||
return(zeros_or_ones(n, 0))
|
||||
}
|
||||
|
||||
#' @title Matrix of ones
|
||||
|
|
@ -26,6 +39,12 @@ 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))
|
||||
#' @param ... extra dimensions
|
||||
ones <- function(n1, n2 = n1, ...) {
|
||||
if (length(n1) == 1) {
|
||||
n <- c(n1, n2, ...)
|
||||
} else {
|
||||
n <- n1
|
||||
}
|
||||
return(zeros_or_ones(n, 1))
|
||||
}
|
||||
|
|
@ -4,12 +4,14 @@
|
|||
\alias{ones}
|
||||
\title{Matrix of ones}
|
||||
\usage{
|
||||
ones(n1, n2 = n1)
|
||||
ones(n1, n2 = n1, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{n1}{number of rows}
|
||||
|
||||
\item{n2}{number of columns}
|
||||
|
||||
\item{...}{extra dimensions}
|
||||
}
|
||||
\description{
|
||||
wrapper of `zeros_or_ones()` that replicates the behavior of
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@
|
|||
\alias{zeros}
|
||||
\title{Matrix of zeros}
|
||||
\usage{
|
||||
zeros(n1, n2 = n1)
|
||||
zeros(n1, n2 = n1, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{n1}{number of rows}
|
||||
|
||||
\item{n2}{number of columns}
|
||||
|
||||
\item{...}{extra dimensions}
|
||||
}
|
||||
\description{
|
||||
wrapper of `zeros_or_ones()` that replicates the behavior of
|
||||
|
|
|
|||
|
|
@ -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]), ])
|
||||
})
|
||||
Loading…
Add table
Reference in a new issue