[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"
|
---|
[3832] | 23 | (:use :cl :utils :monom :polynomial :infix :infix-printer)
|
---|
[3858] | 24 | (:export "SYMBOLIC-POLY" "READ-INFIX-FORM" "STRING->POLY" "POLY->STRING" "->INFIX" "+LIST-MARKER+")
|
---|
[3240] | 25 | (:documentation "Implements symbolic polynomials. A symbolic
|
---|
[3773] | 26 | polynomial is polynomial which uses symbolic variables for reading and
|
---|
[3240] | 27 | printing in standard human-readable (infix) form."))
|
---|
[3124] | 28 |
|
---|
[3231] | 29 | (in-package :symbolic-polynomial)
|
---|
[3124] | 30 |
|
---|
[3380] | 31 | (defparameter +list-marker+ :[
|
---|
| 32 | "A sexp with this head is considered a list of polynomials.")
|
---|
[3371] | 33 |
|
---|
[3125] | 34 | (defclass symbolic-poly (poly)
|
---|
[3268] | 35 | ((vars :initform nil
|
---|
| 36 | :initarg :vars
|
---|
| 37 | :accessor symbolic-poly-vars)
|
---|
| 38 | )
|
---|
[3236] | 39 | (:default-initargs :termlist nil :vars nil))
|
---|
[3125] | 40 |
|
---|
[3263] | 41 | (defmethod print-object ((self symbolic-poly) stream)
|
---|
[3239] | 42 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3269] | 43 | (with-accessors ((dimension poly-dimension)
|
---|
| 44 | (termlist poly-termlist)
|
---|
[3238] | 45 | (order poly-term-order)
|
---|
| 46 | (vars symbolic-poly-vars))
|
---|
| 47 | self
|
---|
[3269] | 48 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A VARS=~A"
|
---|
| 49 | dimension termlist order vars))))
|
---|
[3238] | 50 |
|
---|
| 51 |
|
---|
[3724] | 52 | (defmethod universal-equalp ((self symbolic-poly) (other symbolic-poly))
|
---|
| 53 | (when (universal-equalp (symbolic-poly-vars self) (symbolic-poly-vars other))
|
---|
[3274] | 54 | (call-next-method)))
|
---|
[3273] | 55 |
|
---|
[3727] | 56 | (defmethod universal-equalp ((self symbol) (other symbol))
|
---|
| 57 | (eq self other))
|
---|
| 58 |
|
---|
[3280] | 59 | (defmethod update-instance-for-different-class :after ((old poly) (new symbolic-poly) &key)
|
---|
[3337] | 60 | "After adding variables to NEW, we need to make sure that the number
|
---|
| 61 | of variables given by POLY-DIMENSION is consistent with VARS."
|
---|
[3333] | 62 | (assert (= (length (symbolic-poly-vars new)) (poly-dimension new))))
|
---|
[3279] | 63 |
|
---|
[3864] | 64 | (defun poly-eval (expr vars order)
|
---|
| 65 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
[3124] | 66 | variables VARS. Return the resulting polynomial or list of
|
---|
| 67 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
| 68 | replaced with their analogues in the ring of polynomials, and the
|
---|
| 69 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
| 70 | of polynomials in internal form. A similar operation in another computer
|
---|
[3864] | 71 | algebra system could be called 'expand' or so."
|
---|
| 72 | (labels ((p-eval (p) (poly-eval p vars order))
|
---|
| 73 | (p-eval-scalar (p) (poly-eval p '() order))
|
---|
| 74 | (p-eval-list (plist) (mapcar #'p-eval plist)))
|
---|
| 75 | (cond
|
---|
| 76 | ((eq expr 0)
|
---|
| 77 | (make-instance 'symbolic-poly :dimension (length vars) :vars vars))
|
---|
| 78 | ((member expr vars :test #'equalp)
|
---|
| 79 | (let ((pos (position expr vars :test #'equalp)))
|
---|
| 80 | (make-poly-variable (length vars) pos)))
|
---|
| 81 | ((atom expr)
|
---|
| 82 | expr)
|
---|
| 83 | ((eq (car expr) +list-marker+)
|
---|
| 84 | (cons +list-marker+ (p-eval-list (cdr expr))))
|
---|
| 85 | (t
|
---|
| 86 | (case (car expr)
|
---|
| 87 | (+ (reduce #'add (p-eval-list (cdr expr))))
|
---|
| 88 | (- (apply #'subtract (p-eval-list (cdr expr))))
|
---|
| 89 | (*
|
---|
| 90 | (if (endp (cddr expr)) ;unary
|
---|
| 91 | (p-eval (cdr expr))
|
---|
| 92 | (reduce #'multiply (p-eval-list (cdr expr)))))
|
---|
| 93 | (/
|
---|
| 94 | ;; A polynomial can be divided by a scalar
|
---|
| 95 | (cond
|
---|
| 96 | ((endp (cddr expr))
|
---|
| 97 | ;; A special case (/ ?), the inverse
|
---|
| 98 | (divide (cadr expr)))
|
---|
| 99 | (t
|
---|
| 100 | (let ((num (p-eval (cadr expr)))
|
---|
| 101 | (denom-inverse (apply #'divide (mapcar #'p-eval-scalar (cddr expr)))))
|
---|
| 102 | (multiply denom-inverse num)))))
|
---|
| 103 | (expt
|
---|
| 104 | (cond
|
---|
| 105 | ((member (cadr expr) vars :test #'equalp)
|
---|
| 106 | ;;Special handling of (expt var pow)
|
---|
| 107 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
| 108 | (make-poly-variable (length vars) pos (caddr expr))))
|
---|
| 109 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
| 110 | ;; Negative power means division in coefficient ring
|
---|
| 111 | ;; Non-integer power means non-polynomial coefficient
|
---|
| 112 | expr)
|
---|
| 113 | (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
|
---|
| 114 | (otherwise
|
---|
| 115 | expr))))))
|
---|
[3124] | 116 |
|
---|
[3356] | 117 | #|
|
---|
[3124] | 118 | (defun poly-eval-scalar (expr
|
---|
| 119 | &aux
|
---|
| 120 | (order #'lex>))
|
---|
| 121 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
| 122 | (declare (type ring ring))
|
---|
| 123 | (poly-lc (poly-eval expr nil ring order)))
|
---|
[3356] | 124 | |#
|
---|
[3124] | 125 |
|
---|
| 126 |
|
---|
| 127 | (defun read-infix-form (&key (stream t))
|
---|
| 128 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 129 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 130 |
|
---|
| 131 | - polynomials in fully expanded forms with coefficients
|
---|
| 132 | written in front of symbolic expressions; constants can be optionally
|
---|
| 133 | enclosed in (); for example, the infix form
|
---|
| 134 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 135 | parses to
|
---|
| 136 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 137 |
|
---|
| 138 | - lists of polynomials; for example
|
---|
| 139 | [X-Y, X^2+3*Z]
|
---|
| 140 | parses to
|
---|
| 141 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 142 | where the first symbol [ marks a list of polynomials.
|
---|
| 143 |
|
---|
| 144 | -other infix expressions, for example
|
---|
| 145 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 146 | parses to:
|
---|
| 147 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 148 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 149 | (read-from-string
|
---|
| 150 | (concatenate 'string
|
---|
| 151 | "#I("
|
---|
| 152 | (with-output-to-string (s)
|
---|
| 153 | (loop
|
---|
| 154 | (multiple-value-bind (line eof)
|
---|
| 155 | (read-line stream t)
|
---|
| 156 | (format s "~A" line)
|
---|
| 157 | (when eof (return)))))
|
---|
| 158 | ")")))
|
---|
| 159 |
|
---|
| 160 | (defun read-poly (vars &key
|
---|
| 161 | (stream t)
|
---|
| 162 | (order #'lex>))
|
---|
| 163 | "Reads an expression in prefix form from a stream STREAM.
|
---|
| 164 | The expression read from the strem should represent a polynomial or a
|
---|
| 165 | list of polynomials in variables VARS, over the ring RING. The
|
---|
| 166 | polynomial or list of polynomials is returned, with terms in each
|
---|
| 167 | polynomial ordered according to monomial order ORDER."
|
---|
[3369] | 168 | (poly-eval (read-infix-form :stream stream) vars order))
|
---|
[3124] | 169 |
|
---|
| 170 | (defun string->poly (str vars
|
---|
| 171 | &optional
|
---|
| 172 | (order #'lex>))
|
---|
| 173 | "Converts a string STR to a polynomial in variables VARS."
|
---|
| 174 | (with-input-from-string (s str)
|
---|
[3834] | 175 | (change-class (read-poly vars :stream s :order order) 'symbolic-poly :vars vars)))
|
---|
[3124] | 176 |
|
---|
| 177 | (defun poly->alist (p)
|
---|
| 178 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
| 179 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
| 180 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
| 181 | corresponding coefficient in the ring."
|
---|
| 182 | (cond
|
---|
| 183 | ((poly-p p)
|
---|
[3729] | 184 | (mapcar #'->list (poly-termlist p)))
|
---|
[3124] | 185 | ((and (consp p) (eq (car p) :[))
|
---|
| 186 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
| 187 |
|
---|
| 188 | (defun string->alist (str vars
|
---|
| 189 | &optional
|
---|
| 190 | (order #'lex>))
|
---|
| 191 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
| 192 | an association list (... (MONOM . COEFF) ...)."
|
---|
[3729] | 193 | (poly->alist (string->poly str vars order)))
|
---|
[3124] | 194 |
|
---|
| 195 | (defun poly-equal-no-sugar-p (p q)
|
---|
| 196 | "Compare polynomials for equality, ignoring sugar."
|
---|
| 197 | (declare (type poly p q))
|
---|
| 198 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
| 199 |
|
---|
| 200 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
| 201 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
| 202 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
| 203 |
|
---|
| 204 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
| 205 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
| 206 | (every #'poly-equal-no-sugar-p p q))
|
---|
[3831] | 207 |
|
---|
[3853] | 208 | (defmethod ->infix :around ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
|
---|
[3858] | 209 | "Convert a symbolic polynomial SELF to infix form, using variables VARS. The default
|
---|
[3855] | 210 | value of VARS is the corresponding slot value of SELF."
|
---|
[3854] | 211 | (call-next-method self vars))
|
---|
[3853] | 212 |
|
---|
[3836] | 213 | (defgeneric poly->string (self &optional vars)
|
---|
[3838] | 214 | (:documentation "Render polynomial SELF as a string, using symbolic variables VARS.")
|
---|
[3839] | 215 | (:method ((self poly) &optional (vars nil))
|
---|
[3842] | 216 | ;; Ensure that the number of variables matches the dimension
|
---|
[3843] | 217 | (assert (= (length vars) (poly-dimension self)))
|
---|
[3839] | 218 | (infix-print (->infix self vars)))
|
---|
[3836] | 219 | (:method ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
|
---|
| 220 | (infix-print (->infix self vars))))
|
---|