+Calculator and code looks
[lineal.git] / src / overload / tuples.lisp
blob65e0983cfaf91e8a1b455783dd86fcd7ab12853e
2 (in-package :lineal.overload)
4 (defstruct tuple
5 (dim 0 :type integer)
6 (elems nil :type list))
8 (defun over-dot-prod (a b)
9 (dot-prod
10 (tuple-elems a)
11 (tuple-elems b)))
13 (defun over-cross-prod (a b)
14 (if (and (= 3 (tuple-dim a))
15 (= 3 (tuple-dim b)))
16 (make-tuple
17 :dim 3
18 :elems (tuple-cross3 (tuple-elems a)
19 (tuple-elems b)))
20 (throw 'over-ex "cross products require 3-D vectors")))
22 ;V Projection of u onto v.V
23 (defun over-proj (u v)
24 (let ((tdim (tuple-dim u)))
25 (if (= tdim (tuple-dim v))
26 (make-tuple
27 :dim tdim
28 :elems (tuple-proj (tuple-elems u)
29 (tuple-elems v)))
30 (throw 'over-ex "proj requires vectors of same dimension"))))
32 ;V Orthogonal component of the V
33 ;V projection of u onto v. V
34 (defun over-orth (u v)
35 (let ((tdim (tuple-dim u)))
36 (if (= tdim (tuple-dim v))
37 (make-tuple
38 :dim tdim
39 :elems (tuple-orth (tuple-elems u)
40 (tuple-elems v)))
41 (throw 'over-ex "orth requires vectors of same dimension"))))
43 (defmethod add2n ((a tuple) (b tuple))
44 (if (= (tuple-dim a) (tuple-dim b))
45 (make-tuple
46 :dim (tuple-dim a)
47 :elems (tuple-addn (tuple-elems a) (tuple-elems b)))
48 (throw 'over-ex "don't add vectors of different dimension")))
50 (defmethod subtr2n ((a tuple) (b tuple))
51 (if (= (tuple-dim a) (tuple-dim b))
52 (make-tuple
53 :dim (tuple-dim a)
54 :elemes (tuple-addn a b))
55 (throw 'over-ex "don't subtract vectors of different dimension")))
57 (defmethod mult2n ((k number) (u tuple))
58 (make-tuple :dim (tuple-dim u)
59 :elems (scalar-tuple-multn
60 k (tuple-elems u))))
61 (defmethod mult2n ((u tuple) (k number))
62 (make-tuple :dim (tuple-dim u)
63 :elems (scalar-tuple-multn
64 k (tuple-elems u))))
66 (defmethod divis2n ((u tuple) (k number))
67 (make-tuple :dim (tuple-dim u)
68 :elems (tuple-scalar-divisn
69 (tuple-elems u) k)))