ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 30 May 2021 11:53:58 +0200Symbolic expression not scriptable in Solvehttps://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/This is a follow-up to
- [Ask Sage question 57318: Use solve with inequality extracted from polytope](https://ask.sagemath.org/question/57318)
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]
show(Ineq[0][0])
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?Sun, 30 May 2021 10:32:33 +0200https://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/Answer by tolga for <p>This is a follow-up to</p>
<ul>
<li><a href="https://ask.sagemath.org/question/57318">Ask Sage question 57318: Use solve with inequality extracted from polytope</a></li>
</ul>
<p>The answer by <a href="/users/1092/slelievre/">@slelievre</a> to that question works nicely,
but I need to use indexed variables.</p>
<p>This variation of the code does not work for me:</p>
<pre><code>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]
show(Ineq[0][0])
sol = [solve(Ineq[i][0], x[1]) for i in range(len(Ineq))]
</code></pre>
<p>raising an error saying</p>
<pre><code>'sage.symbolic.expression.Expression' object is not subscriptable
</code></pre>
<p>Why? the problem doesn't seem to come from <code>Ineq[0][0]</code> but from the variables?</p>
https://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/?answer=57362#post-id-57362Does changing x to X solve your problem? Namely,
sol = [solve(Ineq[i][0], X[1]) for i in range(len(Ineq))]Sun, 30 May 2021 11:53:58 +0200https://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/?answer=57362#post-id-57362Answer by slelievre for <p>This is a follow-up to</p>
<ul>
<li><a href="https://ask.sagemath.org/question/57318">Ask Sage question 57318: Use solve with inequality extracted from polytope</a></li>
</ul>
<p>The answer by <a href="/users/1092/slelievre/">@slelievre</a> to that question works nicely,
but I need to use indexed variables.</p>
<p>This variation of the code does not work for me:</p>
<pre><code>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]
show(Ineq[0][0])
sol = [solve(Ineq[i][0], x[1]) for i in range(len(Ineq))]
</code></pre>
<p>raising an error saying</p>
<pre><code>'sage.symbolic.expression.Expression' object is not subscriptable
</code></pre>
<p>Why? the problem doesn't seem to come from <code>Ineq[0][0]</code> but from the variables?</p>
https://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/?answer=57361#post-id-57361Having 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]Sun, 30 May 2021 11:49:46 +0200https://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/?answer=57361#post-id-57361