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