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@ 1058

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