Ask Your Question

Revision history [back]

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)
6

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 = sum(x)
sage: f
x_0 + x_1 + x_2 + x_3 + x_4
sage: f.diff(x_2)
1

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)
6

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 = sum(x)
sage: f
f(x)
x_0 + x_1 + x_2 + x_3 + x_4
sage: f.diff(x_2)
f(x).diff(x_2)
1