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/symbolic-poly.lisp@ 3128

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

* empty log message *

File size: 7.8 KB
Line 
1;;; -*- Mode: Lisp -*-
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(defpackage "POLYNOMIAL"
23 (:use :cl :utils :ring :monom :order :term :poly :infix)
24 (:export "SYMBOLIC-POLY")
25 (:documentation "Implements symbolic polynomials. A symbolic polynomial
26is and object which uses symbolic variables for reading and printing in standard
27human-readable (infix) form."))
28
29(in-package :symbolic-poly)
30
31(defclass symbolic-poly (poly)
32 ((vars :initarg :vars :accessor vars)
33 (:default-initargs :termlist nil :vars nil)))
34
35(defun coerce-coeff (ring expr vars)
36 "Coerce an element of the coefficient ring to a constant polynomial."
37 (declare (type ring ring))
38 (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
39 :coeff (funcall (ring-parse ring) expr)))
40 0))
41
42(defun poly-eval (expr vars
43 &optional
44 (ring +ring-of-integers+)
45 (order #'lex>)
46 (list-marker :[)
47 &aux
48 (ring-and-order (make-ring-and-order :ring ring :order order)))
49 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
50variables VARS. Return the resulting polynomial or list of
51polynomials. Standard arithmetical operators in form EXPR are
52replaced with their analogues in the ring of polynomials, and the
53resulting expression is evaluated, resulting in a polynomial or a list
54of polynomials in internal form. A similar operation in another computer
55algebra system could be called 'expand' or so."
56 (declare (type ring ring))
57 (labels ((p-eval (arg) (poly-eval arg vars ring order))
58 (p-eval-scalar (arg) (poly-eval-scalar arg))
59 (p-eval-list (args) (mapcar #'p-eval args))
60 (p-add (x y) (poly-add ring-and-order x y)))
61 (cond
62 ((null expr) (error "Empty expression"))
63 ((eql expr 0) (make-poly-zero))
64 ((member expr vars :test #'equalp)
65 (let ((pos (position expr vars :test #'equalp)))
66 (make-poly-variable ring (length vars) pos)))
67 ((atom expr)
68 (coerce-coeff ring expr vars))
69 ((eq (car expr) list-marker)
70 (cons list-marker (p-eval-list (cdr expr))))
71 (t
72 (case (car expr)
73 (+ (reduce #'p-add (p-eval-list (cdr expr))))
74 (- (case (length expr)
75 (1 (make-poly-zero))
76 (2 (poly-uminus ring (p-eval (cadr expr))))
77 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
78 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
79 (reduce #'p-add (p-eval-list (cddr expr)))))))
80 (*
81 (if (endp (cddr expr)) ;unary
82 (p-eval (cdr expr))
83 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
84 (/
85 ;; A polynomial can be divided by a scalar
86 (cond
87 ((endp (cddr expr))
88 ;; A special case (/ ?), the inverse
89 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
90 (t
91 (let ((num (p-eval (cadr expr)))
92 (denom-inverse (apply (ring-div ring)
93 (cons (funcall (ring-unit ring))
94 (mapcar #'p-eval-scalar (cddr expr))))))
95 (scalar-times-poly ring denom-inverse num)))))
96 (expt
97 (cond
98 ((member (cadr expr) vars :test #'equalp)
99 ;;Special handling of (expt var pow)
100 (let ((pos (position (cadr expr) vars :test #'equalp)))
101 (make-poly-variable ring (length vars) pos (caddr expr))))
102 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
103 ;; Negative power means division in coefficient ring
104 ;; Non-integer power means non-polynomial coefficient
105 (coerce-coeff ring expr vars))
106 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
107 (otherwise
108 (coerce-coeff ring expr vars)))))))
109
110(defun poly-eval-scalar (expr
111 &optional
112 (ring +ring-of-integers+)
113 &aux
114 (order #'lex>))
115 "Evaluate a scalar expression EXPR in ring RING."
116 (declare (type ring ring))
117 (poly-lc (poly-eval expr nil ring order)))
118
119
120(defun read-infix-form (&key (stream t))
121 "Parser of infix expressions with integer/rational coefficients
122The parser will recognize two kinds of polynomial expressions:
123
124- polynomials in fully expanded forms with coefficients
125 written in front of symbolic expressions; constants can be optionally
126 enclosed in (); for example, the infix form
127 X^2-Y^2+(-4/3)*U^2*W^3-5
128 parses to
129 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
130
131- lists of polynomials; for example
132 [X-Y, X^2+3*Z]
133 parses to
134 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
135 where the first symbol [ marks a list of polynomials.
136
137-other infix expressions, for example
138 [(X-Y)*(X+Y)/Z,(X+1)^2]
139parses to:
140 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
141Currently this function is implemented using M. Kantrowitz's INFIX package."
142 (read-from-string
143 (concatenate 'string
144 "#I("
145 (with-output-to-string (s)
146 (loop
147 (multiple-value-bind (line eof)
148 (read-line stream t)
149 (format s "~A" line)
150 (when eof (return)))))
151 ")")))
152
153(defun read-poly (vars &key
154 (stream t)
155 (ring +ring-of-integers+)
156 (order #'lex>))
157 "Reads an expression in prefix form from a stream STREAM.
158The expression read from the strem should represent a polynomial or a
159list of polynomials in variables VARS, over the ring RING. The
160polynomial or list of polynomials is returned, with terms in each
161polynomial ordered according to monomial order ORDER."
162 (poly-eval (read-infix-form :stream stream) vars ring order))
163
164(defun string->poly (str vars
165 &optional
166 (ring +ring-of-integers+)
167 (order #'lex>))
168 "Converts a string STR to a polynomial in variables VARS."
169 (with-input-from-string (s str)
170 (read-poly vars :stream s :ring ring :order order)))
171
172(defun poly->alist (p)
173 "Convert a polynomial P to an association list. Thus, the format of the
174returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
175MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
176corresponding coefficient in the ring."
177 (cond
178 ((poly-p p)
179 (mapcar #'term->cons (poly-termlist p)))
180 ((and (consp p) (eq (car p) :[))
181 (cons :[ (mapcar #'poly->alist (cdr p))))))
182
183(defun string->alist (str vars
184 &optional
185 (ring +ring-of-integers+)
186 (order #'lex>))
187 "Convert a string STR representing a polynomial or polynomial list to
188an association list (... (MONOM . COEFF) ...)."
189 (poly->alist (string->poly str vars ring order)))
190
191(defun poly-equal-no-sugar-p (p q)
192 "Compare polynomials for equality, ignoring sugar."
193 (declare (type poly p q))
194 (equalp (poly-termlist p) (poly-termlist q)))
195
196(defun poly-set-equal-no-sugar-p (p q)
197 "Compare polynomial sets P and Q for equality, ignoring sugar."
198 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
199
200(defun poly-list-equal-no-sugar-p (p q)
201 "Compare polynomial lists P and Q for equality, ignoring sugar."
202 (every #'poly-equal-no-sugar-p p q))
Note: See TracBrowser for help on using the repository browser.