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

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

* empty log message *

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