least squares integer solution search for linear system with numpy / sympy

I need to solve a system of linear diophantine equations with numpy or sympy.

Is there a way to limit the numiple linalg.solve / linalg.lstsq method to return only integer solutions? (maybe not, but I thought I should ask)

I looked at the Sympy Diophantine solution and this does not seem to be applicable for solving whole systems.

The problem I'm working on is something like

P1(X) + P2(Y) = TargetPro F1(X) + F2(Y) = TargetFat C1(X) + C2(Y) = TargetCarb 

In this case, X, Y, Z will represent the approximate serving sizes, and P1 / F1 / C1 will be the pro / fat / carb profile, respectively.

based on this article https://www.math.uwaterloo.ca/~wgilbert/Research/GilbertPathria.pdf

it seems that I could do a line cut to find a link to this system (line-of-line form) and then connect it to a sympy solver.

Is there an easier way to do this?

Here is a trivial example:

 pro = [4,5] fat = [1,2] carb = [3,6] A = np.array((pro, fat, carb)) b = np.array([22,12,21]) print(np.linalg.lstsq(A, b)) 

I expected to get an integer solution [3,2] and instead got [2.16666667, 2.66666667]

Both solutions are correct, but I want to bind my solutions only to integer solutions

+3
source share

Source: https://habr.com/ru/post/1272762/


All Articles