Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How tho pass an indexed variable to a function ?

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.

How tho pass an indexed variable to a function ?

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

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.