Commits

David Jones committed 18268fe

added file.

Comments (0)

Files changed (2)

lisp/basic-math.lisp

         (dotimes (i nn)
           (setf numerator (/ numerator (+ i 1))))
         numerator)
-      1)
+      1))
 
 
 (defun number-of-permutations (m n)

lisp/examples/clos.lisp

+;; want to figure out how to use generic methods to do even better than
+;; operator overloading (i.e., have diagonal matrices and stuff, such
+;; that multiplying is far more efficient)
+
+;;(defclass matrix ()
+;;  ((rows :accessor matrix-rows)
+;;   (columns :accessor matrix-columns)
+;;   data))
+
+;;(defun create-matrix (nrows ncols)
+;;  (let ((m (make-instance 'matrix)))
+;;    (setf (matrix-rows m) nrows
+;;          (matrix-columns m) ncols)
+;;    
+;;    m))
+
+(defstruct matrix 
+  rows
+  columns)
+
+(defstruct (full-matrix (:include matrix))
+  data)
+
+
+
+(defmethod matrix-get-data ((m full-matrix) row column)
+  (aref (full-matrix-data m) row column))
+
+(defmethod matrix-set-data ((m full-matrix) row column value)
+  (setf (aref (full-matrix-data m) row column) value))
+
+  
+
+(defmethod matrix-multiply ((x matrix) (y matrix))
+  (error "not implemented"))
+
+(defmethod matrix-multiply ((x full-matrix) (y full-matrix))
+  "This is ugly, but it implements the naive triple loop matrix
+multiple on matrices."
+  (let* ((m (matrix-rows x))
+         (n (matrix-columns y))
+         (p (matrix-rows y))
+         (result (make-full-matrix :rows m :columns n 
+                                   :data (make-array (list m n)
+                                                     :initial-element 0d0))))
+    (dotimes (i m result)
+      (dotimes (j n)
+        (dotimes (k p)
+          (format t "~a ~a ~a~%" i j k)
+          (incf (aref (full-matrix-data result) i j)
+                (* (aref (full-matrix-data x) i k)
+                   (aref (full-matrix-data y) k j))))))))
+    
+
+
+    
+