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

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

* empty log message *

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