Skip to content

Commit 87dfce7

Browse files
committed
Added two-sphere example
1 parent df9daeb commit 87dfce7

File tree

3 files changed

+195
-1
lines changed

3 files changed

+195
-1
lines changed

core/DisplaySympy.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ DisplaySympy::DisplaySympy(const Kernel& kernel, const Ex& e)
4444
{"\\tau", "tau" },
4545
{"\\upsilon", "upsilon" },
4646
{"\\phi", "phi" },
47+
{"\\varphi", "varphi" },
4748
{"\\chi", "chi" },
4849
{"\\psi", "psi" },
4950
{"\\omega", "omega" },

core/algorithms/map_sympy.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66

77
using namespace cadabra;
88

9+
#define DEBUG 1
10+
911
map_sympy::map_sympy(const Kernel& k, Ex& tr, const std::string& head)
1012
: Algorithm(k, tr), head_(head)
1113
{
1214
}
1315

1416
bool map_sympy::can_apply(iterator st)
1517
{
18+
// For \components nodes we need to map at the level of the individual
19+
// component values, not the top \components node.
20+
if(*st->name=="\\components") return false;
21+
if(*st->name=="\\equals") return false;
22+
if(*st->name=="\\comma") return false;
23+
1624
left.clear();
1725
index_factors.clear();
1826
index_map_t ind_free, ind_dummy;
@@ -72,7 +80,9 @@ bool map_sympy::can_apply(iterator st)
7280

7381
Algorithm::result_t map_sympy::apply(iterator& it)
7482
{
75-
// std::cerr << "Apply on " << tr << std::endl;
83+
#ifdef DEBUG
84+
std::cerr << "map_sympy on " << Ex(it) << std::endl;
85+
#endif
7686

7787
std::vector<std::string> wrap;
7888
wrap.push_back(head_);

examples/sphere.cnb

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"cells" :
3+
[
4+
{
5+
"cell_origin" : "client",
6+
"cell_type" : "latex",
7+
"cells" :
8+
[
9+
{
10+
"cell_origin" : "client",
11+
"cell_type" : "latex_view",
12+
"source" : "\\section*{Two-sphere}\n\nThis notebook computes the curvature of a 2-sphere by computing connection coefficients and the\nRiemann tensor components."
13+
}
14+
],
15+
"hidden" : true,
16+
"source" : "\\section*{Two-sphere}\n\nThis notebook computes the curvature of a 2-sphere by computing connection coefficients and the\nRiemann tensor components."
17+
},
18+
{
19+
"cell_origin" : "client",
20+
"cell_type" : "input",
21+
"cells" :
22+
[
23+
{
24+
"cell_origin" : "server",
25+
"cell_type" : "latex_view",
26+
"source" : "\\begin{dmath*}{}\\text{Attached property Coordinate to~}\\left[\\theta,~\\discretionary{}{}{} \\varphi\\right].\\end{dmath*}"
27+
},
28+
{
29+
"cell_origin" : "server",
30+
"cell_type" : "latex_view",
31+
"source" : "\\begin{dmath*}{}\\text{Attached property Indices(position=fixed) to~}\\left[\\alpha,~\\discretionary{}{}{} \\beta,~\\discretionary{}{}{} \\gamma,~\\discretionary{}{}{} \\delta,~\\discretionary{}{}{} \\rho,~\\discretionary{}{}{} \\sigma,~\\discretionary{}{}{} \\mu,~\\discretionary{}{}{} \\nu,~\\discretionary{}{}{} \\lambda\\right].\\end{dmath*}"
32+
},
33+
{
34+
"cell_origin" : "server",
35+
"cell_type" : "latex_view",
36+
"source" : "\\begin{dmath*}{}\\text{Attached property PartialDerivative to~}\\partial{\\#}.\\end{dmath*}"
37+
}
38+
],
39+
"source" : "{\\theta, \\varphi}::Coordinate;\n{\\alpha, \\beta, \\gamma, \\delta, \\rho, \\sigma, \\mu, \\nu, \\lambda}::Indices(values={\\varphi, \\theta}, position=fixed);\n\\partial{#}::PartialDerivative;\ng_{\\alpha\\beta}::Metric.\ng^{\\alpha\\beta}::InverseMetric."
40+
},
41+
{
42+
"cell_origin" : "client",
43+
"cell_type" : "input",
44+
"cells" :
45+
[
46+
{
47+
"cell_origin" : "server",
48+
"cell_type" : "latex_view",
49+
"source" : "\\begin{dmath*}{}\\left[g_{\\theta \\theta} = {r}^{2},~\\discretionary{}{}{} g_{\\varphi \\varphi} = {r}^{2} {\\left(\\sin{\\theta}\\right)}^{2},~\\discretionary{}{}{} g^{\\varphi \\varphi} = \\frac{1}{{r}^{2} {\\left(\\sin{\\theta}\\right)}^{2}},~\\discretionary{}{}{} g^{\\theta \\theta} = {r}^{-2}\\right]\\end{dmath*}"
50+
}
51+
],
52+
"source" : "sphe:={ g_{\\theta\\theta} = r**2,\n g_{\\varphi\\varphi} = r**2 \\sin(\\theta)**2 }.\ncomplete(sphe, $g^{\\alpha\\beta}$);"
53+
},
54+
{
55+
"cell_origin" : "client",
56+
"cell_type" : "input",
57+
"cells" :
58+
[
59+
{
60+
"cell_origin" : "server",
61+
"cell_type" : "latex_view",
62+
"source" : "\\begin{dmath*}{}\\Gamma^{\\alpha}\\,_{\\mu \\nu} = \\frac{1}{2}g^{\\alpha \\beta} \\left(\\partial_{\\nu}{g_{\\beta \\mu}}+\\partial_{\\mu}{g_{\\beta \\nu}}-\\partial_{\\beta}{g_{\\mu \\nu}}\\right)\\end{dmath*}"
63+
}
64+
],
65+
"source" : "ch:= \\Gamma^{\\alpha}_{\\mu\\nu} = 1/2 g^{\\alpha\\beta} ( \\partial_{\\nu}{g_{\\beta\\mu}}\n +\\partial_{\\mu}{g_{\\beta\\nu}}\n -\\partial_{\\beta}{g_{\\mu\\nu}} );"
66+
},
67+
{
68+
"cell_origin" : "client",
69+
"cell_type" : "input",
70+
"cells" :
71+
[
72+
{
73+
"cell_origin" : "server",
74+
"cell_type" : "latex_view",
75+
"source" : "\\begin{dmath*}{}\\Gamma^{\\alpha}\\,_{\\mu \\nu} = \\square{}_{\\mu}{}_{\\nu}{}^{\\alpha}\\left\\{\\begin{aligned}\\square{}_{\\varphi}{}_{\\theta}{}^{\\varphi}= & \\frac{1}{\\tan{\\theta}}\\\\[-.5ex]\n\\square{}_{\\theta}{}_{\\varphi}{}^{\\varphi}= & \\frac{1}{\\tan{\\theta}}\\\\[-.5ex]\n\\square{}_{\\varphi}{}_{\\varphi}{}^{\\theta}= & - \\frac{1}{2}\\sin{2\\theta}\\\\[-.5ex]\n\\end{aligned}\\right.\n\\end{dmath*}"
76+
}
77+
],
78+
"source" : "evaluate(ch, sphe, rhsonly=True);"
79+
},
80+
{
81+
"cell_origin" : "client",
82+
"cell_type" : "input",
83+
"cells" :
84+
[
85+
{
86+
"cell_origin" : "server",
87+
"cell_type" : "latex_view",
88+
"source" : "\\begin{dmath*}{}R^{\\rho}\\,_{\\sigma \\mu \\nu} = \\partial_{\\mu}{\\Gamma^{\\rho}\\,_{\\sigma \\nu}}-\\partial_{\\nu}{\\Gamma^{\\rho}\\,_{\\sigma \\mu}}+\\Gamma^{\\rho}\\,_{\\beta \\mu} \\Gamma^{\\beta}\\,_{\\sigma \\nu}-\\Gamma^{\\rho}\\,_{\\beta \\nu} \\Gamma^{\\beta}\\,_{\\sigma \\mu}\\end{dmath*}"
89+
},
90+
{
91+
"cell_origin" : "server",
92+
"cell_type" : "latex_view",
93+
"source" : "\\begin{dmath*}{}R^{\\rho}\\,_{\\sigma \\mu \\nu} = \\square{}_{\\sigma}{}_{\\nu}{}^{\\rho}{}_{\\mu}\\left\\{\\begin{aligned}\\square{}_{\\varphi}{}_{\\varphi}{}^{\\theta}{}_{\\theta}= & \\frac{\\sin{2\\theta}}{2\\tan{\\theta}}-\\cos{2\\theta}\\\\[-.5ex]\n\\square{}_{\\theta}{}_{\\varphi}{}^{\\varphi}{}_{\\theta}= & -1\\\\[-.5ex]\n\\square{}_{\\varphi}{}_{\\theta}{}^{\\theta}{}_{\\varphi}= & - \\frac{\\sin{2\\theta}}{2\\tan{\\theta}}+\\cos{2\\theta}\\\\[-.5ex]\n\\square{}_{\\theta}{}_{\\theta}{}^{\\varphi}{}_{\\varphi}= & 1\\\\[-.5ex]\n\\end{aligned}\\right.\n\\end{dmath*}"
94+
}
95+
],
96+
"source" : "rm:= R^{\\rho}_{\\sigma\\mu\\nu} = +\\partial_{\\mu}{\\Gamma^{\\rho}_{\\sigma\\nu}}\n -\\partial_{\\nu}{\\Gamma^{\\rho}_{\\sigma\\mu}}\n +\\Gamma^{\\rho}_{\\beta\\mu} \\Gamma^{\\beta}_{\\sigma\\nu}\n -\\Gamma^{\\rho}_{\\beta\\nu} \\Gamma^{\\beta}_{\\sigma\\mu};\n\nsubstitute(rm, ch)\nevaluate(rm, sphe, rhsonly=True);"
97+
},
98+
{
99+
"cell_origin" : "client",
100+
"cell_type" : "latex",
101+
"cells" :
102+
[
103+
{
104+
"cell_origin" : "client",
105+
"cell_type" : "latex_view",
106+
"source" : "These are not in the simplest form possible yet, but this is a \\verb|sympy| simplification issue; for the time being\nwe will continue and compute the Ricci tensor and scalar."
107+
}
108+
],
109+
"hidden" : true,
110+
"source" : "These are not in the simplest form possible yet, but this is a \\verb|sympy| simplification issue; for the time being\nwe will continue and compute the Ricci tensor and scalar."
111+
},
112+
{
113+
"cell_origin" : "client",
114+
"cell_type" : "input",
115+
"cells" :
116+
[
117+
{
118+
"cell_origin" : "server",
119+
"cell_type" : "latex_view",
120+
"source" : "\\begin{dmath*}{}R_{\\sigma \\nu} = R^{\\rho}\\,_{\\sigma \\rho \\nu}\\end{dmath*}"
121+
},
122+
{
123+
"cell_origin" : "server",
124+
"cell_type" : "latex_view",
125+
"source" : "\\begin{dmath*}{}R_{\\sigma \\nu} = \\square{}_{\\sigma}{}_{\\nu}\\left\\{\\begin{aligned}\\square{}_{\\varphi}{}_{\\varphi}= & \\frac{\\sin{2\\theta}}{2\\tan{\\theta}}-\\cos{2\\theta}\\\\[-.5ex]\n\\square{}_{\\theta}{}_{\\theta}= & 1\\\\[-.5ex]\n\\end{aligned}\\right.\n\\end{dmath*}"
126+
}
127+
],
128+
"source" : "ricci:= R_{\\sigma\\nu} = R^{\\rho}_{\\sigma\\rho\\nu};\nsubstitute(ricci, rm)\nevaluate(ricci, sphe, rhsonly=True);"
129+
},
130+
{
131+
"cell_origin" : "client",
132+
"cell_type" : "input",
133+
"cells" :
134+
[
135+
{
136+
"cell_origin" : "server",
137+
"cell_type" : "latex_view",
138+
"source" : "\\begin{dmath*}{}R = R_{\\sigma \\nu} g^{\\sigma \\nu}\\end{dmath*}"
139+
},
140+
{
141+
"cell_origin" : "server",
142+
"cell_type" : "latex_view",
143+
"source" : "\\begin{dmath*}{}R = \\frac{2}{{r}^{2}}\\end{dmath*}"
144+
}
145+
],
146+
"source" : "R:= R = R_{\\sigma\\nu} g^{\\sigma\\nu};\nsubstitute(R, ricci)\nevaluate(R, sphe, rhsonly=True);"
147+
},
148+
{
149+
"cell_origin" : "client",
150+
"cell_type" : "latex",
151+
"cells" :
152+
[
153+
{
154+
"cell_origin" : "client",
155+
"cell_type" : "latex_view",
156+
"source" : "The main time computing this is spent in the rather inefficient \\verb|sympy| bridge. You can see timing information\nby querying the \\verb|server| object for the \\verb|totals| list."
157+
}
158+
],
159+
"hidden" : true,
160+
"source" : "The main time computing this is spent in the rather inefficient \\verb|sympy| bridge. You can see timing information\nby querying the \\verb|server| object for the \\verb|totals| list."
161+
},
162+
{
163+
"cell_origin" : "client",
164+
"cell_type" : "input",
165+
"cells" :
166+
[
167+
{
168+
"cell_origin" : "server",
169+
"cell_type" : "latex_view",
170+
"source" : "{}$\\big[$\\verb|cadabra::collect_terms: 20 calls, 0 steps, 0 ms|,\\discretionary{}{}{} \\verb|cadabra::complete: 1 calls, 0 steps, 87 ms|,\\discretionary{}{}{} \\verb|cadabra::evaluate: 4 calls, 0 steps, 2264 ms|,\\discretionary{}{}{} \\verb|cadabra::substitute: 3 calls, 0 steps, 3 ms|,\\discretionary{}{}{} \\verb|sympy: 49 calls, 0 steps, 2261 ms|$\\big]$"
171+
}
172+
],
173+
"source" : "server.totals();"
174+
},
175+
{
176+
"cell_origin" : "client",
177+
"cell_type" : "input",
178+
"source" : ""
179+
}
180+
],
181+
"description" : "Cadabra JSON notebook format",
182+
"version" : 1
183+
}

0 commit comments

Comments
 (0)