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

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

* empty log message *

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