Ask Your Question

Revision history [back]

Like this, for example:

sage: [sol for sol in fm_sol if next((ieq for ieq in sol if ieq.lhs() == z and ieq.operator() is operator.eq), None) is not None]
[[x_1 == 0, x_2 == 300, z == 900],
 [x_1 == 0, x_2 == 700, z == 2100],
 [x_1 == 200, x_2 == 300, z == 1700],
 [x_1 == 200, x_2 == 550, z == 2450],
 [x_1 == 150, x_2 == 650, z == 2550],
 [x_2 == -2*x_1 + 950, z == -2*x_1 + 2850, 150 < x_1, x_1 < 200],
 [x_2 == -1/3*x_1 + 700, z == 3*x_1 + 2100, 0 < x_1, x_1 < 150]]

Or in a more readable way, with a helper function:

def has_exact_value(ieqns, variable):
    for ieq in ieqns:
        if ieq.lhs() == variable and ieq.operator() is operator.eq:
            return True
    return False

Then you can do:

sage: [sol for sol in fm_sol if has_exact_value(sol, z)]
[[x_1 == 0, x_2 == 300, z == 900],
 [x_1 == 0, x_2 == 700, z == 2100],
 [x_1 == 200, x_2 == 300, z == 1700],
 [x_1 == 200, x_2 == 550, z == 2450],
 [x_1 == 150, x_2 == 650, z == 2550],
 [x_2 == -2*x_1 + 950, z == -2*x_1 + 2850, 150 < x_1, x_1 < 200],
 [x_2 == -1/3*x_1 + 700, z == 3*x_1 + 2100, 0 < x_1, x_1 < 150]]

Like this, for example:

sage: [sol for sol in fm_sol if next((ieq for ieq in sol if ieq.lhs() == z and ieq.operator() is operator.eq), None) is not None]
[[x_1 == 0, x_2 == 300, z == 900],
 [x_1 == 0, x_2 == 700, z == 2100],
 [x_1 == 200, x_2 == 300, z == 1700],
 [x_1 == 200, x_2 == 550, z == 2450],
 [x_1 == 150, x_2 == 650, z == 2550],
 [x_2 == -2*x_1 + 950, z == -2*x_1 + 2850, 150 < x_1, x_1 < 200],
 [x_2 == -1/3*x_1 + 700, z == 3*x_1 + 2100, 0 < x_1, x_1 < 150]]

Or in a more readable way, with a helper function:

def has_exact_value(ieqns, has_exact_equality(ieqns, variable):
    for ieq in ieqns:
        if ieq.lhs() == variable and ieq.operator() is operator.eq:
            return True
    return False

Then you can do:

sage: [sol for sol in fm_sol if has_exact_value(sol, has_exact_equality(sol, z)]
[[x_1 == 0, x_2 == 300, z == 900],
 [x_1 == 0, x_2 == 700, z == 2100],
 [x_1 == 200, x_2 == 300, z == 1700],
 [x_1 == 200, x_2 == 550, z == 2450],
 [x_1 == 150, x_2 == 650, z == 2550],
 [x_2 == -2*x_1 + 950, z == -2*x_1 + 2850, 150 < x_1, x_1 < 200],
 [x_2 == -1/3*x_1 + 700, z == 3*x_1 + 2100, 0 < x_1, x_1 < 150]]