Skip to content

Commit

Permalink
* experimental/vector.lisp: added more necessary things for the
Browse files Browse the repository at this point in the history
	case of vector type derived from matrix type -> ugly code
	duplication

	* matrix.lisp (mref, setf mref): minor change in documentation
	(window, strides, transpose): minor change in argument name
  • Loading branch information
evanmonroig committed Jun 11, 2008
1 parent cfc847b commit 0400eb0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 63 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
@@ -1,3 +1,12 @@
2008-06-11 Evan Monroig <evan.monroig@gmail.com>

* experimental/vector.lisp: added more necessary things for the
case of vector type derived from matrix type -> ugly code
duplication

* matrix.lisp (mref, setf mref): minor change in documentation
(window, strides, transpose): minor change in argument name

2008-06-08 Evan Monroig <evan.monroig@gmail.com>

* experimental/vector.lisp: just some 5-minute exploratory
Expand Down
92 changes: 62 additions & 30 deletions experimental/vector.lisp
Expand Up @@ -3,66 +3,98 @@
;;;; * Vectors
;;;;
;;;; Vector can be viewed as matrices that happen to have one row (or
;;;; one column), or as a separate type.
;;;;
;;;; With a separate type, would it make sense to define row vectors
;;;; and column vectors?
;;;; one column), or as a separate type. We will define vectors as
;;;; matrices that have a restriction: one row or one column.
;;;;
;;;; One advantage of having vectors be subtypes of matrices is that
;;;; we don't need to re-specialize many generic functions (e.g., m*,
;;;; m+, m-, etc.), we can just use those that are defined for
;;;; matrices.
;;;;
;;;; But when we need to know that we have a vector, we can use that
;;;; type information..
;;;;
;;;; Anyway, if we have both row vectors and column vectors, we can
;;;; easily switch between the two by using the function TRANSPOSE,
;;;; without making a view.
;;;; A disadvantage is that we will have some code duplication for the
;;;; case of row- and column-vectors.

(defclass vector-like (matrix-like) ()
(:documentation "Abstract base class for 1-D vectors and vector
views."))

(defgeneric vref (vector i)
(:documentation "Return the I-th element of VECTOR. This method is
slow as it requires CLOS method dispatch and index calculation(s),
and should thus be replaced with vectorized or block operations
whenever possible"))

(defclass vector-like (matrix-like) ())
(defgeneric (setf vref) (value vector i)
(:documentation "Set the I-th element of VECTOR to VALUE. This
method is slow as it requires CLOS method dispatch and index
calculation(s), and should thus be replaced with vectorized or block
operations whenever possible."))

(defclass row-vector (vector-like)
((nrows :allocation :class
:initform 1)))

(defmethod vref ((vector row-vector) i)
(mref vector 0 i))

(defmethod (setf vref) (value (vector row-vector) i)
(setf (mref vector 0 i) value))

(defclass col-vector (vector-like)
((ncols :allocation :class
:initform 1)))

(defmethod vref ((vector col-vector) i)
(mref vector i 0))

(defmethod (setf vref) (value (vector col-vector) i)
(setf (mref vector i 0) value))

(defclass vecview (vector-like)
((parent :initarg :parent
:reader parent)))

(defgeneric vref (vector i)
(:method ((vector row-vector) i)
(mref vector 0 i))
(:method ((vector col-vector) i)
(mref vector i 0)))
(defclass transpose-vecview (vecview transpose-matview) ())

(defmethod vref ((vector transpose-vecview) i)
(vref (parent vector) i))

(defmethod (setf vref) (value (vector transpose-vecview) i)
(setf (vref (parent vector) i) value))

(defclass slice (vecview)
(defclass slice-vecview (vecview)
((offset :initarg :offset
:reader offset
:initform 0)
(stride :initarg :stride
:reader stride
:initform 1)))

(defclass row-slice (slice row-vector) ())
(defclass col-slice (slice row-vector) ())
(defclass row-slice-vecview (slice row-vector) ())
(defclass col-slice-vecview (slice row-vector) ())

(defmethod vref ((vector slice) i)
(defmethod vref ((vector slice-vecview) i)
(vref (parent vector)
(+ (offset vector) (* i (stride vector)))))

(defmethod (setf vref) (value (vector slice-vecview) i)
(setf (vref (parent vector)
(+ (offset vector) (* i (stride vector))))
value))

(defgeneric slice-class (vector)
(:method ((vector row-vector)) 'row-slice)
(:method ((vector col-vector)) 'col-slice))

(defun slice (vector &key (offset 0) (stride 1)
(nelts (nelts vector)))
(make-instance (slice-class vector)
:parent vector
:nelts nelts
:offset offset
:stride stride))
(:documentation "Return the name of the class to be used for a slice
of VECTOR.")
(:method ((vector row-vector)) 'row-slice-vecview)
(:method ((vector col-vector)) 'col-slice-vecview))

(defgeneric slice (vector &key offset stride nelts)
(:documentation "Create a slice view of VECTOR.")
(:method (vector &key (offset 0) (stride 1) (nelts (nelts vector)))
(make-instance (slice-class vector)
:parent vector
:nelts nelts
:offset offset
:stride stride)))

(defmethod transpose-class ((matrix vector-like)) 'transpose-vecview)
65 changes: 32 additions & 33 deletions matrix.lisp
@@ -1,6 +1,6 @@
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;
;;; Time-stamp: <2008-06-08 09:25:05 Evan Monroig>
;;; Time-stamp: <2008-06-11 14:57:16 Evan Monroig>

(in-package :lisp-matrix)

Expand Down Expand Up @@ -37,10 +37,9 @@
(ncols :initarg :ncols
:reader ncols
:initform 0))
(:documentation
"Abstract base class for 2-D matrices and matrix views. We assume
for now that matrices are stored in column order (Fortran style),
for BLAS and LAPACK compatibility."))
(:documentation "Abstract base class for 2-D matrices and matrix
views. We assume for now that matrices are stored in column
order (Fortran style), for BLAS and LAPACK compatibility."))

(defgeneric nrows (matrix)
(:documentation "Number of rows in the matrix."))
Expand Down Expand Up @@ -122,13 +121,13 @@
(+ i (* j (nrows matrix)))))

(defgeneric mref (matrix i j)
(:documentation "(MREF MATRIX i j) gives you the (i,j)-th element of
(:documentation "(MREF MATRIX I J) gives you the (I,J)-th element of
MATRIX. This method is slow as it requires CLOS method dispatch and
index calculation(s), and should thus be replaced with vectorized or
block operations whenever possible."))

(defgeneric (setf mref) (value matrix i j)
(:documentation "Set the (i,j)-th element of MATRIX to VALUE. This
(:documentation "Set the (I,J)-th element of MATRIX to VALUE. This
method is slow as it requires CLOS method dispatch and index
calculation(s), and should thus be replaced with vectorized or block
operations whenever possible."))
Expand Down Expand Up @@ -411,45 +410,45 @@
stride of MATRIX.")
(:method ((matrix matrix-like)) 'strided-matview))

(defgeneric transpose (parent)
(defgeneric transpose (matrix)
(:documentation "Creates a transpose view of the given matrix-like
object PARENT.")
(:method ((parent matrix-like))
(make-instance (transpose-class parent)
:parent parent
:nrows (ncols parent)
:ncols (nrows parent))))

(defgeneric window (parent &key nrows ncols row-offset col-offset)
object MATRIX.")
(:method ((matrix matrix-like))
(make-instance (transpose-class matrix)
:parent matrix
:nrows (ncols matrix)
:ncols (nrows matrix))))

(defgeneric window (matrix &key nrows ncols row-offset col-offset)
(:documentation "Creates a window view of the given matrix-like
object PARENT. Note that window views always have the same
object MATRIX. Note that window views always have the same
orientation as their parents.")
(:method ((parent matrix-like)
&key (nrows (nrows parent))
(ncols (ncols parent))
(:method ((matrix matrix-like)
&key (nrows (nrows matrix))
(ncols (ncols matrix))
(row-offset 0)
(col-offset 0))
(check-type nrows (integer 0))
(check-type ncols (integer 0))
(check-type row-offset (integer 0))
(check-type col-offset (integer 0))
(assert (<= (+ row-offset nrows) (nrows parent)))
(assert (<= (+ col-offset ncols) (ncols parent)))
(make-instance (window-class parent)
:parent parent
(assert (<= (+ row-offset nrows) (nrows matrix)))
(assert (<= (+ col-offset ncols) (ncols matrix)))
(make-instance (window-class matrix)
:parent matrix
:nrows nrows
:ncols ncols
:row-offset row-offset
:col-offset col-offset)))

(defgeneric strides (parent &key nrows ncols row-offset col-offset row-stride
(defgeneric strides (matrix &key nrows ncols row-offset col-offset row-stride
col-stride)
(:documentation "Creates a strided view of the given matrix-like
object PARENT.")
(:method ((parent matrix-like)
object MATRIX.")
(:method ((matrix matrix-like)
&key
(nrows (nrows parent))
(ncols (ncols parent))
(nrows (nrows matrix))
(ncols (ncols matrix))
(row-offset 0)
(col-offset 0)
(row-stride 1)
Expand All @@ -460,10 +459,10 @@
(check-type col-offset (integer 0))
(check-type row-stride (integer 1))
(check-type col-stride (integer 1))
(assert (<= (+ row-offset (* row-stride (1- nrows))) (nrows parent)))
(assert (<= (+ col-offset (* col-stride (1- ncols))) (ncols parent)))
(make-instance (stride-class parent)
:parent parent
(assert (<= (+ row-offset (* row-stride (1- nrows))) (nrows matrix)))
(assert (<= (+ col-offset (* col-stride (1- ncols))) (ncols matrix)))
(make-instance (stride-class matrix)
:parent matrix
:nrows nrows
:ncols ncols
:row-offset row-offset
Expand Down

0 comments on commit 0400eb0

Please sign in to comment.