-
Notifications
You must be signed in to change notification settings - Fork 5
/
factor_n.R
134 lines (117 loc) · 3 KB
/
factor_n.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
#' Factor an integer into primes
#'
#' Factor an integer into primes
#'
#' @param n an integer or a polynomial
#' @param code return only the M2 code? (default: \code{FALSE})
#' @param ... ...
#' @return a data frame with integer columns \code{prime} and
#' \code{power} or \code{m2_pointer} referencing the factorization
#' in M2.
#' @name factor_n
#' @examples
#'
#' \dontrun{ requires Macaulay2
#'
#' ##### basic usage
#' ########################################
#'
#' 2^2 * 3^7 * 5^2 # = 218700
#' factor_n(218700)
#' factor_n.(218700)
#'
#' (df <- factor_n(218700))
#' df$prime
#' df$power
#' str(df)
#'
#'
#' factor_n(218700, code = TRUE)
#'
#'
#' ##### other options
#' ########################################
#'
#' (integer_pointer <- m2.("218700"))
#' m2_name(integer_pointer)
#' factor_n(integer_pointer, code = TRUE)
#' factor_n(integer_pointer)
#'
#'
#'
#' factor_n(3234432540)
#' factor_n(323443254223453)
#' factor_n(rpois(1, 1e4))
#'
#'
#' ##### known issues
#' ########################################
#'
#' # R doesn't handle big ints well. note in the following
#' # the m2 code number is different than the supplied number
#' factor_n(32344325422364353453, code = TRUE)
#'
#' # this can be circumvented by passing a string instead
#' factor_n("32344325422364353453", code = TRUE)
#'
#' # but if the factors are large, R can't handle the parsing well
#' factor_n("32344325422364353453")
#'
#' # here's a workaround:
#' factor_pointer <- factor_n.("32344325422364353453")
#' m2_meta(factor_pointer, "ext_str")
#' extract_factors <- function(pointer) {
#' require(stringr)
#' str <- m2_meta(pointer, "ext_str")
#' str <- str_sub(str, 19, -2)
#' str <- str_extract_all(str, "\\{[0-9]+,[0-9]+\\}")[[1]]
#' str <- str_sub(str, 2, -2)
#' str <- str_split(str, ",")
#' df <- as.data.frame(t(simplify2array(str)))
#' names(df) <- c("prime", "power")
#' df
#' }
#' (df <- extract_factors(factor_pointer))
#'
#'
#' # using gmp (currently broken)
#' # factor_n("32344325422364353453", gmp = TRUE)
#' m2("11 * 479 * 6138607975396537")
#' 11 * 479 * 6138607975396537
#'
#' }
#'
#' @rdname factor_n
#' @export
factor_n <- function (n, code = FALSE, ...) {
# run m2
args <- as.list(match.call())[-1]
eargs <- lapply(args, eval, envir = parent.frame())
pointer <- do.call(factor_n., eargs)
if (code) return(invisible(pointer))
# parse output
parsed_out <- m2_parse(pointer)
# reformat and return
list(
prime = vapply(parsed_out, `[[`, integer(1), 1),
power = vapply(parsed_out, `[[`, integer(1), 2)
)
}
#' @rdname factor_n
#' @export
factor_n. <- function (n, code = FALSE, ...) {
# arg checking
# compute factor with m2, e.g. "2^2*67*97*9433"
if (is.m2_pointer(n)) {
param <- m2_name(n)
} else if (is.numeric(n)) {
param <- as.character(format(n, scientific = FALSE)) # for big Z's
} else {
param <- n
}
# create code and message
m2_code <- sprintf("factor(%s)", param)
if(code) { message(m2_code); return(invisible(m2_code)) }
# run m2 and return pointer
m2.(m2_code)
}