Skip to content

Instantly share code, notes, and snippets.

@maruks
Last active February 9, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruks/3701cd7e04c50a3b9ed6c2a0dae976e2 to your computer and use it in GitHub Desktop.
Save maruks/3701cd7e04c50a3b9ed6c2a0dae976e2 to your computer and use it in GitHub Desktop.
matrix multiplication
(defpackage :matrices
(:use :cl :iterate)
(:export multiply))
(in-package :matrices)
(defun multiply (m1 m2)
(flet ((multiply-row (row column size)
(iter
(for i :below size)
(sum (* (aref m1 row i) (aref m2 i column))))))
(destructuring-bind (m1-rows m1-columns m2-rows m2-columns)
(concatenate 'list (array-dimensions m1) (array-dimensions m2))
(when (eql m1-columns m2-rows)
(iter
(with result = (make-array (list m1-rows m2-columns)))
(for column :below m2-columns)
(iter
(for row :below m1-rows)
(setf (aref result row column) (multiply-row row column m2-rows)))
(finally (return result)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment