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/parser.lisp@ 631

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

* empty log message *

File size: 4.2 KB
Line 
1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;
4;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
5;;;
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 2 of the License, or
9;;; (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19;;;
20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
22
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;; Maxima expression parsing
26;;
27;;
28;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29
30(defpackage "PARSER"
31 (:use :cl)
32 (:export "PARSE-POLY"
33 "PARSE-POLY-LIST"
34 "PARSE-POLY-LIST-LIST"))
35
36(in-package :parser)
37
38;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39;;
40;; Functions and macros dealing with internal representation
41;; structure.
42;;
43;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44
45(defun equal-test-p (expr1 expr2) (alike1 expr1 expr2))
46
47(defun coerce-maxima-list (expr)
48 "Convert a Maxima list to Lisp list."
49 (cond
50 ((and (consp (car expr)) (eql (caar expr) 'maxima:mlist)) (cdr expr))
51 (t expr)))
52
53(defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
54
55(defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
56 "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
57 (labels ((parse (arg) (parse-poly arg vars))
58 (parse-list (args) (mapcar #'parse args)))
59 (cond
60 ((eql expr 0) (make-poly-zero))
61 ((member expr vars :test #'equal-test-p)
62 (let ((pos (position expr vars :test #'equal-test-p)))
63 (make-variable *expression-ring* (length vars) pos)))
64 ((free-of-vars expr vars)
65 ;;This means that variable-free CRE and Poisson forms will be converted
66 ;;to coefficients intact
67 (coerce-coeff *expression-ring* expr vars))
68 (t
69 (case (caar expr)
70 (mplus (reduce #'(lambda (x y) (poly-add *expression-ring* x y)) (parse-list (cdr expr))))
71 (mminus (poly-uminus *expression-ring* (parse (cadr expr))))
72 (mtimes
73 (if (endp (cddr expr)) ;unary
74 (parse (cdr expr))
75 (reduce #'(lambda (p q) (poly-mul *expression-ring* p q)) (parse-list (cdr expr)))))
76 (mexpt
77 (cond
78 ((member (cadr expr) vars :test #'equal-test-p)
79 ;;Special handling of (expt var pow)
80 (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
81 (make-variable *expression-ring* (length vars) pos (caddr expr))))
82 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
83 ;; Negative power means division in coefficient ring
84 ;; Non-integer power means non-polynomial coefficient
85 (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
86 expr)
87 (coerce-coeff *expression-ring* expr vars))
88 (t (poly-expt *expression-ring* (parse (cadr expr)) (caddr expr)))))
89 (mrat (parse ($ratdisrep expr)))
90 (mpois (parse ($outofpois expr)))
91 (otherwise
92 (coerce-coeff *expression-ring* expr vars)))))))
93
94(defun parse-poly-list (expr vars)
95 "Parse a Maxima representation of a list of polynomials."
96 (case (caar expr)
97 (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
98 (t (merror "Expression ~M is not a list of polynomials in variables ~M."
99 expr vars))))
100
101(defun parse-poly-list-list (poly-list-list vars)
102 "Parse a Maxima representation of a list of lists of polynomials."
103 (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
104
Note: See TracBrowser for help on using the repository browser.