Skip to content

Commit

Permalink
added nbsphinx, and a test notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
arsenovic committed Sep 26, 2016
1 parent ba4020e commit 4765b56
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 83 deletions.
31 changes: 28 additions & 3 deletions clifford/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,28 @@ def randomRotor(self):

def basis_vectors(self, **kw):
return basis_vectors(self, **kw)

def blades(self,*args, **kw):
'''
Returns a dictionary mapping basis element names to their MultiVector
instances, optionally for specific grades
if you are lazy, you might do this to populate your namespace
with the variables of a given layout.
>>> locals().update(layout.blades())
See Also
---------
bases
'''
return bases(layout=self, *args,**kw)




class MultiVector(object):
"""An element of the algebra
Expand Down Expand Up @@ -1669,7 +1690,12 @@ def Cl(p, q=0, names=None, firstIdx=0, mvClass=MultiVector):
def bases(layout, mvClass=MultiVector,grades=None):
"""Returns a dictionary mapping basis element names to their MultiVector
instances, optionally for specific grades
if you are lazy, you might do this to populate your namespace
with the variables of a given layout.
>>> locals().update(layout.blades())
bases(layout) --> {'name': baseElement, ...}
"""

Expand Down Expand Up @@ -1796,7 +1822,7 @@ def gp(M, N):
M and N must be from the same layout
This function is calls the MultiVector.__and__ operator, but
This function calls the MultiVector.__and__ operator, but
is useful in calculating series of products, like `reduce`
for example
Expand All @@ -1807,4 +1833,3 @@ def gp(M, N):

return M*N


26 changes: 14 additions & 12 deletions clifford/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from numpy import eye
from . import Cl, gp

def mat2Frame(A, layout=None):
def mat2Frame(A, layout=None, is_complex=None):
'''
Translates a [complex] matrix into a real frame
Expand All @@ -49,13 +49,15 @@ def mat2Frame(A, layout=None):
# N = number of vectors
M,N = A.shape

if A.dtype == 'complex':
is_complex = True
if is_complex is None:
if A.dtype == 'complex':
is_complex = True

else:
is_complex = False
if is_complex:
N = N*2
M = M*2

else:
is_complex = False

if layout is None:
layout, blades = Cl(M,firstIdx=0)
Expand Down Expand Up @@ -164,12 +166,12 @@ def orthoFrames2Verser(A,B, eps =1e-6):
dist = [abs((a-b)**2) for a,b in zip(A,B)]
k = dist.index(max(dist))

print str(len(r_list)) + ' reflections found'
#print str(len(r_list)) + ' reflections found'
R = reduce(gp,r_list[::-1] )

return R ,r_list

def orthoMat2Verser(A, eps= 1e-6,layout=None):
def orthoMat2Verser(A, eps= 1e-6,layout=None,is_complex=None):
'''
Translates a [complex] orthogonal matrix to a Verser
Expand All @@ -178,13 +180,13 @@ def orthoMat2Verser(A, eps= 1e-6,layout=None):
transform.
'''
B,layout = mat2Frame(A,layout=layout)
B,layout = mat2Frame(A,layout=layout,is_complex=is_complex)
N = len(B)

if (A.dot(A.conj().T) -eye(N/2)).max()>eps:
warn('A doesnt appear to be a rotation. ')
#if (A.dot(A.conj().T) -eye(N/2)).max()>eps:
# warn('A doesnt appear to be a rotation. ')

A,layout = mat2Frame(eye(N),layout=layout)
A,layout = mat2Frame(eye(N),layout=layout,is_complex=False)
return orthoFrames2Verser(A,B, eps = eps)


Expand Down
270 changes: 270 additions & 0 deletions docs/QuickStartG2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quick Start with G2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instantiate a G2 algebra"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import clifford as cf\n",
"cf.pretty(precision=3) # sets display precision\n",
"layout, blades = cf.Cl(2, firstIdx=1) # creates a 2-dimensional clifford algebra\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inspect blades."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'e1': (1.0^e1), 'e12': (1.0^e12), 'e2': (1.0^e2)}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blades "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assign blades to variables"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"e1 = blades['e1']\n",
"e2 = blades['e2']\n",
"e12 = blades['e12']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basics"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1.0^e12)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1*e2 # geomtric product"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1|e2 # inner product "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1.0^e12)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1^e2 # outer product"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reflection "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-(1.0^e1) + (1.0^e2)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = e1+e2 # the vector\n",
"n = e1 # the reflector\n",
"-n*a*n.inv() # reflect `a` in hyperplane normal to `n`"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Rotation"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.707 + (0.707^e12)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from scipy.constants import pi\n",
"\n",
"R = e**(pi/4*e12) # enacts rotation by pi/2 \n",
"R"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-(1.0^e2)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"R*e1*~R # rotate e1 by pi/2 in the e12-plane"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "IPython (Python 2)",
"language": "python",
"name": "python2"
},
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 4765b56

Please sign in to comment.