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

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

* empty log message *

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