2 ;;; macros, functions and methods for finding matrix extrema
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.
36 (defmethod min-range ((m matrix
) (startr fixnum
) (endr fixnum
) (startc fixnum
) (endc fixnum
))
37 (declare (dynamic-extent startr endr startc endc
)
38 (fixnum startr endr startc endc
))
39 (let ((retval (val m startr startc
)))
40 (map-range m startr endr startc endc
42 (declare (ignore i j
))
43 (setf retval
(min retval v
))))
46 (defmethod max-range ((m matrix
) (startr fixnum
) (endr fixnum
) (startc fixnum
) (endc fixnum
))
47 (let ((retval (val m startr startc
)))
48 (map-range m startr endr startc endc
50 (declare (ignore i j
))
51 (setf retval
(max retval v
))))
54 (defmethod min-val ((m matrix
))
55 (let ((minval (row-major-mref m
0)))
56 (loop for i from
0 below
(matrix-total-size m
)
57 do
(setf minval
(min minval
(row-major-mref m i
))))
60 (defmethod max-val ((m matrix
))
61 (let ((maxval (row-major-mref m
0)))
62 (loop for i from
0 below
(matrix-total-size m
)
63 do
(setf maxval
(max maxval
(row-major-mref m i
))))
68 (defmacro def-matrix-min-max
(type)
69 (let ((element-type (element-type (find-class `,type
)))
70 (accumulator-element-type (element-type (find-class `,type
))))
72 (defmethod min-range ((m ,type
) (startr fixnum
) (endr fixnum
) (startc fixnum
) (endc fixnum
))
73 (let ((acc (coerce (aref (matrix-vals m
) startr startc
) ',accumulator-element-type
)))
74 (declare (type ,accumulator-element-type acc
))
75 (with-map-range m
,element-type startr endr startc endc
(a i j
)
76 (when (< (aref a i j
) acc
)
77 (setf acc
(aref a i j
))))
80 (defmethod max-range ((m ,type
) (startr fixnum
) (endr fixnum
) (startc fixnum
) (endc fixnum
))
81 (let ((acc (coerce (aref (matrix-vals m
) startr startc
) ',accumulator-element-type
)))
82 (declare (type ,accumulator-element-type acc
))
83 (with-map-range m
,element-type startr endr startc endc
(a i j
)
84 (when (> (aref a i j
) acc
)
85 (setf acc
(aref a i j
))))
88 (macrolet ((frob (type)
89 `(def-matrix-min-max ,type
)))
90 (frob double-float-matrix
)
91 (frob single-float-matrix
)
100 (frob integer-matrix
)