Ask Your Question
0

How tho pass an indexed variable to a function ?

asked 2021-06-19 11:08:38 +0200

Cyrille gravatar image

updated 2021-06-19 14:21:59 +0200

In his answer to a earlier question file:///C:/Users/cyril/Downloads/sagemath%20ask/How%20to%20transform%20a%20derived%20set%20of%20inequation%20in%20the%20good%20polyhedron%20format%20-%20ASKSAGE%20Sage%20Q&A%20Forum.html, SLelievre validated the following code.

 def one_dimension_less(representationH):
    x = list(var('x_%i' % i) for i in (0..2))
    sol = [solve_ineq(ieq, [x[0]]) for ieq in Ineq]
    sol_r = flatten([x[1] for x in sol])
    ineq_ge = [z for z in sol_r if z.rhs() == x[0]]
    ineq_le = [z for z in sol_r if z.lhs() == x[0]]
    ineq_a = [z for z in sol_r if z.lhs() != x[0] and z.rhs() != x[0]]
    ineq_aa = [(z.lhs()).full_simplify() >= 0 for z in ineq_a]
    result = flatten(ineq_aa + [
            [(ineq_le[j].rhs()-ineq_ge[i].lhs()).full_simplify() >= 0
             for i in range(len(ineq_ge))]
            for  j in range(len(ineq_le))])
    # result1 = [result[i].factor() for i in range(len(result))]
    return result

But I realized that I should be able to choose the eliminated variable. So I code as follow :

def elim_x(representationH,elim_var,k):
    x = vector(SR, SR.var('x_', 3))
    ieqs = [[el.A()*x + el.b() >= 0] for el in DH]
    sol = [solve_ineq(ieq, [elim_var]) for ieq in ieqs]
    sol_r = flatten([x[1] for x in sol])
    ineq_ge = [z for z in sol_r if z.rhs() == elim_var]
    ineq_le = [z for z in sol_r if z.lhs() == elim_var]
    ineq_a = [z for z in sol_r if z.lhs() != elim_var and z.rhs() != x[0]]
    ineq_aa = [(z.lhs()).full_simplify() >= 0 for z in ineq_a]
    result = flatten(ineq_aa + [[(ineq_le[j].rhs() - ineq_ge[i].lhs()).full_simplify() >= 0
        for i in range(len(ineq_ge))] for  j in range(len(ineq_le))])
    reslhs=[el.lhs() for el in result]
    if k==0 :
        return result
    if k==1 :
        return reslhs

which generate no apparent error, and call this function by :

D = polytopes.dodecahedron()
DH = D.Hrepresentation()
el0=elim_x(DH,[x[0]],1)
el0[0]

but this call generate an error of the type :

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

I am sorry not to understand by myself why since it is just a way to give a variable already inside the first function.

edit retag flag offensive close merge delete

Comments

You are giving a link to a file on your hard drive. I think you meant to point to

slelievre gravatar imageslelievre ( 2021-06-19 16:17:40 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-19 16:32:50 +0200

slelievre gravatar image

There are two mistakes:

  • x is only defined as a vector inside the function, not outside
  • elim_x expects x[0] rather than [x[0]] as its second argument

The function elim_x can be slightly reformatted as follows:

def elim_x(representationH, elim_var, k):
    x = vector(SR, SR.var('x_', 3))
    ieqs = [[el.A()*x + el.b() >= 0] for el in DH]
    sol = [solve_ineq(ieq, [elim_var]) for ieq in ieqs]
    sol_r = flatten([x[1] for x in sol])
    ineq_ge = [z for z in sol_r if z.rhs() == elim_var]
    ineq_le = [z for z in sol_r if z.lhs() == elim_var]
    ineq_a = [z for z in sol_r if z.lhs() != elim_var and z.rhs() != x[0]]
    ineq_aa = [(z.lhs()).full_simplify() >= 0 for z in ineq_a]
    result = flatten(ineq_aa +
                     [[(ineq_le[j].rhs() - ineq_ge[i].lhs()).full_simplify() >= 0
                       for i in range(len(ineq_ge))]
                      for j in range(len(ineq_le))])
    reslhs = [el.lhs() for el in result]
    if k == 0:
        return result
    if k == 1:
        return reslhs

(only minor cosmetic changes: spacing, line breaks), and used as

sage: D = polytopes.dodecahedron()
sage: DH = D.Hrepresentation()
sage: x = vector(SR, SR.var('x_', 3))
sage: el0 = elim_x(DH, x[0], 1)
sage: el0[0]
-x_2*(sqrt(5) + 1) - 2*x_1 + 4
edit flag offensive delete link more

Comments

On more time thanks

Cyrille gravatar imageCyrille ( 2021-06-19 18:08:04 +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: 2021-06-19 11:08:38 +0200

Seen: 68 times

Last updated: Jun 19 '21