Skip to content

Instantly share code, notes, and snippets.

@sham1
Created December 20, 2021 16:54
Show Gist options
  • Save sham1/a1714f65776d1aeb1e6631b3203d9049 to your computer and use it in GitHub Desktop.
Save sham1/a1714f65776d1aeb1e6631b3203d9049 to your computer and use it in GitHub Desktop.
(defun matrix-* (a b)
(let ((ret (make-array '(3 3))))
(loop for i from 0 to 2 do
(loop for j from 0 to 2 do
(setf (aref ret i j)
(loop for k from 0 to 2
summing (* (aref a i k) (aref b k j))))))
ret))
(defconstant +identity+ (make-array
'(3 3) :initial-contents
'((1 0 0) (0 1 0) (0 0 1))))
(defconstant +x-rot+ (make-array
'(3 3) :initial-contents
'((1 0 0) (0 0 1) (0 -1 0))))
(defconstant +x-inv-rot+ (make-array
'(3 3) :initial-contents
'((1 0 0) (0 0 -1) (0 1 0))))
(defconstant +y-rot+ (make-array
'(3 3) :initial-contents
'((0 0 -1) (0 1 0) (1 0 0))))
(defconstant +y-inv-rot+ (make-array
'(3 3) :initial-contents
'((0 0 1) (0 1 0) (-1 0 0))))
(defconstant +z-rot+ (make-array
'(3 3) :initial-contents
'((0 1 0) (-1 0 0) (0 0 1))))
(defconstant +z-inv-rot+ (make-array
'(3 3) :initial-contents
'((0 -1 0) (1 0 0) (0 0 1))))
(defun generate-matrices ()
(let ((ret nil))
(let ((x-mat +identity+)
(x-inv +identity+))
(loop for x from 0 to 3 do
(let ((y-mat +identity+)
(y-inv +identity+))
(loop for y from 0 to 3 do
(let ((z-mat +identity+)
(z-inv +identity+))
(loop for z from 0 to 3 do
(pushnew (cons (matrix-* x-mat (matrix-* y-mat z-mat))
(matrix-* z-inv (matrix-* y-inv x-inv)))
ret
:test #'equalp)
(setf z-mat (matrix-* +z-rot+ z-mat)
z-inv (matrix-* z-inv +z-inv-rot+))))
(setf y-mat (matrix-* +y-rot+ y-mat)
y-inv (matrix-* y-inv +y-inv-rot+))))
(setf x-mat (matrix-* +x-rot+ x-mat)
x-inv (matrix-* x-inv +x-inv-rot+))))
ret))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment