close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/parse.lisp@ 1056

Last change on this file since 1056 was 1056, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 8.0 KB
Line 
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
73(defun parse (&optional stream)
74 "Parser of infix expressions with integer/rational coefficients
75The parser will recognize two kinds of polynomial expressions:
76
77- polynomials in fully expanded forms with coefficients
78 written in front of symbolic expressions; constants can be optionally
79 enclosed in (); for example, the infix form
80 X^2-Y^2+(-4/3)*U^2*W^3-5
81 parses to
82 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
83
84- lists of polynomials; for example
85 [X-Y, X^2+3*Z]
86 parses to
87 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
88 where the first symbol [ marks a list of polynomials.
89
90-other infix expressions, for example
91 [(X-Y)*(X+Y)/Z,(X+1)^2]
92parses to:
93 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
94Currently this function is implemented using M. Kantrowitz's INFIX package."
95 (read-from-string
96 (concatenate 'string
97 "#I("
98 (with-output-to-string (s)
99 (loop
100 (multiple-value-bind (line eof)
101 (read-line stream t)
102 (format s "~A" line)
103 (when eof (return)))))
104 ")")))
105
106;; Translate output from parse to a pure list form
107;; assuming variables are VARS
108
109(defun alist-form (plist vars)
110 "Translates an expression PLIST, which should be a list of polynomials
111in variables VARS, to an alist representation of a polynomial.
112It returns the alist. See also PARSE-TO-ALIST."
113 (cond
114 ((endp plist) nil)
115 ((eql (first plist) '[)
116 (cons '[ (mapcar #'(lambda (x) (alist-form x vars)) (rest plist))))
117 (t
118 (assert (eql (car plist) '+))
119 (alist-form-1 (rest plist) vars))))
120
121(defun alist-form-1 (p vars
122 &aux (ht (make-hash-table
123 :test #'equal :size 16))
124 stack)
125 (dolist (term p)
126 (assert (eql (car term) '*))
127 (incf (gethash (powers (cddr term) vars) ht 0) (second term)))
128 (maphash #'(lambda (key value) (unless (zerop value)
129 (push (cons key value) stack))) ht)
130 stack)
131
132(defun powers (monom vars
133 &aux (tab (pairlis vars (make-list (length vars)
134 :initial-element 0))))
135 (dolist (e monom (mapcar #'(lambda (v) (cdr (assoc v tab))) vars))
136 (assert (equal (first e) '^))
137 (assert (integerp (third e)))
138 (assert (= (length e) 3))
139 (let ((x (assoc (second e) tab)))
140 (if (null x) (error "Variable ~a not in the list of variables."
141 (second e))
142 (incf (cdr x) (third e))))))
143
144
145;; New implementation based on the INFIX package of Mark Kantorowitz
146(defun parse-to-alist (vars &optional stream)
147 "Parse an expression already in prefix form to an association list form
148according to the internal CGBlisp polynomial syntax: a polynomial is an
149alist of pairs (MONOM . COEFFICIENT). For example:
150 (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
151 (PARSE-TO-ALIST '(X Y U W) S))
152evaluates to
153(((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))"
154 (poly-eval (parse stream) vars))
155
156
157(defun parse-string-to-alist (str vars)
158 "Parse string STR and return a polynomial as a sorted association
159list of pairs (MONOM . COEFFICIENT). For example:
160(parse-string-to-alist \"[x^2-y^2+(-4/3)*u^2*w^3-5,y]\" '(x y u w))
161 ([ (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1)
162 ((0 0 0 0) . -5))
163 (((0 1 0 0) . 1)))
164The functions PARSE-TO-SORTED-ALIST and PARSE-STRING-TO-SORTED-ALIST
165sort terms by the predicate defined in the ORDER package."
166 (with-input-from-string (stream str)
167 (parse-to-alist vars stream)))
168
169
170(defun parse-to-sorted-alist (vars &optional (order #'lex>) (stream t))
171 "Parses streasm STREAM and returns a polynomial represented as
172a sorted alist. For example:
173(WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
174 (PARSE-TO-SORTED-ALIST '(X Y U W) S))
175returns
176(((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
177and
178(WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
179 (PARSE-TO-SORTED-ALIST '(X Y U W) T #'GRLEX>) S)
180returns
181(((0 0 2 3) . -4/3) ((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 0 0) . -5))"
182 (sort-poly (parse-to-alist vars stream) order))
183
184(defun parse-string-to-sorted-alist (str vars &optional (order #'lex>))
185 "Parse a string to a sorted alist form, the internal representation
186of polynomials used by our system."
187 (with-input-from-string (stream str)
188 (parse-to-sorted-alist vars order stream)))
189
190(defun sort-poly-1 (p order)
191 "Sort the terms of a single polynomial P using an admissible monomial order ORDER.
192Returns the sorted polynomial. Destructively modifies P."
193 (sort p order :key #'first))
194
195;; Sort a polynomial or polynomial list
196(defun sort-poly (poly-or-poly-list &optional (order #'lex>))
197 "Sort POLY-OR-POLY-LIST, which could be either a single polynomial
198or a list of polynomials in internal alist representation, using
199admissible monomial order ORDER. Each polynomial is sorted using
200SORT-POLY-1."
201 (cond
202 ((eql poly-or-poly-list :syntax-error) nil)
203 ((null poly-or-poly-list) nil)
204 ((eql (car poly-or-poly-list) '[)
205 (cons '[ (mapcar #'(lambda (p) (sort-poly-1 p order))
206 (rest poly-or-poly-list))))
207 (t (sort-poly-1 poly-or-poly-list order))))
208
209
210
Note: See TracBrowser for help on using the repository browser.