Ask Your Question
1

How to expand an symbolic expression.

asked 13 years ago

anonymous user

Anonymous

updated 13 years ago

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?

Preview: (hide)

Comments

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

petropolis gravatar imagepetropolis ( 13 years ago )

2 Answers

Sort by » oldest newest most voted
1

answered 13 years ago

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)
Preview: (hide)
link
1

answered 13 years ago

Shashank gravatar image

updated 13 years ago

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)
Preview: (hide)
link

Comments

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

petropolis gravatar imagepetropolis ( 13 years ago )

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 ( 13 years ago )

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 ( 13 years ago )

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 ( 13 years ago )

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: 13 years ago

Seen: 1,705 times

Last updated: Jan 24 '12