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

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

* empty log message *

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