# [GiNaC-list] arithmetic error on concat two expressions

Kraus Philipp philipp.kraus at flashpixx.de
Fri Aug 6 13:26:29 CEST 2010

```Am 06.08.2010 um 12:48 schrieb Alexei Sheplyakov:

> Hello,
>
> On Fri, Aug 06, 2010 at 12:03:11PM +0200, Kraus Philipp wrote:
>
>> I'm new at GiNaC and I use the lib for create a gradient descent
>> algorithm (symbolic calculating).
>> I have a formula "a*x^3+b*y^4" and I subsitute the formula in the
>> error function "0.5*(t - <formula>)^2".
>
>> After the substitution GiNaC returns the expression
>> "(0.5)*(a*x^3+b*y^4-t)^2".
>>
>> I use this code:
>> GiNaC::ex m_expression;
>> GiNaC::symtab m_exprtable;
>>
>> try {
>> 	m_expression = l_parser( "a*x^3+b*y^4" );
>>        m_exprtable  = l_parser.get_syms();
>> } catch (...) {}
>>
>>
>> GiNaC::ex l_full;
>> GiNaC::symtab l_table(m_exprtable);
>>
>> l_table["formula"] = m_expression;
>> GiNaC::parser l_parser(l_table);
>>
>> try {
>> 	l_full      = l_parser( "0.5*(t - formula)^2" ); // (*)
>> } catch (...) {}
>>
>> std::cout << l_full << std::endl;
>
> The code below does the same thing and is a bit simpler:
>
> symbol a("a"), b("b"), x("x"), y("y"), t("t");
> ex e = a*pow(x, 3) + b*pow(y, 4);
> ex d =  pow(t - e, 2)/2;
>
> Also, "0.5" is inexact number, I guess this is not what you want for
> a symbolic calculation.

That's nice, but not my thinking, because the expressions are
std::string data, that can be set by a user, so I can't use your
example.
The Problem is, that my formula a*x^3 + b*y^4 (*) not correct
substitute into the other formula:

If I add (*) into (0.5)*(t - (*))^2 it musst be (0.5)*( t - ax^3 -
by^4)^4, GiNaC creates (a*x^3+b*y^4-t)
ax^3 and by^4 must be a negative sign!

```