3 ms·
I guess if you want a library for linear algebra operations in Lisp, you just write them yourself. From McCarthy's collection: ;;; multiplying a matrix by a co
by Bostonian 3y ago
I guess if you want a library for linear algebra operations in Lisp, you just write them yourself. From McCarthy's collection:
;;; multiplying a matrix by a column vector
(defun mvmult (matrix vector)
(list (scap (nth 0 matrix) vector) (scap (nth 1 matrix) vector)))
;;; sum of two vectors
(defun vplus (vec1 vec2) (list (+ (nth 0 vec1) (nth 0 vec2))
(+ (nth 1 vec1) (nth 1 vec2))))
;;; difference of two vectors
(defun vminus (vec1 vec2) (list (- (nth 0 vec1) (nth 0 vec2))
(- (nth 1 vec1) (nth 1 vec2))))
;;; scalar product of two vectors
(defun scap (vec1 vec2) (+ (* (nth 0 vec1) (nth 0 vec2))
(* (nth 1 vec1) (nth 1 vec2))))
;;; product of scalar and vector
(defun svmult (sca vec) (list (* sca (nth 0 vec)) (* sca (nth 1 vec))))
;;; sum of a list of vectors
(defun addup (veclist)
(if (null veclist)
zerovec
(vplus (car veclist) (addup (cdr veclist)))))
(defconst zerovec '(0 0) "zero vector with two components"1)
;;; length of a vector
(defun length (x) (sqrt (+ (expt (nth 0 x) 2) (expt (nth 1 x) 2))))
(defconst Imatrix '((1.0 0.0) (0.0 1.0)) "unit 2x2 matrix")
(defun smmult (sca matrix) (list (svmult sca (nth 0 matrix))
(svmult sca (nth 1 matrix))))
(defun mplus (mat1 mat2) (list (vplus (nth 0 mat1) (nth 0 mat2))
(vplus (nth 1 mat1) (nth 1 mat2))))
(defun mminus (mat1 mat2) (list (vminus (nth 0 mat1) (nth 0 mat2))
(vminus (nth 1 mat1) (nth 1 mat2))))
(defun mmult (mat1 mat2)
(list
(list (scap (nth 0 mat1) (col 0 mat1)) (scap (nth 0 mat1) (col 1 mat1)))
(list (scap (nth 1 mat1) (col 0 mat1)) (scap (nth 1 mat1) (col 1 mat1)))))
(defun multiplyup (matlist)
(if (null matlist)
Imatrix
(mmult (car matlist) (multiplyup (car matlist)
(multiplyup (cdr (matlist)))))))
- BaculumMeumEst 3y agosee also https://github.com/jb55/vrscript-samples/blob/master/vr.rkt https://github.com/jb55/vrscript-samples/blob/master/vr.rkt
- zelphirkalt 3y agoOf course one can do this and it might work as a stopgap solution and abstraction layer until something different comes along and I have done similar, but ultimately this will not be very efficient. It might be a better, albeit less beautiful, solution to use any low level language inplemented linear algebra bindings for the lisp at hand. Hopefully there existing one.