Reputation: 1653
Assume that I have set of equations. I would like to find possible positive integer solutions (in the current example it is demonstrated with the variable of "result") if there is any throughout the final equation. Any suggestions here ? Thanks
import sympy as sym
b, g, f, t, go, sa, result = sym.symbols('b g f t go sa result', integer=True, positive=True)
eq1 = sym.Eq(2*b + g, 70)
eq2 = sym.Eq(go + g + b + sa, 59)
eq3 = sym.Eq(2*f + t + go, 95)
eq4 = sym.Eq(t + sa + (f + go)*go, result)
solution = sym.solve((eq1, eq2, eq3, eq4), (b, g, f, t, go, sa, result), dict=True)
#print(sym.nsolve((eq1, eq2, eq3), (b, g, f, t, go, sa)))
print(solution[0])
print("Possible positive integer results (if there is):")
#print(solution[0][result])
print("Program completed...")
Upvotes: 0
Views: 1192
Reputation: 14500
I'm not sure I understand your question but...
You can solve the first 3 equations for 3 of the unknowns and eliminate them from the 4th equation like
In [60]: s = solve([eq1, eq2, eq3], [b, g, t])
In [61]: s
Out[61]: {b: go + sa + 11, g: -2⋅go - 2⋅sa + 48, t: -2⋅f - go + 95}
In [62]: eq4_subs = eq4.subs(s)
In [63]: eq4_subs
Out[63]: -2⋅f + go⋅(f + go) - go + sa + 95 = result
So b, g and t are always integers if the other variables are.
Ideally you would be able to solve that with diophantine
except:
In [64]: diophantine(eq4_subs)
---------------------------------------------------------------------------
...
NotImplementedError: No solver has been written for inhomogeneous_general_quadratic.
It looks like the solver for this case has not been implemented in sympy. Special cases of this are but the general case is not so let's try special cases...
Only the difference result - sa
is meaningful so we'll set sa to zero and try values for result:
In [65]: for r in range(200):
...: print('result =', r, 'go, f =', diophantine(eq4_subs.subs(sa, 0).subs(result, r), syms=(go, f)))
...:
result = 0 go, f = {(1, 95)}
result = 1 go, f = {(1, 94)}
result = 2 go, f = {(1, 93)}
result = 3 go, f = {(1, 92)}
result = 4 go, f = {(1, 91)}
...
result = 94 go, f = {(1, 1)}
result = 95 go, f = {(0, 0)}
result = 96 go, f = set()
result = 97 go, f = {(-t_0 - 1, t_0), (2, n1)}
result = 98 go, f = set()
result = 99 go, f = set()
result = 100 go, f = set()
result = 101 go, f = set()
result = 102 go, f = {(3, 1)}
result = 103 go, f = {(3, 2)}
...
result = 121 go, f = {(3, 20), (4, 7), (5, 2)}
result = 122 go, f = {(3, 21)}
result = 123 go, f = {(4, 8), (3, 22)}
result = 124 go, f = {(3, 23), (5, 3)}
result = 125 go, f = {(4, 9), (3, 24)}
...
result = 193 go, f = {(8, 7), (10, 1), (6, 17), (5, 26), (4, 43), (3, 92)}
result = 194 go, f = {(3, 93)}
result = 195 go, f = {(4, 44), (3, 94), (9, 4)}
result = 196 go, f = {(5, 27), (3, 95)}
result = 197 go, f = {(6, 18), (4, 45), (7, 12), (3, 96)}
result = 198 go, f = {(3, 97)}
result = 199 go, f = {(4, 46), (8, 8), (3, 98), (5, 28)}
I'm not sure how to express this in general but this suggests that there is:
result <= 95
.result >= 102
.result = 97
.result in {96, 98, 99, 100, 101}
.Upvotes: 1