Ask Your Question
1

How to expand an symbolic expression.

asked 2012-01-23 18:14:39 +0100

anonymous user

Anonymous

updated 2012-01-23 19:21:12 +0100

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 07:38:34 +0100 )edit

2 Answers

Sort by Ā» oldest newest most voted
1

answered 2012-01-24 08:58:11 +0100

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 19:48:14 +0100

Shashank gravatar image

updated 2012-01-24 06:16:26 +0100

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-24 05:44:21 +0100 )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-24 06:18:35 +0100 )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 07:38:40 +0100 )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 08:44:15 +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

Stats

Asked: 2012-01-23 18:14:39 +0100

Seen: 1,613 times

Last updated: Jan 24 '12