Ask Your Question

Symbolic expression not scriptable in Solve

asked 2021-05-30 10:32:33 +0200

Cyrille gravatar image

updated 2021-05-31 14:01:34 +0200

slelievre gravatar image

This is a follow-up to

The answer by @slelievre to that question works nicely, but I need to use indexed variables.

This variation of the code does not work for me:

D = polytopes.dodecahedron()
DH = D.Hrepresentation()
X = list(SR.var('x_%i' % i) for i in (0..2))
Ineq = [[el.A()*vector(X) >=el.b()] for el in DH]
sol = [solve(Ineq[i][0], x[1]) for i in range(len(Ineq))]

raising an error saying

'sage.symbolic.expression.Expression' object is not subscriptable

Why? the problem doesn't seem to come from Ineq[0][0] but from the variables?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-05-30 11:53:58 +0200

tolga gravatar image

Does changing x to X solve your problem? Namely,

sol = [solve(Ineq[i][0], X[1]) for i in range(len(Ineq))]
edit flag offensive delete link more

answered 2021-05-30 11:49:46 +0200

slelievre gravatar image

updated 2021-05-30 11:54:00 +0200

Having named X the list [x_0, x_1, x_2], use X[1] and not x[1] to get x_1.

Or, maybe even better, call that list x rather than X.

Also beware that the A and b of polytopes inequalities are for A x + b >= 0, not A x >= b.

Finally, take advantage of Python's iteration by values and change

sol = [solve(Ineq[i][0], x[1]) for i in range(len(Ineq))]

into the lighter

sol = [solve(ieq[0], x[1]) for ieq in Ineq]
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



Asked: 2021-05-30 10:32:33 +0200

Seen: 693 times

Last updated: May 31 '21