[lisplab-cvs] r94 - doc/manual src/matrix

Jørn Inge Vestgården jivestgarden at common-lisp.net
Sat Sep 26 12:38:03 UTC 2009


Author: jivestgarden
Date: Sat Sep 26 08:38:02 2009
New Revision: 94

Log:
improvements and optimizations

Modified:
   doc/manual/lisplab.texi
   lisplab.asd
   package.lisp
   src/matrix/level1-util.lisp
   src/matrix/level2-constructors.lisp
   src/matrix/level2-generic.lisp
   src/matrix/level2-matrix-dge.lisp
   src/matrix/level2-matrix-zge.lisp

Modified: doc/manual/lisplab.texi
==============================================================================
--- doc/manual/lisplab.texi	(original)
+++ doc/manual/lisplab.texi	Sat Sep 26 08:38:02 2009
@@ -662,26 +662,28 @@
 has no known symmetries.
 @item @code{matrix-structure-diagonal}, where only 
 diagonal elements are non-zero.
+ at item Other types to come.
 @end itemize
 
 @subsection The element type
 The element type classes has no other purpose than to be 
-represents a Common Lisp types..
+represents a Common Lisp types.
 @itemize
 @item @code{matrix-element-base}, represents @code{t}
 @item @code{matrix-element-double-float}, represents @code{double-float}
 @item @code{matrix-element-complex-double-float}, represents @code{complex double-float}
 @end itemize 
 
+
 @subsection The implementation 
 Since Lisplab has many competing implementation of the same 
 generic functions, the implementation class structure tells which 
 one too choose. There are currently four classes in a straight 
 line of inheritance
 @itemize
- at item matrix-implementation-base
- at item matrix-implementation-lisp, use native Common Lisp if possible.
- at item matrix-implementation-blas, use foreign libraries if possible.
+ at item @code{matrix-implementation-base}
+ at item @code{matrix-implementation-lisp}, use native Common Lisp if possible.
+ at item @code{matrix-implementation-blas}, use foreign libraries if possible.
 @end itemize
 This standard method dispatch ensures foreign library methods 
 are chosen before native lisp. @i{It is the responsibility of the 

Modified: lisplab.asd
==============================================================================
--- lisplab.asd	(original)
+++ lisplab.asd	Sat Sep 26 08:38:02 2009
@@ -12,14 +12,15 @@
 
 (defun load-lisplab-lib (name)
   (when name  
-    (sb-alien:load-shared-object name)))
+    #+sbcl (sb-alien:load-shared-object name)))
 
 (defun explain-lisplab-lib (name path)
   (format t "Loads ~A. Path ~a" name path))
 
 (declaim (inline |fftw_init_threads|))
-(sb-alien:define-alien-routine |fftw_init_threads|
+#+sbcl (sb-alien:define-alien-routine |fftw_init_threads|
     sb-alien:int)
+#-sbcl (defun fftw_init_threads (n))
 
 (defsystem :lisplab
   ;; Default system, without all libs

Modified: package.lisp
==============================================================================
--- package.lisp	(original)
+++ package.lisp	Sat Sep 26 08:38:02 2009
@@ -150,6 +150,8 @@
    "DCOL"
    "DROW"
    "DRANDOM"
+   "DRANGE"
+   "DGRID"
    "ZMAT"
    "ZNEW" 
    "ZCOL" 

Modified: src/matrix/level1-util.lisp
==============================================================================
--- src/matrix/level1-util.lisp	(original)
+++ src/matrix/level1-util.lisp	Sat Sep 26 08:38:02 2009
@@ -151,3 +151,10 @@
 	   (setf (aref store i) rv)))
     store))
 
+(defun copy-matrix-stores (a b)
+  (let ((len (length a)))
+    (declare (type type-blas-store a b)
+	     (type type-blas-idx len))
+    (dotimes (i len)
+      (setf (aref b i) (aref a i))))
+  b)
\ No newline at end of file

Modified: src/matrix/level2-constructors.lisp
==============================================================================
--- src/matrix/level2-constructors.lisp	(original)
+++ src/matrix/level2-constructors.lisp	Sat Sep 26 08:38:02 2009
@@ -98,6 +98,34 @@
   "Creates a matrix-dge matrix"
   (mnew 'matrix-dge value rows cols))
 
+(defun drange (n from to &optional (shift 0))
+  "Creates a column vector of length n, with elements of equal spacing 
+between from and to. The shift is the a number between 0 and 1 and 
+shifts the start position.
+
+For example: (drange 4 0 1) -> 0 1 2 3, while 
+             (drange 4 0 1 0.5) -> 0.5 1.5 2.5 3.5."
+  (let ((x (dnew 0 n 1))
+	(dx (./ (.- to from))))
+    (dotimes (i n)
+      (setf (vref x i) (.+ from (.* dx (.+ i shift)))))
+    x))
+
+(defun dgrid (xv yv)
+  "Creates grid matrices from input vectors. Input are the x and y vectors 
+and outputs are a list of x and y matrices. The input vectors are  
+typically created with drange."
+  (let* ((r (size xv))
+	 (c (size yv))
+	 (x (dnew 0 r c))
+	 (y (dnew 0 r c)))
+    (dotimes (i r)
+      (dotimes (j c)
+	(setf (mref x i j) (vref xv i)
+	      (mref y i j) (vref yv j))))
+    (list x y)))
+
+
 ;;; Constructors for matrix-zge
 
 (defmacro zmat (&body args)

Modified: src/matrix/level2-generic.lisp
==============================================================================
--- src/matrix/level2-generic.lisp	(original)
+++ src/matrix/level2-generic.lisp	Sat Sep 26 08:38:02 2009
@@ -24,6 +24,12 @@
 
 (in-package :lisplab) 
 
+(defmethod mcreate ((m number) &optional (val 0.0) dim)
+  ;; This is not about matrices at all, but is usefull 
+  ;; when you use the dotted algebra and is not sure is input is numbers or matrices.
+  ;; TODO what the dim, should I use it or ignore it
+  val)
+
 (defmethod square-matrix? ((x matrix-base))
   (= (rows x) (cols x)))
 

Modified: src/matrix/level2-matrix-dge.lisp
==============================================================================
--- src/matrix/level2-matrix-dge.lisp	(original)
+++ src/matrix/level2-matrix-dge.lisp	Sat Sep 26 08:38:02 2009
@@ -31,6 +31,14 @@
 		 :rows (rows matrix)
 		 :cols (cols matrix)))
 
+(defmethod copy-contents ((a matrix-base-dge) (b matrix-base-dge) &optional (converter nil))
+  (let ((store-a (matrix-store a))
+	(store-b (matrix-store b)))
+    (if converter 
+	(map-into store-b converter store-a)
+	(copy-matrix-stores store-a store-b)))    
+    b)
+
 (defmethod mmap-into ((out matrix-base-dge) f (a matrix-base-dge) &rest args)
   (apply #'map-into 
 	 (matrix-store out)

Modified: src/matrix/level2-matrix-zge.lisp
==============================================================================
--- src/matrix/level2-matrix-zge.lisp	(original)
+++ src/matrix/level2-matrix-zge.lisp	Sat Sep 26 08:38:02 2009
@@ -55,22 +55,48 @@
           (incf sum-i (aref m0 (1+ i))))
     (complex sum-r sum-i)))
 
+
+;;; Hm, much shared code here. Could make a unifying macro. 
+
 (defmethod .imagpart ((a matrix-base-zge))
   (let* ((description (create-matrix-description a :et :d))
-	 (b (make-matrix-instance description (dim a) 0)))
-    (copy-contents a b #'imagpart)
+	 (b (make-matrix-instance description (dim a) 0))
+	 (store-a (matrix-store a))
+	 (store-b (matrix-store b))
+	 (len (size a)))
+    (declare (type type-blas-store store-a store-b)
+	     (type type-blas-idx len))
+    (dotimes (i len)
+      (setf (aref store-b i) 
+	    (aref store-a (1+ (* 2 i)))))
     b))
 
 (defmethod .realpart ((a matrix-base-zge))
   (let* ((description (create-matrix-description a :et :d))
-	 (b (make-matrix-instance description (dim a) 0)))
-    (copy-contents a b #'realpart)
+	 (b (make-matrix-instance description (dim a) 0))
+	 (store-a (matrix-store a))
+	 (store-b (matrix-store b))
+	 (len (size a)))
+    (declare (type type-blas-store store-a store-b)
+	     (type type-blas-idx len))
+    (dotimes (i len)
+      (setf (aref store-b i) 
+	    (aref store-a (* 2 i))))
     b))
 
 (defmethod .abs ((a matrix-base-zge))
   (let* ((description (create-matrix-description a :et :d))
-	 (b (make-matrix-instance description (dim a) 0)))
-    (copy-contents a b #'abs)
+	 (b (make-matrix-instance description (dim a) 0))
+	 (store-a (matrix-store a))
+	 (store-b (matrix-store b))
+	 (len (size a)))
+    (declare (type type-blas-store store-a store-b)
+	     (type type-blas-idx len))
+    (dotimes (i len)
+      (setf (aref store-b i) 
+	    (let ((x  (aref store-a (* 2 i)))
+		  (y  (aref store-a (1+ (* 2 i)))))
+	      (sqrt (+ (* x x) (* y y))))))
     b))
 
 (defmacro def-binary-op-matrix-base-zge (new old)




More information about the lisplab-cvs mailing list