Ask Your Question

Symbolic function that sums over variable sequence

asked 2015-12-18 04:16:12 -0500

jonatan gravatar image

How do I define a symbolic function that takes a sum of variables as value?

I have tried the following:

f(x)=sum(x[k], k, 1, 5)

But I get the following error:

TypeError: unable to convert k to an integer

I want to be able to symbolically differentiate f with regards to e.g. x[3].

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-12-18 08:04:35 -0500

tmonteil gravatar image

updated 2015-12-18 08:07:58 -0500

I am not sure to understand your question.

You can define a Python function that sums all elements of a list as follows:

sage: f = lambda x : sum(x)
sage: x = [1,2,3]
sage: f(x)

If you want to create some symbolic variables x_i in a list x and, at the same time inject them into the namespace (i.e. make them Python variables), you can do:

sage: x = [var("x_{}".format(i)) for i in range(5)]
sage: x
[x_0, x_1, x_2, x_3, x_4]

Then you can define their sum and differentiate according to some variable:

sage: f(x)
x_0 + x_1 + x_2 + x_3 + x_4
sage: f(x).diff(x_2)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2015-12-18 04:16:12 -0500

Seen: 130 times

Last updated: Dec 18 '15