GALua
Geometric Algebra Library for the Lua Programming Language

Manipulating multivectors

In this tutorial we cover GALua's overload of the indexing operator ("[]") for multivector values. We have already seen that if the value used as an index with this operator is a multivector value, then we just get the inner product. If, however, you provide an integer index, the result is a multivector that is the part of the indexed multivector being homogeneous of the grade that is the intergral index. An example may make this clearer.

local vecA = e1 + e2
local vecB = e2 + e3
local rotor = vecA * vecB
print( "rotor = " .. tostring( rotor ) )
print( "rotor[0] = " .. tostring( rotor[0] ) )
print( "rotor[1] = " .. tostring( rotor[1] ) )
print( "rotor[2] = " .. tostring( rotor[2] ) )

Running this Lua code, we get the following output.

rotor = 1.00 + e1^e2 + e1^e3 + e2^e3
rotor[0] = 1.00
rotor[1] = 0
rotor[2] = e1^e2 + e1^e3 + e2^e3

We can also use the indexing operator ("[]") to assign certain grade parts of a given multivector.

rotor[0] = e2 .. e1
rotor[2] = e2 ^ e1

Be aware that if you try to assign a multivector homogeneous of the wrong grade, or try to assign a multivector that is not homogeneous of any grade, you will get a fatal error.

rotor[2] = vecB          -- Error!
rotor[0] = vecA * vecB   -- Error!

Being able to manipulate multivectors this way, it also becomes important to realize that a multivector variable is nothing more than a reference to a multivector value. Consider the following Lua code.

local multivecA = 5 + e1 + e1^e2
local multivecB = multivecA
multivecB[1] = e2
print( "multivecA = " .. tostring( multivecA ) )

This script will print "5 + e2 + e1^e2", not "5 + e1 + e1^e2" as one might expect. What the programmer may have intended to do is the following.

local multivecA = 5 + e1 + e1^e2
local multivecB = multivecA:copy()
multivecB[1] = e2
print( "multivecA = " .. tostring( multivecA ) )

By utilizing the :copy() meta-method of the multivector data-type, we're able to return a new copy of the value referenced by the multivecA variable to be referenced by the multivecB variable. This new code now prints "5 + e1 + e1^e2" as originally intended.

Utilizing the CGA companion module

Copyright (C) 2013, by Spencer T. Parkin