Ask Your Question
1

How to expand an symbolic expression.

asked 2012-01-23 11:14:39 -0500

anonymous user

Anonymous

updated 2012-01-23 12:21:12 -0500

Shashank gravatar image

With Maple I can write

T := proc(f,n,x) 
local v; 
add(binomial(n,v)*f(v+1)*(x)^v,v=0..n) 
end:

and then

T(f,4,x)

will give me the answer

f(1)+4*f(2)*x+6*f(3)*x^2+4*f(4)*x^3+f(5)*x^4.

If I write the same with Sage

def T(f,n,x): 
    return add(binomial(n,v)*f(v+1)*(x)^v for v in (0..n))
var('f')
T(f,4,x)

then I will get the answer

5*x^4 + 16*x^3 + 18*x^2 + 8*x + 1.

How do I have to proceed to get Maple's answer?

edit retag flag offensive close merge delete

Comments

The answer, as indicated by burcin, simply is: Replace in your code "var('f')" by "f=function('f')".

petropolis gravatar imagepetropolis ( 2012-01-24 00:38:34 -0500 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2012-01-24 01:58:11 -0500

f=function('f')
def T(f,n,x): 
    ans=0
    for v in range(n+1):
        ans=ans+binomial(n,v)*f(v+1)*(x)^v
    return ans

print T(f,4,x)

gives:

x^4*f(5) + 4*x^3*f(4) + 6*x^2*f(3) + 4*x*f(2) + f(1)
edit flag offensive delete link more
1

answered 2012-01-23 12:48:14 -0500

Shashank gravatar image

updated 2012-01-23 23:16:26 -0500

This should do the job.

f=function('f')
def T(f,n,x): 
    ans=0
    for v in range(n+1):
        ans=ans+binomial(n,v)*f(v+1)*(x)^v
    return ans

print T(f,4,x)
edit flag offensive delete link more

Comments

This gives the answer 16*x^3 + 18*x^2 + 8*x + 1.

petropolis gravatar imagepetropolis ( 2012-01-23 22:44:21 -0500 )edit

I made a couple of changes to fix the code above. Declared f as a symbolic function, removed redundant var('ans'), and fixed the bounds for range().

burcin gravatar imageburcin ( 2012-01-23 23:18:35 -0500 )edit

burcin, changing an answer is not a good way to edit. It makes my comment ridiculous  and gives credit for the right answer to the wrong person. Please give your own answer. I will accept it then.

petropolis gravatar imagepetropolis ( 2012-01-24 00:38:40 -0500 )edit

I would have voted Shashank's answer down (if I had >100 points), not because it was wrong, but because Shashank obviously did not even verify his answer by testing it at least once.

petropolis gravatar imagepetropolis ( 2012-01-24 01:44:15 -0500 )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

Stats

Asked: 2012-01-23 11:14:39 -0500

Seen: 174 times

Last updated: Jan 24 '12