Ask Your Question
0

automatic substitution within functions?

asked 2010-08-25 19:04:25 +0100

ngativ gravatar image

updated 2010-08-25 19:28:39 +0100

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2010-08-25 19:10:55 +0100

Mike Hansen gravatar image

updated 2010-08-25 23:03:16 +0100

The 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.

edit flag offensive delete link more

Comments

thanks, :( 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.

ngativ gravatar imagengativ ( 2010-08-25 19:14:15 +0100 )edit

I've updated my answer. Could you explain the annoying part? Do you mean having to do the ".function(*f.arguments())" part?

Mike Hansen gravatar imageMike Hansen ( 2010-08-25 19:20:11 +0100 )edit

i have updated my question, its about doing f(a=1,b=2,c=3...)

ngativ gravatar imagengativ ( 2010-08-25 19:32:44 +0100 )edit

Thanks, your answer is very usefull

ngativ gravatar imagengativ ( 2010-08-25 23:54:36 +0100 )edit
2

answered 2010-08-25 22:07:58 +0100

kcrisman gravatar image

updated 2012-10-23 11:56:17 +0100

I 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?

edit flag offensive delete link more

Comments

hi, you can prefix the preformatted text with four space characters, then they will not wrap

Evgeny gravatar imageEvgeny ( 2010-08-25 22:11:51 +0100 )edit

Okay, 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!

kcrisman gravatar imagekcrisman ( 2010-08-25 22:15:34 +0100 )edit

You 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.

Evgeny gravatar imageEvgeny ( 2010-08-25 22:38:18 +0100 )edit

Actually, 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.

Mike Hansen gravatar imageMike Hansen ( 2010-08-25 23:02:35 +0100 )edit

i got it now, thanks

ngativ gravatar imagengativ ( 2010-08-25 23:54:59 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2010-08-25 19:04:25 +0100

Seen: 989 times

Last updated: Oct 23 '12