| | 1 | r""" |
|---|
| | 2 | Interface to QEPCAD B |
|---|
| | 3 | |
|---|
| | 4 | The basic function of QEPCAD is to construct cylindrical algebraic |
|---|
| | 5 | decompositions (CADs) of $\RR^k$, given a list of polynomials. Using |
|---|
| | 6 | this CAD, it is possible to perform quantifier elimination and formula |
|---|
| | 7 | simplification. |
|---|
| | 8 | |
|---|
| | 9 | A CAD for a set $A$ of $k$-variate polynomials decomposes $\RR^j$ into |
|---|
| | 10 | disjoint cells, for each $j$ in $0 \leq j \leq k$. The sign of each |
|---|
| | 11 | polynomial in $A$ is constant in each cell of $\RR^k$, and for each |
|---|
| | 12 | cell in $\RR^j$ ($j > 1$), the projection of that cell into |
|---|
| | 13 | $\RR^{j-1}$ is a cell of $\RR^{j-1}$. (This property makes the |
|---|
| | 14 | decomposition ``cylindrical''.) |
|---|
| | 15 | |
|---|
| | 16 | Given a formula $\exists x. P(a,b,x) = 0$ (for a polynomial $P$), and |
|---|
| | 17 | a cylindrical algebraic decomposition for $P$, we can eliminate the |
|---|
| | 18 | quantifier (find an equivalent formula in the two variables $a$, $b$ |
|---|
| | 19 | without the quantifier $\exists$) as follows. For each cell $C$ in |
|---|
| | 20 | $\RR^2$, find the cells of $\RR^3$ which project to $C$. (This |
|---|
| | 21 | collection is called the \emph{stack} over $C$.) Mark $C$ as true if |
|---|
| | 22 | some member of the stack has sign $= 0$; otherwise, mark $C$ as false. |
|---|
| | 23 | Then, construct a polynomial formula in $a$, $b$ which specifies |
|---|
| | 24 | exactly the true cells (this is always possible). The same technique |
|---|
| | 25 | works if the body of the quantifier is any boolean combination of |
|---|
| | 26 | polynomial equalities and inequalities. |
|---|
| | 27 | |
|---|
| | 28 | Formula simplification is a similar technique. Given a formula which |
|---|
| | 29 | describes a simple set of $\RR^k$ in a complicated way as a boolean |
|---|
| | 30 | combination of polynomial equalities and inequalities, QEPCAD can |
|---|
| | 31 | construct a CAD for the polynomials and recover a simple equivalent |
|---|
| | 32 | formula. |
|---|
| | 33 | |
|---|
| | 34 | Note that while the following documentation is tutorial in nature, and |
|---|
| | 35 | is written for people who may not be familiar with QEPCAD, it is |
|---|
| | 36 | documentation for the \sage interface rather than for QEPCAD. As |
|---|
| | 37 | such, it does not cover several issues that are very important to use |
|---|
| | 38 | QEPCAD efficiently, such as variable ordering, the efficient use of |
|---|
| | 39 | the alternate quantifiers and \code{_root_} expressions, the |
|---|
| | 40 | \code{measure-zero-error} command, etc. For more information on |
|---|
| | 41 | QEPCAD, see the online documentation at |
|---|
| | 42 | \url{http://www.cs.usna.edu/~qepcad/B/QEPCAD.html} and Chris Brown's |
|---|
| | 43 | tutorial handout and slides from |
|---|
| | 44 | \url{http://www.cs.usna.edu/~wcbrown/research/ISSAC04/Tutorial.html}. |
|---|
| | 45 | (Several of the examples in this documentation came from these |
|---|
| | 46 | sources.) |
|---|
| | 47 | |
|---|
| | 48 | The examples below require that the optional qepcad package is installed. |
|---|
| | 49 | |
|---|
| | 50 | QEPCAD can be run in a fully automatic fashion, or interactively. |
|---|
| | 51 | We first demonstrate the automatic use of QEPCAD. |
|---|
| | 52 | |
|---|
| | 53 | Since \sage has no built-in support for quantifiers, this interface |
|---|
| | 54 | provides \code{qepcad_formula} which helps construct quantified |
|---|
| | 55 | formulas in the syntax QEPCAD requires. |
|---|
| | 56 | |
|---|
| | 57 | sage: var('a,b,c,d,x,y,z') |
|---|
| | 58 | (a, b, c, d, x, y, z) |
|---|
| | 59 | sage: qf = qepcad_formula |
|---|
| | 60 | |
|---|
| | 61 | We start with a simple example. Consider an arbitrarily-selected |
|---|
| | 62 | ellipse: |
|---|
| | 63 | |
|---|
| | 64 | sage: ellipse = 3*x^2 + 2*x*y + y^2 - x + y - 7 |
|---|
| | 65 | |
|---|
| | 66 | What is the projection onto the $x$ axis of this ellipse? First we |
|---|
| | 67 | construct a formula asking this question. |
|---|
| | 68 | |
|---|
| | 69 | sage: F = qf.exists(y, ellipse == 0); F |
|---|
| | 70 | (E y)[y^2 + 2 x y + y + 3 x^2 - x - 7 = 0] |
|---|
| | 71 | |
|---|
| | 72 | Then we run qepcad to get the answer. |
|---|
| | 73 | |
|---|
| | 74 | sage: qepcad(F) |
|---|
| | 75 | 8 x^2 - 8 x - 29 <= 0 |
|---|
| | 76 | |
|---|
| | 77 | How about the projection onto the $y$ axis? |
|---|
| | 78 | |
|---|
| | 79 | sage: qepcad(qf.exists(x, ellipse == 0)) |
|---|
| | 80 | 8 y^2 + 16 y - 85 <= 0 |
|---|
| | 81 | |
|---|
| | 82 | QEPCAD deals with more quantifiers than just ``exists'', of course. |
|---|
| | 83 | Besides the standard ``forall'', there are also ``for infinitely |
|---|
| | 84 | many'', ``for all but finitely many'', ``for a connected subset'', and |
|---|
| | 85 | ``for exactly $k$''. The \function{qepcad} documentation has examples |
|---|
| | 86 | of all of these; here we'll just give one example. |
|---|
| | 87 | |
|---|
| | 88 | First we construct a circle: |
|---|
| | 89 | |
|---|
| | 90 | sage: circle = x^2 + y^2 - 3 |
|---|
| | 91 | |
|---|
| | 92 | For what values $k$ does a vertical line $x=k$ intersect the combined |
|---|
| | 93 | figure of the circle and ellipse exactly three times? |
|---|
| | 94 | |
|---|
| | 95 | sage: F = qf.exactly_k(3, y, circle * ellipse == 0); F |
|---|
| | 96 | (X3 y)[(y^2 + x^2 - 3) (y^2 + 2 x y + y + 3 x^2 - x - 7) = 0] |
|---|
| | 97 | sage: qepcad(F) |
|---|
| | 98 | 8 x^2 - 8 x - 29 <= 0 /\ x^2 - 3 <= 0 /\ 8 x^4 - 26 x^2 - 4 x + 13 >= 0 /\ [ 8 x^4 - 26 x^2 - 4 x + 13 = 0 \/ x^2 - 3 = 0 \/ 8 x^2 - 8 x - 29 = 0 ] |
|---|
| | 99 | |
|---|
| | 100 | Here we see that the solutions are among the eight ($4 + 2 + 2$) roots |
|---|
| | 101 | of the three polynomials inside the brackets, but not all of these |
|---|
| | 102 | roots are solutions; the polynomial inequalities outside the brackets |
|---|
| | 103 | are needed to select those roots that are solutions. |
|---|
| | 104 | |
|---|
| | 105 | QEPCAD also supports an extended formula language, where |
|---|
| | 106 | $\mbox{_root_}k \quad P(\bar x, y)$ refers to a particular zero of |
|---|
| | 107 | $P(\bar x, y)$ (viewed as a polynomial in $y$). If there are $n$ roots, |
|---|
| | 108 | then _root_$1$ refers to the least root and _root_$n$ refers to the greatest; |
|---|
| | 109 | also, _root_$-n$ refers to the least root and _root_$-1$ refers to the |
|---|
| | 110 | greatest. |
|---|
| | 111 | |
|---|
| | 112 | This extended language is available both on input and output; see the |
|---|
| | 113 | QEPCAD documentation for more information on how to use this syntax on |
|---|
| | 114 | input. We can request output that's intended to be easy to interpret |
|---|
| | 115 | geometrically; then QEPCAD will use the extended language to produce |
|---|
| | 116 | a solution formula without the selection polynomials. |
|---|
| | 117 | |
|---|
| | 118 | sage: qepcad(F, solution='geometric') |
|---|
| | 119 | x = _root_1 8 x^2 - 8 x - 29 |
|---|
| | 120 | \/ |
|---|
| | 121 | 8 x^4 - 26 x^2 - 4 x + 13 = 0 |
|---|
| | 122 | \/ |
|---|
| | 123 | x = _root_-1 x^2 - 3 |
|---|
| | 124 | |
|---|
| | 125 | We then see that the 6 solutions correspond to the vertical tangent on |
|---|
| | 126 | the left side of the ellipse, the four intersections between the |
|---|
| | 127 | ellipse and the circle, and the vertical tangent on the right side of |
|---|
| | 128 | the circle. |
|---|
| | 129 | |
|---|
| | 130 | Let's do some basic formula simplification and visualization. |
|---|
| | 131 | We'll look at the region which is inside both the ellipse and the circle: |
|---|
| | 132 | |
|---|
| | 133 | sage: F = qf.and_(ellipse < 0, circle < 0); F |
|---|
| | 134 | [y^2 + 2 x y + y + 3 x^2 - x - 7 < 0 /\ y^2 + x^2 - 3 < 0] |
|---|
| | 135 | sage: qepcad(F) |
|---|
| | 136 | y^2 + 2 x y + y + 3 x^2 - x - 7 < 0 /\ y^2 + x^2 - 3 < 0 |
|---|
| | 137 | |
|---|
| | 138 | We get back the same formula we put in. This is not surprising (we |
|---|
| | 139 | started with a pretty simple formula, after all), but it's not very |
|---|
| | 140 | enlightening either. Again, if we ask for a 'geometric' output, then we see |
|---|
| | 141 | an output that lets us understand something about the shape of the solution |
|---|
| | 142 | set. |
|---|
| | 143 | |
|---|
| | 144 | sage: qepcad(F, solution='geometric') |
|---|
| | 145 | [ |
|---|
| | 146 | [ |
|---|
| | 147 | x = _root_-2 8 x^4 - 26 x^2 - 4 x + 13 |
|---|
| | 148 | \/ |
|---|
| | 149 | x = _root_2 8 x^4 - 26 x^2 - 4 x + 13 |
|---|
| | 150 | \/ |
|---|
| | 151 | 8 x^4 - 26 x^2 - 4 x + 13 < 0 |
|---|
| | 152 | ] |
|---|
| | 153 | /\ |
|---|
| | 154 | y^2 + 2 x y + y + 3 x^2 - x - 7 < 0 |
|---|
| | 155 | /\ |
|---|
| | 156 | y^2 + x^2 - 3 < 0 |
|---|
| | 157 | ] |
|---|
| | 158 | \/ |
|---|
| | 159 | [ |
|---|
| | 160 | x > _root_2 8 x^4 - 26 x^2 - 4 x + 13 |
|---|
| | 161 | /\ |
|---|
| | 162 | x < _root_-2 8 x^4 - 26 x^2 - 4 x + 13 |
|---|
| | 163 | /\ |
|---|
| | 164 | y^2 + x^2 - 3 < 0 |
|---|
| | 165 | ] |
|---|
| | 166 | |
|---|
| | 167 | There's another reason to prefer output using _root_ expressions; not |
|---|
| | 168 | only does it sometimes give added insight into the geometric |
|---|
| | 169 | structure, it also can be more efficient to construct. Consider this |
|---|
| | 170 | formula for the projection of a particular semicircle onto the $x$ |
|---|
| | 171 | axis: |
|---|
| | 172 | |
|---|
| | 173 | sage: F = qf.exists(y, qf.and_(circle == 0, x + y > 0)); F |
|---|
| | 174 | (E y)[y^2 + x^2 - 3 = 0 /\ y + x > 0] |
|---|
| | 175 | sage: qepcad(F) |
|---|
| | 176 | x^2 - 3 <= 0 /\ [ x > 0 \/ 2 x^2 - 3 < 0 ] |
|---|
| | 177 | |
|---|
| | 178 | Here, the formula $x > 0$ had to be introduced in order to get a |
|---|
| | 179 | solution formula; the original CAD of F did not include the |
|---|
| | 180 | polynomial $x$. To avoid having QEPCAD do the extra work to come up |
|---|
| | 181 | with a solution formula, we can tell it to use the extended language; |
|---|
| | 182 | it's always possible to construct a solution formula in the extended |
|---|
| | 183 | language without introducing new polynomials. |
|---|
| | 184 | |
|---|
| | 185 | sage: qepcad(F, solution='extended') |
|---|
| | 186 | x^2 - 3 <= 0 /\ x > _root_1 2 x^2 - 3 |
|---|
| | 187 | |
|---|
| | 188 | Up to this point, all the output we've seen has basically been in the |
|---|
| | 189 | form of strings; there is no support (yet) for parsing these outputs |
|---|
| | 190 | back into \sage polynomials (partly because \sage does not yet have |
|---|
| | 191 | support for symbolic conjunctions and disjunctions). The function |
|---|
| | 192 | \function{qepcad} supports three more output types that give numbers which |
|---|
| | 193 | can be manipulated in \sage: any-point, all-points, and cell-points. |
|---|
| | 194 | |
|---|
| | 195 | These output types give dictionaries mapping variable names to values. |
|---|
| | 196 | With any-point, \function{qepcad} either produces a single dictionary |
|---|
| | 197 | specifying a point where the formula is true, or raises an exception |
|---|
| | 198 | if the formula is false everywhere. With all-points, |
|---|
| | 199 | \function{qepcad} either produces a list of dictionaries for all |
|---|
| | 200 | points where the formula is true, or raises an exception if the |
|---|
| | 201 | formula is true on infinitely many points. With cell-points, |
|---|
| | 202 | \function{qepcad} produces a list of dictionaries with one point for |
|---|
| | 203 | each cell where the formula is true. (This means you will have at least |
|---|
| | 204 | one point in each connected component of the solution, although you will |
|---|
| | 205 | often have many more points than that.) |
|---|
| | 206 | |
|---|
| | 207 | Let's revisit some of the above examples and get some points to play |
|---|
| | 208 | with. We'll start by finding a point on our ellipse. |
|---|
| | 209 | |
|---|
| | 210 | sage: p = qepcad(ellipse == 0, solution='any-point'); p |
|---|
| | 211 | {'y': [0.96850196850295267 .. 0.96850196850295279], 'x': [-1.4685019685029528 .. -1.4685019685029525]} |
|---|
| | 212 | |
|---|
| | 213 | (Note that despite appearances, these are really exact numbers.) |
|---|
| | 214 | |
|---|
| | 215 | We can verify that this point is a solution. To do so, we create |
|---|
| | 216 | a copy of ellipse as a polynomial over QQ (instead of a symbolic |
|---|
| | 217 | expresision). |
|---|
| | 218 | |
|---|
| | 219 | sage: pellipse = QQ['x,y'](ellipse) |
|---|
| | 220 | sage: pellipse(**p) == 0 |
|---|
| | 221 | True |
|---|
| | 222 | |
|---|
| | 223 | For cell-points, let's look at points \emph{not} on the ellipse. |
|---|
| | 224 | |
|---|
| | 225 | sage: pts = qepcad(ellipse != 0, solution='cell-points'); pts |
|---|
| | 226 | [{'y': 0, 'x': 4}, {'y': 1, 'x': [2.4685019685029523 .. 2.4685019685029528]}, {'y': -9, 'x': [2.4685019685029523 .. 2.4685019685029528]}, {'y': 9, 'x': 1/2}, {'y': -1, 'x': 1/2}, {'y': -5, 'x': 1/2}, {'y': 3, 'x': [-1.4685019685029528 .. -1.4685019685029525]}, {'y': -1, 'x': [-1.4685019685029528 .. -1.4685019685029525]}, {'y': 0, 'x': -3}] |
|---|
| | 227 | |
|---|
| | 228 | For the points here which are in full-dimensional cells, QEPCAD has the |
|---|
| | 229 | freedom to choose rational sample points, and it does so. |
|---|
| | 230 | |
|---|
| | 231 | And, of course, all these points really are not on the ellipse. |
|---|
| | 232 | |
|---|
| | 233 | sage: [pellipse(**p) != 0 for p in pts] |
|---|
| | 234 | [True, True, True, True, True, True, True, True, True] |
|---|
| | 235 | |
|---|
| | 236 | Finally, for all-points, let's look again at finding vertical lines that |
|---|
| | 237 | intersect the union of the circle and the ellipse exactly three times. |
|---|
| | 238 | |
|---|
| | 239 | sage: F = qf.exactly_k(3, y, circle * ellipse == 0); F |
|---|
| | 240 | (X3 y)[(y^2 + x^2 - 3) (y^2 + 2 x y + y + 3 x^2 - x - 7) = 0] |
|---|
| | 241 | sage: pts = qepcad(F, solution='all-points'); pts |
|---|
| | 242 | [{'x': [1.7320508075688771 .. 1.7320508075688775]}, {'x': [1.7310549134625334 .. 1.7310549134625338]}, {'x': [0.67891138420800389 .. 0.67891138420800401]}, {'x': [-0.94177273774171678 .. -0.94177273774171665]}, {'x': [-1.4681935599288210 .. -1.4681935599288207]}, {'x': [-1.4685019685029528 .. -1.4685019685029525]}] |
|---|
| | 243 | |
|---|
| | 244 | Since $y$ is bound by the quantifier, the solutions only refer to $x$. |
|---|
| | 245 | |
|---|
| | 246 | We can substitute one of these solutions into the original equation: |
|---|
| | 247 | |
|---|
| | 248 | sage: pt = pts[0] |
|---|
| | 249 | sage: pcombo = QQ['x,y'](circle * ellipse) |
|---|
| | 250 | sage: intersections = pcombo(y=polygen(AA, 'y'), **pt); intersections |
|---|
| | 251 | y^4 + [4.4641016151377543 .. 4.4641016151377553]*y^3 + [0.26794919243112269 .. 0.26794919243112276]*y^2 |
|---|
| | 252 | |
|---|
| | 253 | and verify that it does have three roots: |
|---|
| | 254 | |
|---|
| | 255 | sage: intersections.roots() |
|---|
| | 256 | [([-4.4032490056009586 .. -4.4032490056009576], 1), ([-0.060852609536796533 .. -0.060852609536796525], 1), ([0.00000000000000000 .. 0.00000000000000000], 2)] |
|---|
| | 257 | |
|---|
| | 258 | Let's check all six solutions. |
|---|
| | 259 | |
|---|
| | 260 | sage: [len(pcombo(y=polygen(AA, 'y'), **p).roots()) for p in pts] |
|---|
| | 261 | [3, 3, 3, 3, 3, 3] |
|---|
| | 262 | |
|---|
| | 263 | We said earlier that we can run QEPCAD either automatically or |
|---|
| | 264 | interactively. Now that we've discussed the automatic modes, let's turn |
|---|
| | 265 | to interactive uses. |
|---|
| | 266 | |
|---|
| | 267 | If the \function{qepcad} function is passed \code{interact=True}, then |
|---|
| | 268 | instead of returning a result, it returns an object of class |
|---|
| | 269 | \class{Qepcad} representing a running instance of QEPCAD that you can |
|---|
| | 270 | interact with. For example: |
|---|
| | 271 | |
|---|
| | 272 | sage: qe = qepcad(qf.forall(x, x^2 + b*x + c > 0), interact=True); qe |
|---|
| | 273 | QEPCAD object in phase 'Before Normalization' |
|---|
| | 274 | |
|---|
| | 275 | This object is a fairly thin wrapper over QEPCAD; most QEPCAD commands |
|---|
| | 276 | are available as methods on the \class{Qepcad} object. Given a |
|---|
| | 277 | \class{Qepcad} object \var{qe}, you can type \code{qe.[tab]} to see |
|---|
| | 278 | the available QEPCAD commands; to see the documentation for an |
|---|
| | 279 | individual QEPCAD command, for example \code{d_setting}, you can type |
|---|
| | 280 | \code{qe.d_setting?}. (In QEPCAD, this command is called |
|---|
| | 281 | \code{d-setting}; we systematically replace hyphens with underscores |
|---|
| | 282 | for this interface.) |
|---|
| | 283 | |
|---|
| | 284 | The execution of QEPCAD is divided into four phases; most commands are |
|---|
| | 285 | not available during all phases. We saw above that QEPCAD starts out |
|---|
| | 286 | in phase `Before Normalization'; we see that the \code{d_cell} command |
|---|
| | 287 | is not available in this phase: |
|---|
| | 288 | |
|---|
| | 289 | sage: qe.d_cell() |
|---|
| | 290 | Error GETCID: This command is not active here. |
|---|
| | 291 | |
|---|
| | 292 | We will focus here on the fourth (and last) phase, `Before Solution', |
|---|
| | 293 | because this interface has special support for some operations in this |
|---|
| | 294 | phase. Consult the QEPCAD documentation for information on the other |
|---|
| | 295 | phases. |
|---|
| | 296 | |
|---|
| | 297 | We can tell QEPCAD to finish off the current phase and move to the |
|---|
| | 298 | next with its \code{go} command. (There is also the \code{step} |
|---|
| | 299 | command, which partially completes a phase for phases that have |
|---|
| | 300 | multiple steps, and the \code{finish} command, which runs QEPCAD to |
|---|
| | 301 | completion.) |
|---|
| | 302 | |
|---|
| | 303 | sage: qe.go() |
|---|
| | 304 | QEPCAD object has moved to phase 'Before Projection (x)' |
|---|
| | 305 | sage: qe.go() |
|---|
| | 306 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 307 | sage: qe.go() |
|---|
| | 308 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 309 | |
|---|
| | 310 | Note that the \class{Qepcad} object returns the new phase whenever the |
|---|
| | 311 | phase changes, as a convenience for interactive use; except that when |
|---|
| | 312 | the new phase is `EXITED', the solution formula printed by QEPCAD is |
|---|
| | 313 | returned instead. |
|---|
| | 314 | |
|---|
| | 315 | sage: qe.go() |
|---|
| | 316 | 4 c - b^2 > 0 |
|---|
| | 317 | sage: qe |
|---|
| | 318 | QEPCAD object in phase 'EXITED' |
|---|
| | 319 | |
|---|
| | 320 | Let's pick a nice, simple example, return to phase 4, and explore the |
|---|
| | 321 | resulting \var{qe} object. |
|---|
| | 322 | |
|---|
| | 323 | sage: qe = qepcad(circle == 0, interact=True); qe |
|---|
| | 324 | QEPCAD object in phase 'Before Normalization' |
|---|
| | 325 | sage: qe.go(); qe.go(); qe.go() |
|---|
| | 326 | QEPCAD object has moved to phase 'Before Projection (y)' |
|---|
| | 327 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 328 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 329 | |
|---|
| | 330 | We said before that QEPCAD creates ``cylindrical algebraic |
|---|
| | 331 | decompositions''; since we have a bivariate polynomial, we get |
|---|
| | 332 | decompositions of $\RR^0$, $\RR^1$, and $\RR^2$. In this case, where |
|---|
| | 333 | our example is a circle of radius $\sqrt{3}$ centered on the origin, |
|---|
| | 334 | these decompositions are as follows: |
|---|
| | 335 | |
|---|
| | 336 | The decomposition of $\RR^0$ is trivial (of course). The |
|---|
| | 337 | decomposition of $\RR^1$ has five cells: $x < -\sqrt{3}$, $x = |
|---|
| | 338 | -\sqrt{3}$, $-\sqrt{3} < x < \sqrt{3}$, $x = \sqrt{3}$, and $x > |
|---|
| | 339 | \sqrt{3}$. These five cells comprise the \emph{stack} over the single |
|---|
| | 340 | cell in the trivial decomposition of $\RR^0$. |
|---|
| | 341 | |
|---|
| | 342 | These five cells give rise to five stacks in $\RR^2$. The first and |
|---|
| | 343 | fifth stack have just one cell apiece. The second and fourth stacks |
|---|
| | 344 | have three cells: $y < 0$, $y = 0$, and $y > 0$. The third stack has |
|---|
| | 345 | five cells: below the circle, the lower semicircle, the interior of |
|---|
| | 346 | the circle, the upper semicircle, and above the circle. |
|---|
| | 347 | |
|---|
| | 348 | QEPCAD (and this QEPCAD interface) number the cells in a stack |
|---|
| | 349 | starting with 1. Each cell has an \emph{index}, which is a tuple of |
|---|
| | 350 | integers describing the path to the cell in the tree of all cells. |
|---|
| | 351 | For example, the cell ``below the circle'' has index (3,1) (the first |
|---|
| | 352 | cell in the stack over the third cell of $\RR^1$) and the interior of |
|---|
| | 353 | the circle has index (3,3). |
|---|
| | 354 | |
|---|
| | 355 | We can view these cells with the QEPCAD command \code{d_cell}. For |
|---|
| | 356 | instance, let's look at the cell for the upper semicircle: |
|---|
| | 357 | |
|---|
| | 358 | sage: qe.d_cell(3, 4) |
|---|
| | 359 | ---------- Information about the cell (3,4) ---------- |
|---|
| | 360 | Level : 2 |
|---|
| | 361 | Dimension : 1 |
|---|
| | 362 | Number of children : 0 |
|---|
| | 363 | Truth value : T by trial evaluation. |
|---|
| | 364 | Degrees after substitution : Not known yet or No polynomial. |
|---|
| | 365 | Multiplicities : ((1,1)) |
|---|
| | 366 | Signs of Projection Factors |
|---|
| | 367 | Level 1 : (-) |
|---|
| | 368 | Level 2 : (0) |
|---|
| | 369 | ---------- Sample point ---------- |
|---|
| | 370 | The sample point is in a PRIMITIVE representation. |
|---|
| | 371 | <BLANKLINE> |
|---|
| | 372 | alpha = the unique root of x^2 - 3 between 0 and 4 |
|---|
| | 373 | = 1.7320508076- |
|---|
| | 374 | <BLANKLINE> |
|---|
| | 375 | Coordinate 1 = 0 |
|---|
| | 376 | = 0.0000000000 |
|---|
| | 377 | Coordinate 2 = alpha |
|---|
| | 378 | = 1.7320508076- |
|---|
| | 379 | ---------------------------------------------------- |
|---|
| | 380 | |
|---|
| | 381 | We see that, the level of this cell is 2, meaning that it is part of |
|---|
| | 382 | the decomposition of $\RR^2$. The dimension is 1, meaning that the |
|---|
| | 383 | cell is homeomorphic to a line (rather than a plane or a point). The |
|---|
| | 384 | sample point gives the coordinates of one point in the cell, both |
|---|
| | 385 | symbolically and numerically. |
|---|
| | 386 | |
|---|
| | 387 | For programmatic access to cells, we've defined a \sage wrapper class |
|---|
| | 388 | \class{QepcadCell}. These cells can be created with the \method{cell} |
|---|
| | 389 | method; for example: |
|---|
| | 390 | |
|---|
| | 391 | sage: c = qe.cell(3, 4); c |
|---|
| | 392 | QEPCAD cell (3, 4) |
|---|
| | 393 | |
|---|
| | 394 | A \class{QepcadCell} has accessor methods for the important state held |
|---|
| | 395 | within a cell. For instance: |
|---|
| | 396 | |
|---|
| | 397 | sage: c.level() |
|---|
| | 398 | 2 |
|---|
| | 399 | sage: c.index() |
|---|
| | 400 | (3, 4) |
|---|
| | 401 | sage: qe.cell(3).number_of_children() |
|---|
| | 402 | 5 |
|---|
| | 403 | sage: len(qe.cell(3)) |
|---|
| | 404 | 5 |
|---|
| | 405 | |
|---|
| | 406 | One particularly useful thing we can get from a cell is its sample point, |
|---|
| | 407 | as \sage algebraic real numbers. |
|---|
| | 408 | |
|---|
| | 409 | sage: c.sample_point() |
|---|
| | 410 | (0, [1.7320508075688771 .. 1.7320508075688775]) |
|---|
| | 411 | sage: c.sample_point_dict() |
|---|
| | 412 | {'y': [1.7320508075688771 .. 1.7320508075688775], 'x': 0} |
|---|
| | 413 | |
|---|
| | 414 | We've seen that we can get cells using the \method{cell} method. |
|---|
| | 415 | There are several QEPCAD commands that print lists of cells; we can |
|---|
| | 416 | also get cells using the \method{make_cells} method, passing it the |
|---|
| | 417 | output of one of these commands. |
|---|
| | 418 | |
|---|
| | 419 | sage: qe.make_cells(qe.d_true_cells()) |
|---|
| | 420 | [QEPCAD cell (4, 2), QEPCAD cell (3, 4), QEPCAD cell (3, 2), QEPCAD cell (2, 2)] |
|---|
| | 421 | |
|---|
| | 422 | Also, the cells in the stack over a given cell can be accessed using |
|---|
| | 423 | array subscripting or iteration. (Remember that cells in a stack are |
|---|
| | 424 | numbered starting with one; we preserve this convention in the |
|---|
| | 425 | array-subscripting syntax.) |
|---|
| | 426 | |
|---|
| | 427 | sage: c = qe.cell(3) |
|---|
| | 428 | sage: c[1] |
|---|
| | 429 | QEPCAD cell (3, 1) |
|---|
| | 430 | sage: [c2 for c2 in c] |
|---|
| | 431 | [QEPCAD cell (3, 1), QEPCAD cell (3, 2), QEPCAD cell (3, 3), QEPCAD cell (3, 4), QEPCAD cell (3, 5)] |
|---|
| | 432 | |
|---|
| | 433 | We can do one more thing with a cell: we can set its truth value. |
|---|
| | 434 | Once the truth values of the cells have been set, we can get QEPCAD to |
|---|
| | 435 | produce a formula which is true in exactly the cells we have selected. |
|---|
| | 436 | This is useful if QEPCAD's quantifier language is insufficient to |
|---|
| | 437 | express your problem. |
|---|
| | 438 | |
|---|
| | 439 | For example, consider again our combined figure of the circle and the |
|---|
| | 440 | ellipse. Suppose you want to find all vertical lines that intersect |
|---|
| | 441 | the circle twice, and also intersect the ellipse twice. The vertical |
|---|
| | 442 | lines that intersect the circle twice can be found by simplifying: |
|---|
| | 443 | |
|---|
| | 444 | sage: F = qf.exactly_k(2, y, circle == 0); F |
|---|
| | 445 | (X2 y)[y^2 + x^2 - 3 = 0] |
|---|
| | 446 | |
|---|
| | 447 | and the vertical lines that intersect the ellipse twice are expressed by: |
|---|
| | 448 | |
|---|
| | 449 | sage: G = qf.exactly_k(2, y, ellipse == 0); G |
|---|
| | 450 | (X2 y)[y^2 + 2 x y + y + 3 x^2 - x - 7 = 0] |
|---|
| | 451 | |
|---|
| | 452 | and the lines that intersect both figures would be: |
|---|
| | 453 | |
|---|
| | 454 | sage: qf.and_(F, G) |
|---|
| | 455 | Traceback (most recent call last): |
|---|
| | 456 | ... |
|---|
| | 457 | ValueError: QEPCAD formulas must be in prenex (quantifiers outermost) form |
|---|
| | 458 | |
|---|
| | 459 | ...except that QEPCAD does not support formulas like this; in QEPCAD |
|---|
| | 460 | input, all logical connectives must be inside all quantifiers. |
|---|
| | 461 | |
|---|
| | 462 | Instead, we can get QEPCAD to construct a CAD for our combined figure |
|---|
| | 463 | and set the truth values ourselves. (The exact formula we use doesn't |
|---|
| | 464 | matter, since we're going to replace the truth values in the cells; we |
|---|
| | 465 | just need to use a formula that uses both polynomials.) |
|---|
| | 466 | |
|---|
| | 467 | sage: qe = qepcad(qf.and_(ellipse == 0, circle == 0), interact=True) |
|---|
| | 468 | sage: qe.go(); qe.go(); qe.go() |
|---|
| | 469 | QEPCAD object has moved to phase 'Before Projection (y)' |
|---|
| | 470 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 471 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 472 | |
|---|
| | 473 | Now we want to find all cells $c$ in the decomposition of $\RR^1$ such |
|---|
| | 474 | that the stack over $c$ contains exactly two cells on the ellipse, and |
|---|
| | 475 | also contains exactly two cells on the circle. |
|---|
| | 476 | |
|---|
| | 477 | Our input polynomials are ``level-2 projection factors'', we see: |
|---|
| | 478 | |
|---|
| | 479 | sage: qe.d_proj_factors() |
|---|
| | 480 | P_1,1 = fac(J_1,1) = fac(dis(A_2,1)) |
|---|
| | 481 | = 8 x^2 - 8 x - 29 |
|---|
| | 482 | P_1,2 = fac(J_1,2) = fac(dis(A_2,2)) |
|---|
| | 483 | = x^2 - 3 |
|---|
| | 484 | P_1,3 = fac(J_1,3) = fac(res(A_2,1|A_2,2)) |
|---|
| | 485 | = 8 x^4 - 26 x^2 - 4 x + 13 |
|---|
| | 486 | A_2,1 = input |
|---|
| | 487 | = y^2 + 2 x y + y + 3 x^2 - x - 7 |
|---|
| | 488 | A_2,2 = input |
|---|
| | 489 | = y^2 + x^2 - 3 |
|---|
| | 490 | |
|---|
| | 491 | so we can test whether a cell is on the ellipse by checking that the |
|---|
| | 492 | sign of the corresponding projection factor is 0 in our cell. |
|---|
| | 493 | For instance, the cell (12,2) is on the ellipse: |
|---|
| | 494 | |
|---|
| | 495 | sage: qe.cell(12,2).signs()[1][0] |
|---|
| | 496 | 0 |
|---|
| | 497 | |
|---|
| | 498 | So we can update the truth values as desired like this: |
|---|
| | 499 | |
|---|
| | 500 | sage: for c in qe.cell(): |
|---|
| | 501 | ... count_ellipse = 0 |
|---|
| | 502 | ... count_circle = 0 |
|---|
| | 503 | ... for c2 in c: |
|---|
| | 504 | ... count_ellipse += (c2.signs()[1][0] == 0) |
|---|
| | 505 | ... count_circle += (c2.signs()[1][1] == 0) |
|---|
| | 506 | ... c.set_truth(count_ellipse == 2 and count_circle == 2) |
|---|
| | 507 | |
|---|
| | 508 | and then we can get our desired solution formula. (The 'G' stands for |
|---|
| | 509 | 'geometric', and gives solutions using the same rules as |
|---|
| | 510 | \code{solution='geometric'} described above.) |
|---|
| | 511 | |
|---|
| | 512 | sage: qe.solution_extension('G') |
|---|
| | 513 | 8 x^2 - 8 x - 29 < 0 |
|---|
| | 514 | /\ |
|---|
| | 515 | x^2 - 3 < 0 |
|---|
| | 516 | |
|---|
| | 517 | AUTHORS: |
|---|
| | 518 | -- Carl Witty (2008-03): initial version |
|---|
| | 519 | """ |
|---|
| | 520 | |
|---|
| | 521 | ########################################################################## |
|---|
| | 522 | # |
|---|
| | 523 | # Copyright (C) 2008 Carl Witty <Carl.Witty@gmail.com> |
|---|
| | 524 | # |
|---|
| | 525 | # Distributed under the terms of the GNU General Public License (GPL) |
|---|
| | 526 | # |
|---|
| | 527 | # http://www.gnu.org/licenses/ |
|---|
| | 528 | # |
|---|
| | 529 | ########################################################################## |
|---|
| | 530 | |
|---|
| | 531 | from __future__ import with_statement |
|---|
| | 532 | |
|---|
| | 533 | import sage.misc.misc |
|---|
| | 534 | import pexpect |
|---|
| | 535 | import re |
|---|
| | 536 | import sys |
|---|
| | 537 | |
|---|
| | 538 | from sage.misc.flatten import flatten |
|---|
| | 539 | from sage.misc.sage_eval import sage_eval |
|---|
| | 540 | from sage.misc.preparser import implicit_mul |
|---|
| | 541 | |
|---|
| | 542 | from expect import Expect, ExpectFunction, AsciiArtString |
|---|
| | 543 | |
|---|
| | 544 | def _qepcad_cmd(memcells=None): |
|---|
| | 545 | r""" |
|---|
| | 546 | Construct a QEPCAD command line. Optionally set the |
|---|
| | 547 | number of memory cells to use. |
|---|
| | 548 | |
|---|
| | 549 | EXAMPLES: |
|---|
| | 550 | sage: from sage.interfaces.qepcad import _qepcad_cmd |
|---|
| | 551 | sage: _qepcad_cmd() |
|---|
| | 552 | 'env qe=.../local/ qepcad ' |
|---|
| | 553 | sage: _qepcad_cmd(memcells=8000000) |
|---|
| | 554 | 'env qe=.../local/ qepcad +N8000000' |
|---|
| | 555 | """ |
|---|
| | 556 | if memcells is not None: |
|---|
| | 557 | memcells_arg = '+N%s' % memcells |
|---|
| | 558 | else: |
|---|
| | 559 | memcells_arg = '' |
|---|
| | 560 | return "env qe=%s qepcad %s"%(sage.misc.misc.SAGE_LOCAL, memcells_arg) |
|---|
| | 561 | |
|---|
| | 562 | _command_info_cache = None |
|---|
| | 563 | |
|---|
| | 564 | def _update_command_info(): |
|---|
| | 565 | r""" |
|---|
| | 566 | Read the file \file{qepcad.help} to find the list of commands |
|---|
| | 567 | supported by QEPCAD. Used for tab-completion and documentation. |
|---|
| | 568 | |
|---|
| | 569 | EXAMPLES: |
|---|
| | 570 | sage: from sage.interfaces.qepcad import _update_command_info, _command_info_cache |
|---|
| | 571 | sage: _update_command_info() # optional |
|---|
| | 572 | sage: _command_info_cache['approx_precision'] # optional |
|---|
| | 573 | ('46', 'abcde', 'm', 'approx-precision N\n\nApproximate algeraic numbers to N decimal places.\n', None) |
|---|
| | 574 | """ |
|---|
| | 575 | global _command_info_cache |
|---|
| | 576 | if _command_info_cache is not None: |
|---|
| | 577 | return |
|---|
| | 578 | |
|---|
| | 579 | cache = {} |
|---|
| | 580 | |
|---|
| | 581 | with open('%s/bin/qepcad.help'%sage.misc.misc.SAGE_LOCAL) as help: |
|---|
| | 582 | assert(help.readline().strip() == '@') |
|---|
| | 583 | |
|---|
| | 584 | while True: |
|---|
| | 585 | cmd_line = help.readline() |
|---|
| | 586 | while len(cmd_line.strip()) == 0: |
|---|
| | 587 | cmd_line = help.readline() |
|---|
| | 588 | cmd_line = cmd_line.strip() |
|---|
| | 589 | if cmd_line == '@@@': |
|---|
| | 590 | break |
|---|
| | 591 | |
|---|
| | 592 | (cmd, id, phases, kind) = cmd_line.split() |
|---|
| | 593 | assert(help.readline().strip() == '@') |
|---|
| | 594 | |
|---|
| | 595 | help_text = '' |
|---|
| | 596 | help_line = help.readline() |
|---|
| | 597 | while help_line.strip() != '@': |
|---|
| | 598 | help_text += help_line |
|---|
| | 599 | help_line = help.readline() |
|---|
| | 600 | |
|---|
| | 601 | # I went through qepcad.help and picked out commands that |
|---|
| | 602 | # I thought might be worth a little extra tweaking... |
|---|
| | 603 | special = None |
|---|
| | 604 | |
|---|
| | 605 | # These commands have been tweaked. |
|---|
| | 606 | if cmd in ['d-all-cells-in-subtree', 'd-cell', 'd-pcad', |
|---|
| | 607 | 'd-pscad', 'd-stack', 'manual-choose-cell']: |
|---|
| | 608 | special = 'cell' |
|---|
| | 609 | if cmd in ['ipfzt', 'rational-sample', 'triv-convert', 'use-db', |
|---|
| | 610 | 'use-selected-cells-cond', 'verbose']: |
|---|
| | 611 | special = 'yn' |
|---|
| | 612 | |
|---|
| | 613 | # The tweaking for these commands has not been implemented yet. |
|---|
| | 614 | if cmd in ['selected-cells-cond']: |
|---|
| | 615 | special = 'formula' |
|---|
| | 616 | if cmd in ['ch-pivot', 'rem-pf', 'rem-pj']: |
|---|
| | 617 | special = 'i,j' |
|---|
| | 618 | if cmd in ['d-2d-cad', 'set-truth-value', 'solution-extension']: |
|---|
| | 619 | special = 'interactive' |
|---|
| | 620 | if cmd in ['p-2d-cad', 'trace-alg', 'trace-data']: |
|---|
| | 621 | special = 'optional' |
|---|
| | 622 | |
|---|
| | 623 | cmd = cmd.replace('-', '_') |
|---|
| | 624 | |
|---|
| | 625 | cache[cmd] = (id, phases, kind, help_text, special) |
|---|
| | 626 | |
|---|
| | 627 | _command_info_cache = cache |
|---|
| | 628 | |
|---|
| | 629 | _rewrote_qepcadrc = False |
|---|
| | 630 | def _rewrite_qepcadrc(): |
|---|
| | 631 | r""" |
|---|
| | 632 | Write the file \file{default.qepcadrc} to specify a path to Singular |
|---|
| | 633 | (which QEPCAD uses for Gr\"obner basis computations). |
|---|
| | 634 | |
|---|
| | 635 | EXAMPLES: |
|---|
| | 636 | sage: from sage.interfaces.qepcad import _rewrite_qepcadrc |
|---|
| | 637 | sage: _rewrite_qepcadrc() |
|---|
| | 638 | sage: from sage.misc.misc import SAGE_LOCAL |
|---|
| | 639 | sage: open('%s/default.qepcadrc'%SAGE_LOCAL).readlines()[-1] |
|---|
| | 640 | 'SINGULAR .../local//bin' |
|---|
| | 641 | """ |
|---|
| | 642 | global _rewrote_qepcadrc |
|---|
| | 643 | if _rewrote_qepcadrc: return |
|---|
| | 644 | |
|---|
| | 645 | SL = sage.misc.misc.SAGE_LOCAL |
|---|
| | 646 | fn = '%s/default.qepcadrc'%SL |
|---|
| | 647 | text = \ |
|---|
| | 648 | """# THIS FILE IS AUTOMATICALLY GENERATED -- DO NOT EDIT |
|---|
| | 649 | |
|---|
| | 650 | ##################################################### |
|---|
| | 651 | # QEPCAD rc file. |
|---|
| | 652 | # This file allows for some customization of QEPCADB. |
|---|
| | 653 | # Right now, the ability to give a path to Singular, |
|---|
| | 654 | # so that it gets used for some computer algebra |
|---|
| | 655 | # computations is the only feature. |
|---|
| | 656 | ##################################################### |
|---|
| | 657 | SINGULAR %s/bin""" % SL |
|---|
| | 658 | |
|---|
| | 659 | open(fn, 'w').write(text) |
|---|
| | 660 | |
|---|
| | 661 | # QEPCAD does not have a typical "computer algebra system" interaction |
|---|
| | 662 | # model. Instead, you run QEPCAD once for each problem you wish to solve, |
|---|
| | 663 | # then interact with it while you solve that problem. |
|---|
| | 664 | |
|---|
| | 665 | # For this reason, most of the methods on the Expect class are |
|---|
| | 666 | # inapplicable. To avoid confusing the user with useless commands |
|---|
| | 667 | # in tab completion, we split the control into two classes: |
|---|
| | 668 | # Qepcad_expect is a subclass of Expect, and is never seen by the user; |
|---|
| | 669 | # Qepcad is a wrapper for Qepcad_expect, and is what the user interacts with. |
|---|
| | 670 | |
|---|
| | 671 | class Qepcad_expect(Expect): |
|---|
| | 672 | r""" |
|---|
| | 673 | The low-level wrapper for QEPCAD. |
|---|
| | 674 | """ |
|---|
| | 675 | def __init__(self, memcells=None, |
|---|
| | 676 | maxread=100000, |
|---|
| | 677 | logfile=None, |
|---|
| | 678 | server=None): |
|---|
| | 679 | r""" |
|---|
| | 680 | Initialize a low-level wrapper for QEPCAD. You can specify |
|---|
| | 681 | the number of memory cells that QEPCAD allocates on startup |
|---|
| | 682 | (which controls the maximum problem size QEPCAD can handle), |
|---|
| | 683 | and specify a logfile. You can also specify a server, and the |
|---|
| | 684 | interface will run QEPCAD on that server, using ssh. (UNTESTED) |
|---|
| | 685 | |
|---|
| | 686 | EXAMPLES: |
|---|
| | 687 | sage: from sage.interfaces.qepcad import Qepcad_expect |
|---|
| | 688 | sage: Qepcad_expect(memcells=100000, logfile=sys.stdout) |
|---|
| | 689 | Qepcad |
|---|
| | 690 | """ |
|---|
| | 691 | _rewrite_qepcadrc() |
|---|
| | 692 | Expect.__init__(self, |
|---|
| | 693 | name="QEPCAD", |
|---|
| | 694 | # yuck: when QEPCAD first starts, |
|---|
| | 695 | # it doesn't give prompts |
|---|
| | 696 | prompt="\nEnter an .*:\r", |
|---|
| | 697 | command=_qepcad_cmd(memcells), |
|---|
| | 698 | maxread=maxread, |
|---|
| | 699 | server=server, |
|---|
| | 700 | restart_on_ctrlc=False, |
|---|
| | 701 | verbose_start=False, |
|---|
| | 702 | logfile=logfile) |
|---|
| | 703 | |
|---|
| | 704 | class Qepcad: |
|---|
| | 705 | r""" |
|---|
| | 706 | The wrapper for QEPCAD. |
|---|
| | 707 | """ |
|---|
| | 708 | |
|---|
| | 709 | def __init__(self, formula, |
|---|
| | 710 | vars=None, logfile=None, verbose=False, |
|---|
| | 711 | memcells=None, server=None): |
|---|
| | 712 | r""" |
|---|
| | 713 | Construct a QEPCAD wrapper object. Requires a formula, which |
|---|
| | 714 | may be a \class{qformula} as returned by the methods of |
|---|
| | 715 | \code{qepcad_formula}, a symbolic equality or inequality, a |
|---|
| | 716 | polynomial $p$ (meaning $p = 0$), or a string, which is passed |
|---|
| | 717 | straight to QEPCAD. |
|---|
| | 718 | |
|---|
| | 719 | \var{vars} specifies the variables to use; this gives the variable |
|---|
| | 720 | ordering, which may be very important. If \var{formula} is |
|---|
| | 721 | given as a string, then \var{vars} is required; otherwise, |
|---|
| | 722 | if \var{vars} is omitted, then a default ordering is used |
|---|
| | 723 | (alphabetical ordering for the free variables). |
|---|
| | 724 | |
|---|
| | 725 | A logfile can be specified with \var{logfile}. |
|---|
| | 726 | If \code{verbose=True} is given, then the logfile is automatically |
|---|
| | 727 | set to \code{sys.stdout}, so all QEPCAD interaction is echoed to |
|---|
| | 728 | the terminal. |
|---|
| | 729 | |
|---|
| | 730 | You can set the amount of memory that QEPCAD allocates with |
|---|
| | 731 | \var{memcells}, and you can use \var{server} to run QEPCAD on |
|---|
| | 732 | another machine using ssh. (UNTESTED) |
|---|
| | 733 | |
|---|
| | 734 | Usually you will not call this directly, but use \function{qepcad} |
|---|
| | 735 | to do so. Check the \function{qepcad} documentation for more |
|---|
| | 736 | information. |
|---|
| | 737 | |
|---|
| | 738 | EXAMPLES: |
|---|
| | 739 | sage: from sage.interfaces.qepcad import Qepcad |
|---|
| | 740 | sage: Qepcad(x^2 - 1 == 0) # optional |
|---|
| | 741 | QEPCAD object in phase 'Before Normalization' |
|---|
| | 742 | """ |
|---|
| | 743 | self._cell_cache = {} |
|---|
| | 744 | |
|---|
| | 745 | if verbose: |
|---|
| | 746 | logfile=sys.stdout |
|---|
| | 747 | |
|---|
| | 748 | varlist = None |
|---|
| | 749 | if vars is not None: |
|---|
| | 750 | if isinstance(vars, str): |
|---|
| | 751 | varlist = vars.strip('()').split(',') |
|---|
| | 752 | else: |
|---|
| | 753 | varlist = [str(v) for v in vars] |
|---|
| | 754 | |
|---|
| | 755 | if isinstance(formula, str): |
|---|
| | 756 | if varlist is None: |
|---|
| | 757 | raise ValueError, "vars must be specified if formula is a string" |
|---|
| | 758 | |
|---|
| | 759 | free_vars = len(varlist) - formula.count('(') |
|---|
| | 760 | else: |
|---|
| | 761 | formula = qepcad_formula.formula(formula) |
|---|
| | 762 | fvars = formula.vars |
|---|
| | 763 | fqvars = formula.qvars |
|---|
| | 764 | if varlist is None: |
|---|
| | 765 | varlist = sorted(fvars) + fqvars |
|---|
| | 766 | else: |
|---|
| | 767 | # Do some error-checking here. Parse the given vars |
|---|
| | 768 | # and ensure they match up with the variables in the formula. |
|---|
| | 769 | if frozenset(varlist) != (fvars | frozenset(fqvars)): |
|---|
| | 770 | raise ValueError, "specified vars don't match vars in formula" |
|---|
| | 771 | if len(fqvars) and varlist[-len(fqvars):] != fqvars: |
|---|
| | 772 | raise ValueError, "specified vars don't match quantified vars" |
|---|
| | 773 | free_vars = len(fvars) |
|---|
| | 774 | formula = repr(formula) |
|---|
| | 775 | |
|---|
| | 776 | self._varlist = varlist |
|---|
| | 777 | self._free_vars = free_vars |
|---|
| | 778 | |
|---|
| | 779 | varlist = [v.replace('_', '') for v in varlist] |
|---|
| | 780 | if len(frozenset(varlist)) != len(varlist): |
|---|
| | 781 | raise ValueError, "variables collide after stripping underscores" |
|---|
| | 782 | formula = formula.replace('_', '') |
|---|
| | 783 | |
|---|
| | 784 | qex = Qepcad_expect(logfile=logfile) |
|---|
| | 785 | qex._send('[ input from Sage ]') |
|---|
| | 786 | qex._send('(' + ','.join(varlist) + ')') |
|---|
| | 787 | qex._send(str(free_vars)) |
|---|
| | 788 | # I hope this prompt is distinctive enough... |
|---|
| | 789 | # if not, we could list all the cases separately |
|---|
| | 790 | qex._change_prompt(['\r\n([^\r\n]*) >\r\n', pexpect.EOF]) |
|---|
| | 791 | qex.eval(formula + '.') |
|---|
| | 792 | self._qex = qex |
|---|
| | 793 | |
|---|
| | 794 | def __repr__(self): |
|---|
| | 795 | r""" |
|---|
| | 796 | Return a string representation of this \class{Qepcad} object. |
|---|
| | 797 | |
|---|
| | 798 | EXAMPLES: |
|---|
| | 799 | sage: qepcad(x - 1 == 0, interact=True) # optional |
|---|
| | 800 | QEPCAD object in phase 'Before Normalization' |
|---|
| | 801 | """ |
|---|
| | 802 | return "QEPCAD object in phase '%s'"%self.phase() |
|---|
| | 803 | |
|---|
| | 804 | def assume(self, assume): |
|---|
| | 805 | r""" |
|---|
| | 806 | The following documentation is from \file{qepcad.help}: |
|---|
| | 807 | |
|---|
| | 808 | Add an assumption to the problem. These will not be |
|---|
| | 809 | included in the solution formula. |
|---|
| | 810 | |
|---|
| | 811 | For example, with input (E x)[ a x^2 + b x + c = 0], |
|---|
| | 812 | if we issue the command |
|---|
| | 813 | |
|---|
| | 814 | assume [ a /= 0 ] |
|---|
| | 815 | |
|---|
| | 816 | we'll get the solution formula b^2 - 4 a c >= 0. Without |
|---|
| | 817 | the assumption we'd get something like [a = 0 /\ b /= 0] \/ |
|---|
| | 818 | [a /= 0 /\ 4 a c - b^2 <= 0] \/ [a = 0 /\ b = 0 /\ c = 0]. |
|---|
| | 819 | |
|---|
| | 820 | EXAMPLES: |
|---|
| | 821 | sage: var('a,b,c,x') |
|---|
| | 822 | (a, b, c, x) |
|---|
| | 823 | sage: qf = qepcad_formula |
|---|
| | 824 | sage: qe = qepcad(qf.exists(x, a*x^2 + b*x + c == 0), interact=True) # optional |
|---|
| | 825 | sage: qe.assume(a != 0) # optional |
|---|
| | 826 | sage: qe.finish() # optional |
|---|
| | 827 | 4 a c - b^2 <= 0 |
|---|
| | 828 | """ |
|---|
| | 829 | if not isinstance(assume, str): |
|---|
| | 830 | assume = qepcad_formula.formula(assume) |
|---|
| | 831 | if len(assume.qvars): |
|---|
| | 832 | raise ValueError, "assumptions cannot be quantified" |
|---|
| | 833 | if not assume.vars.issubset(frozenset(self._varlist[:self._free_vars])): |
|---|
| | 834 | raise ValueError, "assumption contains variables not present in formula" |
|---|
| | 835 | assume = repr(assume) |
|---|
| | 836 | assume = assume.replace('_', '') |
|---|
| | 837 | result = self._eval_line("assume [%s]" % assume) |
|---|
| | 838 | if len(result): |
|---|
| | 839 | return AsciiArtString(result) |
|---|
| | 840 | |
|---|
| | 841 | def solution_extension(self, kind): |
|---|
| | 842 | r""" |
|---|
| | 843 | The following documentation is modified from \file{qepcad.help}: |
|---|
| | 844 | |
|---|
| | 845 | solution-extension x |
|---|
| | 846 | |
|---|
| | 847 | Use an alternative solution formula construction method. The |
|---|
| | 848 | parameter x is allowed to be T,E, or G. If x is T, then a |
|---|
| | 849 | formula in the usual language of Tarski formulas is produced. |
|---|
| | 850 | If x is E, a formula in the language of Extended Tarski formulas |
|---|
| | 851 | is produced. If x is G, then a geometry-based formula is |
|---|
| | 852 | produced. |
|---|
| | 853 | |
|---|
| | 854 | EXAMPLES: |
|---|
| | 855 | sage: var('x,y') |
|---|
| | 856 | (x, y) |
|---|
| | 857 | sage: qf = qepcad_formula |
|---|
| | 858 | sage: qe = qepcad(qf.and_(x^2 + y^2 - 3 == 0, x + y > 0), interact=True) # optional |
|---|
| | 859 | sage: qe.go(); qe.go(); qe.go() # optional |
|---|
| | 860 | QEPCAD object has moved to phase 'Before Projection (y)' |
|---|
| | 861 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 862 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 863 | sage: qe.solution_extension('E') # optional |
|---|
| | 864 | x > _root_1 2 x^2 - 3 /\ y^2 + x^2 - 3 = 0 /\ [ 2 x^2 - 3 > 0 \/ y = _root_-1 y^2 + x^2 - 3 ] |
|---|
| | 865 | sage: qe.solution_extension('G') # optional |
|---|
| | 866 | [ |
|---|
| | 867 | [ |
|---|
| | 868 | 2 x^2 - 3 < 0 |
|---|
| | 869 | \/ |
|---|
| | 870 | x = _root_-1 2 x^2 - 3 |
|---|
| | 871 | ] |
|---|
| | 872 | /\ |
|---|
| | 873 | y = _root_-1 y^2 + x^2 - 3 |
|---|
| | 874 | ] |
|---|
| | 875 | \/ |
|---|
| | 876 | [ |
|---|
| | 877 | x^2 - 3 <= 0 |
|---|
| | 878 | /\ |
|---|
| | 879 | x > _root_-1 2 x^2 - 3 |
|---|
| | 880 | /\ |
|---|
| | 881 | y^2 + x^2 - 3 = 0 |
|---|
| | 882 | ] |
|---|
| | 883 | sage: qe.solution_extension('T') # optional |
|---|
| | 884 | y + x > 0 /\ y^2 + x^2 - 3 = 0 |
|---|
| | 885 | """ |
|---|
| | 886 | if kind == 'I': |
|---|
| | 887 | raise ValueError, "Interactive solution construction not handled by Sage interface" |
|---|
| | 888 | result = self._eval_line('solution-extension %s'%kind) |
|---|
| | 889 | tagline = 'An equivalent quantifier-free formula:' |
|---|
| | 890 | loc = result.find(tagline) |
|---|
| | 891 | if loc >= 0: |
|---|
| | 892 | result = result[loc + len(tagline):] |
|---|
| | 893 | result = result.strip() |
|---|
| | 894 | if len(result): |
|---|
| | 895 | return AsciiArtString(result) |
|---|
| | 896 | |
|---|
| | 897 | def set_truth_value(self, index, nv): |
|---|
| | 898 | r""" |
|---|
| | 899 | Given a cell index (or a cell) and an integer, set the truth value |
|---|
| | 900 | of the cell to that integer. Valid integers are 0 (false), |
|---|
| | 901 | 1 (true), and 2 (undetermined). |
|---|
| | 902 | |
|---|
| | 903 | EXAMPLES: |
|---|
| | 904 | sage: qe = qepcad(x == 1, interact=True) # optional |
|---|
| | 905 | sage: qe.go(); qe.go(); qe.go() # optional |
|---|
| | 906 | QEPCAD object has moved to phase 'At the end of projection phase' |
|---|
| | 907 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 908 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 909 | sage: qe.set_truth_value(1, 1) # optional |
|---|
| | 910 | """ |
|---|
| | 911 | index_str = _format_cell_index([index]) |
|---|
| | 912 | self._eval_line('set-truth-value\n%s\n%s' % (index_str, nv)) |
|---|
| | 913 | |
|---|
| | 914 | def phase(self): |
|---|
| | 915 | r""" |
|---|
| | 916 | Return the current phase of the QEPCAD program. |
|---|
| | 917 | |
|---|
| | 918 | EXAMPLES: |
|---|
| | 919 | sage: qe = qepcad(x > 2/3, interact=True) # optional |
|---|
| | 920 | sage: qe.phase() # optional |
|---|
| | 921 | 'Before Normalization' |
|---|
| | 922 | sage: qe.go() # optional |
|---|
| | 923 | QEPCAD object has moved to phase 'At the end of projection phase' |
|---|
| | 924 | sage: qe.phase() # optional |
|---|
| | 925 | 'At the end of projection phase' |
|---|
| | 926 | sage: qe.go() # optional |
|---|
| | 927 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 928 | sage: qe.phase() # optional |
|---|
| | 929 | 'Before Choice' |
|---|
| | 930 | sage: qe.go() # optional |
|---|
| | 931 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 932 | sage: qe.phase() # optional |
|---|
| | 933 | 'Before Solution' |
|---|
| | 934 | sage: qe.go() # optional |
|---|
| | 935 | 3 x - 2 > 0 |
|---|
| | 936 | sage: qe.phase() # optional |
|---|
| | 937 | 'EXITED' |
|---|
| | 938 | """ |
|---|
| | 939 | match = self._qex.expect().match |
|---|
| | 940 | if match == pexpect.EOF: |
|---|
| | 941 | return 'EXITED' |
|---|
| | 942 | else: |
|---|
| | 943 | return match.group(1) |
|---|
| | 944 | |
|---|
| | 945 | def _parse_answer_stats(self): |
|---|
| | 946 | r""" |
|---|
| | 947 | Parse the final string printed by QEPCAD, which should be |
|---|
| | 948 | the simplified quantifier-free formula followed by some |
|---|
| | 949 | statistics. Return a pair of the formula and the statistics |
|---|
| | 950 | (both as strings). |
|---|
| | 951 | |
|---|
| | 952 | EXAMPLES: |
|---|
| | 953 | sage: qe = qepcad(x^2 > 2, interact=True) # optional |
|---|
| | 954 | sage: qe.finish() # optional |
|---|
| | 955 | x^2 - 2 > 0 |
|---|
| | 956 | sage: (ans, stats) = qe._parse_answer_stats() # optional |
|---|
| | 957 | sage: ans # optional |
|---|
| | 958 | 'x^2 - 2 > 0' |
|---|
| | 959 | sage: stats # random, optional |
|---|
| | 960 | '-----------------------------------------------------------------------------\r\n0 Garbage collections, 0 Cells and 0 Arrays reclaimed, in 0 milliseconds.\r\n492514 Cells in AVAIL, 500000 Cells in SPACE.\r\n\r\nSystem time: 16 milliseconds.\r\nSystem time after the initialization: 4 milliseconds.\r\n-----------------------------------------------------------------------------\r\n' |
|---|
| | 961 | """ |
|---|
| | 962 | if self.phase() != 'EXITED': |
|---|
| | 963 | raise ValueError, "QEPCAD is not finished yet" |
|---|
| | 964 | final = self._qex.expect().before |
|---|
| | 965 | match = re.search('\nAn equivalent quantifier-free formula:(.*)\n=+ The End =+\r\n\r\n(.*)$', final, re.DOTALL) |
|---|
| | 966 | |
|---|
| | 967 | if match: |
|---|
| | 968 | return (match.group(1).strip(), match.group(2)) |
|---|
| | 969 | else: |
|---|
| | 970 | return (final, '') |
|---|
| | 971 | |
|---|
| | 972 | def answer(self): |
|---|
| | 973 | r""" |
|---|
| | 974 | For a QEPCAD instance which is finished, return the |
|---|
| | 975 | simplified quantifier-free formula that it printed just before |
|---|
| | 976 | exiting. |
|---|
| | 977 | |
|---|
| | 978 | EXAMPLES: |
|---|
| | 979 | sage: qe = qepcad(x^3 - x == 0, interact=True) # optional |
|---|
| | 980 | sage: qe.finish() # optional |
|---|
| | 981 | x - 1 <= 0 /\ x + 1 >= 0 /\ [ x = 0 \/ x - 1 = 0 \/ x + 1 = 0 ] |
|---|
| | 982 | sage: qe.answer() # optional |
|---|
| | 983 | x - 1 <= 0 /\ x + 1 >= 0 /\ [ x = 0 \/ x - 1 = 0 \/ x + 1 = 0 ] |
|---|
| | 984 | """ |
|---|
| | 985 | return AsciiArtString(self._parse_answer_stats()[0]) |
|---|
| | 986 | |
|---|
| | 987 | def final_stats(self): |
|---|
| | 988 | r""" |
|---|
| | 989 | For a QEPCAD instance which is finished, return the |
|---|
| | 990 | statistics that it printed just before exiting. |
|---|
| | 991 | |
|---|
| | 992 | EXAMPLES: |
|---|
| | 993 | sage: qe = qepcad(x == 0, interact=True) # optional |
|---|
| | 994 | sage: qe.finish() # optional |
|---|
| | 995 | x = 0 |
|---|
| | 996 | sage: qe.final_stats() # random, optional |
|---|
| | 997 | ----------------------------------------------------------------------------- |
|---|
| | 998 | 0 Garbage collections, 0 Cells and 0 Arrays reclaimed, in 0 milliseconds. |
|---|
| | 999 | 492840 Cells in AVAIL, 500000 Cells in SPACE. |
|---|
| | 1000 | System time: 8 milliseconds. |
|---|
| | 1001 | System time after the initialization: 4 milliseconds. |
|---|
| | 1002 | ----------------------------------------------------------------------------- |
|---|
| | 1003 | """ |
|---|
| | 1004 | return AsciiArtString(self._parse_answer_stats()[1]) |
|---|
| | 1005 | |
|---|
| | 1006 | def trait_names(self): |
|---|
| | 1007 | r""" |
|---|
| | 1008 | Returns a list of the QEPCAD commands which are available as |
|---|
| | 1009 | extra methods on a \class{Qepcad} object. |
|---|
| | 1010 | |
|---|
| | 1011 | EXAMPLES: |
|---|
| | 1012 | sage: qe = qepcad(x^2 < 0, interact=True) # optional |
|---|
| | 1013 | sage: len(qe.trait_names()) # optional |
|---|
| | 1014 | 95 |
|---|
| | 1015 | sage: 'd_cell' in qe.trait_names() # optional |
|---|
| | 1016 | True |
|---|
| | 1017 | """ |
|---|
| | 1018 | _update_command_info() |
|---|
| | 1019 | return _command_info_cache.keys() |
|---|
| | 1020 | |
|---|
| | 1021 | def cell(self, *index): |
|---|
| | 1022 | r""" |
|---|
| | 1023 | Given a cell index, returns a \class{QepcadCell} wrapper for that |
|---|
| | 1024 | cell. Uses a cache for efficiency. |
|---|
| | 1025 | |
|---|
| | 1026 | EXAMPLES: |
|---|
| | 1027 | sage: qe = qepcad(x + 3 == 42, interact=True) # optional |
|---|
| | 1028 | sage: qe.go(); qe.go(); qe.go() # optional |
|---|
| | 1029 | QEPCAD object has moved to phase 'At the end of projection phase' |
|---|
| | 1030 | QEPCAD object has moved to phase 'Before Choice' |
|---|
| | 1031 | QEPCAD object has moved to phase 'Before Solution' |
|---|
| | 1032 | sage: qe.cell(2) # optional |
|---|
| | 1033 | |
|---|