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

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