source: CGBLisp/src/printer.lisp@ 1

Last change on this file since 1 was 1, checked in by Marek Rychlik, 15 years ago

First import of a version circa 1997.

File size: 3.9 KB
Line 
1#|
2 $Id: printer.lisp,v 1.3 2009/01/22 04:06:32 marek Exp $
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
17#+debug(proclaim '(optimize (speed 0) (debug 3)))
18#-debug(proclaim '(optimize (speed 3) (safety 0)))
19
20(defun poly-print (plist vars &optional (stream t))
21 "Prints a polynomial or a list of polynomials PLIST using infix syntax compatible
22with most software systems. The following data representations are assumed:
231) Polynomial list is ([ poly1 poly2 ...)
242) Polynomial is a list (term1 term2 ...)
253) Term is (monom . number)
264) Monom is (number1 number2 ...) and is a list of powers at corresponding
27variables
28Variable names must be provided to the printer explicitly."
29 (cond
30 ((and (symbolp (car plist)) (string= (symbol-name (car plist)) "["))
31 (format stream "[ ")
32 (poly-print-2 (rest plist) vars stream)
33 (format stream " ]"))
34 ;; Not a list but a single polynomial
35 (t (poly-print-1 plist vars stream))))
36
37(defun poly-print-1 (p vars &optional (stream t))
38 "An auxillary function of POLY-PRINT. It prints a single polynomial P."
39 (labels ((infix-sum (l &optional (beg t))
40 (unless (endp l)
41 (infix-term (first l) beg)
42 (infix-sum (rest l) nil)))
43 (infix-term (l &optional beg)
44 (unless (zerop (cdr l))
45 (case (cdr l)
46 (1 (unless beg (format stream " + "))
47 (infix-monom (first l) vars t))
48 (-1 (format stream " - ") (infix-monom (first l) vars t))
49 (otherwise
50 (cond
51 ((plusp (cdr l)) (unless beg (format stream " + ")))
52 ((minusp (cdr l)) (format stream " - ")))
53 (format stream "~d" (abs (cdr l)))
54 (infix-monom (first l) vars nil)))))
55 (infix-monom (l vars &optional beg)
56 (cond
57 ((endp l) (when beg (format stream "~d" 1)))
58 (t
59 (unless (zerop (car l))
60 (unless beg (format stream " * "))
61 ;; if in x^n we have n=1 then print just x
62 (format stream "~a~:[^~d~;~*~]" (car vars)
63 (= (car l) 1) (car l)))
64 (infix-monom (rest l) (rest vars) (and (zerop (car l)) beg)))
65 )))
66 (if (endp p)
67 (format stream "0")
68 (infix-sum p)))
69 p)
70
71(defun poly-print-2 (plist vars stream &optional (beg t))
72 "An auxillary function of POLY-PRINT. It prints a comma-separated list of polynomials PLIST."
73 (unless (endp plist)
74 (unless beg (format stream ", "))
75 (poly-print-1 (first plist) vars stream)
76 (poly-print-2 (rest plist) vars stream nil)))
77
78
79(defun print-term (l vars &optional (stream t) beg)
80 "An auxillary function of POLY-PRINT. It prints a single term L."
81 (unless (zerop (cdr l))
82 (case (cdr l)
83 (1 (unless beg (format stream " + "))
84 (print-monom (first l) vars stream t))
85 (-1 (format stream " - ") (print-monom (first l) vars stream t))
86 (otherwise
87 (cond
88 ((plusp (cdr l)) (unless beg (format stream " + ")))
89 ((minusp (cdr l)) (format stream " - ")))
90 (format stream "~d" (abs (cdr l)))
91 (print-monom (first l) vars stream nil)))))
92
93(defun print-monom (l vars &optional (stream t) beg)
94 "An auxillary function of POLY-PRINT. It prints a single monomial L."
95 (cond
96 ((endp l) (when beg (format stream "~d" 1)))
97 (t
98 (unless (zerop (car l))
99 (unless beg (format stream " * "))
100 ;; if in x^n we have n=1 then print just x
101 (format stream "~a~:[^~d~;~*~]" (car vars)
102 (= (car l) 1) (car l)))
103 (print-monom (rest l) (rest vars) stream (and (zerop (car l)) beg)))
104 ))
Note: See TracBrowser for help on using the repository browser.