On Fri, Nov 18, 2011 at 6:27 AM, Aaron Meurer <asmeu...@gmail.com> wrote:
> On Thu, Nov 17, 2011 at 3:16 PM, Joon Lee <jundo...@gmail.com> wrote:
>> I'm trying to solve the 'barnyard' problem with sympy with two
>> equations for 3 variables (it's underconstrained).  I want all
>> possible solutions to be displayed, possibly by iterating
>>
>> There are 20 heads and 56 legs, how many chicken, pigs and spiders are
>> there?
>>
>> chickens = Symbol('chickens')
>> pigs = Symbol('pigs')
>> spiders = Symbol('spiders')
>>
>> solve([Eq(chickens+pigs+spiders, 20), Eq(2*chickens+4*pigs+8*spiders,
>> 56)])
>>
>> this works, but defines chickens and pigs in terms of spiders.

Yes...and those equations will tell you the range that spiders can have:

>>> solve([Eq(chickens+pigs+spiders, 20), Eq(2*chickens+4*pigs+8*spiders,56)])
{chickens: 2*spiders + 12, pigs: -3*spiders + 8}
>>> [solve(k) for k in _.values()]
[[-6], [8/3]]
>>> flatten(_)
[-6, 8/3]

So that is the range that will give positive values for spiders:
greater than -6 and less then 8/3. And as Aaron pointed out, that
range contains 0, 1 and 2 as "integer spiders" that will still make
the other equations work. I don't work with set or interval functions,
but I bet there is a way to turn that range into the set of integers
or perhaps to solve those equations as inequalities. But that's sugar.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to