#7742: add a compose function to sage
------------------------------------------------+---------------------------
Reporter: was | Owner: AlexGhitza
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-4.6.1
Component: misc | Keywords:
Author: Christopher Olah, Felix Lawrence | Upstream: N/A
Reviewer: Paul Zimmermann | Merged:
Work_issues: |
------------------------------------------------+---------------------------
Changes (by flawrence):
* status: needs_work => needs_review
* type: defect => enhancement
* component: basic arithmetic => misc
Comment:
Replying to [comment:27 jrp]:
> With normal python functions, I don't think there is any gain from
powers of two.
>
> With the self_compose from adds-compose-etc.patch:
(snip)
> Since self_compose is implemented via {{{lambda x: f(g(x))}}}, we have
to eventually pass the argument through {{{f}}} the correct number of
times.
Yes, `nest` is always faster than `self_compose` if you actually want to
evaluate the function. However the functions do different things: `nest`
isn't really a compose function, since it returns f^n(x) not f^n.
`self_compose` is more useful than `nest` if you want to forget about f
and only want to apply `f^n`. In hindsight, `self_compose` should
actually look like this, which I agree would be faster to construct and to
evaluate than the powers of two method:
{{{
def self_compose(f,n):
return lambda x: nest(f,n,x)
}}}
> Let's move the powers of two, and the symbolics handling, into a method
of symbolic functions. Then the compose tools for python functions can
have fast, dirt-simple implementations.
I agree that we should scrap the powers of two, and not go out of our way
to support symbolics in this ticket. I'm uploading a patch that makes the
change detailed above. I'm not going to have any more time to work on
this for at least a month, so someone else will have to finish the ticket
if further changes are required.
The new patch works for n = 0, but still does not check whether n is
symbolic. I don't think this is the worst thing in the world: the
documentation does state that n should be a nonnegative integer, and if it
is symbolic then the error raised is appropriate IMO:
{{{
sage: _ = var('x y')
sage: nest(sin,x,y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
/Applications/sage/devel/sage-dev/<ipython console> in <module>()
/Applications/sage/local/lib/python2.6/site-packages/sage/misc/misc.pyc in
nest(f, n, x)
939
940 """
--> 941 for i in xrange(n):
942 x = f(x)
943 return x
/Applications/sage/local/lib/python2.6/site-
packages/sage/symbolic/expression.so in
sage.symbolic.expression.Expression.__int__
(sage/symbolic/expression.cpp:4375)()
/Applications/sage/local/lib/python2.6/site-
packages/sage/symbolic/expression.so in
sage.symbolic.expression.Expression.n
(sage/symbolic/expression.cpp:17185)()
TypeError: cannot evaluate symbolic expression numerically
}}}
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/7742#comment:28>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.