/
lsd-normalize.R
273 lines (231 loc) · 7.99 KB
/
lsd-normalize.R
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
## Normalize lsd values ##
# Error messages ----------------------------------------------------------
lsd_msg <- c("{.var bases} must be a numeric vector of length {.val {2}}
to normalize a tripartite value.",
"x" = "You've supplied a {.cls {class(bases)}} vector of
length {.val {vec_size(bases)}}.")
tetra_msg <- c("{.var bases} must be a numeric vector of length {.val {3}}
to normalize a tetrapartite value.",
"x" = "You've supplied a {.cls {class(bases)}} vector of
length {.val {vec_size(bases)}}.")
# Normalization pieces ----------------------------------------------------
#' Check for non-whole numbers in libra and solidus units
#'
#' Move any decimals in libra and solidus units to the denarius unit
#' for lsd and any decimals is libra, solidus, or denarius to the farthing
#' unit in tetra. The function uses the utility `should_be_int()` to deal
#' with floating point problems.
#'
#' @keywords internal
#' @noRd
decimal_check <- function(x) {
l <- field(x, "l")
s <- field(x, "s")
d <- field(x, "d")
field(x, "l") <- trunc(l)
temp_s <- s + (l - trunc(l)) * deb_bases(x)[[1]]
field(x, "s") <- trunc(temp_s)
temp_d <- d + (temp_s - trunc(temp_s)) * deb_bases(x)[[2]]
if (deb_is_lsd(x)) {
# Deal with floating point problems potentially introduced by the above
field(x, "d") <- dplyr::if_else(should_be_int(temp_d),
round(temp_d),
temp_d)
} else if (deb_is_tetra(x)) {
field(x, "d") <- trunc(temp_d)
temp_f <- field(x, "f") + (temp_d - trunc(temp_d)) * deb_bases(x)[[3]]
field(x, "f") <- dplyr::if_else(should_be_int(temp_f),
round(temp_f),
temp_f)
}
x
}
#' Check whether lsd value is positive or negative
#' @keywords internal
#' @noRd
is_negative <- function(x) {
if (deb_is_lsd(x)) {
field(x, "l") +
field(x, "s") / deb_bases(x)[[1]] +
field(x, "d") / prod(deb_bases(x)) < 0
} else if (deb_is_tetra(x)) {
field(x, "l") +
field(x, "s") / deb_bases(x)[[1]] +
field(x, "d") / prod(deb_bases(x)[1:2]) +
field(x, "f") / prod(deb_bases(x)) < 0
}
}
#' Normalization function
#'
#' Function that actually performs the normalization of lsd value
#' @keywords internal
#' @noRd
normalize <- function(l, s, d, bases) {
solidus <- s + (d %/% bases[[2]])
libra <- l + (solidus %/% bases[[1]])
new_lsd(l = libra,
s = solidus %% bases[[1]],
d = d %% bases[[2]],
bases = bases)
}
#' Normalization path for positive values
#'
#' Separate normalization functions for positive and negative values to be
#' used in `dplyr::if_else()`. Making them functions simplifies the process.
#' @keywords internal
#' @noRd
lsd_normalize <- function(lsd) {
normalize(l = field(lsd, "l"),
s = field(lsd, "s"),
d = field(lsd, "d"),
bases = deb_bases(lsd))
}
#' Normalization path for negative values
#'
#' Turn values positive and then return to negative value.
#' @keywords internal
#' @noRd
lsd_normalize_neg <- function(lsd) {
ret <- normalize(l = -field(lsd, "l"),
s = -field(lsd, "s"),
d = -field(lsd, "d"),
bases = deb_bases(lsd))
-ret
}
#' Tetra normalization function
#'
#' Function that actually performs the normalization of tetra values
#' @keywords internal
#' @noRd
tetra_normalize <- function(l, s, d, f, bases) {
denarius <- d + (f %/% bases[[3]])
solidus <- s + (denarius %/% bases[[2]])
libra <- l + (solidus %/% bases[[1]])
new_tetra(l = libra,
s = solidus %% bases[[1]],
d = denarius %% bases[[2]],
f = f %% bases[[3]],
bases = bases)
}
#' Normalization path for positive tetra values
#'
#' Separate normalization functions for positive and negative values to be
#' used in `dplyr::if_else()`. Making them functions simplifies the process.
#' @keywords internal
#' @noRd
tetra_normalize_pos <- function(tetra) {
tetra_normalize(l = field(tetra, "l"),
s = field(tetra, "s"),
d = field(tetra, "d"),
f = field(tetra, "f"),
bases = deb_bases(tetra))
}
#' Normalization path for negative tetra values
#'
#' Turn values positive and then return to negative value.
#' @keywords internal
#' @noRd
tetra_normalize_neg <- function(tetra) {
ret <- tetra_normalize(l = -field(tetra, "l"),
s = -field(tetra, "s"),
d = -field(tetra, "d"),
f = -field(tetra, "f"),
bases = deb_bases(tetra))
-ret
}
# deb_normalize methods ---------------------------------------------------
#' Normalize tripartite and tetrapartite values
#'
#' Normalize tripartite and tetrapartite values values to given bases.
#'
#' @param x Either an vector of class `deb_lsd`, `deb_tetra`, or a numeric
#' vector of length 3 or 4 representing the values to be normalized.
#' @param bases Used only if `x` is a numeric vector. A Numeric vector of
#' length 2 or 3 used to specify the bases for the solidus or s, denarius or
#' d, and optionally the farthing or f units. Default is `c(20, 12)`, which
#' conforms to the most widely used system of 1 pound = 20 shillings and 1
#' shilling = 12 pence.
#' @param ... Arguments passed on to further methods.
#'
#' @returns Returns a vector of class `deb_lsd` with normalized solidus and
#' denarius units or a vector of class `deb_tetra` with normalized solidus,
#' denarius, and farthing units.
#' @examples
#'
#' # Normalize a deb_lsd vector
#' x <- deb_lsd(12, 93, 78)
#' x_alt <- deb_lsd(12, 93, 78, bases = c(60, 16))
#' deb_normalize(x)
#' deb_normalize(x_alt)
#'
#' # Normalize a deb_tetra vector
#' t <- deb_tetra(12, 83, 78, 42)
#' t_alt <- deb_tetra(12, 83, 78, 42, bases = c(60, 16, 8))
#' deb_normalize(t)
#' deb_normalize(t_alt)
#'
#' # Normalize a numeric vector of length 3
#' deb_normalize(c(12, 93, 78))
#' deb_normalize(c(12, 93, 78), bases = c(60, 16))
#'
#' # Normalize a numeric vector of length 4
#' # Must provide bases of length 3
#' deb_normalize(c(12, 93, 78, 42), bases = c(20, 12, 4))
#' deb_normalize(c(12, 93, 78, 42), bases = c(60, 16, 8))
#'
#' @name normalization
NULL
#' @rdname normalization
#' @export
deb_normalize <- function(x, ...) {
UseMethod("deb_normalize")
}
#' @rdname normalization
#' @export
deb_normalize.default <- function(x, ...) {
cli::cli_abort(c("{.var x } must be a <deb_lsd> or <deb_tetra> vector or a
numeric vector of length {.val {3}} or {.val {4}}.",
"x" = "You've supplied a {.cls {class(x)}} vector."))
}
#' @rdname normalization
#' @export
deb_normalize.deb_lsd <- function(x, ...) {
checked <- decimal_check(x)
dplyr::if_else(is_negative(x),
lsd_normalize_neg(checked),
lsd_normalize(checked))
}
#' @rdname normalization
#' @export
deb_normalize.numeric <- function(x, bases = c(20, 12), ...) {
if (vec_size(x) == 3L) {
# Check bases
if (!is.numeric(bases) || vec_size(bases) != 2L) {
cli::cli_abort(message = lsd_msg)
}
lsd <- deb_lsd(x[[1]], x[[2]], x[[3]], bases)
ret <- deb_normalize(lsd)
} else if (vec_size(x) == 4L) {
# Check bases
if (!is.numeric(bases) || vec_size(bases) != 3L) {
cli::cli_abort(tetra_msg)
}
tetra <- deb_tetra(x[[1]], x[[2]], x[[3]], x[[4]], bases)
ret <- deb_normalize(tetra)
} else {
cli::cli_abort(c(
"{.var x } must be a <deb_lsd> or <deb_tetra> vector or a numeric
vector of length {.val {3}} or {.val {4}}.",
"x" = "You've supplied a {.cls {class(x)}} vector of length
{.val {vec_size(x)}}."))
}
ret
}
#' @rdname normalization
#' @export
deb_normalize.deb_tetra <- function(x, ...) {
checked <- decimal_check(x)
dplyr::if_else(is_negative(x),
tetra_normalize_neg(checked),
tetra_normalize_pos(checked))
}