[GiNaC-list] comparing ex

Richard B. Kreckel kreckel at ginac.de
Mon Aug 22 22:27:23 CEST 2005


On Mon, 22 Aug 2005, Vera Louise Hauge wrote:
> It is almost what I want to do. However, in my case also the symbols
> a0 and a1 are different objects with the same text:
>
> int main(){
>
>    ex A = symbolic_matrix(1,2, "a");
>    ex B = symbolic_matrix(1,2, "a");
>    symbol x("x");
>
>    ex a, b;
>    int n=0;
>    for (const_iterator i = A.begin(); i != A.end(); ++i)  {
>      a += (*i)*pow(x,n);
>      n++;
>    }
>    n=0;
>    for (const_iterator i = B.begin(); i != B.end(); ++i)  {
>      b += (*i)*pow(x,n);
>      n++;
>    }
>    cout << "a-b=" << (a-b).expand() << endl;
>    return 0;
> }
>
> Any suggestions when the expressions "a0+a1*x" are produced this way?
>
> Maybe string comparison of the texts in the coefficient objects is a way
> to do it?

Of course, there are two symbols a0 and a1 each in your example above.
They are different objects that just happen to share the same print-name.

Why don't you assign the elements of B that are supposed to be equal to
some previously used symbols to these symbols?  Try initializing all of B
as a copy of A

   ex B = A;

and you'll see.  If for some reason there is no way you can do this, a
global symbol directory might be of help.  See this FAQ:
<http://www.ginac.de/FAQ.html#flyweightfactory>.

Regards
  -richy.
-- 
Richard B. Kreckel
<http://www.ginac.de/~kreckel/>





More information about the GiNaC-list mailing list