_expand_hint() should just be a function called recursively_call (or
something like that). All it does take a method (it is currently
implemented to use methods called _eval_expand_hint(), but that is
easy to change), and recursively call it on the tree. If the method
doesn't exist, it doesn't call it. If it does exist, it calls it, and
if the result is different, it replaces the result with that in the
tree. So for example, with expand_trig(x**2 + cos(x + y)), it skips
x**2 (because nothing in that has the _eval_expand_trig method, but
for cos, it does expand it.

The nice thing about this method is that things like deep handling are
abstracted out, and you can easily implement a local cache with it.

Now that I think about it, this won't work as-is with diff(), but
rather only with simplification functions, because you can't just go
in and replace each expression in the tree with its derivative.

Aaron Meurer


On Wed, Aug 7, 2013 at 1:38 PM, Gilbert Gede <gilbertg...@gmail.com> wrote:
> Yeah, in the PR Aaron pointed out there is some code profiling I pastebin'd
> in the comments.
> I haven't gotten around to implementing the expand functionality yet - I
> still had some questions for you Aaron. When you sugessted we promote
> _expand_hint() to a user-level function, what did you mean by that (other
> than removing the leading _)? Would there be any other changes to that
> method?
>
> Also, is there any problem with putting @cacheit before many of the
> Derviative methods, including __new__()? I did so, and it made the
> linearization code run in 40% of the original time, and the tests seem to
> still pass... Is there a reason cacheing isn't used more frequently?
>
> -Gilbert
>
>
> On Tue, Aug 6, 2013 at 8:59 PM, Aaron Meurer <asmeu...@gmail.com> wrote:
>>
>> I suggested an idea of how to make diff, and in fact a very large
>> class of functions, including expand(), much faster at
>> https://github.com/sympy/sympy/pull/2182#issuecomment-19654806.
>> Basically, you can play around with using cse() or caching internal
>> operations, since functions like diff() are pure in the functional
>> programming sense (the same input always produces the same output with
>> no side-effects), so the implementation can choose to compute
>> different sub-operations only once. We can also be smarter than any
>> functional language would ever be by using mathematical facts that we
>> know as well.
>>
>> Aaron Meurer
>>
>> On Tue, Aug 6, 2013 at 9:09 PM, Dale Lukas Peterson
>> <hazelnu...@gmail.com> wrote:
>> >> https://gist.github.com/certik/6170161
>> >>
>> >> It has over 80,000 operations. Now, what exact operation are we doing
>> >> with this?
>> >> I've been trying to understand the code of KM.linearize(), but it's
>> >> not clear to me.
>> >
>> > linearize() was our initial implementation of the work we began in
>> > 2011 during Gilbert's GSoC and has since been refined (the derivation,
>> > that is) in a paper we just submitted our first batch of revisions to
>> > the Journal of Multibody System Dynamics [0]. This method (and the
>> > whole class, really), needs an overhaul of it's implementation.
>> > Gilbert and I have talked about doing it but haven't, yet. Since the
>> > expressions tend to be quite long, I think we might need to be think
>> > carefully if we are accidentally creating expensive copies of things,
>> > there is a good article on this on the Theano website [1]. This might
>> > not be the problem at all, it is just something I read recently that
>> > seemed relevant.  I haven't done any profiling myself.
>> >
>> >> I assume you need to take a derivative with respect to "t"? What else?
>> >
>> > Actually not 't'. If I recall correctly, we are taking derivatives
>> > with respect to 1) coordinates, 2) coordinate time derivatives, 3)
>> > generalized speeds, 4) generalized speed time derivatives.
>> >
>> >> Are we multiplying this with some other expression of similar size?
>> >> I would like to create a real life benchmark out of this, of your real
>> >> expression + an operation that needs to be done and is slow. So that I
>> >> can try for example Mathematica or some other CAS to get an idea about
>> >> how fast one can get. As such, I want just some simple calculus
>> >> operation, like second derivative or something else that is
>> >> representative.
>> >
>> > There are some Matrix products that occur, and their might be a
>> > dubious use of inv() or solve() that is causing problems. Beside those
>> > two, the only other thing that I can think might be the bottleneck is
>> > .subs(), I don't think we use much else in linearize().
>> >
>> > [0] -- https://github.com/hazelnusse/SympyMechanicsPaper
>> > [1] --
>> > http://deeplearning.net/software/theano/tutorial/python-memory-management.html#python-memory-management
>> >
>> >>
>> >> Thanks a lot,
>> >> Ondrej
>> >>
>> >
>> >> P.S. I recently revived a C++ core (https://github.com/certik/csympy),
>> >> and I want to benchmark it on something real life, as opposed to
>> >> artificial benchmarks.
>> >
>> > Another piece of code you can try is:
>> >
>> > https://github.com/hazelnusse/bicycle.model/blob/master/derivation/derivation.py
>> >
>> > If you do use it in the benchmark, ignore anything after line 555,
>> > that would be testing some of my own wrapping of sympy's ccode
>> > functionality. The resulting generated C++ file (class implementation
>> > file) is:
>> >
>> >
>> > https://github.com/hazelnusse/bicycle.model/blob/master/src/bicycle_generated.cc
>> >
>> > which, as you can see, is a big mess, and that is with the equations
>> > "modularized" in to sub-pieces, and using the cse() functionality to
>> > simplify the output. If you try to compute expressions with everything
>> > together, it gets worse than that, and there are serious bottlenecks
>> > in cse().  If you run the script as is, you'll see those show up after
>> > line 555.
>> >
>> > Thanks for looking into this! I'm much more comfortable with C++ than
>> > I was 4 years ago, so maybe I can help out with csympy somehow.
>> >
>> > Luke
>> >
>> >
>> >
>> >
>> >
>> >
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups "sympy" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an email to sympy+unsubscr...@googlegroups.com.
>> >> To post to this group, send email to sympy@googlegroups.com.
>> >> Visit this group at http://groups.google.com/group/sympy.
>> >> For more options, visit https://groups.google.com/groups/opt_out.
>> >>
>> >>
>> >
>> >
>> >
>> > --
>> > "People call me a perfectionist, but I'm not. I'm a rightist. I do
>> > something until it's right, and then I move on to the next thing."
>> > -- James Cameron
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an email to sympy+unsubscr...@googlegroups.com.
>> > To post to this group, send email to sympy@googlegroups.com.
>> > Visit this group at http://groups.google.com/group/sympy.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sympy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to sympy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sympy@googlegroups.com.
>> Visit this group at http://groups.google.com/group/sympy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+unsubscr...@googlegroups.com.
> To post to this group, send email to sympy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to