symbolic icon indicating copy to clipboard operation
symbolic copied to clipboard

Wrong result

Open newmen opened this issue 13 years ago • 5 comments

Hello,

When I tried to use your beauty gem, I wrote this code: x = var :name => 'x' y = var :name => 'y' puts x - (y + x) / 5

And I got the next result: x-0.2*y+x

but its wrong :(

Best wishes, Gleb

newmen avatar Feb 25 '12 20:02 newmen

Indeed, I can confirm your findings. I will investigate it later, hope to get back at you soon.

Thanks for report.

brainopia avatar Feb 26 '12 01:02 brainopia

The bug was only in the representation (#to_s), so the internal expression is correct, although not totally optimized. You can try the latest symbolic with Bundler, fetching directly from the master, or wait the next release.

eregon avatar Feb 26 '12 12:02 eregon

integration_spec.rb says that failures still occur

Failures:

  1) Symbolic optimization: x-0.2*(y+x)
     Failure/Error: non_optimized.should == optimized
       expected: Symbolic(0.8*x-0.2*y)
            got: Symbolic(x-0.2*(y+x)) (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -Symbolic(0.8*x-0.2*y)
       +Symbolic(x-0.2*(y+x))
     # ./integration_spec.rb:96:in `block (4 levels) in <top (required)>'

  2) Symbolic to_s: -x**y+7
     Failure/Error: expr.to_s.should == str
       expected: "-x**y+7"
            got: "-(x**y)+7" (using ==)
     # ./integration_spec.rb:166:in `block (4 levels) in <top (required)>'

Finished in 0.01495 seconds
81 examples, 2 failures

Thus, I'm still not getting the correct result when using the expression x-(y + x)/5, i.e. is not 0.8*x-0.2*y.

newmen avatar Feb 26 '12 13:02 newmen

Indeed, this expression is "not totally optimized", but both expressions are mathematically equal. So we need to work on this, but at least it's coherent now (the representation).

Do you need such simplification?

eregon avatar Feb 26 '12 23:02 eregon

Your gem is perfect, because ultimately you can get an expression that contains only one time mention of a particular variable. In the case of incomplete simplify the expression - it does not. At the same time, the expression x-0.2*(y+x) can be simplified by symbolic to the correct expression. In this case you must call symbolic again, but its better not to do so. :)

newmen avatar Feb 27 '12 06:02 newmen