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

Last change on this file since 4471 was 4463, checked in by Marek Rychlik, 9 years ago
File size: 6.5 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"
[4346]23 (:use :cl :utils :monom :polynomial :infix :infix-printer :ring)
[3902]24 (:export "SYMBOLIC-POLY" "READ-INFIX-FORM" "STRING->POLY" "POLY->STRING" "->INFIX")
[3240]25 (:documentation "Implements symbolic polynomials. A symbolic
[3773]26polynomial is polynomial which uses symbolic variables for reading and
[3240]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)
[4463]40 (with-accessors ((termlist poly-termlist)
[3238]41 (order poly-term-order)
42 (vars symbolic-poly-vars))
43 self
[4463]44 (format stream "TERMLIST=~A ORDER=~A VARS=~A"
45 termlist order vars))))
[3238]46
47
[3724]48(defmethod universal-equalp ((self symbolic-poly) (other symbolic-poly))
49 (when (universal-equalp (symbolic-poly-vars self) (symbolic-poly-vars other))
[3274]50 (call-next-method)))
[3273]51
[4435]52(defmethod universal-equalp ((self symbolic-poly) (other poly))
53 "Compare SELF, which is an instance of SYMBOLIC-POLY, to OTHER, which
54is an instance of POLY. We simply ignore variables of SELF, and compare
55SELF and OTHER as POLY."
56 (call-next-method))
57
[3727]58(defmethod universal-equalp ((self symbol) (other symbol))
59 (eq self other))
60
[3356]61#|
[3124]62(defun poly-eval-scalar (expr
63 &aux
64 (order #'lex>))
65 "Evaluate a scalar expression EXPR in ring RING."
66 (declare (type ring ring))
67 (poly-lc (poly-eval expr nil ring order)))
[3356]68|#
[3124]69
70
71(defun read-infix-form (&key (stream t))
72 "Parser of infix expressions with integer/rational coefficients
73The parser will recognize two kinds of polynomial expressions:
74
75- polynomials in fully expanded forms with coefficients
76 written in front of symbolic expressions; constants can be optionally
77 enclosed in (); for example, the infix form
78 X^2-Y^2+(-4/3)*U^2*W^3-5
79 parses to
80 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
81
82- lists of polynomials; for example
83 [X-Y, X^2+3*Z]
84 parses to
85 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
86 where the first symbol [ marks a list of polynomials.
87
88-other infix expressions, for example
89 [(X-Y)*(X+Y)/Z,(X+1)^2]
90parses to:
91 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
92Currently this function is implemented using M. Kantrowitz's INFIX package."
93 (read-from-string
94 (concatenate 'string
95 "#I("
96 (with-output-to-string (s)
97 (loop
98 (multiple-value-bind (line eof)
99 (read-line stream t)
100 (format s "~A" line)
101 (when eof (return)))))
102 ")")))
103
104(defun read-poly (vars &key
105 (stream t)
[4346]106 (order #'lex>)
[4365]107 (coefficient-class *coefficient-class*))
[3124]108 "Reads an expression in prefix form from a stream STREAM.
109The expression read from the strem should represent a polynomial or a
110list of polynomials in variables VARS, over the ring RING. The
111polynomial or list of polynomials is returned, with terms in each
112polynomial ordered according to monomial order ORDER."
[4346]113 (poly-eval (read-infix-form :stream stream) vars order coefficient-class))
[3124]114
115(defun string->poly (str vars
116 &optional
[4346]117 (order #'lex>)
[4365]118 (coefficient-class *coefficient-class*))
[3124]119 "Converts a string STR to a polynomial in variables VARS."
120 (with-input-from-string (s str)
[4346]121 (let ((p-or-plist (read-poly vars :stream s :order order :coefficient-class coefficient-class)))
[4076]122 (etypecase p-or-plist
[4067]123 (poly (change-class p-or-plist 'symbolic-poly :vars vars))
124 (cons
125 (setf (cdr p-or-plist) (mapcar #'(lambda (p) (change-class p 'symbolic-poly :vars vars)) (cdr p-or-plist)))
126 p-or-plist)))))
[3124]127
128(defun string->alist (str vars
129 &optional
[4346]130 (order #'lex>)
[4365]131 (coefficient-class *coefficient-class*))
[3124]132 "Convert a string STR representing a polynomial or polynomial list to
133an association list (... (MONOM . COEFF) ...)."
[4346]134 (poly->alist (string->poly str vars order coefficient-class)))
[3124]135
136(defun poly-equal-no-sugar-p (p q)
137 "Compare polynomials for equality, ignoring sugar."
138 (declare (type poly p q))
139 (equalp (poly-termlist p) (poly-termlist q)))
140
141(defun poly-set-equal-no-sugar-p (p q)
142 "Compare polynomial sets P and Q for equality, ignoring sugar."
143 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
144
145(defun poly-list-equal-no-sugar-p (p q)
146 "Compare polynomial lists P and Q for equality, ignoring sugar."
147 (every #'poly-equal-no-sugar-p p q))
[3831]148
[4019]149(defmethod ->sexp :around ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
[3858]150 "Convert a symbolic polynomial SELF to infix form, using variables VARS. The default
[3855]151value of VARS is the corresponding slot value of SELF."
[3854]152 (call-next-method self vars))
[3853]153
[3836]154(defgeneric poly->string (self &optional vars)
[3838]155 (:documentation "Render polynomial SELF as a string, using symbolic variables VARS.")
[4088]156 (:method ((self list) &optional (vars nil vars-p))
[4076]157 (assert (eql (car self) :[))
[4088]158 (cond (vars-p
[4202]159 (format nil "[~{~a~^, ~}]" (mapcar #'(lambda (p) (poly->string p vars)) (cdr self))))
[4088]160 (t
[4202]161 (format nil "[~{~a~^, ~}]" (mapcar #'(lambda (p) (poly->string p)) (cdr self))))))
[3839]162 (:method ((self poly) &optional (vars nil))
[4066]163 (infix-print-to-string (->sexp self vars)))
[3836]164 (:method ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
[4066]165 (infix-print-to-string (->sexp self vars))))
Note: See TracBrowser for help on using the repository browser.