Skip to content

Commit b3d83ea

Browse files
author
Kasper Peeters
committed
Add sympy integration wrapper.
1 parent 1751669 commit b3d83ea

File tree

1 file changed

+151
-3
lines changed

1 file changed

+151
-3
lines changed

core/packages/cdb/sympy/calculus.cnb

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@
77
"cell_type": "latex",
88
"cells": [
99
{
10-
"cell_id": 5114489047998022221,
10+
"cell_id": 7825517350706958358,
1111
"cell_origin": "client",
1212
"cell_type": "latex_view",
13-
"source": "\\package{cdb.sympy.calculus}{Glue to use SymPy calculus functionality on Cadabra expressions.}\n\nA lot of functionality in SymPy can be used on Cadabra expressions, but in order to correctly\nconvert from and to the SymPy expression form, we need a bridge. This package contains such\nbridging fuctions, which will ensure that e.g. tensor indices are handled correctly when\nfed through SymPy's scalar algebra algorithms.\n\n\\algorithm{diff}{Differentiate an expression with respect to a variable.}"
13+
"source": "\\package{cdb.sympy.calculus}{Use SymPy calculus functionality on Cadabra expressions.}\n\nA lot of functionality in SymPy can be used on Cadabra expressions, but in order to correctly\nconvert from and to the SymPy expression form, we need a bridge. This package contains such\nbridging fuctions, which will ensure that e.g. tensor indices are handled correctly when\nfed through SymPy's scalar algebra algorithms.\n\n\\algorithm{diff}{Differentiate an expression with respect to a variable.}"
1414
}
1515
],
1616
"hidden": true,
17-
"source": "\\package{cdb.sympy.calculus}{Glue to use SymPy calculus functionality on Cadabra expressions.}\n\nA lot of functionality in SymPy can be used on Cadabra expressions, but in order to correctly\nconvert from and to the SymPy expression form, we need a bridge. This package contains such\nbridging fuctions, which will ensure that e.g. tensor indices are handled correctly when\nfed through SymPy's scalar algebra algorithms.\n\n\\algorithm{diff}{Differentiate an expression with respect to a variable.}"
17+
"source": "\\package{cdb.sympy.calculus}{Use SymPy calculus functionality on Cadabra expressions.}\n\nA lot of functionality in SymPy can be used on Cadabra expressions, but in order to correctly\nconvert from and to the SymPy expression form, we need a bridge. This package contains such\nbridging fuctions, which will ensure that e.g. tensor indices are handled correctly when\nfed through SymPy's scalar algebra algorithms.\n\n\\algorithm{diff}{Differentiate an expression with respect to a variable.}"
1818
},
1919
{
2020
"cell_id": 3804610120254550473,
2121
"cell_origin": "client",
2222
"cell_type": "input",
2323
"source": "def diff(ex1, ex2):\n ret = $@(ex1)$\n sb = SympyBridge(ret)\n sb.from_sympy( str(sympy.diff( sb.to_sympy(), ex2 ) ) )\n return ret"
2424
},
25+
{
26+
"cell_id": 12308609622224851480,
27+
"cell_origin": "client",
28+
"cell_type": "latex",
29+
"cells": [
30+
{
31+
"cell_id": 13996360633394007671,
32+
"cell_origin": "client",
33+
"cell_type": "latex_view",
34+
"source": "A typical example, differentiating with respect to a single variable:"
35+
}
36+
],
37+
"hidden": true,
38+
"source": "A typical example, differentiating with respect to a single variable:"
39+
},
2540
{
2641
"cell_id": 1389897799554975903,
2742
"cell_origin": "client",
@@ -59,10 +74,143 @@
5974
"ignore_on_import": true,
6075
"source": "ex:=\\sin(x) A_{\\mu}(x);\ndiff(ex, $x$);"
6176
},
77+
{
78+
"cell_id": 6234003092100779795,
79+
"cell_origin": "client",
80+
"cell_type": "latex",
81+
"cells": [
82+
{
83+
"cell_id": 10150353556520051186,
84+
"cell_origin": "client",
85+
"cell_type": "latex_view",
86+
"source": "\\algorithm{integrate}{Integrate a definite or indefinite integral.}\n\nThis function mimics the SymPy \\verb|integrate| function, except that all\nmathematical expressions need to be Cadabra expressions. Indefinite integration\nis done by passing an argument which is just an expression, while definite\nintegration is done by passing a tuple consisting of the integration variable,\nthe starting point and the end point."
87+
}
88+
],
89+
"hidden": true,
90+
"source": "\\algorithm{integrate}{Integrate a definite or indefinite integral.}\n\nThis function mimics the SymPy \\verb|integrate| function, except that all\nmathematical expressions need to be Cadabra expressions. Indefinite integration\nis done by passing an argument which is just an expression, while definite\nintegration is done by passing a tuple consisting of the integration variable,\nthe starting point and the end point."
91+
},
6292
{
6393
"cell_id": 15467993639628642890,
6494
"cell_origin": "client",
6595
"cell_type": "input",
96+
"source": "def integrate(ex1, *args):\n ret = $@(ex1)$\n sb = SympyBridge(ret)\n iranges = []\n for arg in args:\n if isinstance(arg, tuple):\n # print(arg)\n sbtmp = SympyBridge(arg[0])\n tpl = (sbtmp.to_sympy(), arg[1], arg[2])\n iranges.append(tpl)\n # print(iranges)\n sb.from_sympy( str(sympy.integrate( sb.to_sympy(), *iranges) ) )\n else:\n sbtmp = SympyBridge(arg)\n sb.from_sympy( str(sympy.integrate( sb.to_sympy(), sbtmp.to_sympy() ) ))\n return ret"
97+
},
98+
{
99+
"cell_id": 3801120048138997797,
100+
"cell_origin": "client",
101+
"cell_type": "latex",
102+
"cells": [
103+
{
104+
"cell_id": 13060402605556641930,
105+
"cell_origin": "client",
106+
"cell_type": "latex_view",
107+
"source": "The following is an example of a definite integration:"
108+
}
109+
],
110+
"hidden": true,
111+
"source": "The following is an example of a definite integration:"
112+
},
113+
{
114+
"cell_id": 14525293143474443676,
115+
"cell_origin": "client",
116+
"cell_type": "input",
117+
"cells": [
118+
{
119+
"cell_id": 12012117006062441490,
120+
"cell_origin": "server",
121+
"cell_type": "latex_view",
122+
"cells": [
123+
{
124+
"cell_id": 316157390348515815,
125+
"cell_origin": "server",
126+
"cell_type": "input_form",
127+
"source": " 27/2 "
128+
}
129+
],
130+
"source": "\\begin{dmath*}{}\\frac{27}{2}\\end{dmath*}"
131+
}
132+
],
133+
"ignore_on_import": true,
134+
"source": "integrate($x**2 y$, ($x$, 0, 3), ($y$, 0, 1) );"
135+
},
136+
{
137+
"cell_id": 2354178610469859185,
138+
"cell_origin": "client",
139+
"cell_type": "latex",
140+
"cells": [
141+
{
142+
"cell_id": 17727729829154494875,
143+
"cell_origin": "client",
144+
"cell_type": "latex_view",
145+
"source": "Here is an indefinite integration:"
146+
}
147+
],
148+
"hidden": true,
149+
"source": "Here is an indefinite integration:"
150+
},
151+
{
152+
"cell_id": 2273072162227922772,
153+
"cell_origin": "client",
154+
"cell_type": "input",
155+
"cells": [
156+
{
157+
"cell_id": 15728598564050855914,
158+
"cell_origin": "server",
159+
"cell_type": "latex_view",
160+
"cells": [
161+
{
162+
"cell_id": 13682162971051715272,
163+
"cell_origin": "server",
164+
"cell_type": "input_form",
165+
"source": " 1/3 (x)**3 y"
166+
}
167+
],
168+
"source": "\\begin{dmath*}{}\\frac{1}{3}{x}^{3} y\\end{dmath*}"
169+
}
170+
],
171+
"source": "integrate($x**2$, $x$, $y$);"
172+
},
173+
{
174+
"cell_id": 14437617558705092816,
175+
"cell_origin": "client",
176+
"cell_type": "latex",
177+
"cells": [
178+
{
179+
"cell_id": 9935958107203452733,
180+
"cell_origin": "client",
181+
"cell_type": "latex_view",
182+
"source": "Mixed versions are also possible:"
183+
}
184+
],
185+
"hidden": true,
186+
"source": "Mixed versions are also possible:"
187+
},
188+
{
189+
"cell_id": 6556436360376410122,
190+
"cell_origin": "client",
191+
"cell_type": "input",
192+
"cells": [
193+
{
194+
"cell_id": 4095815210246821736,
195+
"cell_origin": "server",
196+
"cell_type": "latex_view",
197+
"cells": [
198+
{
199+
"cell_id": 13733098240311066994,
200+
"cell_origin": "server",
201+
"cell_type": "input_form",
202+
"source": " 1/4 (y)**2"
203+
}
204+
],
205+
"source": "\\begin{dmath*}{}\\frac{1}{4}{y}^{2}\\end{dmath*}"
206+
}
207+
],
208+
"source": "integrate($x y$, ($x$, 0, 1), $y$);"
209+
},
210+
{
211+
"cell_id": 14261374590043911759,
212+
"cell_origin": "client",
213+
"cell_type": "input",
66214
"source": ""
67215
}
68216
],

0 commit comments

Comments
 (0)