ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 10 May 2020 14:58:57 -0500Self composition of a function with symbolic variablehttps://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/I have a function in one variable with a placeholder variable, and would like to find the nth iterate. For example, using:
$f(z) = z^3 + az^2$, a function in variable z with placeholder coefficient a.
then I would like to see
$f(f(z)) = ( z^3 + az^2)^3 + a( z^3 + az^2)^2$
However I am struggling to perform this in sage.
$f(f)$ returns $(a*z^2 + z^3)*z^2 + z^3$
, so it substitutes for a instead of z, and declaring $f(f(z))$ returns the function only in terms of z .
How can we self compose functions with a placeholder variable for a coefficient?Sun, 10 May 2020 13:55:28 -0500https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/Answer by tmonteil for <p>I have a function in one variable with a placeholder variable, and would like to find the nth iterate. For example, using:</p>
<p>$f(z) = z^3 + az^2$, a function in variable z with placeholder coefficient a. </p>
<p>then I would like to see</p>
<p>$f(f(z)) = ( z^3 + az^2)^3 + a( z^3 + az^2)^2$</p>
<p>However I am struggling to perform this in sage.</p>
<p>$f(f)$ returns $(a<em>z^2 + z^3)</em>z^2 + z^3$
, so it substitutes for a instead of z, and declaring $f(f(z))$ returns the function only in terms of z .</p>
<p>How can we self compose functions with a placeholder variable for a coefficient?</p>
https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?answer=51371#post-id-51371What is wrong here :
sage: var('a')
a
sage: f(z)=z^3+a*z^2
sage: f(f(z))
(a*z^2 + z^3)^3 + (a*z^2 + z^3)^2*a
Sun, 10 May 2020 14:15:18 -0500https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?answer=51371#post-id-51371Comment by tmonteil for <p>What is wrong here : </p>
<pre><code>sage: var('a')
a
sage: f(z)=z^3+a*z^2
sage: f(f(z))
(a*z^2 + z^3)^3 + (a*z^2 + z^3)^2*a
</code></pre>
https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?comment=51373#post-id-51373No, the construction `f(z)=` is not Pythonic, so there is some Sage preparsing that does the job of defining the symbol `z` :
sage: preparse('f(z)=z^3+a*z^2')
'__tmp__=var("z"); f = symbolic_expression(z**Integer(3)+a*z**Integer(2)).function(z)'Sun, 10 May 2020 14:58:57 -0500https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?comment=51373#post-id-51373Comment by sberner for <p>What is wrong here : </p>
<pre><code>sage: var('a')
a
sage: f(z)=z^3+a*z^2
sage: f(f(z))
(a*z^2 + z^3)^3 + (a*z^2 + z^3)^2*a
</code></pre>
https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?comment=51372#post-id-51372Ahh, my mistake was that I was also declaring z as a variable. Why don't I need to do this? Does sage "know" that this is over the complex field?Sun, 10 May 2020 14:20:08 -0500https://ask.sagemath.org/question/51369/self-composition-of-a-function-with-symbolic-variable/?comment=51372#post-id-51372