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 = " .. tostring( rotor ) )
print( "rotor = " .. tostring( rotor ) )
print( "rotor = " .. tostring( rotor ) )
```

Running this Lua code, we get the following output.

```rotor = 1.00 + e1^e2 + e1^e3 + e2^e3
rotor = 1.00
rotor = 0
rotor = e1^e2 + e1^e3 + e2^e3
```

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

```rotor = e2 .. e1
rotor = 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 = vecB          -- Error!
rotor = 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 = 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 = 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