Geometric algebra: Difference between revisions

m
J: give up on the dense implementation, since there's something wrong with it and we've got a working version
m (J: conform to task change)
m (J: give up on the dense implementation, since there's something wrong with it and we've got a working version)
Line 262:
 
=={{header|J}}==
 
Based on the quaternion implementation at [[Quaternion_type#J|quaternion type]], but implementing a clifford algebra which specifically corresponds to the task requirements. If performance mattered, we might want to use sparse arrays here.
 
Implementation:
 
<lang J>dim=.32
T=.3 :0 dim
s=: _1^~:/@,@(* 0,_1}.~:/\)/@#:@,"0/~x: i. y
s (<@([ , #.@:(~:/)@#:@, , ])"0/~i.y) } (3#y)$0
)
domain=: 32&{. :.(#~ +./\.@:(0&~:))"1
ip=: +/ .*
gmul=: (ip T ip ])&.domain
gadd=: +/@,:&.domain
gdot=: (gmul gadd gmul~)&.domain % 2:
e=: {&((2^i.2^.dim){=i. dim)</lang>
 
See the alternate implementation for more detail on what we are doing here.
 
In this case, though, we are using a non-sparse representation, 32 element multivectors and, thus five element vectors. This approach is slow, but the code is a bit more concise because we do not have to deal with the details of the sparse implementation.
 
Use:
 
<lang J> NB. test arbitrary vector being real
gmul~ gadd/ (e 0 1 2 3 4) * 1 _1 2 3 _2
19
NB. required orthogonality
gdot&e&.>/~i.4
┌─┬─┬─┬─┐
│1│ │ │ │
├─┼─┼─┼─┤
│ │1│ │ │
├─┼─┼─┼─┤
│ │ │1│ │
├─┼─┼─┼─┤
│ │ │ │1│
└─┴─┴─┴─┘
NB. i j k
i=: 0 gmul&e 1
j=: 1 gmul&e 2
k=: 0 gmul&e 2
i gmul i
_1
j gmul j
_1
k gmul k
_1
i gmul j gmul k
_1
NB. I J K
I=: 1 gmul&e 2
J=: 2 gmul&e 3
K=: 1 gmul&e 3
I gmul I
_1
J gmul J
_1
K gmul K
_1
I gmul J gmul K
_1
K gadd -J
0 0 0 0 0 0 0 0 0 0 _1 0 1
I gmul J gadd K
0 0 0 0 0 0 0 0 0 0 _1 0 1</lang>
 
=== J: alternate implementation ===
 
Sparse arrays give better performance for this task, and support relatively arbitrary dimensions, but can be a bit quirky because current implementations of J do not support some features for sparse arrays. We add vectors x and y using <code>x + y</code>. Also, the first element of one of these vectors represents the "real valued" or "scalar component" of the vector.
6,951

edits