2 (in-package :lineal.math
)
4 ;V Basic vector/tuple operations, V
5 ;V they behave like the normal V
6 ;V lisp math functions. V
8 (defun tuple-addn (&rest vecs
)
9 (apply #'mapcar
#'+ vecs
))
11 (defun tuple-n2addn (u v
)
12 (do ((u-rem u
(cdr u-rem
))
13 (v-rem v
(cdr v-rem
)))
14 ((or (not u-rem
) (not v-rem
)) u
)
15 (incf (car u-rem
) (car v-rem
))))
17 (defun tuple-subtrn (&rest vecs
)
18 (apply #'mapcar
#'- vecs
))
20 ;V Take 2 vectors and destructively V
21 ;V modify /u/ to reflect its V
22 ;V difference with vector /v/ V
23 (defun tuple-n2subtrn (u v
)
24 (do ((u-rem u
(cdr u-rem
))
25 (v-rem v
(cdr v-rem
)))
26 ((or (not u-rem
) (not v-rem
)) u
)
27 (decf (car u-rem
) (car v-rem
))))
29 (defun scalar-tuple-multn (k u
)
30 (mapcar #'(lambda (ui) (* k ui
)) u
))
32 (defun tuple-scalar-divisn (u k
)
33 "The vector /u/ divided by scalar /k/"
35 (mapcar #'(lambda (ui) (/ ui k
)) u
)))
37 ;V Exact same as above function but V
38 ;V destructively modifies vector /u/.V
39 (defun tuple-scalar-ndivisn (u k
)
41 (do ((tail u
(cdr tail
)))
43 (rplaca tail
(/ (car tail
) k
)))))
45 (defun tuple-magnitude (u)
46 "Find the magnitude of a vector /u/"
48 ;V Sum the squares of the vector's elements.V
49 (mapc #'(lambda (ui) (incf s
(expt ui
2))) u
)
50 (sqrt s
)));-> Return the sum's square root.
52 ;V Euclidean inner product.V
58 ;V Quick cross product, all elems V
59 ;V passed individually for speed. V
60 (defun qcross3 (a b c d e f
)
65 (defun tuple-cross3 (a b
)
67 #'qcross3
(values-list a
)
70 ;V Projection of u onto v.V
71 (defun tuple-proj (u v
)
72 (declare (inline dot-prod
))
78 ;V Orthojection - orthogonal component V
79 ;V to the projection of u onto v. V
80 (defun tuple-orth (u v
)
81 (tuple-subtrn u
(tuple-proj u v
)))