On Wed, Nov 4, 2020 at 1:57 PM Jason Moore <
moore...@gmail.com> wrote:
>
> I think you meant to do this:
>
> expr = ....
> com_sub_exprs, simplified_expr = cse(expr)
> new_expr = do_operations_on(simplified_expr)
> com_sub_exprs, simpflied_new_expr = cse(new_expr.subs(dict(com_sub_exprs))
>
> If so, seems like you have to cse the same set of sub expressions twice.
Yes, except com_sub_exprs has to be reversed first. I'm still not sure
about your duplicate work comment. The same subexpresisons would be
replaced by the subs, so the second cse would only see the CSE'd
replacement symbol.
But I think there are some technicalities that make this more
complicated than this naive version:
- cse does some preprocessing that makes subexpressions easier to
find. It may not be possible to reverse substitute successfully unless
these are done. Actually there may be cases where is still doesn't
work, because the subs() behavior for replacing expressions isn't
guaranteed.
- The subs dict has to be reversed.
- If it did work, merging the two cses would require renumbering the symbols.
So I think it is worth adding this directly to cse. It's possible that
this can be done at a more internal point in the cse algorithm to make
it more robust.
Aaron Meurer
> To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAP7f1Ah4dWVcQyWuscnXkb0H6MC51MReGR-OBpPmUODS_AdDPA%40mail.gmail.com.