Skip to content

Commit a517872

Browse files
committed
Fixing map_sympy
1 parent b3acf29 commit a517872

File tree

7 files changed

+107
-51
lines changed

7 files changed

+107
-51
lines changed

core/algorithms/evaluate.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Ex::iterator evaluate::handle_sum(iterator it)
159159
auto sib2=sib1;
160160
++sib2;
161161
while(sib2!=tr.end(it)) {
162+
std::cerr << "merging components " << Ex(sib1) << " and " << Ex(sib2) << std::endl;
162163
merge_components(sib1, sib2);
163164
sib2=tr.erase(sib2);
164165
}

core/algorithms/map_sympy.cc

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,80 @@ map_sympy::map_sympy(const Kernel& k, Ex& tr, const std::string& head)
1313

1414
bool map_sympy::can_apply(iterator st)
1515
{
16+
left.clear();
17+
index_factors.clear();
1618
index_map_t ind_free, ind_dummy;
1719
classify_indices(st, ind_free, ind_dummy);
20+
21+
bool still_ok=true;
22+
23+
// Determine if any of the free indices are harmless (Coordinates or Symbols).
1824
for(auto& ind: ind_free) {
1925
const Coordinate *cdn=kernel.properties.get_composite<Coordinate>(ind.second, true);
2026
const Symbol *smb=kernel.properties.get_composite<Symbol>(ind.second, true);
21-
if(cdn==0 && smb==0)
22-
return false;
27+
if(cdn==0 && smb==0) {
28+
still_ok=false;
29+
break;
30+
}
31+
}
32+
33+
if(still_ok && ind_dummy.size()==0) return true;
34+
35+
// In a product, it is still possible that there is a sub-product which
36+
// contains no indices.
37+
if(*st->name=="\\prod") {
38+
for(auto& ind: ind_free) {
39+
const Coordinate *cdn=kernel.properties.get_composite<Coordinate>(ind.second, true);
40+
const Symbol *smb=kernel.properties.get_composite<Symbol>(ind.second, true);
41+
if(cdn==0 && smb==0) {
42+
auto fac=tr.parent(ind.second);
43+
while(tr.parent(fac)!=iterator(st))
44+
fac=tr.parent(fac);
45+
index_factors.insert(fac);
46+
}
47+
}
48+
for(auto& ind: ind_dummy) {
49+
const Coordinate *cdn=kernel.properties.get_composite<Coordinate>(ind.second, true);
50+
const Symbol *smb=kernel.properties.get_composite<Symbol>(ind.second, true);
51+
if(cdn==0 && smb==0) {
52+
auto fac=tr.parent(ind.second);
53+
while(tr.parent(fac)!=iterator(st))
54+
fac=tr.parent(fac);
55+
index_factors.insert(fac);
56+
}
57+
}
58+
sibling_iterator sib=tr.begin(st);
59+
while(sib!=tr.end(st)) {
60+
if(index_factors.find(iterator(sib))==index_factors.end())
61+
left.push_back(sib);
62+
++sib;
63+
}
64+
return left.size()>0;
2365
}
24-
return (ind_dummy.size()==0);
66+
67+
return false;
2568
}
2669

2770
Algorithm::result_t map_sympy::apply(iterator& it)
2871
{
2972
std::vector<std::string> wrap;
3073
wrap.push_back(head_);
31-
sympy::apply(kernel, tr, it, wrap, "", "");
32-
it.skip_children();
33-
return result_t::l_applied;
74+
75+
if(left.size()>0) {
76+
std::cerr << "Sub-product with " << left.size() << " non-index carrying factors" << std::endl;
77+
Ex prod("\\prod");
78+
for(auto& fac: left)
79+
prod.append_child(fac);
80+
auto top=prod.begin();
81+
sympy::apply(kernel, prod, top, wrap, "", "");
82+
for(auto& kl: index_factors)
83+
tr.erase(kl);
84+
85+
return result_t::l_no_action;
86+
}
87+
else {
88+
sympy::apply(kernel, tr, it, wrap, "", "");
89+
it.skip_children();
90+
return result_t::l_applied;
91+
}
3492
}

core/algorithms/map_sympy.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class map_sympy : public Algorithm {
1919
virtual result_t apply(iterator&);
2020

2121
private:
22-
std::string head_;
22+
std::string head_;
23+
std::vector<Ex::iterator> left;
24+
std::set<Ex::iterator> index_factors;
2325
};
2426

2527
}

doc/cadabra2_hep.tex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ \subsection{Tensors, indices and symmetries}
333333
\botrule
334334
The first line indicates that the tensor has the symmetries of the
335335
\raisebox{2ex}{$\ftableau{ac,bd}$} tableau (the numbers in the \verb|indices| argument
336-
refer to the index positions). The \verb|canonicalise| command writes
336+
refer to the index positions). The \verb|canonicalise| algorithm writes
337337
the input in canonical form with respect to mono-term symmetries
338338
(anti-symmetry in the two index pairs and symmetry under pair
339339
exchange). The Ricci symmetry is also encoded in the Young tableau and
@@ -421,10 +421,10 @@ \subsection{Bianchi, Ricci and Schouten identities}
421421
have seen earlier.} Here it effectively associates the $\nabla$
422422
operator to the Riemann tensor. We can see this in action, for
423423
instance, by verifying that the Bianchi identity indeed holds,
424-
\footnote{The ``2'' following the exclamation mark on
425-
{\tt @young\_project\_tensor!2(\%)} indicates that this command should
426-
only be applied at ``level 2'' of the expression, i.e.~at the level
427-
of the terms of the sum.}
424+
\footnote{The {\tt depth=1} parameter of the
425+
{\tt young\_project\_tensor} algorithm indicates that this command
426+
should only be applied at ``level 1'' of the expression, i.e.~at the
427+
level of the terms of the sum.}
428428
\end{cdbcom}
429429
\begin{cdbcont}
430430
ex:= \nabla_{m}{R_{p q r s}} + \nabla_{p}{R_{q m r s}} + \nabla_{q}{R_{m p r s}}:

tests/components.cdb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,19 @@ test23()
452452
# ex:= \partial_{a}{ A_{b} };
453453
# rl:= \partial_{r}{ A_{t} } = 1;
454454
# evaluate(ex, rl);
455+
456+
{\theta, \varphi}::Coordinate;
457+
{\alpha, \beta, \gamma, \delta, \rho, \sigma, \mu, \nu, \lambda}::Indices(values={\varphi, \theta}, position=fixed);
458+
\partial{#}::PartialDerivative;
459+
g_{\alpha\beta}::Metric.
460+
g^{\alpha\beta}::InverseMetric.
461+
462+
sphe:={ g_{\theta\theta} = r**2,
463+
g_{\varphi\varphi} = r**2 \sin(\theta)**2 }.
464+
complete(sphe, $g^{\alpha\beta}$);
465+
466+
ch:= \Gamma^{\alpha}_{\mu\nu} = 1/2 g^{\alpha\beta} ( \partial_{\nu}{g_{\beta\mu}}
467+
+\partial_{\mu}{g_{\beta\nu}}
468+
-\partial_{\beta}{g_{\mu\nu}} );
469+
470+
evaluate(ch, sphe, rhsonly=True);

tests/display.cnb

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
{
1010
"cell_origin" : "server",
1111
"cell_type" : "latex_view",
12-
"source" : "\\begin{dmath*}{}\\text{Attached property PartialDerivative to~}\\partial{\\#}.\\end{dmath*}"
12+
"source" : "\\begin{dmath*}{}\\text{Attached property Coordinate to~}r.\\end{dmath*}"
1313
},
1414
{
1515
"cell_origin" : "server",
1616
"cell_type" : "latex_view",
17-
"source" : "\\begin{dmath*}{}\\text{Attached property Depends to~}A.\\end{dmath*}"
17+
"source" : "\\begin{dmath*}{}\\text{Attached property PartialDerivative to~}\\partial{\\#}.\\end{dmath*}"
1818
},
1919
{
2020
"cell_origin" : "server",
2121
"cell_type" : "latex_view",
22-
"source" : "\\begin{dmath*}{}\\text{Attached property Coordinate to~}r.\\end{dmath*}"
22+
"source" : "\\begin{dmath*}{}\\text{Attached property Depends to~}A.\\end{dmath*}"
2323
}
2424
],
25-
"source" : "\\partial{#}::PartialDerivative;\nA::Depends(r);\nr::Coordinate;"
25+
"source" : "r::Coordinate;\n\\partial{#}::PartialDerivative;\nA::Depends(r);"
2626
},
2727
{
2828
"cell_origin" : "client",
@@ -48,20 +48,7 @@
4848
"source" : "\\begin{dmath*}{}4{r}^{2}+\\frac{1}{2}\\left(-\\cos{4A}+1\\right) {\\left(\\partial_{r}{A}\\right)}^{2}+4{A}^{2} {\\left(\\partial_{r}{A}\\right)}^{2}\\end{dmath*}"
4949
}
5050
],
51-
"source" : "scas(ex,\"simplify\");"
52-
},
53-
{
54-
"cell_origin" : "client",
55-
"cell_type" : "input",
56-
"cells" :
57-
[
58-
{
59-
"cell_origin" : "server",
60-
"cell_type" : "output",
61-
"source" : "\\begin{verbatim}{\\sum} (0x7f26d4029738)\n 1:{\\pow} (0x7f26d401f8d8)\n 2: {\\partial} (0x7f26d4021e78)\n 3: _{r} (0x7f26d4033008)\n 4: {\\pow} (0x7f26d4033058)\n 5: {\\sin} (0x7f26d40341c8)\n 6: {A} (0x7f26d400b148)\n 7: {1} 2 (0x7f26d4034218)\n 8: {1} 2 (0x7f26d4044828)\n 9:{\\pow} (0x7f26d4044878)\n 10: {\\partial} (0x7f26d4027ca8)\n 11: _{r} (0x7f26d4027cf8)\n 12: {\\pow} (0x7f26d4031228)\n 13: {A} (0x7f26d4031278)\n 14: {1} 2 (0x7f26d40344b8)\n 15: {1} 2 (0x7f26d4034508)\n 16:{\\pow} (0x7f26d4027e88)\n 17: {\\partial} (0x7f26d4027ed8)\n 18: _{r} (0x7f26d4046c18)\n 19: {\\pow} (0x7f26d4046c68)\n 20: {r} (0x7f26d4036bf8)\n 21: {1} 2 (0x7f26d4036c48)\n 22: {1} 2 (0x7f26d4036c98)\n\n\\end{verbatim}"
62-
}
63-
],
64-
"source" : "print(tree(ex))"
51+
"source" : "map_sympy(ex,\"simplify\");"
6552
},
6653
{
6754
"cell_origin" : "client",
@@ -87,25 +74,7 @@
8774
"source" : "\\begin{dmath*}{}2r\\end{dmath*}"
8875
}
8976
],
90-
"source" : "ex3=scas(ex2, \"simplify\");"
91-
},
92-
{
93-
"cell_origin" : "client",
94-
"cell_type" : "input",
95-
"cells" :
96-
[
97-
{
98-
"cell_origin" : "server",
99-
"cell_type" : "latex_view",
100-
"source" : "\\begin{dmath*}{}\\partial_{r}{{r}^{2}}\\end{dmath*}"
101-
},
102-
{
103-
"cell_origin" : "server",
104-
"cell_type" : "latex_view",
105-
"source" : "\\begin{dmath*}{}2r\\end{dmath*}"
106-
}
107-
],
108-
"source" : "ex2;\nex3;"
77+
"source" : "ex3=map_sympy(ex2, \"simplify\");"
10978
},
11079
{
11180
"cell_origin" : "client",
@@ -115,7 +84,7 @@
11584
{
11685
"cell_origin" : "server",
11786
"cell_type" : "latex_view",
118-
"source" : "\\begin{dmath*}{}4 r^{2} + 4 A^{2}{\\left (r \\right )} \\frac{d}{d r} A{\\left (r \\right )}^{2} + 4 \\sin^{2}{\\left (A{\\left (r \\right )} \\right )} \\cos^{2}{\\left (A{\\left (r \\right )} \\right )} \\frac{d}{d r} A{\\left (r \\right )}^{2}\\end{dmath*}"
87+
"source" : "\\begin{dmath*}{}4 r^{2} + \\left(- \\frac{1}{2} \\cos{\\left (4 A{\\left (r \\right )} \\right )} + \\frac{1}{2}\\right) \\frac{d}{d r} A{\\left (r \\right )}^{2} + 4 A^{2}{\\left (r \\right )} \\frac{d}{d r} A{\\left (r \\right )}^{2}\\end{dmath*}"
11988
},
12089
{
12190
"cell_origin" : "server",
@@ -133,7 +102,7 @@
133102
{
134103
"cell_origin" : "server",
135104
"cell_type" : "latex_view",
136-
"source" : "\\begin{dmath*}{}{\\left(\\partial_{r}{{\\left(\\sin{A}\\right)}^{2}}\\right)}^{2}+{\\left(\\partial_{r}{{A}^{2}}\\right)}^{2}+{\\left(\\partial_{r}{{r}^{2}}\\right)}^{2}\\end{dmath*}"
105+
"source" : "\\begin{dmath*}{}4{r}^{2}+\\frac{1}{2}\\left(-\\cos{4A}+1\\right) {\\left(\\partial_{r}{A}\\right)}^{2}+4{A}^{2} {\\left(\\partial_{r}{A}\\right)}^{2}\\end{dmath*}"
137106
}
138107
],
139108
"source" : "ex;"

tests/paper.cdb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ print(timeit.timeit(test08,number=1))
129129

130130
#-------
131131

132+
def test0b():
133+
__cdbkernel__=create_scope()
134+
\ftableau{#}::FilledTableau(dimension=10);
135+
ex:=\ftableau{0,0}{1,1} \ftableau{a,a}{b,b};
136+
lr_tensor(_);
137+
138+
print(timeit.timeit(test08b, number=1))
139+
140+
#-------
141+
132142
def test09():
133143
__cdbkernel__=create_scope()
134144

0 commit comments

Comments
 (0)