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

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

* empty log message *

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