File: rem.R

package info (click to toggle)
r-cran-matlab 1.0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: sh: 13; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 2,363 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
###
### $Id: rem.R 30 2022-05-30 23:11:34Z proebuck $
###


##-----------------------------------------------------------------------------
test.rem <- function(input, expected) {
    output <- do.call(getFromNamespace("rem", "matlab"), input)
    identical(output, expected)
}


## Remainder After Division of Scalar
rem.expected.div.scalar <- 3
test.rem(list(x = 23, y = 5), rem.expected.div.scalar)

## Remainder After Division of Vector
X.vec <- 1:5
rem.expected.div.vec <- c(1, 2, 0, 1, 2)
test.rem(list(x = X.vec, y = 3), rem.expected.div.vec)

## Remainder After Division of Vector by Zero
X.vec <- 1:5
rem.expected.div.vec.by0 <- rep(NaN, length(X.vec))
test.rem(list(x = X.vec, y = 0), rem.expected.div.vec.by0)

## Remainder After Division for Positive and Negative Values
## Note that nonzero results have the same sign as the dividend.
X.posneg.vec <- c(-4, -1, 7, 9)
rem.expected.div.posneg.vec <- c(-1, -1, 1, 0)
test.rem(list(x = X.posneg.vec, y = 3), rem.expected.div.posneg.vec)

## Remainder After Division for Floating-Point Values
X.theta <- c(0.0, 3.5, 5.9, 6.2, 9.0, 4 * pi)
b <- 2 * pi;
expected.div.fp.vec <- c(0, 3.5, 5.9, 6.2, 2.716815, 0)
test.rem(list(x = X.theta, y = b), expected.div.fp.vec)

## Remainder After Division of Matrix
X.mat <- matrix(1:9, nrow = 3, byrow = TRUE)
rem.expected.X.mat <- matrix(c(3:1, 6:4, 9:7), nrow = 3, byrow = TRUE)
rem.expected.X.mat.Y0 <- matrix(rep(NaN, length(X.mat)), nrow = nrow(X.mat))
rem.expected.X.mat.Y1 <- matrix(rep(1, length(X.mat)), nrow = nrow(X.mat))
rem.expected.X.mat.Y2 <- matrix(rep(c(1, 0), 5)[1:length(X.mat)],
                                nrow = nrow(X.mat))
rem.expected.X.mat.Y3 <- matrix(rep(c(1, 2, 0), nrow(X.mat)),
                                nrow = nrow(X.mat),
                                byrow = TRUE)

test.rem(list(x = X.mat, y = 0), rem.expected.X.mat.Y0)
test.rem(list(x = X.mat, y = 1), rem.expected.X.mat.Y1)
test.rem(list(x = X.mat, y = 2), rem.expected.X.mat.Y2)
test.rem(list(x = X.mat, y = 3), rem.expected.X.mat)


## rem & mod give same results with X, Y having same sign
test.rem(list(x = 5, y = 3), matlab::mod(5, 3))
test.rem(list(x = -5, y = -3), matlab::mod(-5, -3))

## alternate formula used when X, Y having different signs
test.rem(list(x = 5, y = -3), (matlab::mod(5, -3) - -3))
test.rem(list(x = -5, y = 3), (matlab::mod(-5, 3) - 3))