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