-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.Rmd
182 lines (121 loc) · 4.97 KB
/
README.Rmd
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
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.path = "tools/"
)
```
# latte
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/latte)](https://cran.r-project.org/package=latte)
[![Travis build status](https://travis-ci.org/dkahle/latte.svg?branch=master)](https://travis-ci.org/dkahle/latte)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/dkahle/latte?branch=master&svg=true)](https://ci.appveyor.com/project/dkahle/latte)
<!-- badges: end -->
__latte__ is an R package that makes back-end connections to [LattE](https://www.math.ucdavis.edu/~latte/software.php) and [4ti2](http://www.4ti2.de). Most of its functions were previously part of the [__algstat__ package](https://github.com/dkahle/algstat), but have been pulled out and improved upon.
Note: the following assumes you have [LattE](https://www.math.ucdavis.edu/~latte/) and [4ti2](http://www.4ti2.de) installed and __latte__ recognizes their path. To help __latte__ find 4ti2 and LattE on your machine, you can use `set_4ti2_path()` and `set_latte_path()` at the beginning of the session. Better, you can save environment variables that point to where their executables are using `usethis::edit_r_environ()`; just add two lines. For example, my lines look like:
```{bash, eval=FALSE}
LATTE=/Applications/latte/bin
FOURTITWO=/Applications/latte/bin
```
You load __latte__ like this:
```{r load}
library("latte")
```
## Lattice point counting
Most [LattE](https://www.math.ucdavis.edu/~latte/) programs are available as functions in __latte__. For example, `latte_count()` uses LattE's `count` to determine the number of integer points in a [polytope](http://en.wikipedia.org/wiki/Polytope):
```{r latte-count}
latte_count(c("x + y <= 10", "x >= 0", "y >= 0"))
```
It's easy to confirm the solution with a simple visualization:
```{r countExample, fig.height=4, dpi=200}
library(ggplot2); theme_set(theme_bw())
polytope <- data.frame(x = c(0,10,0), y = c(0,0,10))
points <- expand.grid(x = 0:10, y = 0:10)
points <- subset(points, x + y <= 10)
points$number <- 1:nrow(points)
ggplot(aes(x = x, y = y), data = polytope) +
geom_polygon(fill = "red", alpha = .2) +
geom_text(aes(y = y + .25, label = number), size = 3.5, data = points) +
geom_point(data = points) +
coord_equal()
```
## Integer programming
In addition to table counting, it can also do integer programming with LattE's `latte-maximize` and `latte-minimize` programs. To do this, it uses tools from [__mpoly__](http://github.com/dkahle/mpoly):
```{r ip}
latte_max("-2 x + 3 y", c("x + y <= 10", "x >= 0", "y >= 0"))
latte_min("-2 x + 3 y", c("x + y <= 10", "x >= 0", "y >= 0"))
```
We can check that the solution given above is correct, but the value is not. So, it needs some more work:
```{r ipCheck, fig.height=4, dpi=200}
points$objective <- with(points, -2*x + 3*y)
ggplot(aes(x = x, y = y), data = polytope) +
geom_polygon(fill = "red", alpha = .2) +
geom_point(aes(size = objective), data = points) +
coord_equal()
```
## Lattice bases
You can compute all sorts of bases of integer lattices using [4ti2](http://www.4ti2.de). For example, if $\textbf{A}$ is the matrix
```{r lattice-bases-1}
(A <- genmodel(varlvls = c(2, 2), facets = list(1, 2)))
```
Its Markov basis can be computed
```{r lattice-bases-2}
markov(A, p = "arb")
```
Note that the `p = "arb"` signifies that arbitrary precision arithmetic is supported by using the `-parb` flag at the command line.
Other bases are available as well:
```{r lattice-bases-3}
zbasis(A, p = "arb")
groebner(A, p = "arb")
graver(A)
```
## `zsolve()` and `qsolve()`
You can solve linear systems over the integers and rationals uing `zsolve()` and `qsolve()`:
```{r zsolve}
mat <- rbind(
c( 1, -1),
c(-3, 1),
c( 1, 1)
)
rel <- c("<", "<", ">")
rhs <- c(2, 1, 1)
sign <- c(0, 1)
zsolve(mat, rel, rhs, sign)
```
```{r qsolve}
mat <- rbind(
c( 1, 1),
c( 1, 1)
)
rel <- c(">", "<")
sign <- c(0, 0)
qsolve(mat, rel, sign, p = "arb")
```
## Primitive partition identities
The primitive partition identities can be computed with `ppi()`:
```{r ppi-1}
ppi(3)
```
This is equivalent to the Graver basis of the numbers 1 to 3. In other words, its the numbers whose columns, when multiplied by $[1, 2, 3]$ element-wise, sum to zero:
```{r ppi-2}
t(1:3) %*% ppi(3)
```
## Installation
* From CRAN
```{r, eval=FALSE}
install.packages("latte")
```
* From Github (dev version):
```{r, eval=FALSE}
if (!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/mpoly")
devtools::install_github("dkahle/latte")
```
## Acknowledgements
This material is based upon work supported by the National Science Foundation under Grant Nos. [1622449](https://nsf.gov/awardsearch/showAward?AWD_ID=1622449) and [1622369](https://www.nsf.gov/awardsearch/showAward?AWD_ID=1622369).