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-polynomial.lisp@ 3339

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

* empty log message *

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