Basic math operations
Once we have a geometric algebra defined, we're ready to start making GA calculations. One way to get started is by creating multivectors for each basis vector we registered with the module. This can be accomplisehd as follows.
local e1 = galua_api( "e1" ) local e2 = galua_api( "e2" ) local e3 = galua_api( "e3" ) local no = galua_api( "no" ) local ni = galua_api( "ni" )
Here we see that the function call syntax has been implemented for the API table so that we may call it with a string value. Each of the strings given above correspond to one of the basis vectors we registered with the module earlier. (Here we are assuming that we have defined the conformal geometric algebra of 3-dimensional space.)
We're now ready to get down to business! Consider the following sequence of script code.
local pos = 3*e1 + 2*e2 - e3 local radius = 5.5 local sphere = no + pos + 0.5*( pos*pos - radius*radius )*ni pos = 2*e2 local norm = e2 + 2*e3 norm = norm / #norm local plane = norm + ( norm .. pos )*ni local circle = sphere ^ norm
This example calculates a dual circle that is the intersection of a dual sphere and a dual plane. Convenience methods for composing and decomposing such geometries are available in a companion module named CGAUtil. More on that is given in a later tutorial. For now, the above script code serves to illustrate what kinds of math operations we can perform and how to to perform them. Much of it is fairly intuitive and straight-forward, but we'll continue now to explain in detail what math operations are available and what they do.
The binary operations supported by the GALua module are summarized in the following table.
|Binary Operation||Overloaded Lua Operator|
As you can see, there are quite a few options available for the inner product. Some effort has been made
to allow the use of the indexing operator ("
."), but its use is not recommended, because it requires the GALua
module to synthesize Lua's scoping algorithm. Though its use may make your scripts easier to read, refraining
from its use gives you a greater guarentee of correctness. The concationation operator ("
..") is therefore the
preferred method while the indexing operator ("
") is also supported for those that are assuming a left or
perhaps right contraction. Of course, Lua syntax always requires that the indexing operator ("
on the right, so this may not be entirely useful. Never-the-less, it's there.
It is important to realize that GALua cannot change the operator precedence of Lua. Consequently, you must be aware that
that the ("
..") operator does not bind as tightly as addition ("
+"), which is certainly not
the case in pure mathematics. If you're not careful to wrap all inner products with parantheses, you might introduce bugs in your
The multilication operator ("
*") implements the geometric product. The division operator ("
simply short-hand for multiplying its left operand by the inverse of its right operand. If the
right operand is a singular multivector, a Lua exception will be thrown, and your program will
halt at this location. A way to attempt the calculation of multivector inverses without incurring
such a fatal error will be given shortly.
The idea of coercion is supported by all of the binary operations given in the table above. This is important to realize, and means that the GALua module, if not given a multivector value for both arguments in a call to one of its binary operations, will try to coerce any non-multivector value into an appropriate corresponding multivector value. This is mainly useful for Lua number values as can be seen in the formulation of the position of the dual sphere given above, and in the subsequent calculations made. Any strings that are encountered will be treated in the same way that the function call syntax overload of the API table treats such strings. That is, the following two lines of Lua are the same.
bivec = -3 * e1 ^ "e2" bivec = -3 * e1 ^ galua_api( "e2" )
Now lets talk about the unary operators. These are summarized in the following table.
|Unary Operation||Overloaded Lua Operator|
Of course, this is the not the full list of unary operations supported by GALua, just the full list of such operations that are implemented as overloads of existing Lua unary operators. The rest of the unary operations are given as meta-methods you can call on a multivector value.
Like any binary operation, every unary operator returns a new multivector value, leaving the multivector variable untouched. That is, it does not alter the multivector value referenced by the multivector variable it is operating upon.
inverse meta-method is called on a singular multivector, the result
will be zero. If you are ever unsure about the invertibility of a given multivector value,
it is best to call this instead of performing a division operation. By default, the
meta-method calculates a right-inverse. That is, the multivector that, if multiplied by the given
multivector on the right in the geometric product, gives the multiplicative identity. Use the
:right_inverse() meta-methods to be explicit about
which inverse you desire to calculate. (At the time of this writing, I'm not sure if multivectors
generally commute in the geometric product with their inverses. For now, left and right inverse operations
are supported under an absense of making such an assumption.)
Now that we're capable of doing some basic math, we're ready to cover our printing options.