| 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" "->INFIX")
|
---|
| 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 | (defclass symbolic-poly (poly)
|
---|
| 32 | ((vars :initform nil
|
---|
| 33 | :initarg :vars
|
---|
| 34 | :accessor symbolic-poly-vars)
|
---|
| 35 | )
|
---|
| 36 | (:default-initargs :termlist nil :vars nil))
|
---|
| 37 |
|
---|
| 38 | (defmethod print-object ((self symbolic-poly) stream)
|
---|
| 39 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 40 | (with-accessors ((dimension poly-dimension)
|
---|
| 41 | (termlist poly-termlist)
|
---|
| 42 | (order poly-term-order)
|
---|
| 43 | (vars symbolic-poly-vars))
|
---|
| 44 | self
|
---|
| 45 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A VARS=~A"
|
---|
| 46 | dimension termlist order vars))))
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | (defmethod universal-equalp ((self symbolic-poly) (other symbolic-poly))
|
---|
| 50 | (when (universal-equalp (symbolic-poly-vars self) (symbolic-poly-vars other))
|
---|
| 51 | (call-next-method)))
|
---|
| 52 |
|
---|
| 53 | (defmethod universal-equalp ((self symbol) (other symbol))
|
---|
| 54 | (eq self other))
|
---|
| 55 |
|
---|
| 56 | (defmethod update-instance-for-different-class :after ((old poly) (new symbolic-poly) &key)
|
---|
| 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."
|
---|
| 59 | (assert (= (length (symbolic-poly-vars new)) (poly-dimension new))))
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | #|
|
---|
| 63 | (defun poly-eval-scalar (expr
|
---|
| 64 | &aux
|
---|
| 65 | (order #'lex>))
|
---|
| 66 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
| 67 | (declare (type ring ring))
|
---|
| 68 | (poly-lc (poly-eval expr nil ring order)))
|
---|
| 69 | |#
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | (defun read-infix-form (&key (stream t))
|
---|
| 73 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 74 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 75 |
|
---|
| 76 | - polynomials in fully expanded forms with coefficients
|
---|
| 77 | written in front of symbolic expressions; constants can be optionally
|
---|
| 78 | enclosed in (); for example, the infix form
|
---|
| 79 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 80 | parses to
|
---|
| 81 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 82 |
|
---|
| 83 | - lists of polynomials; for example
|
---|
| 84 | [X-Y, X^2+3*Z]
|
---|
| 85 | parses to
|
---|
| 86 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 87 | where the first symbol [ marks a list of polynomials.
|
---|
| 88 |
|
---|
| 89 | -other infix expressions, for example
|
---|
| 90 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 91 | parses to:
|
---|
| 92 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 93 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 94 | (read-from-string
|
---|
| 95 | (concatenate 'string
|
---|
| 96 | "#I("
|
---|
| 97 | (with-output-to-string (s)
|
---|
| 98 | (loop
|
---|
| 99 | (multiple-value-bind (line eof)
|
---|
| 100 | (read-line stream t)
|
---|
| 101 | (format s "~A" line)
|
---|
| 102 | (when eof (return)))))
|
---|
| 103 | ")")))
|
---|
| 104 |
|
---|
| 105 | (defun read-poly (vars &key
|
---|
| 106 | (stream t)
|
---|
| 107 | (order #'lex>))
|
---|
| 108 | "Reads an expression in prefix form from a stream STREAM.
|
---|
| 109 | The expression read from the strem should represent a polynomial or a
|
---|
| 110 | list of polynomials in variables VARS, over the ring RING. The
|
---|
| 111 | polynomial or list of polynomials is returned, with terms in each
|
---|
| 112 | polynomial ordered according to monomial order ORDER."
|
---|
| 113 | (poly-eval (read-infix-form :stream stream) vars order))
|
---|
| 114 |
|
---|
| 115 | (defun string->poly (str vars
|
---|
| 116 | &optional
|
---|
| 117 | (order #'lex>))
|
---|
| 118 | "Converts a string STR to a polynomial in variables VARS."
|
---|
| 119 | (with-input-from-string (s str)
|
---|
| 120 | (let ((p-or-plist (read-poly vars :stream s :order order)))
|
---|
| 121 | (etypecase p-or-plist
|
---|
| 122 | (poly (change-class p-or-plist 'symbolic-poly :vars vars))
|
---|
| 123 | (cons
|
---|
| 124 | (setf (cdr p-or-plist) (mapcar #'(lambda (p) (change-class p 'symbolic-poly :vars vars)) (cdr p-or-plist)))
|
---|
| 125 | p-or-plist)))))
|
---|
| 126 |
|
---|
| 127 | (defun poly->alist (p)
|
---|
| 128 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
| 129 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
| 130 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
| 131 | corresponding coefficient in the ring."
|
---|
| 132 | (cond
|
---|
| 133 | ((poly-p p)
|
---|
| 134 | (mapcar #'->list (poly-termlist p)))
|
---|
| 135 | ((and (consp p) (eq (car p) :[))
|
---|
| 136 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
| 137 |
|
---|
| 138 | (defun string->alist (str vars
|
---|
| 139 | &optional
|
---|
| 140 | (order #'lex>))
|
---|
| 141 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
| 142 | an association list (... (MONOM . COEFF) ...)."
|
---|
| 143 | (poly->alist (string->poly str vars order)))
|
---|
| 144 |
|
---|
| 145 | (defun poly-equal-no-sugar-p (p q)
|
---|
| 146 | "Compare polynomials for equality, ignoring sugar."
|
---|
| 147 | (declare (type poly p q))
|
---|
| 148 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
| 149 |
|
---|
| 150 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
| 151 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
| 152 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
| 153 |
|
---|
| 154 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
| 155 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
| 156 | (every #'poly-equal-no-sugar-p p q))
|
---|
| 157 |
|
---|
| 158 | (defmethod ->sexp :around ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
|
---|
| 159 | "Convert a symbolic polynomial SELF to infix form, using variables VARS. The default
|
---|
| 160 | value of VARS is the corresponding slot value of SELF."
|
---|
| 161 | (call-next-method self vars))
|
---|
| 162 |
|
---|
| 163 | (defgeneric poly->string (self &optional vars)
|
---|
| 164 | (:documentation "Render polynomial SELF as a string, using symbolic variables VARS.")
|
---|
| 165 | (:method ((self list) &optional (vars nil vars-p))
|
---|
| 166 | (assert (eql (car self) :[))
|
---|
| 167 | (cond (vars-p
|
---|
| 168 | (cons :[ (mapcar #'(lambda (p) (poly->string p vars)) (cdr self))))
|
---|
| 169 | (t
|
---|
| 170 | (cons :[ (mapcar #'(lambda (p) (poly->string p)) (cdr self))))))
|
---|
| 171 | (:method ((self poly) &optional (vars nil))
|
---|
| 172 | ;; Ensure that the number of variables matches the dimension
|
---|
| 173 | (assert (= (length vars) (poly-dimension self)))
|
---|
| 174 | (infix-print-to-string (->sexp self vars)))
|
---|
| 175 | (:method ((self symbolic-poly) &optional (vars (symbolic-poly-vars self)))
|
---|
| 176 | (infix-print-to-string (->sexp self vars))))
|
---|