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

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

* empty log message *

File size: 6.9 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
109;; New implementation based on the INFIX package of Mark Kantorowitz
110(defun parse-to-alist (vars &optional stream)
111 "Parse an expression already in prefix form to an association list form
112according to the internal CGBlisp polynomial syntax: a polynomial is an
113alist of pairs (MONOM . COEFFICIENT). For example:
114 (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
115 (PARSE-TO-ALIST '(X Y U W) S))
116evaluates to
117(((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))"
118 (poly-eval (parse stream) vars))
119
120
121(defun parse-string-to-alist (str vars)
122 "Parse string STR and return a polynomial as a sorted association
123list of pairs (MONOM . COEFFICIENT). For example:
124(parse-string-to-alist \"[x^2-y^2+(-4/3)*u^2*w^3-5,y]\" '(x y u w))
125 ([ (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1)
126 ((0 0 0 0) . -5))
127 (((0 1 0 0) . 1)))
128The functions PARSE-TO-SORTED-ALIST and PARSE-STRING-TO-SORTED-ALIST
129sort terms by the predicate defined in the ORDER package."
130 (with-input-from-string (stream str)
131 (parse-to-alist vars stream)))
132
133
134(defun parse-to-sorted-alist (vars &optional (order #'lex>) (stream t))
135 "Parses streasm STREAM and returns a polynomial represented as
136a sorted alist. For example:
137(WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
138 (PARSE-TO-SORTED-ALIST '(X Y U W) S))
139returns
140(((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
141and
142(WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\")
143 (PARSE-TO-SORTED-ALIST '(X Y U W) T #'GRLEX>) S)
144returns
145(((0 0 2 3) . -4/3) ((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 0 0) . -5))"
146 (sort-poly (parse-to-alist vars stream) order))
147
148(defun parse-string-to-sorted-alist (str vars &optional (order #'lex>))
149 "Parse a string to a sorted alist form, the internal representation
150of polynomials used by our system."
151 (with-input-from-string (stream str)
152 (parse-to-sorted-alist vars order stream)))
153
154(defun sort-poly-1 (p order)
155 "Sort the terms of a single polynomial P using an admissible monomial order ORDER.
156Returns the sorted polynomial. Destructively modifies P."
157 (sort p order :key #'first))
158
159;; Sort a polynomial or polynomial list
160(defun sort-poly (poly-or-poly-list &optional (order #'lex>))
161 "Sort POLY-OR-POLY-LIST, which could be either a single polynomial
162or a list of polynomials in internal alist representation, using
163admissible monomial order ORDER. Each polynomial is sorted using
164SORT-POLY-1."
165 (cond
166 ((eql poly-or-poly-list :syntax-error) nil)
167 ((null poly-or-poly-list) nil)
168 ((eql (car poly-or-poly-list) '[)
169 (cons '[ (mapcar #'(lambda (p) (sort-poly-1 p order))
170 (rest poly-or-poly-list))))
171 (t (sort-poly-1 poly-or-poly-list order))))
172
173
174
Note: See TracBrowser for help on using the repository browser.