source: CGBLisp/src/RCS/printer.lisp,v@ 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: 4.5 KB
Line 
1head 1.3;
2access;
3symbols;
4locks; strict;
5comment @;;; @;
6
7
81.3
9date 2009.01.22.04.06.32; author marek; state Exp;
10branches;
11next 1.2;
12
131.2
14date 2009.01.19.09.29.46; author marek; state Exp;
15branches;
16next 1.1;
17
181.1
19date 2009.01.19.07.51.57; author marek; state Exp;
20branches;
21next ;
22
23
24desc
25@@
26
27
281.3
29log
30@*** empty log message ***
31@
32text
33@#|
34 $Id$
35 *--------------------------------------------------------------------------*
36 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@@math.arizona.edu) |
37 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
38 | |
39 | Everyone is permitted to copy, distribute and modify the code in this |
40 | directory, as long as this copyright note is preserved verbatim. |
41 *--------------------------------------------------------------------------*
42|#
43(defpackage "PRINTER"
44 (:export poly-print print-term print-monom)
45 (:use "COMMON-LISP"))
46
47(in-package "PRINTER")
48
49#+debug(proclaim '(optimize (speed 0) (debug 3)))
50#-debug(proclaim '(optimize (speed 3) (safety 0)))
51
52(defun poly-print (plist vars &optional (stream t))
53 "Prints a polynomial or a list of polynomials PLIST using infix syntax compatible
54with most software systems. The following data representations are assumed:
551) Polynomial list is ([ poly1 poly2 ...)
562) Polynomial is a list (term1 term2 ...)
573) Term is (monom . number)
584) Monom is (number1 number2 ...) and is a list of powers at corresponding
59variables
60Variable names must be provided to the printer explicitly."
61 (cond
62 ((and (symbolp (car plist)) (string= (symbol-name (car plist)) "["))
63 (format stream "[ ")
64 (poly-print-2 (rest plist) vars stream)
65 (format stream " ]"))
66 ;; Not a list but a single polynomial
67 (t (poly-print-1 plist vars stream))))
68
69(defun poly-print-1 (p vars &optional (stream t))
70 "An auxillary function of POLY-PRINT. It prints a single polynomial P."
71 (labels ((infix-sum (l &optional (beg t))
72 (unless (endp l)
73 (infix-term (first l) beg)
74 (infix-sum (rest l) nil)))
75 (infix-term (l &optional beg)
76 (unless (zerop (cdr l))
77 (case (cdr l)
78 (1 (unless beg (format stream " + "))
79 (infix-monom (first l) vars t))
80 (-1 (format stream " - ") (infix-monom (first l) vars t))
81 (otherwise
82 (cond
83 ((plusp (cdr l)) (unless beg (format stream " + ")))
84 ((minusp (cdr l)) (format stream " - ")))
85 (format stream "~d" (abs (cdr l)))
86 (infix-monom (first l) vars nil)))))
87 (infix-monom (l vars &optional beg)
88 (cond
89 ((endp l) (when beg (format stream "~d" 1)))
90 (t
91 (unless (zerop (car l))
92 (unless beg (format stream " * "))
93 ;; if in x^n we have n=1 then print just x
94 (format stream "~a~:[^~d~;~*~]" (car vars)
95 (= (car l) 1) (car l)))
96 (infix-monom (rest l) (rest vars) (and (zerop (car l)) beg)))
97 )))
98 (if (endp p)
99 (format stream "0")
100 (infix-sum p)))
101 p)
102
103(defun poly-print-2 (plist vars stream &optional (beg t))
104 "An auxillary function of POLY-PRINT. It prints a comma-separated list of polynomials PLIST."
105 (unless (endp plist)
106 (unless beg (format stream ", "))
107 (poly-print-1 (first plist) vars stream)
108 (poly-print-2 (rest plist) vars stream nil)))
109
110
111(defun print-term (l vars &optional (stream t) beg)
112 "An auxillary function of POLY-PRINT. It prints a single term L."
113 (unless (zerop (cdr l))
114 (case (cdr l)
115 (1 (unless beg (format stream " + "))
116 (print-monom (first l) vars stream t))
117 (-1 (format stream " - ") (print-monom (first l) vars stream t))
118 (otherwise
119 (cond
120 ((plusp (cdr l)) (unless beg (format stream " + ")))
121 ((minusp (cdr l)) (format stream " - ")))
122 (format stream "~d" (abs (cdr l)))
123 (print-monom (first l) vars stream nil)))))
124
125(defun print-monom (l vars &optional (stream t) beg)
126 "An auxillary function of POLY-PRINT. It prints a single monomial L."
127 (cond
128 ((endp l) (when beg (format stream "~d" 1)))
129 (t
130 (unless (zerop (car l))
131 (unless beg (format stream " * "))
132 ;; if in x^n we have n=1 then print just x
133 (format stream "~a~:[^~d~;~*~]" (car vars)
134 (= (car l) 1) (car l)))
135 (print-monom (rest l) (rest vars) stream (and (zerop (car l)) beg)))
136 ))
137@
138
139
1401.2
141log
142@*** empty log message ***
143@
144text
145@d17 2
146a18 2
147;;(proclaim '(optimize (speed 0) (debug 3)))
148(proclaim '(optimize (speed 3) (safety 0)))
149@
150
151
1521.1
153log
154@Initial revision
155@
156text
157@d2 1
158a2 1
159 $Id: printer.lisp,v 1.7 1997/12/03 06:05:32 marek Exp $
160d17 2
161a18 1
162(proclaim '(optimize (speed 0) (debug 3)))
163@
Note: See TracBrowser for help on using the repository browser.