2 ;;; macros, functions and methods for matrix element access
4 ;;; Copyright (c) 2004-2006 Cyrus Harmon (ch-lisp@bobobeach.com)
5 ;;; All rights reserved.
7 ;;; Redistribution and use in source and binary forms, with or without
8 ;;; modification, are permitted provided that the following conditions
11 ;;; * Redistributions of source code must retain the above copyright
12 ;;; notice, this list of conditions and the following disclaimer.
14 ;;; * Redistributions in binary form must reproduce the above
15 ;;; copyright notice, this list of conditions and the following
16 ;;; disclaimer in the documentation and/or other materials
17 ;;; provided with the distribution.
19 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
20 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 (defparameter *print-matrix-newlines
* t
)
35 (defparameter *print-matrix-float-format
* nil
)
36 (defparameter *matrix-print
* t
)
37 (defparameter *matrix-print-row-limit
* 8)
38 (defparameter *matrix-print-col-limit
* 8)
40 (defgeneric print-range
(m startr endr startc endc
&optional stream
))
41 (defmethod print-range ((m matrix
)
42 (startr fixnum
) (endr fixnum
)
43 (startc fixnum
) (endc fixnum
)
45 (let ((val-format-spec (if *print-matrix-float-format
*
46 *print-matrix-float-format
*
47 (val-format (class-of m
)))))
49 (do ((i startr
(1+ i
)))
53 (if *print-matrix-newlines
*
55 (format stream
"~&~1,0T"))))
56 (do ((j startc
(1+ j
)))
58 (format stream
(if (= j startc
)
60 (concatenate 'string
" " val-format-spec
)) (mref m i j
))))
63 (defgeneric print-matrix
(m))
64 (defmethod print-matrix ((m matrix
))
65 (destructuring-bind (endr endc
) (mapcar #'1-
(dim m
))
66 (print-range m
0 endr
0 endc
))
69 (defun print-matrix-line (obj stream val-format-spec i startc endc lastcol
)
70 (do ((j startc
(1+ j
)))
72 (format stream
(if (= j startc
)
74 (concatenate 'string
" " val-format-spec
)) (mref obj i j
)))
75 (cond ((>= lastcol
(1- *matrix-print-col-limit
*))
76 (if (= lastcol
(1- *matrix-print-col-limit
*))
77 (format stream
(concatenate 'string
" " val-format-spec
) (mref obj i lastcol
))
78 (format stream
(concatenate 'string
" ... " val-format-spec
) (mref obj i lastcol
))))))
80 (defmethod print-object ((obj matrix
) stream
)
81 (print-unreadable-object (obj stream
:type t
:identity
(not *matrix-print
*))
83 (cond ((= (length (matrix-dimensions obj
)) 2)
84 (let ((startr 0) (endr (min (1- (rows obj
)) (- *matrix-print-row-limit
* 2)))
85 (startc 0) (endc (min (1- (cols obj
)) (- *matrix-print-col-limit
* 2))))
86 (let ((val-format-spec (if *print-matrix-float-format
*
87 *print-matrix-float-format
*
88 (val-format (class-of obj
))))
89 (lastrow (1- (rows obj
)))
90 (lastcol (1- (cols obj
))))
92 (do ((i startr
(1+ i
)))
96 (if *print-matrix-newlines
*
98 (format stream
"~&~1,0T"))))
99 (print-matrix-line obj stream val-format-spec i startc endc lastcol
))
100 (cond ((>= lastrow
(1- *matrix-print-row-limit
*))
101 (if (= lastrow
(1- *matrix-print-row-limit
*))
102 (format stream
(concatenate 'string
";~&~1,0T"))
103 (format stream
(concatenate 'string
";~& ... ~&~1,0T")))
104 (print-matrix-line obj stream val-format-spec lastrow startc endc lastcol
)))
105 (format stream
"]"))))
106 (t (format stream
"of dimensions ~A" (matrix-dimensions obj
)))))))