Skip to content

Instantly share code, notes, and snippets.

@arsenovic
Created November 20, 2017 15:43
Show Gist options
  • Save arsenovic/4639ab4144d91376edf07a4842fa910c to your computer and use it in GitHub Desktop.
Save arsenovic/4639ab4144d91376edf07a4842fa910c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Algebra Caching Idea"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For performance reasons is might be nice to have a set of cached algebras available to the user. These could be generated at install or manually after install, depending on the users needs. also the name `Layout` is annoying, i wish it were called `Algebra`"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from clifford import Cl\n",
"\n",
"import pickle\n",
"import os\n",
"from os.path import join\n",
"from types import ModuleType\n",
"\n",
"\n",
"def generate_layouts(p_max=3, q_max=3, r_max=0,**kw):\n",
" #TODO: handle list of arbitrary signatures,\n",
" layouts = {}\n",
"\n",
" for p in range(p_max+1):\n",
" for q in range(q_max+1):\n",
" for r in range(r_max+1):\n",
" if p+q<=1:\n",
" continue\n",
"\n",
" key = 'g%i%i%i'%(p,q,r)\n",
" layouts[key],blades = Cl(p=p, q=q, **kw)\n",
" return layouts \n",
"\n",
"def write_layouts(layout_dict, tmp_dir='/tmp/clifford'):\n",
" try:\n",
" os.mkdir(tmp_dir)\n",
" except(FileExistsError):\n",
" pass\n",
" \n",
" for key in layouts:\n",
" with open(join(tmp_dir, key+'.p'),'wb') as f:\n",
" pickle.dump(layouts[key],f)\n",
" \n",
"\n",
"\n",
"def read_layouts( tmp_dir='/tmp/clifford'): \n",
" layouts = {}\n",
" for fname in os.listdir(tmp_dir):\n",
" \n",
" with open(join(tmp_dir, fname),'rb') as f:\n",
" key = os.path.splitext(fname)[0]\n",
" layouts[key] = pickle.load(f)\n",
" \n",
" return layouts \n",
"\n",
"def generate_layout_submodules(layouts):\n",
" #TODO: putting the whole cache into memory is dumb. need dynamic loading.\n",
" submods = {}\n",
" for key in layouts:\n",
" # create layout submodule\n",
" this_layout = layouts[key]\n",
" mod = ModuleType(key)\n",
" mod.__dict__.update({'layout':this_layout,\n",
" 'blades':this_layout.blades})\n",
" mod.__dict__.update(this_layout.blades)\n",
" submods[key]=mod\n",
" return submods\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# first two lines only executed once, or whenever user makes changes to their algebra cache\n",
"layouts = generate_layouts() \n",
"write_layouts(layouts)\n",
"\n",
"# submods should be attached as requested, or all at once. \n",
"layouts = read_layouts()\n",
"submods = generate_layout_submodules(layouts)\n",
"\n",
"# attach all algebra instances to clifford , maybe we do this in __init__?\n",
"import clifford as cf \n",
"cf.__dict__.update(submods)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1^e12)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# this is nice. \n",
"cf.g310.e12"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"locals().update(cf.g300.blades) # this is nice too\n",
"\n",
"M = e1 + e2 + e12 +e123"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ImportError",
"evalue": "No module named 'clifford.g300'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-35-873140b51196>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mclifford\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg300\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;31m# would be nice to have this work\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mImportError\u001b[0m: No module named 'clifford.g300'"
]
}
],
"source": [
"from clifford.g300 import * # would be nice to have this work"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment