Ask Your Question
0

Solve a system of equation but shown variable is not defined.

asked 2022-11-20 16:48:18 +0200

lijr07 gravatar image

I am trying to solve a system of equations as follows.

for i in range(1,4):
for j in range(1,4):
    var('y'+'_'+str(j)+'_'+str(i))
    var('x'+'_'+str(j)+'_'+str(i))
t3=[x_1_1*x_1_2*x_2_3/((x_1_1 + 1)*(x_2_3 + 1)),
x_2_1*(x_3_1 + 1)/(x_1_1*x_2_1*x_3_1 + x_1_1*x_2_1 + x_1_1 + 1),
  (x_1_1 + 1)/(x_1_1*x_2_1*(x_3_1 + 1)),
 1/x_2_3,
 (x_3_1*x_3_2 + x_3_1 + 1)*(x_1_1 + 1)*x_2_2*(x_2_3 + 1)/(x_3_1 + 1),
 (x_1_1*x_2_1*x_3_1 + x_1_1*x_2_1 + x_1_1 + 1)*x_3_2/((x_3_1*x_3_2 + x_3_1 + 1)*(x_1_1 + 1)),
 x_1_3*(x_2_3 + 1),
 x_2_3*x_3_1*x_3_2*x_3_3/((x_3_1*x_3_2 + x_3_1 + 1)*(x_2_3 + 1)),
 (x_3_1 + 1)/(x_3_1*x_3_2)]

Now define

a1=[]
va=[]


for i in range(1,4):
    for j in range(1,4):
        a1.append(y[j,i])
        va.append(x[j,i])
print(va)
print(a1)
#print(len(a1))
a2=[]
a3=[t3[i]-a1[i] for i in range(len(a1))]
print(a3)
solve(a3, va)

But it is shown that ``NameError: name 'y' is not defined''. How to avoid this problem? Thank you very much!

edit retag flag offensive close merge delete

Comments

Change y[j,i] to var('y'+'_'+str(j)+'_'+str(1)) as above.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-11-20 17:20:50 +0200 )edit

@max, thank you very much! Your codes work in a separate file. But when I tried to use the same codes in the original file, it has some error:

a1=[]
va=[]
for i in range(1,4):
for j in range(1,4):
    a1.append(var('y'+'_'+str(j)+'_'+str(i)))
    va.append(var('x'+'_'+str(j)+'_'+str(i)))

<ipython-input-164-91009a15f11e> in <module>
  1 for i in range(Integer(1),Integer(4)):
  2     for j in range(Integer(1),Integer(4)):

----> 3 a1.append(var('y'+'_'+str(j)+'_'+str(i))) 4 va.append(var('x'+'_'+str(j)+'_'+str(i)))

TypeError: 'list' object is not callable

Do you know why this happens? Thank you very much!

lijr07 gravatar imagelijr07 ( 2022-11-20 19:08:30 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-11-20 17:37:42 +0200

Emmanuel Charpentier gravatar image

You define a bunch of variables named x_1-1...y_3_3, as well the t3 list of expressions ... but fail to define x andy.

BTW : x[j, i] is not a valid Python expression ; you probably meanx[j][i]...

Possible suggestions (with some lightening...) :

Xs = matrix([[var("x_%s_%s"%(u, v), latex_name="x_{%s,%s}"%(u, v))
              for v in range(1, 4)]
             for u in range(1, 4)])
Ys = matrix([[var("y_%s_%s"%(u, v), latex_name="y_{%s,%s}"%(u, v))
              for v in range(1, 4)]
             for u in range(1, 4)])
t3=[x_1_1*x_1_2*x_2_3/((x_1_1 + 1)*(x_2_3 + 1)),
    x_2_1*(x_3_1 + 1)/(x_1_1*x_2_1*x_3_1 + x_1_1*x_2_1 + x_1_1 + 1),
    (x_1_1 + 1)/(x_1_1*x_2_1*(x_3_1 + 1)),
    1/x_2_3,
    (x_3_1*x_3_2 + x_3_1 + 1)*(x_1_1 + 1)*x_2_2*(x_2_3 + 1)/(x_3_1 + 1),
    (x_1_1*x_2_1*x_3_1 + x_1_1*x_2_1 + x_1_1 + 1)*x_3_2/\
    ((x_3_1*x_3_2 + x_3_1 + 1)*(x_1_1 + 1)),
    x_1_3*(x_2_3 + 1),
    x_2_3*x_3_1*x_3_2*x_3_3/((x_3_1*x_3_2 + x_3_1 + 1)*(x_2_3 + 1)),
    (x_3_1 + 1)/(x_3_1*x_3_2)]
va = Xs.list() ## Or possibly va=Xs.transpose().list()
a1 = Ys.list() ## Ditto. You're not overly clear...
a3 = list(map(lambda a, b:a-b, t3, a1))
Sol = solve(a3, va)

Meta-suggestion : read a Python tutorial (zillions of them) and this excellent book.

HTH,

edit flag offensive delete link more

Comments

Hi Emmanuel, thank you very much! Your codes works perfectly in a new sage file. But when I try to use the same code in the original file, it has some problem:

Xs = Matrix([[var("x_%s_%s"%(u, v), latex_name="x_{%s,%s}"%(u, v))
          for v in range(1, 4)]
         for u in range(1, 4)])

<ipython-input-163-ce9f631f15a9> in <listcomp>(.0)

----> 1 Xs = Matrix([[var("x_%s_%s"%(u, v), latex_name="x_{%s,%s}"%(u, v)) 2 for v in range(Integer(1), Integer(4))] 3 for u in range(Integer(1), Integer(4))])

TypeError: 'list' object is not callable

Do you know why this error appears? Thank you very much!

lijr07 gravatar imagelijr07 ( 2022-11-20 19:03:37 +0200 )edit

@Emmanuel x[j,i] can be valid, e.g. x = dict(); x[2,1] = 3 results in x == {(2, 1): 3}. @lijr07 Usually 'object is not callable' means that you are trying to call a (often global, built-in) function which you have accidentally overwritten by a variable of your own with the same name. In this case it is probably var (judging from in <listcomp>(.0)). The usual remedy is to change the name of your own variable in your script/notebook to something more personalized, and restart the Python process.

rburing gravatar imagerburing ( 2022-11-20 20:02:26 +0200 )edit

@rburing, thank you very much for your help!

lijr07 gravatar imagelijr07 ( 2022-11-20 20:22:58 +0200 )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

1 follower

Stats

Asked: 2022-11-20 16:48:18 +0200

Seen: 245 times

Last updated: Nov 20 '22