Skip to content

Commit

Permalink
first relmath prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Nov 21, 2018
1 parent b4287cc commit 73cf33f
Show file tree
Hide file tree
Showing 2 changed files with 549 additions and 0 deletions.
270 changes: 270 additions & 0 deletions exercises/relmath/relmath.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Relational Mathematics\n",
"\n",
"### WARNING: MEANINGLESS DRAFT !!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'CIAO'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" getattr(\"ciao\", \"upper\")()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'relmath.MatMul'>\n",
"[[9, 0, 6], [0, 5, 7]]\n",
"[[9, 0], [0, 5], [6, 7]]\n",
"['a', 'b', 'x', 'y', 'z', 'a', 'b']\n",
"[{'value': 9, 'source': 0, 'target': 2}, {'value': 0, 'source': 0, 'target': 3}, {'value': 6, 'source': 0, 'target': 4}, {'value': 0, 'source': 1, 'target': 2}, {'value': 5, 'source': 1, 'target': 3}, {'value': 7, 'source': 1, 'target': 4}, {'value': 9, 'source': 2, 'target': 5}, {'value': 0, 'source': 2, 'target': 6}, {'value': 0, 'source': 3, 'target': 5}, {'value': 5, 'source': 3, 'target': 6}, {'value': 6, 'source': 4, 'target': 5}, {'value': 7, 'source': 4, 'target': 6}]\n",
"[100, 100, 200, 200, 200, 300, 300]\n",
"[0, 1, 0, 1, 2, 0, 1]\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "89f836499223426c81eaeea3d92bf4a2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Figure(fig_margin={'bottom': 60, 'left': 60, 'right': 60, 'top': 60}, layout=Layout(height='500px', width='960…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"from bqplot import *\n",
"from bqplot.marks import Graph\n",
"from ipywidgets import Layout\n",
"from relmath import * \n",
"type(M)\n",
"\n",
"fig_layout = Layout(width='960px', height='500px')\n",
"def disp(expr):\n",
" \n",
" print(type(expr))\n",
" if type(expr) is MatMul:\n",
" binop = expr\n",
" \n",
" left = binop.left.simp()\n",
" right = binop.right.simp()\n",
" \n",
" if type(left) is not Rel:\n",
" raise ValueError(\"Can't simplify left operand to a Rel ! Found %s \" % left)\n",
"\n",
" if type(right) is not Rel:\n",
" raise ValueError(\"Can't simplify right operand to a Rel ! Found %s \" % right)\n",
" \n",
" print(left)\n",
" print(right)\n",
" \n",
" node_data = left.dom + left.cod + right.cod\n",
" print(node_data)\n",
"\n",
" link_data = []\n",
" n = len(left.dom)\n",
" m = len(left.cod)\n",
" w = len(right.cod)\n",
" for i in range(n):\n",
" for j in range(m):\n",
" link_data.append({'source': i, 'target': n+j, 'value': left.g[i][j].val})\n",
"\n",
" for i in range(m):\n",
" for j in range(w):\n",
" link_data.append({'source': n+i, 'target': n+m+j, 'value': right.g[i][j].val})\n",
" \n",
" \n",
" print(link_data)\n",
"\n",
" xs = LinearScale()\n",
" ys = LinearScale()\n",
" lcs = ColorScale(scheme='Greens')\n",
" x = ([100] * n) + ([200]*m) + ([300]*w)\n",
" y = list(range(n)) + list(range(m)) + list(range(w))\n",
" print(x)\n",
" print(y)\n",
" graph = Graph(node_data=node_data, link_data=link_data, link_type='line',\n",
" colors=['orange'], directed=False, \n",
" scales={'x': xs, 'y': ys, 'link_color': lcs}, \n",
" x=x, y=y, color=np.random.rand(len(node_data)))\n",
" return Figure(marks=[graph], layout=fig_layout) \n",
" \n",
" \n",
" elif type(expr) is Rel:\n",
" node_data = expr.dom() + expr.cod()\n",
" #print(node_data)\n",
"\n",
" link_data = []\n",
" for i in range(len(expr.dom)):\n",
" for j in range(len(expr.cod)):\n",
" link_data.append({'source': i, 'target': i+j, 'value': expr.g[i][j].val})\n",
"\n",
" #print(link_data)\n",
"\n",
" xs = LinearScale()\n",
" ys = LinearScale()\n",
" lcs = ColorScale(scheme='Greens')\n",
" x = ([100] * len(expr.dom)) + ([200]*len(expr.cod))\n",
" y = list(range(len(expr.dom))) + list(range(len(expr.cod)))\n",
" #print(x)\n",
" #print(y)\n",
" graph = Graph(node_data=node_data, link_data=link_data, link_type='line',\n",
" colors=['orange'], directed=False, \n",
" scales={'x': xs, 'y': ys, 'link_color': lcs}, \n",
" x=x, y=y, color=np.random.rand(len(node_data)))\n",
" return Figure(marks=[graph], layout=fig_layout) \n",
" else:\n",
" raise ValueError(\"not supported type: %s\" % type(expr) )\n",
" \n",
"disp(E) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand(len(node_data))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"M.nodes()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"node_data = list('ABCDEFG')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"node_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 7, (20, 2))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"link_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 73cf33f

Please sign in to comment.