Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.89 KB | None | 0 0
  1. (defmethod mult ((m1 matrix-r) (m2 matrix-r))
  2.   (if (not (= (number-of-cols m1) (number-of-rows m2)))
  3.       (error "Can't perform matrix multiplication")
  4.       (progn
  5.     (let* ((elements-m1 (elements m1)) (elements-m2 (elements m2))
  6.            (n (number-of-cols m1))
  7.            (n-rows (number-of-rows m1))
  8.            (n-cols (number-of-cols m2))
  9.            (m (make-instance 'matrix-r :number-of-rows n-rows :number-of-cols n-cols))
  10.            (elements-m (elements m)))
  11.       (declare
  12.        (type (simple-array double-float) elements-m1 elements-m2 elements-m)
  13.        (type fixnum n-rows n-cols n)
  14.        (optimize (debug 0) (safety 0) (speed 3)))
  15.       (loop for i fixnum from 0 below n-rows do
  16.            (loop for j fixnum from 0 below n-cols do
  17.             (setf (aref elements-m (+ j (* i n-cols))) (loop for k fixnum from 0 below n
  18.                         sum (* (aref elements-m1 (+ k (* i n))) (aref elements-m2 (+ j (* k n))))))))
  19.       m))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement