Get all positive integral solutions for a linear equation

SymPy can solve Diophantine equations but doesn’t have a built-in way to generate positive solutions. With Sage one can do that easily: here is four-line code that generates all nonnegative integer solutions of your equation. p = MixedIntegerLinearProgram() w = p.new_variable(integer=True, nonnegative=True) p.add_constraint(411*w[0] + 295*w[1] + 161*w[2] == 3200) p.polyhedron().integral_points() The output is ((4, 2, … Read more

Prevent Sympy from rearranging the equation

Currently, there is no way in SymPy to print things exactly as they are entered, because that information isn’t even saved anywhere. I believe in a multiplication, symbols are ordered alphabetically, with capital letters coming before lowercase letters (basically, the order from ord). The best trick I can come up with is to use the … Read more

How to extract all coefficients in sympy

all_coeffs() can be sometime better than using coeffs() for a Poly. The difference lies in output of these both. coeffs() returns a list containing all coefficients which has a value and ignores those whose coefficient is 0 whereas all_coeffs() returns all coefficients including those whose coefficient is zero. >>> a = Poly(x**3 + a*x**2 – … Read more