{ "cell_id": 15583880456908114958, "cells": [ { "cell_id": 17303979617964142236, "cell_origin": "client", "cell_type": "input", "source": "from cdb.utils.node import n_indices\nfrom copy import copy" }, { "cell_id": 13419242331187320623, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 4122877639113717994, "cell_origin": "client", "cell_type": "latex_view", "source": "\\package{cdb.utils.indices}{Additional functionality to deal with indices}" } ], "hidden": true, "source": "\\package{cdb.utils.indices}{Additional functionality to deal with indices}" }, { "cell_id": 13857590062838682174, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 18354051870315698989, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{all_index_positions(ex: ExNode) -> Generator[Ex]}{Generate all possible combinations of covariant \nand contravariant indices}" } ], "hidden": true, "source": "\\algorithm{all_index_positions(ex: ExNode) -> Generator[Ex]}{Generate all possible combinations of covariant and contravariant indices}" }, { "cell_id": 10479853794613744564, "cell_origin": "client", "cell_type": "input", "source": "def all_index_positions(ex_):\n\tex = copy(ex_)\n\tn = n_indices(ex.top())\n\tfor i in range(2**n):\n\t\tfor height, index in zip(format(i, f\"0{n}b\"), ex.top().indices()):\n\t\t\tindex.parent_rel = parent_rel_t.sub if height == '0' else parent_rel_t.super\n\t\tyield ex" }, { "cell_id": 1567279626437013803, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 10625918768741539985, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 5744285270842328701, "cell_origin": "server", "cell_type": "input_form", "source": "A_{\\mu \\nu \\rho}" } ], "source": "\\begin{dmath*}{}A_{\\mu \\nu \\rho}\\end{dmath*}" }, { "cell_id": 808607824366024774, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 7391862823510323362, "cell_origin": "server", "cell_type": "input_form", "source": "A_{\\mu \\nu}^{\\rho}" } ], "source": "\\begin{dmath*}{}A_{\\mu \\nu}\\,^{\\rho}\\end{dmath*}" }, { "cell_id": 17820102242399456702, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 4056150914491624274, "cell_origin": "server", "cell_type": "input_form", "source": "A_{\\mu}^{\\nu}_{\\rho}" } ], "source": "\\begin{dmath*}{}A_{\\mu}\\,^{\\nu}\\,_{\\rho}\\end{dmath*}" }, { "cell_id": 11153558184430743234, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 4690332253586664815, "cell_origin": "server", "cell_type": "input_form", "source": "A_{\\mu}^{\\nu \\rho}" } ], "source": "\\begin{dmath*}{}A_{\\mu}\\,^{\\nu \\rho}\\end{dmath*}" }, { "cell_id": 17054390684282561432, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 5447125216017949069, "cell_origin": "server", "cell_type": "input_form", "source": "A^{\\mu}_{\\nu \\rho}" } ], "source": "\\begin{dmath*}{}A^{\\mu}\\,_{\\nu \\rho}\\end{dmath*}" }, { "cell_id": 13468401569599865394, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 399306766743001184, "cell_origin": "server", "cell_type": "input_form", "source": "A^{\\mu}_{\\nu}^{\\rho}" } ], "source": "\\begin{dmath*}{}A^{\\mu}\\,_{\\nu}\\,^{\\rho}\\end{dmath*}" }, { "cell_id": 17321927018441561683, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 5814884726611788367, "cell_origin": "server", "cell_type": "input_form", "source": "A^{\\mu \\nu}_{\\rho}" } ], "source": "\\begin{dmath*}{}A^{\\mu \\nu}\\,_{\\rho}\\end{dmath*}" }, { "cell_id": 16957255301193719326, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 5138394693482285977, "cell_origin": "server", "cell_type": "input_form", "source": "A^{\\mu \\nu \\rho}" } ], "source": "\\begin{dmath*}{}A^{\\mu \\nu \\rho}\\end{dmath*}" } ], "ignore_on_import": true, "source": "ex := A_{\\mu \\nu \\rho}.\nfor var in all_index_positions(ex):\n\tvar;" }, { "cell_id": 10591913784695924168, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 6309883738445027171, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{replace_index(ex: Ex, top_name: str, old_index: str, new_index: str) -> Ex}{Rename indices}\n\nRenames with \\texttt{new\\_index}\nall occurences of indices named \\texttt{old\\_index} in any subtree whose top node is named \\texttt{top\\_name}\n\\textbf{Note:}~This is useful for avoiding triple index-repeated errors due to the presence of tensor idnices in the argument of\nthe exponential function i.e. $\\exp(\\pm i k_\\lambda x^\\lambda)$" } ], "hidden": true, "source": "\\algorithm{replace_index(ex: Ex, top_name: str, old_index: str, new_index: str) -> Ex}{Rename indices}\n\nRenames with \\texttt{new\\_index}\nall occurences of indices named \\texttt{old\\_index} in any subtree whose top node is named \\texttt{top\\_name}\n\\textbf{Note:}~This is useful for avoiding triple index-repeated errors due to the presence of tensor idnices in the argument of\nthe exponential function i.e. $\\exp(\\pm i k_\\lambda x^\\lambda)$" }, { "cell_id": 12268357943789445408, "cell_origin": "client", "cell_type": "input", "source": "def replace_index(ex, top_name, old_index, new_index):\n\tfor node in ex[top_name]:\n\t\tfor match in node[old_index]:\n\t\t\tif match.parent_rel == parent_rel_t.super or match.parent_rel == parent_rel_t.sub:\n\t\t\t\tmatch.name = new_index\n\treturn ex" }, { "cell_id": 17409970256726760074, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 13795957434526174235, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 13060781541372264531, "cell_origin": "server", "cell_type": "input_form", "source": "h_{\\mu \\nu} = A_{\\mu \\nu} \\exp(i k_{\\mu} x^{\\mu})" } ], "source": "\\begin{dmath*}{}h_{\\mu \\nu} = A_{\\mu \\nu} \\exp\\left(i k_{\\mu} x^{\\mu}\\right)\\end{dmath*}" }, { "cell_id": 9993906878758157671, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 17742477934095676240, "cell_origin": "server", "cell_type": "input_form", "source": "h_{\\mu \\nu} = A_{\\mu \\nu} \\exp(i k_{\\alpha} x^{\\alpha})" } ], "source": "\\begin{dmath*}{}h_{\\mu \\nu} = A_{\\mu \\nu} \\exp\\left(i k_{\\alpha} x^{\\alpha}\\right)\\end{dmath*}" } ], "ignore_on_import": true, "source": "test := h_{\\mu\\nu} = A_{\\mu\\nu} \\exp(i*k_\\mu*x^{\\mu});\ntest = replace_index(test,r\"\\exp\",r'\\mu',r'\\alpha');" }, { "cell_id": 11510030216175255073, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 14243730794336119716, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{get_indices(node: ExNode) -> List[Ex]}{Return a list of all indices inside \\texttt{Node}}" } ], "hidden": true, "source": "\\algorithm{get_indices(node: ExNode) -> List[Ex]}{Return a list of all indices inside \\texttt{Node}}" }, { "cell_id": 4114061066973510892, "cell_origin": "client", "cell_type": "input", "source": "def get_indices(node):\n\t# Require monomial structure\n\tif isinstance(node, Ex):\n\t\tnode = node.top()\n\tassert node.name != r\"\\sum\" and node.name != r\"\\equals\", f\"get_indices() requires a monomial structure, got {node.name}\"\n\n\tindices = []\n\tinherits_indices = (IndexInherit.get(node) is not None)\n\n\t# Walk through children collecting indices recursively\n\tfor child in node.children():\n\t\tif child.parent_rel == parent_rel_t.super or child.parent_rel == parent_rel_t.sub:\n\t\t\t# Discard integers, symbols and coordinates\n\t\t\tsymb = Symbol.get(child)\n\t\t\tcoord = Coordinate.get(child)\n\t\t\tinteger = child.name == \"1\"\n\t\t\tif not (symb or coord or integer):\n\t\t\t\tindex = child.ex()\n\t\t\t\tindex.top().parent_rel = parent_rel_t.none\n\t\t\t\tindices.append(index)\n\t\telif inherits_indices:\n\t\t\tindices.extend(get_indices(child))\n\n\treturn indices" }, { "cell_id": 7471126205299592191, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 3926702791067015050, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 3419282733763189790, "cell_origin": "server", "cell_type": "input_form", "source": "a_{\\mu} \\partial_{\\sigma}(b_{\\mu} b_{\\lambda}) \\delta(p_{\\rho} p^{\\rho}) + g_{\\sigma \\lambda} g_{\\kappa} g^{\\kappa} = p_{\\sigma} p_{\\lambda}" } ], "source": "\\begin{dmath*}{}a_{\\mu} \\partial_{\\sigma}\\left(b_{\\mu} b_{\\lambda}\\right) \\delta\\left(p_{\\rho} p^{\\rho}\\right)+g_{\\sigma \\lambda} g_{\\kappa} g^{\\kappa} = p_{\\sigma} p_{\\lambda}\\end{dmath*}" }, { "cell_id": 467978901566468640, "cell_origin": "server", "cell_type": "verbatim", "source": "\\begin{verbatim}Caught error\\end{verbatim}" }, { "cell_id": 9282048507038981528, "cell_origin": "server", "cell_type": "latex_view", "source": "{}$\\big[$$\\sigma$,\\discretionary{}{}{} $\\lambda$$\\big]$" } ], "ignore_on_import": true, "source": "\\partial{#}::Derivative.\nex := 0 + a_{\\mu} \\partial_{\\sigma}{b_{\\mu}b_{\\lambda}} \\delta{p_{\\rho}p^{\\rho}} + g_{\\sigma\\lambda}g_{\\kappa}g^{\\kappa} = p_{\\sigma}p_{\\lambda};\ntry: \n\tget_indices(ex);\nexcept AssertionError:\n\t\"Caught error\";\nget_indices(ex[1]);" }, { "cell_id": 9875547432092551433, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 2437072492852689017, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{get_dummy_indices(node: ExNode) -> List[Ex]}{Return a list of all dummy indices inside \\texttt{Node}}" } ], "hidden": true, "source": "\\algorithm{get_dummy_indices(node: ExNode) -> List[Ex]}{Return a list of all dummy indices inside \\texttt{Node}}" }, { "cell_id": 16142500167825125558, "cell_origin": "client", "cell_type": "input", "source": "def get_dummy_indices(node):\n\tindices = get_indices(node)\n\tdummies = []\n\tfor pos, idx in enumerate(indices):\n\t\tif idx in dummies:\n\t\t\tcontinue\n\t\ttry:\n\t\t\tother = indices.index(idx, pos + 1)\n\t\t\tdummies.append(idx)\n\t\texcept ValueError:\n\t\t\tpass\n\treturn dummies" }, { "cell_id": 5826709331712786624, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 3965206875785443255, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 12964034287132943198, "cell_origin": "server", "cell_type": "input_form", "source": "a_{\\mu} \\partial_{\\sigma}(b_{\\mu} b_{\\lambda}) \\delta(p_{\\rho} p^{\\rho})" } ], "source": "\\begin{dmath*}{}a_{\\mu} \\partial_{\\sigma}\\left(b_{\\mu} b_{\\lambda}\\right) \\delta\\left(p_{\\rho} p^{\\rho}\\right)\\end{dmath*}" }, { "cell_id": 5118101628968331098, "cell_origin": "server", "cell_type": "latex_view", "source": "{}$\\big[$$\\mu$$\\big]$" } ], "ignore_on_import": true, "source": "\\partial{#}::Derivative.\nex := a_{\\mu} \\partial_{\\sigma}{b_{\\mu}b_{\\lambda}} \\delta{p_{\\rho}p^{\\rho}};\nget_dummy_indices(ex);" }, { "cell_id": 8198227195091703517, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 16224454427660616799, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{get_free_indices(node: ExNode) -> List[Ex]}{Return a list of all free indices inside \\texttt{Node}}" } ], "hidden": true, "source": "\\algorithm{get_free_indices(node: ExNode) -> List[Ex]}{Return a list of all free indices inside \\texttt{Node}}" }, { "cell_id": 12107868658700828741, "cell_origin": "client", "cell_type": "input", "source": "def get_free_indices(node):\n\t# Find a monomial\n\tif isinstance(node, Ex):\n\t\tnode = node.top()\n\twhile node.name == r\"\\sum\" or node.name == r\"\\equals\":\n\t\tfor child in node.children():\n\t\t\tif child.name != \"1\":\n\t\t\t\tnode = child\n\t\t\t\tbreak\n\t\telse:\n\t\t\treturn []\n\n\tindices = []\n\tinherits_indices = (IndexInherit.get(node) is not None)\n\n\t# Walk through children collecting indices recursively\n\tfor child in node.children():\n\t\tif child.parent_rel == parent_rel_t.super or child.parent_rel == parent_rel_t.sub:\n\t\t\t# Discard integers, symbols and coordinates\n\t\t\tsymb = Symbol.get(child)\n\t\t\tcoord = Coordinate.get(child)\n\t\t\tinteger = child.name == \"1\"\n\t\t\tif not (symb or coord or integer):\n\t\t\t\tindex = child.ex()\n\t\t\t\tindex.top().parent_rel = parent_rel_t.none\n\t\t\t\tindices.append(index)\n\t\telif inherits_indices:\n\t\t\tindices.extend(get_free_indices(child))\n\n\t# Find all repeated indices\n\tdummies = []\n\tfor pos, idx in enumerate(indices):\n\t\tif idx in dummies:\n\t\t\tcontinue\n\t\ttry:\n\t\t\tother = indices.index(idx, pos + 1)\n\t\t\tdummies.append(idx)\n\t\texcept ValueError:\n\t\t\tpass\n\n\t# Remove all instances of dummies from the set of indices and return\n\tfor dummy in dummies:\n\t\tindices = [i for i in indices if i != dummy]\n\treturn indices" }, { "cell_id": 13119901376492792968, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 669024716363112975, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 6297175375627481469, "cell_origin": "server", "cell_type": "input_form", "source": "a_{\\mu} \\partial_{\\sigma}(b_{\\mu} b_{\\lambda}) \\delta(p_{\\rho} p^{\\rho}) + g_{\\sigma \\lambda} g_{\\kappa} g^{\\kappa} = p_{\\sigma} p_{\\lambda}" } ], "source": "\\begin{dmath*}{}a_{\\mu} \\partial_{\\sigma}\\left(b_{\\mu} b_{\\lambda}\\right) \\delta\\left(p_{\\rho} p^{\\rho}\\right)+g_{\\sigma \\lambda} g_{\\kappa} g^{\\kappa} = p_{\\sigma} p_{\\lambda}\\end{dmath*}" }, { "cell_id": 4595981957722617540, "cell_origin": "server", "cell_type": "latex_view", "source": "{}$\\big[$$\\sigma$,\\discretionary{}{}{} $\\lambda$$\\big]$" } ], "ignore_on_import": true, "source": "\\partial{#}::Derivative.\nex := 0 + a_{\\mu} \\partial_{\\sigma}{b_{\\mu}b_{\\lambda}} \\delta{p_{\\rho}p^{\\rho}} + g_{\\sigma\\lambda}g_{\\kappa}g^{\\kappa} = p_{\\sigma}p_{\\lambda};\nget_free_indices(ex);" }, { "cell_id": 1627815774396580848, "cell_origin": "client", "cell_type": "latex", "cells": [ { "cell_id": 7144980246086443385, "cell_origin": "client", "cell_type": "latex_view", "source": "\\algorithm{symmetrise_slots(ex: Ex, slots: list[int], antisym: bool = False)}{Symmetrise\nor antisymmetrise an expression in the given slots}\n\nGiven an expression, iterates over all top level terms and symmetrises the indices in the\ngiven \\texttt{slots}, so that e.g. \\verb|symmetrise_slots($A_{a b c}$, [0,1])|\n$\\rightarrow$ $A_{a b c} + A_{b a c}$. For an expression containing a sum of terms,\nall terms in the expression are symmetrised." } ], "source": "\\algorithm{symmetrise_slots(ex: Ex, slots: list[int], antisym: bool = False)}{Symmetrise\nor antisymmetrise an expression in the given slots}\n\nGiven an expression, iterates over all top level terms and symmetrises the indices in the\ngiven \\texttt{slots}, so that e.g. \\verb|symmetrise_slots($A_{a b c}$, [0,1])|\n$\\rightarrow$ $A_{a b c} + A_{b a c}$. For an expression containing a sum of terms,\nall terms in the expression are symmetrised." }, { "cell_id": 16310576225572836586, "cell_origin": "client", "cell_type": "input", "source": "from itertools import permutations\n\ndef symmetrise_slots(ex, slots, antisym=False):\n\tdef is_even(p):\n\t\tif len(p) == 1: return True\n\t\ttrans = 0\n\t\tfor i in range(0,len(p)):\n\t\t\tj = i + 1\n\t\t\tfor j in range(j, len(p)):\n\t\t\t\tif p[i] > p[j]: trans = trans + 1\n\t\treturn ((trans % 2) == 0)\n\tdef _sym_slots(it, slots, antisym=False):\n\t\trepl = $a$\n\t\trepl.top().name = \"\\\\sum\"\n\t\tindices = [$@(index)$ for index in it.indices()]\n\t\trels = [index.parent_rel for index in it.indices()]\n\t\tfor permutation in permutations(slots):\n\t\t\tnew_indices = [$@(index)$ for index in indices]\n\t\t\tnew_rels = [rel for rel in rels]\n\t\t\tfor a, b in zip(slots, permutation):\n\t\t\t\tnew_indices[a] = indices[b]\n\t\t\t\tnew_rels[a] = rels[b]\n\t\t\tif not antisym or is_even(permutation): new_term = $@(it)$\n\t\t\telse: new_term = $-@(it)$\n\t\t\tfor i, index in enumerate(new_term.top().indices()):\n\t\t\t\tindex.replace(new_indices[i])\n\t\t\t\tindex.parent_rel = new_rels[i]\n\t\t\trepl.top().append_child(new_term)\n\t\tit.replace(repl)\n\tslots = sorted(slots)\n\tif ex.top().name == r\"\\sum\":\n\t\tfor term in ex.top().children():\n\t\t\t_sym_slots(term, slots, antisym)\n\telse:\n\t\t_sym_slots(ex.top(), slots, antisym)\n\tex.cleanup()\n\treturn ex" }, { "cell_id": 8743806279518418158, "cell_origin": "client", "cell_type": "input", "cells": [ { "cell_id": 3660017727901812283, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 9316541226681817515, "cell_origin": "server", "cell_type": "input_form", "source": "R_{a b c d}-R_{a b d c}-R_{a c d b} + R_{a d c b}-R_{b a c d} + R_{b a d c} + R_{b c d a}-R_{b d c a}-R_{c a b d} + R_{c b a d} + R_{c d a b}-R_{c d b a} + R_{d a b c}-R_{d b a c}-R_{d c a b} + R_{d c b a}" } ], "source": "\\begin{dmath*}{}R_{a b c d}-R_{a b d c}-R_{a c d b}+R_{a d c b}-R_{b a c d}+R_{b a d c}+R_{b c d a}-R_{b d c a}-R_{c a b d}+R_{c b a d}+R_{c d a b}-R_{c d b a}+R_{d a b c}-R_{d b a c}-R_{d c a b}+R_{d c b a}\\end{dmath*}" }, { "cell_id": 11071786508319260826, "cell_origin": "server", "cell_type": "latex_view", "cells": [ { "cell_id": 13751431965017289945, "cell_origin": "server", "cell_type": "input_form", "source": "R_{a b c d}-R_{a b d c}-R_{a c d b} + R_{a d c b}-R_{b a c d} + R_{b a d c} + R_{b c d a}-R_{b d c a}-R_{c a b d} + R_{c b a d} + R_{c d a b}-R_{c d b a} + R_{d a b c}-R_{d b a c}-R_{d c a b} + R_{d c b a}" } ], "source": "\\begin{dmath*}{}R_{a b c d}-R_{a b d c}-R_{a c d b}+R_{a d c b}-R_{b a c d}+R_{b a d c}+R_{b c d a}-R_{b d c a}-R_{c a b d}+R_{c b a d}+R_{c d a b}-R_{c d b a}+R_{d a b c}-R_{d b a c}-R_{d c a b}+R_{d c b a}\\end{dmath*}" } ], "ignore_on_import": true, "source": "ex1 := R_{a b c d}:\nsymmetrise_slots(ex1, [0,1], True)\nsymmetrise_slots(ex1, [2,3], True)\nsymmetrise_slots(ex1, [0,2])\nsymmetrise_slots(ex1, [1,3])\nsort_sum(ex1);\n\nR_{a b c d}::RiemannTensor.\nex2 := 12 R_{a b c d}:\nyoung_project_tensor(ex2)\nsort_sum(ex2);\n\nassert ex1 == ex2" }, { "cell_id": 5089890689640016182, "cell_origin": "client", "cell_type": "input", "source": "" } ], "description": "Cadabra JSON notebook format", "version": 1.0 }