<div dir="ltr">I am new to ginac, and I want to simplify some expected values of indexed expressions. As a toy example, I wanted to simplify<br>E[\sum_{i,j} A.i.j ^ 2]<br>where it is known that E[A.i.j * A.i.j] = 1.<br>The answer should be N + E[ \sum{i,j} A.i.j \sum_{k != i,l != j} A.k.l].<br>
<br>I tried to use the following program but it doesnt work. Any help would be greatly appreciated as I need to implement similar but more involved expressions. Thanks!<br><br><span style="color: rgb(204, 0, 0);">#include <iostream></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">#include <ginac/ginac.h></span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">using namespace std;</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">using namespace GiNaC;</span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> int main()</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> {</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> symbol N("N");</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> symbol i_sym("i"), j_sym("j");</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> idx i(i_sym, N), j(j_sym, N);</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> symbol A("A");</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> ex e = indexed(A,i,j) * indexed(A,i,j);</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> cout << "e = "<< e<< endl;</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> cout << "Free indices = " << exprseq(e.get_free_indices()) << "\n";</span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // I want to substitute A.i.j * A.i.j = 1, for any i,j</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // The following gives a run time error</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> //ex e3 = subs(e,indexed(A,wild(),wild()) * indexed(A,wild(),wild()) == 1);</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> // The following doesnt substitute/simplify anything</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> idx i0(wild(),N), i1(wild(),N);</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> ex e3 = subs(e,indexed(A,i0,i1) * indexed(A,i0,i1) == 1);</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> cout << "e3 = " << e3 << endl;</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> cout << "Free indices = " << exprseq(e3.get_free_indices()) << "\n";</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">}</span><br style="color: rgb(204, 0, 0);">
<br></div>