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 | ;; TODO: Make Maxima-style polynomial expression work
|
---|
28 | ;; for integer coefficients outside Maxima.
|
---|
29 | ;;
|
---|
30 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
31 |
|
---|
32 | (defpackage "PARSER"
|
---|
33 | (:use :cl)
|
---|
34 | (:export "PARSE-POLY"
|
---|
35 | "PARSE-POLY-LIST"
|
---|
36 | "PARSE-POLY-LIST-LIST"))
|
---|
37 |
|
---|
38 | (in-package :parser)
|
---|
39 |
|
---|
40 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
41 | ;;
|
---|
42 | ;; Functions and macros dealing with internal representation
|
---|
43 | ;; structure.
|
---|
44 | ;;
|
---|
45 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
46 |
|
---|
47 | (defun equal-test-p (expr1 expr2) (alike1 expr1 expr2))
|
---|
48 |
|
---|
49 | (defun coerce-maxima-list (expr)
|
---|
50 | "Convert a Maxima list to Lisp list."
|
---|
51 | (cond
|
---|
52 | ((and (consp (car expr)) (eql (caar expr) 'maxima:mlist)) (cdr expr))
|
---|
53 | (t expr)))
|
---|
54 |
|
---|
55 | (defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
|
---|
56 |
|
---|
57 | (defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
|
---|
58 | "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
|
---|
59 | (labels ((parse (arg) (parse-poly arg vars))
|
---|
60 | (parse-list (args) (mapcar #'parse args)))
|
---|
61 | (cond
|
---|
62 | ((eql expr 0) (make-poly-zero))
|
---|
63 | ((member expr vars :test #'equal-test-p)
|
---|
64 | (let ((pos (position expr vars :test #'equal-test-p)))
|
---|
65 | (make-variable *expression-ring* (length vars) pos)))
|
---|
66 | ((free-of-vars expr vars)
|
---|
67 | ;;This means that variable-free CRE and Poisson forms will be converted
|
---|
68 | ;;to coefficients intact
|
---|
69 | (coerce-coeff *expression-ring* expr vars))
|
---|
70 | (t
|
---|
71 | (case (caar expr)
|
---|
72 | (mplus (reduce #'(lambda (x y) (poly-add *expression-ring* x y)) (parse-list (cdr expr))))
|
---|
73 | (mminus (poly-uminus *expression-ring* (parse (cadr expr))))
|
---|
74 | (mtimes
|
---|
75 | (if (endp (cddr expr)) ;unary
|
---|
76 | (parse (cdr expr))
|
---|
77 | (reduce #'(lambda (p q) (poly-mul *expression-ring* p q)) (parse-list (cdr expr)))))
|
---|
78 | (mexpt
|
---|
79 | (cond
|
---|
80 | ((member (cadr expr) vars :test #'equal-test-p)
|
---|
81 | ;;Special handling of (expt var pow)
|
---|
82 | (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
|
---|
83 | (make-variable *expression-ring* (length vars) pos (caddr expr))))
|
---|
84 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
85 | ;; Negative power means division in coefficient ring
|
---|
86 | ;; Non-integer power means non-polynomial coefficient
|
---|
87 | (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
|
---|
88 | expr)
|
---|
89 | (coerce-coeff *expression-ring* expr vars))
|
---|
90 | (t (poly-expt *expression-ring* (parse (cadr expr)) (caddr expr)))))
|
---|
91 | (mrat (parse ($ratdisrep expr)))
|
---|
92 | (mpois (parse ($outofpois expr)))
|
---|
93 | (otherwise
|
---|
94 | (coerce-coeff *expression-ring* expr vars)))))))
|
---|
95 |
|
---|
96 | (defun parse-poly-list (expr vars)
|
---|
97 | "Parse a Maxima representation of a list of polynomials."
|
---|
98 | (case (caar expr)
|
---|
99 | (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
|
---|
100 | (t (merror "Expression ~M is not a list of polynomials in variables ~M."
|
---|
101 | expr vars))))
|
---|
102 |
|
---|
103 | (defun parse-poly-list-list (poly-list-list vars)
|
---|
104 | "Parse a Maxima representation of a list of lists of polynomials."
|
---|
105 | (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
|
---|
106 |
|
---|