[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)
|
---|
[3373] | 24 | (:export "SYMBOLIC-POLY" "READ-INFIX-FORM" "STRING->POLY" "+LIST-MARKER+")
|
---|
[3240] | 25 | (:documentation "Implements symbolic polynomials. A symbolic
|
---|
| 26 | polynomial is and object which uses symbolic variables for reading and
|
---|
| 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 |
|
---|
[3273] | 52 | (defmethod r-equalp ((self symbolic-poly) (other symbolic-poly))
|
---|
[3278] | 53 | (when (r-equalp (symbolic-poly-vars self) (symbolic-poly-vars other))
|
---|
[3274] | 54 | (call-next-method)))
|
---|
[3273] | 55 |
|
---|
[3280] | 56 | (defmethod update-instance-for-different-class :after ((old poly) (new symbolic-poly) &key)
|
---|
[3337] | 57 | "After adding variables to NEW, we need to make sure that the number
|
---|
| 58 | of variables given by POLY-DIMENSION is consistent with VARS."
|
---|
[3333] | 59 | (assert (= (length (symbolic-poly-vars new)) (poly-dimension new))))
|
---|
[3279] | 60 |
|
---|
[3367] | 61 | (defgeneric poly-eval (expr vars order)
|
---|
[3354] | 62 | (:documentation "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
[3124] | 63 | variables VARS. Return the resulting polynomial or list of
|
---|
| 64 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
| 65 | replaced with their analogues in the ring of polynomials, and the
|
---|
| 66 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
| 67 | of polynomials in internal form. A similar operation in another computer
|
---|
[3354] | 68 | algebra system could be called 'expand' or so.")
|
---|
[3367] | 69 | (:method ((expr symbolic-poly) vars order) expr)
|
---|
| 70 | (:method (expr vars order)
|
---|
[3382] | 71 | (labels ((p-eval (p) (poly-eval p vars order))
|
---|
[3381] | 72 | (p-eval-list (plist) (mapcar #'p-eval plist)))
|
---|
| 73 | (cond
|
---|
| 74 | ((eq expr 0)
|
---|
| 75 | (make-instance 'symbolic-poly :dimension (length vars) :vars vars))
|
---|
| 76 | ((member expr vars :test #'equalp)
|
---|
| 77 | (let ((pos (position expr vars :test #'equalp)))
|
---|
| 78 | (make-monom-variable (length vars) pos)))
|
---|
| 79 | ((atom expr)
|
---|
| 80 | expr)
|
---|
| 81 | ((eq (car expr) +list-marker+)
|
---|
| 82 | (cons +list-marker+ (p-eval-list (cdr expr))))
|
---|
| 83 | (t
|
---|
| 84 | (case (car expr)
|
---|
| 85 | (+ (reduce #'r+ (p-eval-list (cdr expr))))
|
---|
| 86 | (- (case (length expr)
|
---|
| 87 | (1 (make-poly-zero))
|
---|
| 88 | (2 (poly-uminus ring (p-eval (cadr expr))))
|
---|
| 89 | (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
|
---|
| 90 | (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
|
---|
| 91 | (reduce #'p-add (p-eval-list (cddr expr)))))))
|
---|
| 92 | (*
|
---|
| 93 | (if (endp (cddr expr)) ;unary
|
---|
| 94 | (p-eval (cdr expr))
|
---|
| 95 | (reduce #'(lambda (p q) (r* p q)) (p-eval-list (cdr expr)))))
|
---|
| 96 | (/
|
---|
| 97 | ;; A polynomial can be divided by a scalar
|
---|
| 98 | (cond
|
---|
| 99 | ((endp (cddr expr))
|
---|
| 100 | ;; A special case (/ ?), the inverse
|
---|
| 101 | (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
|
---|
| 102 | (t
|
---|
| 103 | (let ((num (p-eval (cadr expr)))
|
---|
| 104 | (denom-inverse (apply (ring-div ring)
|
---|
| 105 | (cons (funcall (ring-unit ring))
|
---|
| 106 | (mapcar #'p-eval-scalar (cddr expr))))))
|
---|
| 107 | (scalar-times-poly ring denom-inverse num)))))
|
---|
| 108 | (expt
|
---|
| 109 | (cond
|
---|
| 110 | ((member (cadr expr) vars :test #'equalp)
|
---|
| 111 | ;;Special handling of (expt var pow)
|
---|
| 112 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
| 113 | (make-poly-variable ring (length vars) pos (caddr expr))))
|
---|
| 114 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
| 115 | ;; Negative power means division in coefficient ring
|
---|
| 116 | ;; Non-integer power means non-polynomial coefficient
|
---|
| 117 | (coerce-coeff ring expr vars))
|
---|
| 118 | (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
|
---|
| 119 | (otherwise
|
---|
| 120 | (coerce-coeff ring expr vars))))))))
|
---|
[3124] | 121 |
|
---|
[3356] | 122 | #|
|
---|
[3124] | 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)))
|
---|
[3356] | 131 | |#
|
---|
[3124] | 132 |
|
---|
| 133 |
|
---|
| 134 | (defun read-infix-form (&key (stream t))
|
---|
| 135 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 136 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 137 |
|
---|
| 138 | - polynomials in fully expanded forms with coefficients
|
---|
| 139 | written in front of symbolic expressions; constants can be optionally
|
---|
| 140 | enclosed in (); for example, the infix form
|
---|
| 141 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 142 | parses to
|
---|
| 143 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 144 |
|
---|
| 145 | - lists of polynomials; for example
|
---|
| 146 | [X-Y, X^2+3*Z]
|
---|
| 147 | parses to
|
---|
| 148 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 149 | where the first symbol [ marks a list of polynomials.
|
---|
| 150 |
|
---|
| 151 | -other infix expressions, for example
|
---|
| 152 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 153 | parses to:
|
---|
| 154 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 155 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 156 | (read-from-string
|
---|
| 157 | (concatenate 'string
|
---|
| 158 | "#I("
|
---|
| 159 | (with-output-to-string (s)
|
---|
| 160 | (loop
|
---|
| 161 | (multiple-value-bind (line eof)
|
---|
| 162 | (read-line stream t)
|
---|
| 163 | (format s "~A" line)
|
---|
| 164 | (when eof (return)))))
|
---|
| 165 | ")")))
|
---|
| 166 |
|
---|
| 167 | (defun read-poly (vars &key
|
---|
| 168 | (stream t)
|
---|
| 169 | (order #'lex>))
|
---|
| 170 | "Reads an expression in prefix form from a stream STREAM.
|
---|
| 171 | The expression read from the strem should represent a polynomial or a
|
---|
| 172 | list of polynomials in variables VARS, over the ring RING. The
|
---|
| 173 | polynomial or list of polynomials is returned, with terms in each
|
---|
| 174 | polynomial ordered according to monomial order ORDER."
|
---|
[3369] | 175 | (poly-eval (read-infix-form :stream stream) vars order))
|
---|
[3124] | 176 |
|
---|
| 177 | (defun string->poly (str vars
|
---|
| 178 | &optional
|
---|
| 179 | (order #'lex>))
|
---|
| 180 | "Converts a string STR to a polynomial in variables VARS."
|
---|
| 181 | (with-input-from-string (s str)
|
---|
[3370] | 182 | (read-poly vars :stream s :order order)))
|
---|
[3124] | 183 |
|
---|
| 184 | (defun poly->alist (p)
|
---|
| 185 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
| 186 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
| 187 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
| 188 | corresponding coefficient in the ring."
|
---|
| 189 | (cond
|
---|
| 190 | ((poly-p p)
|
---|
| 191 | (mapcar #'term->cons (poly-termlist p)))
|
---|
| 192 | ((and (consp p) (eq (car p) :[))
|
---|
| 193 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
| 194 |
|
---|
| 195 | (defun string->alist (str vars
|
---|
| 196 | &optional
|
---|
| 197 | (ring +ring-of-integers+)
|
---|
| 198 | (order #'lex>))
|
---|
| 199 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
| 200 | an association list (... (MONOM . COEFF) ...)."
|
---|
| 201 | (poly->alist (string->poly str vars ring order)))
|
---|
| 202 |
|
---|
| 203 | (defun poly-equal-no-sugar-p (p q)
|
---|
| 204 | "Compare polynomials for equality, ignoring sugar."
|
---|
| 205 | (declare (type poly p q))
|
---|
| 206 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
| 207 |
|
---|
| 208 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
| 209 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
| 210 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
| 211 |
|
---|
| 212 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
| 213 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
| 214 | (every #'poly-equal-no-sugar-p p q))
|
---|
[3235] | 215 |
|
---|