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.
![]() | 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
![]() | 3 | No.3 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))
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
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
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.