arithmetic error on concat two expressions
Hi, 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; I using the version 1.5.8 (on 1.5.7 I had got a compiler error). Is this the correct way for concating two symbolic expression? Do I have forgot anything? Thanks Phil
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. Best regards, Alexei
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!
Hi, On Fri, Aug 06, 2010 at 01:26:29PM +0200, Kraus Philipp wrote:
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.
Correct. But you don't need to parse "(t - formula)^2/2", instead you can construct the expression directly: symbol t("t"); ex l_full = pow(t - m_expression, 2)/2;
The Problem is, that my formula a*x^3 + b*y^4 (*) not correct substitute into the other formula:
What makes you think so? As far as I understand (t - a*x^3 - b*y^4)^2 is the same as (a*x^3 + b*y^4 - t)^2 Best regards, Alexei
Am 06.08.2010 um 17:53 schrieb Alexei Sheplyakov:
Hi,
On Fri, Aug 06, 2010 at 01:26:29PM +0200, Kraus Philipp wrote:
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.
Correct. But you don't need to parse "(t - formula)^2/2", instead you can construct the expression directly:
I can't construct it directly, because the error function can be changed, so in my template class it must be a little bit dynamic.
The Problem is, that my formula a*x^3 + b*y^4 (*) not correct substitute into the other formula:
What makes you think so? As far as I understand (t - a*x^3 - b*y^4)^2 is the same as (a*x^3 + b*y^4 - t)^2
I'm now a little bit confused, cause I'm working for a long time with this problem and I don't see my "thinking error". If I have an expression eg exp(x), so (t-exp(x))^2 is not the same (exp(x)-t)^2. Sorry I don't see it Thanks Phil
(-1)^2 == 1^2 therefore (-expr)^2 == expr^2 therefore (t-exp(x))^2 == (-(t-exp(x))^2 == (exp(x) - t)^2 Do you see it now? I'm now a little bit confused, cause I'm working for a long time with this problem and I don't see my "thinking error". If I have an expression eg exp(x), so (t-exp(x))^2 is not the same (exp(x)-t)^2. Sorry I don't see it Thanks Phil _______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
Thanks, I see it, but if I create a derivatrion "diff" the sign is important: If I run formula.diff(a, 1): [0.5*(t-ax^3)^2]' => -(t-a*x^3)*x^3 [0.5*(ax^3-t)^2]' => (a*x^3-t)*x^3 On my code I parse the formula (0.5)*(t -(formula)^2 and substitute formula with another formula. I do not understand why the minus sign is not interpreted correctly. I think GiNaC does create a pattern matching and not evaluate the correct expression. In my opinion the first expression is the correct one, GiNaC creates the second. Why? How can I tell GiNaC that it interpret the expression arithmetically correct? Thanks Phil Am 06.08.2010 um 22:41 schrieb Doug:
(-1)^2 == 1^2 therefore (-expr)^2 == expr^2 therefore (t-exp(x))^2 == (-(t-exp(x))^2 == (exp(x) - t)^2
Do you see it now?
I'm now a little bit confused, cause I'm working for a long time with this problem and I don't see my "thinking error". If I have an expression eg exp(x), so (t-exp(x))^2 is not the same (exp(x)-t)^2. Sorry I don't see it
Thanks
Phil _______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
participants (4)
-
Alexei Sheplyakov
-
Doug
-
Kraus Philipp
-
Stephen Montgomery-Smith