Last active
February 9, 2020 19:24
-
-
Save maruks/3701cd7e04c50a3b9ed6c2a0dae976e2 to your computer and use it in GitHub Desktop.
matrix multiplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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