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-poly.lisp@ 3125

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

* empty log message *

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