# [GiNaC-list] indexed

Sheplyakov Alexei varg at theor.jinr.ru
Thu Dec 9 11:16:10 CET 2004

```Hello!

On Tue, Dec 07, 2004 at 01:10:09AM +0100, Andrea Bedini wrote:
> Hi,
> I'm new to ginac, I'd like to know if is it possible to unroll indexed
> objects. As example, I would like to do something like this
>
>  a_i a~i = (a_1)^2 + (a_2)^2 + (a_3)^2 + ...
>
> or just substituting a_i values in a_i * b~i
>
> a_i = =(1,0,0)
> a_i b~i -> b~1

You might try this:

#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;

int main(int argc, char** argv)
{
idx i(symbol("i"), 4);
idx j(symbol("j"), 4);
symbol a_1("a_1"), a_2("a_2"), a_3("a_3"), a_4("a_4");
symbol b_1("b_1"), b_2("b_2"), b_3("b_3"), b_4("b_4");

matrix a_mat(4, 1, lst(a_1, a_2, a_3, a_4));
matrix b_mat(4, 1, lst(b_1, b_2, b_3, b_4));
matrix uni_mat(4, 1, lst(1, 0, 0, 0));

ex res = indexed(a_mat, i)*indexed(b_mat, j)*delta_tensor(i,j);
cout << res << endl << " ==> " << res.simplify_indexed() << endl;
// This will print
// [[a_1],[a_2],[a_3],[a_4]].i*[[b_1],[b_2],[b_3],[b_4]].j*delta.j.i
// ==> a_4*b_4+a_3*b_3+a_1*b_1+a_2*b_2

cout << res << endl << " ==> " <<
res.subs(indexed(b_mat, j)==indexed(uni_mat, j)).simplify_indexed() << endl;
// [[a_1],[a_2],[a_3],[a_4]].i*[[b_1],[b_2],[b_3],[b_4]].j*delta.j.i
// ==> a_1

return 0;
}

Best regards,
Alexei.

```