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