[GiNaC-list] parser and subs problem

Alexei Sheplyakov alexei.sheplyakov at gmail.com
Tue Jun 29 21:54:57 CEST 2010


On Tue, Jun 29, 2010 at 05:55:35PM +0200, Felipe Bordeu wrote:

> I'm trying to use the ginac parser, but doesn't work as expected.
> Page 81 of GiNaC 1.5.7 (an open framework ...) tutorial (29 march 2010)
> " It’s also possible to map input (sub)strings to arbitrary expressions:
> GiNaC::symbol x, y;
> GiNaC::symtab table;
> table["x"] = x+log(y)+1;
> GiNaC::parser reader(table);
> GiNaC::ex e = reader("5*x^3 - x^2");
> "
> but I get the error
> terminate called after throwing an instance of 'std::invalid_argument'
> what(): find_or_insert_symbol: name "x" does not correspond to a symbol
> Abandon
> function find_or_insert_symbol in file
> ginac/parser/parse_context.cpp:49 can handle only symbols not
> expressions.

Thanks for reporting this. I'll post a fix tomorrow.
> So I tried to use only symbols and then do some subs to get the
> correct expression without success.
> GiNaC::symbol x, y;
> GiNaC::ex expr1 = x*y;
> GiNaC::ex mat1 = GiNaC::symbolic_matrix(2, 2, "K");
> GiNaC::ex mat2 = GiNaC::symbolic_matrix(2, 2, "T");
> GiNaC::ex expr2 = expr1.subs(GiNaC::lst(x==mat1,y==mat2));
> cout << expr2 << endl;
> cout << expr2.evalm() << endl;

You are trying to convert a commutative product into a non-commutative one.
This is a bad idea (and it's not supported). GiNaC is free to permute terms
of the commutative product. Even if substitution actually worked, you'd get
an unpredictable result, that is, either K*T, or T*K (and it might be 
different within different runs).

Also, GiNaC parser cannot directly handle non-commutative products.
The trick I use is to introduce some dummy function which represents
a non-commutative product, write corresponding reader_func, and create
a custom prototype table.

Best regards,

More information about the GiNaC-list mailing list