I'm trying to solve a simple linear inequality in Sympy but can't seem to get it to work.
Basically, I want to be able to calculate the optimal values of a
when the inequality happens to be false.
import sympy
num_attractors = 4
attractor_size = 64
network_size = 256
a, s, n = sympy.symbols('a s n')
expr = a * (s + 16) <= n
if not expr.subs([(a, num_attractors), (s, attractor_size), (n, network_size)]):
# compute optimal value for a i.e., the closest integer a s.t. a <= n / (s + 16)
I have tried using solve(expr, a , sympy.Integer)
and sympy.solvers.inequalities.reduce_inequalities(expr,a)
but can't seem to make sense of their output.
solveset
can be used:
if expr.subs([(a, num_attractors), (s, attractor_size), (n, network_size)]) != True:
asol = solveset(expr.subs([(s, attractor_size), (n, network_size)]),a,S.Integers)
>>> asol
{..., 2, 3}
>>> _.sup
3
You can also just take the floor
of the solution to the equality (and do so symbolically):
>>> ans = floor(solve(expr.lhs - expr.rhs, a)[0]); ans
floor(n/(s + 16))