# [GiNaC-list] Vector versus indexed (Was: extending symbol)

Krishna Mohan Gundu gkmohan at gmail.com
Sun Oct 26 05:59:34 CET 2008

```Hello All,

I am attaching the code for performing vector algebra using GiNaC. It
supports inner products, cross products and components of vectors.
Currently cross product between two 2D vectors returns a scalar and
between two 3D vectors returns a vector. I hope that someone will find
it useful. Here are a few examples

// define vectors
vector v1("v1"), v2("v2"), v3("v3");

// vector components
cout << v1[2] << endl; // v1[2]

// various products
cout << inner_prod(v1, v2) << endl; // (v1 . v2)
cout << cross_prod(v1, v2) << endl; // (v1 x v2)
cout << inner_prod(v1, cross_prod(v2, v3)) << endl; // (v1 . (v2 x v3))
cout << cross_prod(v1, cross_prod(v2, v3)) << endl; // (v1 x (v2 x v3))

// identities (nothing fancy yet)
cout << cross_prod(v1, v1) << endl; // 0

// expand
ex e1 = inner_prod(v1, v2 + v3);
cout << e1 << endl; // (v1 . (v2 + v3))
cout << expand(e1) << endl; // (v1 . v2) + (v1 . v3);

// substitution (expand must be used before substitution)
lst l1(1,2,3), l2(1,0,1), l3(1,1,0);
cout << v1[2].subs(v1 == l1) << endl; // 3
cout << expand(e1).subs(lst(v1 == l1, v2 == l2, v3 == l3)) << endl; // 7

cheers,
Krishna.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ginac_vector_algebra.cc
Type: text/x-c++src
Size: 14832 bytes
Desc: not available
Url : http://www.cebix.net/pipermail/ginac-list/attachments/20081026/5a6ed0f5/attachment.cc
```