-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse fractions properly and implement GMP #150
Comments
Running |
R's gmp package connects to gmp and would allow us to better address the problem; but this is actually part of a bigger issue. I kind of hate to add a dependency, but maybe we should require gmp and then parse things accordingly. Thoughts? |
How would gmp help with this? It gives us more precision (helps with overflow, etc) but doesn't help with the fact that rationals dont exist in R. Could we implement a basic "rational" data type, essentially a vector with 2 integers (numerator and denominator) with overloaded +, *, etc? |
gmp provides one with the > as.bigq(1L, 2L)
Big Rational ('bigq') :
[1] 1/2
> as.bigq(3L, 6L)
Big Rational ('bigq') :
[1] 1/2 (Arithmetic operators are also defined for the class.) |
Ah, I see. This definitely looks like the way to go. If you add the dependency, I will fix the parsing. Do we want to use this to store larger precision integers/floats as well? Is there a disadvantage to doing so? |
Sure, I'll do that shortly. gmp seems portable; I just installed it no problem on my Windows box. So... seems like a reliable resource. It's been around for a long time, and obviously the underling gmp library is widely used. |
Agreed. What I mean is, do we want to use gmp for ALL of our integers and floating points, since Macaulay2 doesn't discriminate based on required precision? |
From a mathematical standpoint, that seems nice. Would that be natural to a statistician though? |
If possible, I think it'd be nice to have a logical global option set that governs this, like what you get with > getOption("m2r")
$m2_path
[1] "/Applications/Macaulay2-1.9.2/bin"
> m2r:::set_m2r_option(gmp = TRUE) # we would make this into something like m2_use_gmp()
> getOption("m2r")
$m2_path
[1] "/Applications/Macaulay2-1.9.2/bin"
$gmp
[1] TRUE
> using_gmp <- function() getOption("m2r")$gmp
> using_gmp()
[1] TRUE How hard do you think that'd be to implement? We may have to distinguish between Z's and Q's? |
I agree this would be a nice option. One weird thing: even if they disable gmp, they would still need to have it installed. Also, how would we handle fractions if they disable it? |
You mean if they set it to If it's disabled, it makes most sense to me to just evaluate the fractions R style. So, something like: string <- "1/3"
out <- as.numeric(str_split(string, "/")[[1]])
out[1] / out[2]
# [1] 0.3333333 |
Also, I am inclined to think that gmp should be turned off by default. Since gmp numbers print differently than ordinary numbers, I don't want to users to be scared away by the printing. I also don't know how nicely gmp objects play with the rest of the R ecosystem. Thoughts? To let users know it's available, we can randomly add a package startup message to the effect of |
library(m2r)
# Loading required package: mpoly
# Loading required package: stringr
# please cite mpoly if you use it; see citation("mpoly")
# M2 found in /Applications/Macaulay2-1.9.2/bin
mat <- matrix(1:6, 3)
m2_matrix(mat)
# Starting M2... done.
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
# M2 Matrix over ZZ[]
m2_toggle_gmp()
# m2r is now using gmp.
# m2_matrix(mat)
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
# M2 Matrix over ZZ[]
m2_matrix(mat) %>% str
# 'm2_matrix' int [1:3, 1:2] 1 2 3 4 5 6
# - attr(*, "m2_name")= chr "m2rintmatrix00000003"
# - attr(*, "m2_meta")=List of 1
# ..$ ring:Classes 'm2_polynomialring', 'm2' atomic [1:1] NA
# .. .. ..- attr(*, "m2_name")= chr "ZZ"
# .. .. ..- attr(*, "m2_meta")=List of 3
# .. .. .. ..$ vars : NULL
# .. .. .. ..$ coefring: chr "ZZ"
# .. .. .. ..$ order : chr "grevlex" |
Perhaps we should open a new issue for this? Something along the lines of "Improve GMP support"? |
Shouldn't be too hard, just check next token for
/
when you think you're looking at an int.The text was updated successfully, but these errors were encountered: