Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The best approach I've found so far is to define an eliminate(vars, eqns) procedure (see below) which attempts to remove all the listed vars from the system, so that:

var('x, y, z') eqns = [ x^2 + y^2 == z, x == y] view(eliminate([x], eqns))

gives [2y^2 == z] and solve(eliminate([x], eqns), z) gives me what I was after originally. But I figure I must be missing something obvious.

click to hide/show revision 2
forgot my function definition

The best approach I've found so far is to define an eliminate(vars, eqns) procedure (see below) which attempts to remove all the listed vars from the system, so that:

var('x, y, z') eqns = [ x^2 + y^2 == z, x == y] view(eliminate([x], eqns))

gives [2y^2 == z] and solve(eliminate([x], eqns), z) gives me what I was after originally. But I figure I must be missing something obvious.

def eliminate(vars, eqns): """Eliminate the listed variables from the system of equations""" if type(vars) is not list: vars = [vars] if type(eqns) is not list: eqns = [eqns]

# for each variable to be eliminated, 
# look for an equation that contains it
# and that we can solve uniquely for it
for var in vars:
    soln = None
    for eqn in eqns:
        if var in eqn.args():
            soln = solve(eqn,var)
            if len(soln) == 1: break

    # if we found an equation determining the variable to be eliminated
    # substitute its value throughout, and omit the defining equation     
    if soln:
        val = soln[0].rhs()
        eqns = [e.subs({var : val}) for e in eqns if e is not eqn]

# give back whatever we ended up with
return eqns

The best approach I've found so far is to define an eliminate(vars, eqns) procedure (see below) which attempts to remove all the listed vars from the system, so that:

var('x, y, z') eqns = [ x^2 + y^2 == z, x == y] view(eliminate([x], eqns))

gives [2y^2 == z] and solve(eliminate([x], eqns), z) gives me what I was after originally. But I figure I must be missing something obvious.

def eliminate(vars, eqns):
    """Eliminate the listed variables from the system of equations"""
    if type(vars) is not list: vars = [vars]
    if type(eqns) is not list: eqns = [eqns]

[eqns]

    # for each variable to be eliminated, 
 # look for an equation that contains it
 # and that we can solve uniquely for it
 for var in vars:
     soln = None
     for eqn in eqns:
         if var in eqn.args():
             soln = solve(eqn,var)
             if len(soln) == 1: break

     # if we found an equation determining the variable to be eliminated
     # substitute its value throughout, and omit the defining equation     
     if soln:
         val = soln[0].rhs()
         eqns = [e.subs({var : val}) for e in eqns if e is not eqn]

 # give back whatever we ended up with
 return eqns
click to hide/show revision 4
No.4 Revision

The best approach I've found so far is to define an eliminate(vars, eqns) procedure (see below) which attempts to remove all the listed vars from the system, so that:

var('x, y, z')
eqns = [ x^2 + y^2 == z, x == y]
view(eliminate([x], eqns))

eqns))

gives [2y^2 == z] and solve(eliminate([x], eqns), z) gives me what I was after originally. But I figure I must be missing something obvious.

def eliminate(vars, eqns):
    """Eliminate the listed variables from the system of equations"""
    if type(vars) is not list: vars = [vars]
    if type(eqns) is not list: eqns = [eqns]

    # for each variable to be eliminated, 
    # look for an equation that contains it
    # and that we can solve uniquely for it
    for var in vars:
        soln = None
        for eqn in eqns:
            if var in eqn.args():
                soln = solve(eqn,var)
                if len(soln) == 1: break

        # if we found an equation determining the variable to be eliminated
        # substitute its value throughout, and omit the defining equation     
        if soln:
            val = soln[0].rhs()
            eqns = [e.subs({var : val}) for e in eqns if e is not eqn]

    # give back whatever we ended up with
    return eqns