ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Apr 2011 00:07:50 +0200Composite functionhttps://ask.sagemath.org/question/8066/composite-function/If I define:
var('x,y,z,t')
g(t) = (t, t^2, t^3)
f(x,y,z) = (2*x,y+x+z,y*x)
Is there a way for me to define the composite f(g(t)) without going through:
f(g(t)[0],g(t)[1],g(t)[2])
or something equally ugly?Sat, 09 Apr 2011 19:32:57 +0200https://ask.sagemath.org/question/8066/composite-function/Answer by Ben Reynwar for <p>If I define:</p>
<p>var('x,y,z,t')</p>
<p>g(t) = (t, t^2, t^3)</p>
<p>f(x,y,z) = (2<em>x,y+x+z,y</em>x)</p>
<p>Is there a way for me to define the composite f(g(t)) without going through:</p>
<p>f(g(t)[0],g(t)[1],g(t)[2])</p>
<p>or something equally ugly?</p>
https://ask.sagemath.org/question/8066/composite-function/?answer=12277#post-id-12277h(t) = f(*g(t))Sat, 09 Apr 2011 20:00:05 +0200https://ask.sagemath.org/question/8066/composite-function/?answer=12277#post-id-12277Comment by John Palmieri for <p>h(t) = f(*g(t))</p>
https://ask.sagemath.org/question/8066/composite-function/?comment=21882#post-id-21882It's a python thing -- see benjaminfjones' answer.Sat, 09 Apr 2011 20:09:22 +0200https://ask.sagemath.org/question/8066/composite-function/?comment=21882#post-id-21882Comment by kcrisman for <p>h(t) = f(*g(t))</p>
https://ask.sagemath.org/question/8066/composite-function/?comment=21857#post-id-21857But it might be nice to have this work without that. See http://trac.sagemath.org/sage_trac/ticket/11180; if someone can come up with a better title for that ticket, be my guest.Tue, 12 Apr 2011 00:07:50 +0200https://ask.sagemath.org/question/8066/composite-function/?comment=21857#post-id-21857Comment by BobB for <p>h(t) = f(*g(t))</p>
https://ask.sagemath.org/question/8066/composite-function/?comment=21883#post-id-21883Great! And very quick, thank you.
I suppose I would have known that if I knew something about python?
Or is * a sage operator of some sort?Sat, 09 Apr 2011 20:08:29 +0200https://ask.sagemath.org/question/8066/composite-function/?comment=21883#post-id-21883Answer by benjaminfjones for <p>If I define:</p>
<p>var('x,y,z,t')</p>
<p>g(t) = (t, t^2, t^3)</p>
<p>f(x,y,z) = (2<em>x,y+x+z,y</em>x)</p>
<p>Is there a way for me to define the composite f(g(t)) without going through:</p>
<p>f(g(t)[0],g(t)[1],g(t)[2])</p>
<p>or something equally ugly?</p>
https://ask.sagemath.org/question/8066/composite-function/?answer=12278#post-id-12278If you let `g` be a tuple of expressions involving `t` you can use the Python `*` operator to use the tuple entries as your inputs `x`, `y`, and `z`:
sage: g = (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g)
(2*t, t^3 + t^2 + t, t^3)
If you want to define both `g` and `f` as lambda functions you could do:
sage: g = lambda t: (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g(t))
(2*t, t^3 + t^2 + t, t^3)
Sat, 09 Apr 2011 20:02:15 +0200https://ask.sagemath.org/question/8066/composite-function/?answer=12278#post-id-12278Comment by BobB for <p>If you let <code>g</code> be a tuple of expressions involving <code>t</code> you can use the Python <code>*</code> operator to use the tuple entries as your inputs <code>x</code>, <code>y</code>, and <code>z</code>: </p>
<pre><code>sage: g = (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g)
(2*t, t^3 + t^2 + t, t^3)
</code></pre>
<p>If you want to define both <code>g</code> and <code>f</code> as lambda functions you could do:</p>
<pre><code>sage: g = lambda t: (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g(t))
(2*t, t^3 + t^2 + t, t^3)
</code></pre>
https://ask.sagemath.org/question/8066/composite-function/?comment=21878#post-id-21878Thank you for the alternative. For the moment it looks like non-lambda functions work well. I'm not well versed in python and I'd like to make things look as much like mathematical notation as possible. Also, it seems using other properties (methods, functions?) like jacobian and derivative is more straight forward if lambda functions are avoided.Sun, 10 Apr 2011 05:05:12 +0200https://ask.sagemath.org/question/8066/composite-function/?comment=21878#post-id-21878Comment by John Palmieri for <p>If you let <code>g</code> be a tuple of expressions involving <code>t</code> you can use the Python <code>*</code> operator to use the tuple entries as your inputs <code>x</code>, <code>y</code>, and <code>z</code>: </p>
<pre><code>sage: g = (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g)
(2*t, t^3 + t^2 + t, t^3)
</code></pre>
<p>If you want to define both <code>g</code> and <code>f</code> as lambda functions you could do:</p>
<pre><code>sage: g = lambda t: (t, t^2, t^3)
sage: f = lambda x,y,z: (2*x, y+x+z, y*x)
sage: f(*g(t))
(2*t, t^3 + t^2 + t, t^3)
</code></pre>
https://ask.sagemath.org/question/8066/composite-function/?comment=21884#post-id-21884You don't need f to be a lambda function for this; using "f(x,y,z) = (2*x,y+x+z,y*x)" works just fine.Sat, 09 Apr 2011 20:07:54 +0200https://ask.sagemath.org/question/8066/composite-function/?comment=21884#post-id-21884