|
47 | 47 | "cell_id": 13334862458884777602, |
48 | 48 | "cell_origin": "client", |
49 | 49 | "cell_type": "input", |
50 | | - "source": "from cdb.utils._algorithm import apply_algo_base as _apply_algo_base\n\ndef _cast_algo(apply, can_apply = lambda node: True):\n\tif len(inspect.getfullargspec(apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: function must only take one 'node' parameter\")\n\tif len(inspect.getfullargspec(can_apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: predicate must only take one 'node' parameter\")\n\treturn type(apply.__name__, (Algorithm, ), { \n\t\t\"can_apply\": lambda self, node: can_apply(node), \n\t\t\"apply\": lambda self, node: apply(node)\n\t})\n\ndef _create_algo(cls, pre_order):\n\tif Algorithm not in cls.__bases__:\n\t\traise TypeError(\"Algorithm must derive from Algorithm type\")\n\tdef wrapper(ex, *args, deep=True, repeat=False, depth=0, **kwargs):\n\t\tx = cls(ex, *args, **kwargs)\n\t\treturn _apply_algo_base(x, ex, deep, repeat, depth, pre_order)\n\treturn wrapper" |
| 50 | + "source": "from cdb.utils._algorithm import apply_algo_base as _apply_algo_base\n\ndef _cast_algo(apply, can_apply = lambda node: True):\n\tif len(inspect.getfullargspec(apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: function must only take one 'node' parameter\")\n\tif len(inspect.getfullargspec(can_apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: predicate must only take one 'node' parameter\")\n\treturn type(apply.__name__, (Algorithm, ), { \n\t\t\"can_apply\": lambda self, node: can_apply(node), \n\t\t\"apply\": lambda self, node: apply(node),\n\t\t\"__doc__\": apply.__doc__\n\t})\n\ndef _create_algo(cls, pre_order):\n\tif Algorithm not in cls.__bases__:\n\t\traise TypeError(\"Algorithm must derive from Algorithm type\")\n\tdef wrapper(ex, *args, deep=True, repeat=False, depth=0, **kwargs):\n\t\tx = cls(ex, *args, **kwargs)\n\t\treturn _apply_algo_base(x, ex, deep, repeat, depth, pre_order)\n\n\t# Generate correct meta-information\n\twrapper.__name__ = cls.__name__\n\twrapper.__doc__ = cls.__doc__\n\t# Get argument list of Algorithm constructor\n\ttry:\n\t\tparameters = list(inspect.signature(cls\t.__init__).parameters.values())[1:]\n\texcept:\n\t\tparameters = [ inspect.Parameter(\"ex\", inspect.Parameter.POSITIONAL_OR_KEYWORD) ]\n\t# Parameters of apply_algo_base\n\talgo_parameters = [\n\t\tinspect.Parameter(\"deep\", inspect.Parameter.KEYWORD_ONLY, default=True),\n\t\tinspect.Parameter(\"repeat\", inspect.Parameter.KEYWORD_ONLY, default=False),\n\t\tinspect.Parameter(\"depth\", inspect.Parameter.KEYWORD_ONLY, default=0) ]\n\t# If parameters has a **kwargs at the end, algo_parameters must be inserted before this\n\tif parameters[-1].kind == inspect.Parameter.VAR_KEYWORD:\n\t\twrapper.__signature__ = inspect.Signature(parameters[:-1] + algo_parameters + parameters[-1:])\n\telse:\n\t\twrapper.__signature__ = inspect.Signature(parameters + algo_parameters)\n\treturn wrapper" |
51 | 51 | }, |
52 | 52 | { |
53 | 53 | "cell_id": 2421359616022599147, |
|
76 | 76 | "cell_type": "input", |
77 | 77 | "cells": [ |
78 | 78 | { |
79 | | - "cell_id": 4043273741898506206, |
| 79 | + "cell_id": 15761994739137138802, |
80 | 80 | "cell_origin": "server", |
81 | 81 | "cell_type": "latex_view", |
82 | 82 | "cells": [ |
83 | 83 | { |
84 | | - "cell_id": 11767503884852531506, |
| 84 | + "cell_id": 17415156100168742565, |
85 | 85 | "cell_origin": "server", |
86 | 86 | "cell_type": "input_form", |
87 | 87 | "source": "A^{\\mu} + B^{\\mu}" |
88 | 88 | } |
89 | 89 | ], |
90 | 90 | "source": "\\begin{dmath*}{}A^{\\mu}+B^{\\mu}\\end{dmath*}" |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_id": 15144194947899395553, |
| 94 | + "cell_origin": "server", |
| 95 | + "cell_type": "output", |
| 96 | + "source": "\\begin{verbatim}Help on function switch_indices in module __main__:\n\nswitch_indices(ex, *, deep=True, repeat=False, depth=0)\n Switch positions on all indices\n\n\\end{verbatim}" |
91 | 97 | } |
92 | 98 | ], |
93 | 99 | "ignore_on_import": true, |
94 | | - "source": "@algo\ndef switch_indices(node):\n\tif node.parent_rel == parent_rel_t.sub:\n\t\tnode.parent_rel = parent_rel_t.super\n\t\treturn result_t.changed\n\tif node.parent_rel == parent_rel_t.super:\n\t\tnode.parent_rel = parent_rel_t.sub\n\t\treturn result_t.changed\n\treturn result_t.unchanged\n\n# also takes optional 'deep', 'repeat' and 'depth' arguments\nswitch_indices($A_{\\mu} + B_{\\mu}$);" |
| 100 | + "source": "@algo\ndef switch_indices(node):\n\t\"Switch positions on all indices\"\n\tif node.parent_rel == parent_rel_t.sub:\n\t\tnode.parent_rel = parent_rel_t.super\n\t\treturn result_t.changed\n\tif node.parent_rel == parent_rel_t.super:\n\t\tnode.parent_rel = parent_rel_t.sub\n\t\treturn result_t.changed\n\treturn result_t.unchanged\n\n# also takes optional 'deep', 'repeat' and 'depth' arguments\nswitch_indices($A_{\\mu} + B_{\\mu}$);\nhelp(switch_indices)" |
95 | 101 | }, |
96 | 102 | { |
97 | 103 | "cell_id": 9340011096170931285, |
98 | 104 | "cell_origin": "client", |
99 | 105 | "cell_type": "input", |
100 | 106 | "cells": [ |
101 | 107 | { |
102 | | - "cell_id": 14835945528453774882, |
| 108 | + "cell_id": 5850074756591759657, |
103 | 109 | "cell_origin": "server", |
104 | 110 | "cell_type": "latex_view", |
105 | 111 | "cells": [ |
106 | 112 | { |
107 | | - "cell_id": 15750930497414250141, |
| 113 | + "cell_id": 9620504524171758578, |
108 | 114 | "cell_origin": "server", |
109 | 115 | "cell_type": "input_form", |
110 | 116 | "source": "2y" |
111 | 117 | } |
112 | 118 | ], |
113 | 119 | "source": "\\begin{dmath*}{}2y\\end{dmath*}" |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_id": 17850582158135921593, |
| 123 | + "cell_origin": "server", |
| 124 | + "cell_type": "output", |
| 125 | + "source": "\\begin{verbatim}Help on function x_to_y in module __main__:\n\nx_to_y(ex, *, deep=True, repeat=False, depth=0)\n\n\\end{verbatim}" |
114 | 126 | } |
115 | 127 | ], |
116 | 128 | "ignore_on_import": true, |
117 | | - "source": "@algo(pre_order=True, pred=lambda node: node.name == 'x')\ndef x_to_y(node):\n\tnode.name = 'y'\n\treturn result_t.changed\n\nx_to_y($x + y$);" |
| 129 | + "source": "@algo(pre_order=True, pred=lambda node: node.name == 'x')\ndef x_to_y(node):\n\tnode.name = 'y'\n\treturn result_t.changed\n\nx_to_y($x + y$);\nhelp(x_to_y)" |
118 | 130 | }, |
119 | 131 | { |
120 | 132 | "cell_id": 15634397355979861807, |
121 | 133 | "cell_origin": "client", |
122 | 134 | "cell_type": "input", |
123 | 135 | "cells": [ |
124 | 136 | { |
125 | | - "cell_id": 960346398456001684, |
| 137 | + "cell_id": 14537821031780487327, |
126 | 138 | "cell_origin": "server", |
127 | 139 | "cell_type": "latex_view", |
128 | 140 | "cells": [ |
129 | 141 | { |
130 | | - "cell_id": 1313190532695810937, |
| 142 | + "cell_id": 3417601803473011010, |
131 | 143 | "cell_origin": "server", |
132 | 144 | "cell_type": "input_form", |
133 | 145 | "source": "2t_{\\mu}" |
134 | 146 | } |
135 | 147 | ], |
136 | 148 | "source": "\\begin{dmath*}{}2t_{\\mu}\\end{dmath*}" |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_id": 17754394564063656935, |
| 152 | + "cell_origin": "server", |
| 153 | + "cell_type": "output", |
| 154 | + "source": "\\begin{verbatim}Help on function a_to_b in module __main__:\n\na_to_b(ex, a, b, *args, deep=True, repeat=False, depth=0, **kwargs)\n\n\\end{verbatim}" |
137 | 155 | } |
138 | 156 | ], |
139 | 157 | "ignore_on_import": true, |
140 | | - "source": "@algo\nclass a_to_b(Algorithm):\n\tdef __init__(self, ex, a, b):\n\t\tAlgorithm.__init__(self, ex)\n\t\tself.a, self.b = a, b\n\t\n\tdef can_apply(self, node):\n\t\treturn node.name == self.a.head()\n\n\tdef apply(self, node):\n\t\tnode.name = self.b.head()\n\t\treturn result_t.changed\n\na_to_b($s_{\\mu} + t_{\\mu}$, $s_{\\mu}$, $t_{\\mu}$);" |
| 158 | + "source": "@algo\nclass a_to_b(Algorithm):\n\tdef __init__(self, ex, a, b, *args, **kwargs):\n\t\tAlgorithm.__init__(self, ex)\n\t\tself.a, self.b = a, b\n\t\n\tdef can_apply(self, node):\n\t\treturn node.name == self.a.head()\n\n\tdef apply(self, node):\n\t\tnode.name = self.b.head()\n\t\treturn result_t.changed\n\na_to_b($s_{\\mu} + t_{\\mu}$, $s_{\\mu}$, $t_{\\mu}$);\nhelp(a_to_b)" |
141 | 159 | }, |
142 | 160 | { |
143 | 161 | "cell_id": 10135200674124616797, |
|
173 | 191 | "cell_type": "input", |
174 | 192 | "cells": [ |
175 | 193 | { |
176 | | - "cell_id": 16504503887878721184, |
| 194 | + "cell_id": 6295148871247203489, |
177 | 195 | "cell_origin": "server", |
178 | 196 | "cell_type": "verbatim", |
179 | | - "source": "\\begin{verbatim}0:00:00.008331\\end{verbatim}" |
| 197 | + "source": "\\begin{verbatim}0:00:00.000875\\end{verbatim}" |
180 | 198 | } |
181 | 199 | ], |
182 | 200 | "ignore_on_import": true, |
|
188 | 206 | "cell_type": "input", |
189 | 207 | "cells": [ |
190 | 208 | { |
191 | | - "cell_id": 17622425082194156367, |
| 209 | + "cell_id": 13532413926824958590, |
192 | 210 | "cell_origin": "server", |
193 | 211 | "cell_type": "verbatim", |
194 | | - "source": "\\begin{verbatim}0:00:00.003194\\end{verbatim}" |
| 212 | + "source": "\\begin{verbatim}0:00:00.000493\\end{verbatim}" |
195 | 213 | } |
196 | 214 | ], |
197 | 215 | "ignore_on_import": true, |
|
245 | 263 | "cell_type": "input", |
246 | 264 | "cells": [ |
247 | 265 | { |
248 | | - "cell_id": 17109594158398788775, |
| 266 | + "cell_id": 4855635716783607432, |
249 | 267 | "cell_origin": "server", |
250 | 268 | "cell_type": "output", |
251 | | - "source": "\\begin{verbatim}sort_sum_test01 passed\nsort_sum_test02 FAILED\n Expected: a + b\n Produced: a + c\nsort_sum_test03 FAILED\n Expected: a + b\n Produced: a + c\n\\end{verbatim}" |
| 269 | + "source": "\\begin{verbatim}sort_sum_test01 passed\nsort_sum_test02 FAILED\n Expected: a + b\n Produced: a + c\nsort_sum_test03 FAILED\n Expected: a + b\n Produced: a + c\nRaised CadabraTestError\n\\end{verbatim}" |
252 | 270 | } |
253 | 271 | ], |
254 | 272 | "ignore_on_import": true, |
255 | | - "source": "@test_algo($a + b + c$)\ndef sort_sum_test01():\n\tex := b + a + c.\n\treturn sort_sum(ex)\nsort_sum_test01()\n\n@test_algo($a + b$)\ndef sort_sum_test02():\n\tex := c + a.\n\treturn sort_sum(ex)\nsort_sum_test02()\n\n@test_algo($a + b$, throw_on_fail=True)\ndef sort_sum_test03():\n\tex := c + a.\n\treturn sort_sum(ex)\n\ntry:\n\tsort_sum_test03()\n\traise AssertionError(\"CadabraTestError not raised!\")\nexcept CadabraTestError:\n\tpass" |
| 273 | + "source": "@test_algo($a + b + c$)\ndef sort_sum_test01():\n\tex := b + a + c.\n\treturn sort_sum(ex)\nsort_sum_test01()\n\n@test_algo($a + b$)\ndef sort_sum_test02():\n\tex := c + a.\n\treturn sort_sum(ex)\nsort_sum_test02()\n\n@test_algo($a + b$, throw_on_fail=True)\ndef sort_sum_test03():\n\tex := c + a.\n\treturn sort_sum(ex)\n\ntry:\n\tsort_sum_test03()\n\traise AssertionError(\"CadabraTestError not raised!\")\nexcept CadabraTestError:\n\tprint(\"Raised CadabraTestError\")" |
256 | 274 | }, |
257 | 275 | { |
258 | 276 | "cell_id": 17571440201905887785, |
|
0 commit comments