4 (defmacro def-unary-op
(name op type-1 accumulator-type
&key suffix
)
5 (let ((element-type-1 (element-type (find-class `,type-1
)))
6 (accumulator-element-type (element-type (find-class `,accumulator-type
))))
8 (defmethod ,(ch-util:make-intern
(concatenate 'string name
"-range" suffix
))
9 ((m ,type-1
) startr endr startc endc
&key in-place
)
10 (destructuring-bind (mr mc
) (dim m
)
12 (with-typed-mref (m ,element-type-1
)
13 (do ((i startr
(1+ i
)))
15 (declare (dynamic-extent i
) (type fixnum i
))
16 (do ((j startc
(1+ j
)))
18 (declare (dynamic-extent j
) (type fixnum j
))
22 (let ((p (make-instance ',accumulator-type
:rows mr
:cols mc
)))
23 (with-typed-mref (m ,element-type-1
)
24 (with-typed-mref (p ,accumulator-element-type
)
25 (do ((i startr
(1+ i
)))
27 (declare (dynamic-extent i
) (type fixnum i
))
28 (do ((j startc
(1+ j
)))
30 (declare (dynamic-extent j
) (type fixnum j
))
32 (,op
(mref m i j
)))))))
35 (defmethod ,(ch-util:make-intern
(concatenate 'string name suffix
))
36 ((m ,type-1
) &key in-place
)
38 (with-typed-mref (m ,element-type-1
)
39 (loop for i from
0 below
(matrix-total-size m
)
40 do
(setf (row-major-mref m i
)
41 (,op
(row-major-mref m i
))))
43 (let ((p (make-instance ',accumulator-type
:dimensions
(matrix-dimensions m
))))
44 (with-typed-mref (m ,element-type-1
)
45 (with-typed-mref (p ,accumulator-element-type
)
46 (loop for i from
0 below
(matrix-total-size m
)
47 do
(setf (row-major-mref p i
)
48 (,op
(row-major-mref m i
))))))
51 (defmacro def-binary-op
(name op type-1 type-2 accumulator-type
&key suffix
(allow-in-place t
))
52 (let ((element-type-1 (element-type (find-class `,type-1
)))
53 (element-type-2 (element-type (find-class `,type-2
)))
54 (accumulator-element-type (element-type (find-class `,accumulator-type
))))
56 (defmethod ,(ch-util:make-intern
(concatenate 'string name
"-range" suffix
))
57 ((m ,type-1
) (n ,type-2
) startr endr startc endc
&key in-place
)
58 (destructuring-bind (mr mc
) (dim m
)
61 `(with-typed-mref (m ,element-type-1
)
62 (with-typed-mref (n ,element-type-2
)
63 (do ((i startr
(1+ i
)))
65 (declare (dynamic-extent i
) (type fixnum i
))
66 (do ((j startc
(1+ j
)))
68 (declare (dynamic-extent j
) (type fixnum j
))
70 (,op
(mref m i j
) (mref n i j
))))))
72 `(error 'matrix-argument-error
74 "in-place operation not allowed (~S of ~S and ~S"
75 :format-arguments
(list '+ ',type-1
',type-2
)))
76 (let ((p (make-instance ',accumulator-type
:rows mr
:cols mc
)))
77 (with-typed-mref (m ,element-type-1
)
78 (with-typed-mref (p ,accumulator-element-type
)
79 (with-typed-mref (n ,element-type-2
)
80 (do ((i startr
(1+ i
)))
82 (declare (dynamic-extent i
) (type fixnum i
))
83 (do ((j startc
(1+ j
)))
85 (declare (dynamic-extent j
) (type fixnum j
))
87 (,op
(mref m i j
) (mref n i j
))))))))
90 (defmethod ,(ch-util:make-intern
(concatenate 'string name suffix
))
91 ((m ,type-1
) (n ,type-2
) &key in-place
)
94 `(with-typed-mref (m ,element-type-1
)
95 (with-typed-mref (n ,element-type-2
)
96 (loop for i from
0 below
(matrix-total-size m
)
97 do
(setf (row-major-mref m i
)
98 (,op
(row-major-mref m i
) (row-major-mref n i
)))))
100 `(error 'matrix-argument-error
102 "in-place operation not allowed (~S of ~S and ~S"
103 :format-arguments
(list '+ ',type-1
',type-2
)))
104 (let ((p (make-instance ',accumulator-type
:dimensions
(matrix-dimensions m
))))
105 (with-typed-mref (m ,element-type-1
)
106 (with-typed-mref (n ,element-type-2
)
107 (with-typed-mref (p ,accumulator-element-type
)
108 (loop for i from
0 below
(matrix-total-size m
)
109 do
(setf (row-major-mref p i
)
110 (,op
(row-major-mref m i
) (row-major-mref n i
)))))))