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

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

* empty log message *

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