File: ring.R

package info (click to toggle)
r-cran-m2r 1.0.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 472 kB
  • sloc: cpp: 195; python: 59; sh: 14; makefile: 2
file content (291 lines) | stat: -rw-r--r-- 5,696 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#' Create a new ring in Macaulay2
#'
#' Create a new ring in Macaulay2
#'
#' @param vars vector of variable names
#' @param coefring coefficient ring (default: \code{"CC"})
#' @param order a term order (default: \code{"grevlex"})
#' @param code return only the M2 code? (default: \code{FALSE})
#' @param x formal argument for print method
#' @param params e.g. `[x,y,z]` or `[x..z]`
#' @param ... ...
#' @return a reference to a Macaulay2 ring
#' @name ring
#' @examples
#'
#' \dontrun{ requires Macaulay2
#'
#' ##### basic usage
#' ########################################
#'
#' ring("x", "y")
#' ring("x", "y", coefring = "QQ")
#'
#'
#' ##### standard evaluation
#' ########################################
#'
#' ring_(c("x", "y"))
#' ring_(c("x", "y"), code = TRUE)
#'
#' (myring <- ring_(c("x1","x2","x3","y"), coefring = "QQ", order = "lex"))
#'
#' m2_name(myring)
#' m2_meta(myring, "vars")
#' m2_meta(myring, "coefring")
#' m2_meta(myring, "order")
#'
#' ##### other options
#' ########################################
#'
#' ring_.(c("x", "y"))
#' ring_.(c("x", "y"), code = TRUE)
#'
#' }









#' @rdname ring
#' @export
ring <- function(..., coefring = m2_coefrings(), order = m2_termorders(),
  code = FALSE
) {

  # grab args
  x <- list(vars = lapply(dots(...), eval, envir = parent.frame()))
  otherArgs <- as.list(match.call(expand.dots = FALSE))[-c(1:2)]

  # eval
  args <- lapply(c(x, otherArgs), eval)

  # run standard evaluation gb
  do.call("ring_", args)

}





#' @rdname ring
#' @export
ring. <- function(..., coefring = m2_coefrings(), order = m2_termorders(),
  code = FALSE
) {

  # grab args
  x <- list(vars = lapply(dots(...), eval, envir = parent.frame()))
  otherArgs <- as.list(match.call(expand.dots = FALSE))[-c(1:2)]

  # eval
  args <- lapply(c(x, otherArgs), eval)

  # run standard evaluation gb
  do.call("ring_.", args)

}








#' @rdname ring
#' @export
ring_ <- function(vars, coefring = m2_coefrings(), order = m2_termorders(),
  code = FALSE, ...
) {

  # arg checking
  coefring <- match.arg(coefring)
  order <- match.arg(order)

  # run ideal.
  args <- as.list(match.call())[-1]
  eargs <- lapply(args, eval, envir = parent.frame())
  pointer <- do.call(ring_., eargs)
  if(code) return(invisible(pointer))

  # run ring.
  # pointer <- ring_.(vars, coefring, order, code)
  # if(code) return(invisible(pointer))

  # construct R-side ring, class and return
  m2_structure(
    m2_name = m2_name(pointer),
    m2_class = "m2_polynomialring",
    m2_meta = list(
      vars = vars,
      coefring = coefring,
      order = order
    )
  )

}



#' @rdname ring
#' @export
ring_. <- function(vars, coefring = m2_coefrings(), order = m2_termorders(),
  code = FALSE, ...
) {

  # check args
  coefring <- match.arg(coefring)
  order <- match.arg(order)

  # caps order for M2
  order <- switch(order, lex = "Lex", glex = "GLex", grevlex = "GRevLex")

  # set glex order
  if (order == "GLex") {
    # {Weights => {1,1,1,1,1}, Lex => 4}
    m2order <- sprintf(
      "{Weights => {%s}, Lex => %d}",
      paste(rep("1",length(vars)), collapse = ","),
      length(vars)-1
    )
  } else {
    # {Lex => 5}
    m2order <- paste0("{", order, " => ", length(vars), "}")
  }

  # make ring name
  ringname <- name_and_increment("ring", "m2_ring_count")

  # sortedvars <- vars(reorder(mp(paste(vars, collapse = " ")), order = order))
  sortedvars <- vars

  # construct code and message
  line <- sprintf(
    "%s = %s[%s,MonomialOrder=>%s]",
    ringname, coefring, paste(sortedvars, collapse = ","), m2order
  )
  if(code) { message(line); return(invisible(line)) }

  # run m2
  ret <- m2.(line)

  m2_name(ret) <- ringname
  ret
}





# m2 coefficient rings currently supported
coefring_as_ring <- function(coefring) {

  m2_structure(
    m2_name = coefring,
    m2_class = "m2_polynomialring",
    m2_meta = list(
      vars = NULL,
      coefring = coefring,
      order = "grevlex"
    )
  )

}



#' @rdname ring
#' @export
m2_parse_object_as_function.m2_polynomialring <- function(x, params) {

  monoid <- params[[c(1)]]
  vars <- c(m2_meta(x, "vars"))
  order <- "grevlex"

  for (i in 1:length(monoid)) {
    if (!is.m2_option(monoid[[i]])) {
      vars <- c(vars, unlist(monoid[[i]]))
    } else if (monoid[[c(i,1)]] == "MonomialOrder") {
      for (j in 1:length(monoid[[c(i,2)]])) {
        if (
          is.m2_option(monoid[[c(i,2,j)]]) &&
          monoid[[c(i,2,j,1)]] %in% c("Lex", "Weights", "GRevLex")
        ) {

          order <- monoid[[c(i,2,j,1)]]

          # extra checking for glex
          if (
            order == "Weights" &&
            all(unlist(lapply(monoid[[c(i,2,j,2)]], function(x) x==1)))
          ) {
            order <- "glex"
            break()
          }

          order <- switch(order, Lex = "lex", GRevLex = "grevlex")

        }
      }
    }
  }

  m2_structure(
    m2_name = "",
    m2_class = "m2_polynomialring",
    m2_meta = list(
      vars = vars,
      coefring = m2_meta(x, "coefring"),
      order = order
    )
  )

}




# m2 coefficient rings currently supported
#' @rdname ring
#' @export
m2_coefrings <- function() c("CC", "RR", "QQ", "ZZ")




# m2 term orders currently supported
#' @rdname ring
#' @export
m2_termorders <- function() c("grevlex", "lex", "glex")




m2_ring_class_names <- function() {
  c(
    "Ring","PolynomialRing","QuotientRing",
    "InexactFieldFamily","InexactField"
  )
}


#' @rdname ring
#' @export
print.m2_polynomialring <- function(x, ...){

  s <- sprintf(
    "M2 Ring: %s[%s], %s order",
    m2_meta(x, "coefring"),
    paste(m2_meta(x, "vars"), collapse = ","),
    m2_meta(x, "order")
  )
  cat(s, "\n", sep = "")

  invisible(x)
}