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 | ;; Maxima expression parsing
|
---|
25 | ;;
|
---|
26 | ;; NOTE: This code depends on several Maxima lisp functions:
|
---|
27 | ;;
|
---|
28 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
29 |
|
---|
30 | (defun equal-test-p (expr1 expr2)
|
---|
31 | (alike1 expr1 expr2))
|
---|
32 |
|
---|
33 | (defun coerce-maxima-list (expr)
|
---|
34 | "convert a maxima list to lisp list."
|
---|
35 | (cond
|
---|
36 | ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
|
---|
37 | (t expr)))
|
---|
38 |
|
---|
39 | (defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
|
---|
40 |
|
---|
41 | (defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
|
---|
42 | "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
|
---|
43 | (labels ((parse (arg) (parse-poly arg vars))
|
---|
44 | (parse-list (args) (mapcar #'parse args)))
|
---|
45 | (cond
|
---|
46 | ((eql expr 0) (make-poly-zero))
|
---|
47 | ((member expr vars :test #'equal-test-p)
|
---|
48 | (let ((pos (position expr vars :test #'equal-test-p)))
|
---|
49 | (make-variable *maxima-ring* (length vars) pos)))
|
---|
50 | ((free-of-vars expr vars)
|
---|
51 | ;;This means that variable-free CRE and Poisson forms will be converted
|
---|
52 | ;;to coefficients intact
|
---|
53 | (coerce-coeff *maxima-ring* expr vars))
|
---|
54 | (t
|
---|
55 | (case (caar expr)
|
---|
56 | (mplus (reduce #'(lambda (x y) (poly-add *maxima-ring* x y)) (parse-list (cdr expr))))
|
---|
57 | (mminus (poly-uminus *maxima-ring* (parse (cadr expr))))
|
---|
58 | (mtimes
|
---|
59 | (if (endp (cddr expr)) ;unary
|
---|
60 | (parse (cdr expr))
|
---|
61 | (reduce #'(lambda (p q) (poly-mul *maxima-ring* p q)) (parse-list (cdr expr)))))
|
---|
62 | (mexpt
|
---|
63 | (cond
|
---|
64 | ((member (cadr expr) vars :test #'equal-test-p)
|
---|
65 | ;;Special handling of (expt var pow)
|
---|
66 | (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
|
---|
67 | (make-variable *maxima-ring* (length vars) pos (caddr expr))))
|
---|
68 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
69 | ;; Negative power means division in coefficient ring
|
---|
70 | ;; Non-integer power means non-polynomial coefficient
|
---|
71 | (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
|
---|
72 | expr)
|
---|
73 | (coerce-coeff *maxima-ring* expr vars))
|
---|
74 | (t (poly-expt *maxima-ring* (parse (cadr expr)) (caddr expr)))))
|
---|
75 | (mrat (parse ($ratdisrep expr)))
|
---|
76 | (mpois (parse ($outofpois expr)))
|
---|
77 | (otherwise
|
---|
78 | (coerce-coeff *maxima-ring* expr vars)))))))
|
---|
79 |
|
---|
80 | (defun parse-poly-list (expr vars)
|
---|
81 | (case (caar expr)
|
---|
82 | (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
|
---|
83 | (t (merror "Expression ~M is not a list of polynomials in variables ~M."
|
---|
84 | expr vars))))
|
---|
85 | (defun parse-poly-list-list (poly-list-list vars)
|
---|
86 | (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
|
---|