Skip to content

Instantly share code, notes, and snippets.

@arsenovic
Created November 19, 2017 16:53
Show Gist options
  • Save arsenovic/125cb38cbfe3008e5f417b18267c845d to your computer and use it in GitHub Desktop.
Save arsenovic/125cb38cbfe3008e5f417b18267c845d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Changing Backend Demo "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"This notebook demonstrates how the same GA-algorithm can be evaluated with different computational backends. The algorithm, which we call *Cartans algorithm*, determines a verser from a pair of frames that are related by an orthogonal transformation. Details on this can be found in [1] and [2]. This algorithm is evaluated with the symbolic package `galgebra` and the numeric one `clifford`. \n",
"\n",
"\n",
"\n",
" [1] http://ctz.dk/geometric-algebra/frames-to-versor-algorithm/\n",
"\n",
" [2] \"Reconstructing Rotations and Rigid Body Motions from Exact Point Correspondences Through Reflections\" Daniel Fontijne and Leo Dorst\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cartans algorithm"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from functools import reduce \n",
"from scipy import e\n",
"\n",
"\n",
"def cartans(A,B,delta=None):\n",
" if delta is None: \n",
" delta = eps() * 1e3\n",
" \n",
" A = A[:] # make copy of frame\n",
" rs = [] # list of reflectors \n",
"\n",
" for k in range(len(A)):\n",
" r = A[0] - B[0] # compute reflector \n",
"\n",
" A = A[1:] # pop off this pair\n",
" B = B[1:]\n",
"\n",
" if abs(r) < delta: \n",
" continue \n",
"\n",
" rs.append(r)\n",
" for l in range(len(A)):\n",
" A[l] = -r*A[l]*r.inv() \n",
" \n",
" gp = lambda x,y:x*y # define geometric product as function\n",
" V_found = reduce(gp, rs[::-1]) # recursivly apply geometric product to rs\n",
" V_found /= abs(V_found) # normalize\n",
" \n",
" return V_found"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `clifford` backed "
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-(0.5^e12) + (0.5^e14) - (0.5^e23) - (0.5^e34)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from clifford import Cl\n",
"\n",
"layout,blades = Cl(4)\n",
"locals().update(blades)\n",
"\n",
"A = [e1,e2,e3,e4]\n",
"B = [e3,e4,e1,e2]\n",
"R = cartans(A,B)\n",
"R"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `galgebra` backend"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{equation*} - \\frac{1}{2} \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{2} + \\frac{1}{2} \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{4} - \\frac{1}{2} \\boldsymbol{e}_{2}\\wedge \\boldsymbol{e}_{3} - \\frac{1}{2} \\boldsymbol{e}_{3}\\wedge \\boldsymbol{e}_{4} \\end{equation*}"
],
"text/plain": [
"-e_1^e_2/2 + e_1^e_4/2 - e_2^e_3/2 - e_3^e_4/2"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from galgebra import ga \n",
"\n",
"g4g = '1 0 0 0, 0 1 0 0, 0 0 1 0, 0 0 0 1' \n",
"g4 = ga.Ga('e_1 e_2 e_3 e_4',g=g4g) \n",
"e1,e2,e3,e4 = g4.mv() \n",
"\n",
"A = [e1,e2,e3,e4]\n",
"B = [e3,e4,e1,e2]\n",
"R = cartans(A,B)\n",
"R"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:py27]",
"language": "python",
"name": "conda-env-py27-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@hugohadfield
Copy link

@arsenovic I think this is a great idea, would be great to mix symbolic and numeric packages easily. Have you had much luck with the galgebra package? Doesn't seem to be still under very active development and I don't think it's compatible with python3...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment