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