[98] | 1 | ;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*-
|
---|
[1] | 2 | #|
|
---|
| 3 | *--------------------------------------------------------------------------*
|
---|
| 4 | | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
|
---|
| 5 | | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
|
---|
| 6 | | |
|
---|
| 7 | | Everyone is permitted to copy, distribute and modify the code in this |
|
---|
| 8 | | directory, as long as this copyright note is preserved verbatim. |
|
---|
| 9 | *--------------------------------------------------------------------------*
|
---|
| 10 | |#
|
---|
| 11 | (defpackage "PRINTER"
|
---|
| 12 | (:export poly-print print-term print-monom)
|
---|
| 13 | (:use "COMMON-LISP"))
|
---|
| 14 |
|
---|
| 15 | (in-package "PRINTER")
|
---|
| 16 |
|
---|
[98] | 17 | (proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 3)))
|
---|
[1] | 18 |
|
---|
| 19 | (defun poly-print (plist vars &optional (stream t))
|
---|
| 20 | "Prints a polynomial or a list of polynomials PLIST using infix syntax compatible
|
---|
| 21 | with most software systems. The following data representations are assumed:
|
---|
| 22 | 1) Polynomial list is ([ poly1 poly2 ...)
|
---|
| 23 | 2) Polynomial is a list (term1 term2 ...)
|
---|
| 24 | 3) Term is (monom . number)
|
---|
| 25 | 4) Monom is (number1 number2 ...) and is a list of powers at corresponding
|
---|
| 26 | variables
|
---|
| 27 | Variable names must be provided to the printer explicitly."
|
---|
| 28 | (cond
|
---|
| 29 | ((and (symbolp (car plist)) (string= (symbol-name (car plist)) "["))
|
---|
| 30 | (format stream "[ ")
|
---|
| 31 | (poly-print-2 (rest plist) vars stream)
|
---|
| 32 | (format stream " ]"))
|
---|
| 33 | ;; Not a list but a single polynomial
|
---|
| 34 | (t (poly-print-1 plist vars stream))))
|
---|
| 35 |
|
---|
| 36 | (defun poly-print-1 (p vars &optional (stream t))
|
---|
| 37 | "An auxillary function of POLY-PRINT. It prints a single polynomial P."
|
---|
| 38 | (labels ((infix-sum (l &optional (beg t))
|
---|
| 39 | (unless (endp l)
|
---|
| 40 | (infix-term (first l) beg)
|
---|
| 41 | (infix-sum (rest l) nil)))
|
---|
| 42 | (infix-term (l &optional beg)
|
---|
| 43 | (unless (zerop (cdr l))
|
---|
| 44 | (case (cdr l)
|
---|
| 45 | (1 (unless beg (format stream " + "))
|
---|
| 46 | (infix-monom (first l) vars t))
|
---|
| 47 | (-1 (format stream " - ") (infix-monom (first l) vars t))
|
---|
| 48 | (otherwise
|
---|
| 49 | (cond
|
---|
| 50 | ((plusp (cdr l)) (unless beg (format stream " + ")))
|
---|
| 51 | ((minusp (cdr l)) (format stream " - ")))
|
---|
| 52 | (format stream "~d" (abs (cdr l)))
|
---|
| 53 | (infix-monom (first l) vars nil)))))
|
---|
| 54 | (infix-monom (l vars &optional beg)
|
---|
| 55 | (cond
|
---|
| 56 | ((endp l) (when beg (format stream "~d" 1)))
|
---|
| 57 | (t
|
---|
| 58 | (unless (zerop (car l))
|
---|
| 59 | (unless beg (format stream " * "))
|
---|
| 60 | ;; if in x^n we have n=1 then print just x
|
---|
| 61 | (format stream "~a~:[^~d~;~*~]" (car vars)
|
---|
| 62 | (= (car l) 1) (car l)))
|
---|
| 63 | (infix-monom (rest l) (rest vars) (and (zerop (car l)) beg)))
|
---|
| 64 | )))
|
---|
| 65 | (if (endp p)
|
---|
| 66 | (format stream "0")
|
---|
| 67 | (infix-sum p)))
|
---|
| 68 | p)
|
---|
| 69 |
|
---|
| 70 | (defun poly-print-2 (plist vars stream &optional (beg t))
|
---|
| 71 | "An auxillary function of POLY-PRINT. It prints a comma-separated list of polynomials PLIST."
|
---|
| 72 | (unless (endp plist)
|
---|
| 73 | (unless beg (format stream ", "))
|
---|
| 74 | (poly-print-1 (first plist) vars stream)
|
---|
| 75 | (poly-print-2 (rest plist) vars stream nil)))
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | (defun print-term (l vars &optional (stream t) beg)
|
---|
| 79 | "An auxillary function of POLY-PRINT. It prints a single term L."
|
---|
| 80 | (unless (zerop (cdr l))
|
---|
| 81 | (case (cdr l)
|
---|
| 82 | (1 (unless beg (format stream " + "))
|
---|
| 83 | (print-monom (first l) vars stream t))
|
---|
| 84 | (-1 (format stream " - ") (print-monom (first l) vars stream t))
|
---|
| 85 | (otherwise
|
---|
| 86 | (cond
|
---|
| 87 | ((plusp (cdr l)) (unless beg (format stream " + ")))
|
---|
| 88 | ((minusp (cdr l)) (format stream " - ")))
|
---|
| 89 | (format stream "~d" (abs (cdr l)))
|
---|
| 90 | (print-monom (first l) vars stream nil)))))
|
---|
| 91 |
|
---|
| 92 | (defun print-monom (l vars &optional (stream t) beg)
|
---|
| 93 | "An auxillary function of POLY-PRINT. It prints a single monomial L."
|
---|
| 94 | (cond
|
---|
| 95 | ((endp l) (when beg (format stream "~d" 1)))
|
---|
| 96 | (t
|
---|
| 97 | (unless (zerop (car l))
|
---|
| 98 | (unless beg (format stream " * "))
|
---|
| 99 | ;; if in x^n we have n=1 then print just x
|
---|
| 100 | (format stream "~a~:[^~d~;~*~]" (car vars)
|
---|
| 101 | (= (car l) 1) (car l)))
|
---|
| 102 | (print-monom (rest l) (rest vars) stream (and (zerop (car l)) beg)))
|
---|
| 103 | ))
|
---|