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

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

* empty log message *

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