Atoms of an expression
Christian Bauer
Christian.Bauer at Uni-Mainz.DE
Tue Jun 10 13:17:59 CEST 2003
Hi!
On Mon, Jun 09, 2003 at 04:17:54PM +0100, Dr. Vassilis S. Vassiliadis wrote:
> How is it possible to get GiNaC to yield all the
> atoms occuring in an expression? (i.e. return a
> list of all the leaves of the expression tree).
This should be the easiest way:
void collect_atoms(const ex & e, lst & atoms)
{
if (e.nops() == 0)
atoms.append(e);
else
for (size_t i = 0; i < e.nops(); ++i)
collect_atoms(e.op(i), atoms);
}
lst atoms_of(const ex & e)
{
lst atoms;
collect_atoms(e, atoms);
atoms.sort();
atoms.unique();
return atoms;
}
symbol x("x"), y("y");
ex e = 3 * y * pow(x, 2) + sin(x + Pi/2);
cout << e << endl;
// 3*y*x^2+sin(1/2*Pi+x)
cout << atoms_of(e) << endl;
// {3,2,y,Pi,1/2,x}
Bye,
Christian
--
/ Physics is an algorithm
\/ http://www.uni-mainz.de/~bauec002/
More information about the GiNaC-list
mailing list