| 1 | ;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*-
|
---|
| 2 |
|
---|
| 3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 4 | ;;;
|
---|
| 5 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
| 6 | ;;;
|
---|
| 7 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 8 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 9 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | ;;; (at your option) any later version.
|
---|
| 11 | ;;;
|
---|
| 12 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 13 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | ;;; GNU General Public License for more details.
|
---|
| 16 | ;;;
|
---|
| 17 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 18 | ;;; along with this program; if not, write to the Free Software
|
---|
| 19 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
| 20 | ;;;
|
---|
| 21 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 22 |
|
---|
| 23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 24 | ;;
|
---|
| 25 | ;; Parser of infix notation. This package enables input
|
---|
| 26 | ;; of polynomials in human-readable notation outside of Maxima,
|
---|
| 27 | ;; which is very useful for debugging.
|
---|
| 28 | ;;
|
---|
| 29 | ;; NOTE: This package is adapted from CGBLisp.
|
---|
| 30 | ;;
|
---|
| 31 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 32 |
|
---|
| 33 | (defpackage "PARSE"
|
---|
| 34 | (:use :cl :order :ring-and-order :monomial :term :polynomial :ring)
|
---|
| 35 | (:export "PARSE PARSE-TO-ALIST" "PARSE-STRING-TO-ALIST"
|
---|
| 36 | "PARSE-TO-SORTED-ALIST" "PARSE-STRING-TO-SORTED-ALIST" "^" "["
|
---|
| 37 | "POLY-EVAL"
|
---|
| 38 | ))
|
---|
| 39 |
|
---|
| 40 | (in-package "PARSE")
|
---|
| 41 |
|
---|
| 42 | (proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 3)))
|
---|
| 43 |
|
---|
| 44 | ;; The function PARSE yields the representations as above. The two functions
|
---|
| 45 | ;; PARSE-TO-ALIST and PARSE-STRING-TO-ALIST parse polynomials to the alist
|
---|
| 46 | ;; representations. For example
|
---|
| 47 | ;;
|
---|
| 48 | ;; >(parse)x^2-y^2+(-4/3)*u^2*w^3-5 --->
|
---|
| 49 | ;; (+ (* 1 (^ X 2)) (* -1 (^ Y 2)) (* -4/3 (^ U 2) (^ W 3)) (* -5))
|
---|
| 50 | ;;
|
---|
| 51 | ;; >(parse-to-alist '(x y u w))x^2-y^2+(-4/3)*u^2*w^3-5 --->
|
---|
| 52 | ;; (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))
|
---|
| 53 | ;;
|
---|
| 54 | ;; >(parse-string-to-alist "x^2-y^2+(-4/3)*u^2*w^3-5" '(x y u w)) --->
|
---|
| 55 | ;; (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))
|
---|
| 56 | ;;
|
---|
| 57 | ;; >(parse-string-to-alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w))
|
---|
| 58 | ;; ([ (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1)
|
---|
| 59 | ;; ((0 0 0 0) . -5))
|
---|
| 60 | ;; (((0 1 0 0) . 1)))
|
---|
| 61 | ;; The functions PARSE-TO-SORTED-ALIST and PARSE-STRING-TO-SORTED-ALIST
|
---|
| 62 | ;; in addition sort terms by the predicate defined in the ORDER package
|
---|
| 63 | ;; For instance:
|
---|
| 64 | ;; >(parse-to-sorted-alist '(x y u w))x^2-y^2+(-4/3)*u^2*w^3-5
|
---|
| 65 | ;; (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
|
---|
| 66 | ;; >(parse-to-sorted-alist '(x y u w) t #'grlex>)x^2-y^2+(-4/3)*u^2*w^3-5
|
---|
| 67 | ;; (((0 0 2 3) . -4/3) ((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 0 0) . -5))
|
---|
| 68 |
|
---|
| 69 | ;;(eval-when (compile)
|
---|
| 70 | ;; (proclaim '(optimize safety)))
|
---|
| 71 |
|
---|
| 72 | (defun parse (&optional stream)
|
---|
| 73 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 74 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 75 |
|
---|
| 76 | - polynomials in fully expanded forms with coefficients
|
---|
| 77 | written in front of symbolic expressions; constants can be optionally
|
---|
| 78 | enclosed in (); for example, the infix form
|
---|
| 79 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 80 | parses to
|
---|
| 81 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 82 |
|
---|
| 83 | - lists of polynomials; for example
|
---|
| 84 | [X-Y, X^2+3*Z]
|
---|
| 85 | parses to
|
---|
| 86 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 87 | where the first symbol [ marks a list of polynomials.
|
---|
| 88 |
|
---|
| 89 | -other infix expressions, for example
|
---|
| 90 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 91 | parses to:
|
---|
| 92 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 93 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 94 | (read-from-string
|
---|
| 95 | (concatenate 'string
|
---|
| 96 | "#I("
|
---|
| 97 | (with-output-to-string (s)
|
---|
| 98 | (loop
|
---|
| 99 | (multiple-value-bind (line eof)
|
---|
| 100 | (read-line stream t)
|
---|
| 101 | (format s "~A" line)
|
---|
| 102 | (when eof (return)))))
|
---|
| 103 | ")")))
|
---|
| 104 |
|
---|
| 105 | ;; New implementation based on the INFIX package of Mark Kantorowitz
|
---|
| 106 | (defun parse-to-alist (vars &optional stream)
|
---|
| 107 | "Parse an expression already in prefix form to an association list form
|
---|
| 108 | according to the internal CGBlisp polynomial syntax: a polynomial is an
|
---|
| 109 | alist of pairs (MONOM . COEFFICIENT). For example:
|
---|
| 110 | (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
|
---|
| 111 | (PARSE-TO-ALIST '(X Y U W) S))
|
---|
| 112 | evaluates to
|
---|
| 113 | (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))"
|
---|
| 114 | (poly-eval (parse stream) vars))
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 |
|
---|