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.Thu, 26 Aug 2010 02:46:38 -0500automatic substitution within functions?https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/what i do to do this?
sage: var('t w')
sage: f(t) = sin(w*t)
t |--> sin(t*w)
sage: w = 2
sage: f
t |--> sin(2*w)
Without doing f(w=2)!!! imagine that the function is f->f(a,b,c,d,f....,t) like doing this:
...
...
sage: aw1=aw1(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw2=aw2(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw4=aw4(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
...
...
etc
Wed, 25 Aug 2010 12:04:25 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/Answer by kcrisman for <p>what i do to do this?</p>
<pre><code>sage: var('t w')
sage: f(t) = sin(w*t)
t |--> sin(t*w)
sage: w = 2
sage: f
t |--> sin(2*w)
</code></pre>
<p>Without doing f(w=2)!!! imagine that the function is f->f(a,b,c,d,f....,t) like doing this:</p>
<pre><code>...
...
sage: aw1=aw1(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw2=aw2(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw4=aw4(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
...
...
</code></pre>
<p>etc</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?answer=11553#post-id-11553I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary
sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
and then do `f.subs(params)` as above. In his example,
var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?
Wed, 25 Aug 2010 15:07:58 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?answer=11553#post-id-11553Comment by kcrisman for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22862#post-id-22862Okay, but then there should be much more information about the wiki formatting somewhere (FAQ?). Maybe even a little button for multiline code, sort of like the backticks for inline code? Sorry for us all requesting so many new things!Wed, 25 Aug 2010 15:15:34 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22862#post-id-22862Comment by kcrisman for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22847#post-id-22847@Mike Hansen - Thanks, I was surprised that that was needed. Is it a bug that `f(w=2)` isn't callable, or a feature?
@Evgeny - great. I didn't realize that I had to do the 0101 button for each line. Maybe it should be the same way as block quote, where you need to return twice to leave the env.Thu, 26 Aug 2010 02:46:38 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22847#post-id-22847Comment by Evgeny for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22861#post-id-22861You are right, we keep track of all these issues and will fix them. Thanks. Actually the button with a whole bunch of zeroes and ones helps format both multiline and inline code.Wed, 25 Aug 2010 15:38:18 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22861#post-id-22861Comment by Mike Hansen for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22860#post-id-22860Actually, you don't need. However, if you did `f(w=2) then that doesn't return a CallableSymbolicExpression, but `f.subs(params)` does return one. I'll update my answer.Wed, 25 Aug 2010 16:02:35 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22860#post-id-22860Comment by Evgeny for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22863#post-id-22863hi, you can prefix the preformatted text with four space characters, then they will not wrapWed, 25 Aug 2010 15:11:51 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22863#post-id-22863Comment by ngativ for <p>I think I'm just explaining Mike's answer a little, but I think that you should be able to define a dictionary </p>
<pre><code>sage: params = {m1:1,m2:0.5,l1:1,l2:0.5,g:9.8}
</code></pre>
<p>and then do <code>f.subs(params)</code> as above. In his example,</p>
<pre><code>var('t, w')
params = {w: 2,d:1,c:1}
f(a,b,c,d,e,f,g,t) = sin(t*w*d-c)
g=f.subs(params)
g
</code></pre>
<p>should do it. Mike, is that extra thing necessary, given that the arguments are the same in both cases?</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22858#post-id-22858i got it now, thanksWed, 25 Aug 2010 16:54:59 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22858#post-id-22858Answer by Mike Hansen for <p>what i do to do this?</p>
<pre><code>sage: var('t w')
sage: f(t) = sin(w*t)
t |--> sin(t*w)
sage: w = 2
sage: f
t |--> sin(2*w)
</code></pre>
<p>Without doing f(w=2)!!! imagine that the function is f->f(a,b,c,d,f....,t) like doing this:</p>
<pre><code>...
...
sage: aw1=aw1(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw2=aw2(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw4=aw4(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
...
...
</code></pre>
<p>etc</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?answer=11537#post-id-11537The `w` in `sin(t*w)` does not refer to whatever the object with identifier `w` in the current scope. There isn't a way to get Python to work like this. You need to do something like:
sage: var('t, w')
(t, w)
sage: params = {w: 2}
sage: f(a,b,c,d,e,f,g,t) = sin(t*w)
sage: f.subs(params)
(a, b, c, d, e, f, g, t) |--> sin(2*t)
You just have to define the replacements you want once in a dictionary. You still have to call subs once for every function you want to change.
Wed, 25 Aug 2010 12:10:55 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?answer=11537#post-id-11537Comment by ngativ for <p>The <code>w</code> in <code>sin(t*w)</code> does not refer to whatever the object with identifier <code>w</code> in the current scope. There isn't a way to get Python to work like this. You need to do something like:</p>
<pre><code>sage: var('t, w')
(t, w)
sage: params = {w: 2}
sage: f(a,b,c,d,e,f,g,t) = sin(t*w)
sage: f.subs(params)
(a, b, c, d, e, f, g, t) |--> sin(2*t)
</code></pre>
<p>You just have to define the replacements you want once in a dictionary. You still have to call subs once for every function you want to change.</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22867#post-id-22867thanks, :( thats exactly what i don't want. what if f->f(a,b,c,d,f....,t)? it would be a little annoying to do that. Wed, 25 Aug 2010 12:14:15 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22867#post-id-22867Comment by ngativ for <p>The <code>w</code> in <code>sin(t*w)</code> does not refer to whatever the object with identifier <code>w</code> in the current scope. There isn't a way to get Python to work like this. You need to do something like:</p>
<pre><code>sage: var('t, w')
(t, w)
sage: params = {w: 2}
sage: f(a,b,c,d,e,f,g,t) = sin(t*w)
sage: f.subs(params)
(a, b, c, d, e, f, g, t) |--> sin(2*t)
</code></pre>
<p>You just have to define the replacements you want once in a dictionary. You still have to call subs once for every function you want to change.</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22859#post-id-22859Thanks, your answer is very usefullWed, 25 Aug 2010 16:54:36 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22859#post-id-22859Comment by Mike Hansen for <p>The <code>w</code> in <code>sin(t*w)</code> does not refer to whatever the object with identifier <code>w</code> in the current scope. There isn't a way to get Python to work like this. You need to do something like:</p>
<pre><code>sage: var('t, w')
(t, w)
sage: params = {w: 2}
sage: f(a,b,c,d,e,f,g,t) = sin(t*w)
sage: f.subs(params)
(a, b, c, d, e, f, g, t) |--> sin(2*t)
</code></pre>
<p>You just have to define the replacements you want once in a dictionary. You still have to call subs once for every function you want to change.</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22866#post-id-22866I've updated my answer. Could you explain the annoying part? Do you mean having to do the ".function(*f.arguments())" part?Wed, 25 Aug 2010 12:20:11 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22866#post-id-22866Comment by ngativ for <p>The <code>w</code> in <code>sin(t*w)</code> does not refer to whatever the object with identifier <code>w</code> in the current scope. There isn't a way to get Python to work like this. You need to do something like:</p>
<pre><code>sage: var('t, w')
(t, w)
sage: params = {w: 2}
sage: f(a,b,c,d,e,f,g,t) = sin(t*w)
sage: f.subs(params)
(a, b, c, d, e, f, g, t) |--> sin(2*t)
</code></pre>
<p>You just have to define the replacements you want once in a dictionary. You still have to call subs once for every function you want to change.</p>
https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22865#post-id-22865i have updated my question, its about doing f(a=1,b=2,c=3...)Wed, 25 Aug 2010 12:32:44 -0500https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/?comment=22865#post-id-22865