Ask Your Question

error while assigning variables

asked 2019-12-24 06:37:57 +0200

Sha gravatar image

Hi. I have the following coding of iteration where I want to find the values of u[1,1], u[1,2], u[1,3] and u[1,4].

u,t,x = var('u t x')
for i in range(1,4):
for i in range(1,4):

But it gave me an error saying that: 'sage.symbolic.expression.Expression' object does not support item assignment Can someone explain to me what I might be doing wrong here.

edit retag flag offensive close merge delete


u is not an array. so the error is from u[1]=x

Nasser gravatar imageNasser ( 2019-12-24 09:42:50 +0200 )edit

How can i fix that? What's the correct way of writing it. Thank you.

Sha gravatar imageSha ( 2019-12-24 09:43:53 +0200 )edit

I think you meant to us subscripted variables? I do not think these are in python but I could be wrong. To make what you want in python var('x,u'); u=[None]*2; u[1]=x and this works. this makes empty array u of length 2. Since index starts at zero in python. Now you can assign u[1]

Nasser gravatar imageNasser ( 2019-12-24 10:06:54 +0200 )edit

Hello, @Sha! Is this by any chance what you are trying to do? Please, let me know so I can post it as an answer or I can adapt it as necessary.

dsejas gravatar imagedsejas ( 2019-12-25 00:17:54 +0200 )edit

@dsejas yes this looks very good and understandable. But I have 2 questions. I need to keep the h value till the end. and then only sub h=-1. One more thing is that: the integration is in terms of dt where the bounds goes from 0 to x. thank you..

Sha gravatar imageSha ( 2019-12-26 03:58:16 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2020-01-03 00:37:35 +0200

dsejas gravatar image

Hello, @Sha! Based on your original question and the exchange of information we had through the comments section, the following code should solve your particular problem for this case:

from __future__ import print_function


h, t, x = var('h t x')

g = vector(SR, 5)
print('g(x): ***************************************************')
g[1] = -x^3-x; print(g[1])
g[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(g[2])
g[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(g[3])
g[4] = x^3-5; print(g[4])

K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 1; print(K2[1,1])
K2[1,2] = 0; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])

u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
print('u(x): ***************************************************')
print('-------------------- For m = 1: --------------------')
for i in range(1, 5):
    u[i,1] = -h*integrate(K2.row(i)*g.subs(x=t),t, 0, x); print(u[i,1].full_simplify())

for m in range(2, NUMBER_OF_ITERATIONS+1):
    print('-------------------- For m = ' + str(m) + ': --------------------')
    for i in range(1, 5):
        u[i,m] = (1+h)*u[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())

A couple of comments on the previous cod: 1. The statement from __future__ import print_function enables the more modern use of the print function. Sage now uses Python3, starting with version 9.0, available since January 1st, 2020. In that case, that line is useless. 2. I have set the parameter NUMBER_OF_ITERATIONS to 10, so Sage iterates your formulas 10 times. You can change that to the particular number you wish. However, I wouldn't recommend using nothing more than 25 if you don't want to wait a lot of time or run out of memory. 3. Note that instead of the summations in your formulas (instead of using the sum function), I have used matrix multiplications (just an optimization). 4. I have used the function full_simplify() simply for Sage to show you the simplified results of your formulas. You can safely remove them. 5. Although this code uses g in terms of the variablet, I have written it in terms of x, and then I changed the variable with g.subs(x=t). I made that because apparently you need g(x) later in your formulas.

I hope this helps!

edit flag offensive delete link more


Thank you so much. This helped me a lot.

Sha gravatar imageSha ( 2020-01-03 17:00:31 +0200 )edit

answered 2019-12-24 10:16:56 +0200

Emmanuel Charpentier gravatar image

This is a Frequently Asked Question, under very various guises...

The problem is bound to the ambiguity in the uses of the word "variable" :

  • A Python variable is a label (technically some form of pointer) attached to some Python object.

  • A symbolic variable is a Python object behaving in Sage according to the properties of a mathematical variable in a symbolic expression.

Your first statement, u,t,x = var('u t x'), is, ultimately, a shortcut for:


The first statement is itself a shortcut for:

  • create a Python object representing a symbolic variable named "u"
  • create a label (a Python variable) named "u" attached to this Python object.

Your second statement, u[1]=x means "Assuming that the Python object pointed to by u is a list, store whatever the Python variable x points to in u's second element". Since the assumption that u is a list does not hold, the expression u[1] has no meaning, hence the Python interpreter's complaint...

This is explained in detail in §1.2 of this excellent book, whose reading and perusal is currently the best shortcut to Sage I'm aware of.

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: 2019-12-24 06:37:57 +0200

Seen: 159 times

Last updated: Jan 03 '20