Skip to content

Commit

Permalink
Created print method for reduceVector() (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
wleoncio committed Aug 31, 2023
1 parent 922a46d commit 3a55a97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,chacko_test)
S3method(print,reduced_vector)
export(chacko63_tab1)
export(chacko66_sec3)
export(chacko66_sec5)
Expand Down
23 changes: 23 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ print.chacko_test <- function(x, ...) {
)
)
}

#' @export
print.reduced_vector <- function(x, details = TRUE, ...) {
if (x[["verbose"]] >= 1L) {
cat(sprintf("Original vector has been reduced %d times", x[["reductions"]]))
} else {
cat(
sprintf(
paste0(
"Original vector: %s\n",
"Reduced vector : %s\n",
"Final weights : %s\n",
"Original vector has been reduced %d times\n\n",
"Run reduceVector() with verbosity > 1) to see the reduction process"
),
paste0(x[["original_vector"]], collapse = "\t"),
paste0(x[["reduced_vector"]], collapse = "\t"),
paste0(x[["weights"]], collapse = "\t"),
x[["reductions"]]
)
)
}
}
13 changes: 12 additions & 1 deletion R/reduceVector.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
#' reduceVector(chacko66_sec5)
reduceVector <- function(x, verbosity = 0L) {
x_t <- cbind("x" = unname(x), "t" = unname(x) ^ 0L)
reductions <- 0L
while (nrow(x_t) > 1L && isMonotoneIncreasing(x_t[, "x"])) {
if (verbosity >= 1L) {
message("\nVector needs reduction\nInitial vector")
print(t(x_t))
}
reductions <- reductions + 1L
x_t <- orderingProcess(x_t, verbosity)
}
return(x_t)
out <- list(
"original_vector" = x,
"reduced_vector" = x_t[, "x"],
"weights" = x_t[, "t"],
"x_t" = x_t,
"reductions" = reductions,
"verbose" = verbosity
)
class(out) <- "reduced_vector"
return(out)
}

0 comments on commit 3a55a97

Please sign in to comment.