pythonnumpysympylinear-programmingsimplex

NumPy vs SymPy Row operations different?


I cannot understand for the life of me why a row operation with NumPy just clearly leads to the wrong answer. The correct answer is in the SymPy matrix. Can anyone tell me why NumPy is unable to perform the correct calculation? I'm going crazy. Thank you!

# simplex tableau
import numpy as np
import sympy as sp

#NumPy
simplex = np.array([[2,4,3,1,0,0,0, 400], 
                    [4,1,1,0,1,0,0, 200], 
                    [7,4,4,0,0,1,0, 800], 
                    [-3,-4,-2,0,0,0,1, 0]])
simplex[1,:] = simplex[1,:] - (1/4)*simplex[0,:]
print(simplex)

#SymPy
simplex = sp.Matrix([[2,4,3,1,0,0,0, 400], 
                     [4,1,1,0,1,0,0, 200], 
                     [7,4,4,0,0,1,0, 800], 
                     [-3,-4,-2,0,0,0,1, 0]])
simplex[1,:] = simplex[1,:] - (1/4)*simplex[0,:]
simplex

Numpy:

[[  2   4   3   1   0   0   0 400]
 [  3   0   0   0   1   0   0 100]
 [  7   4   4   0   0   1   0 800]
 [ -3  -4  -2   0   0   0   1   0]]

Sympy:

Matrix([
[  2,  4,    3,     1, 0, 0, 0,   400],
[3.5,  0, 0.25, -0.25, 1, 0, 0, 100.0],
[  7,  4,    4,     0, 0, 1, 0,   800],
[ -3, -4,   -2,     0, 0, 0, 1,     0]])

Solution

  • Your NumPy array has an integer dtype. It literally can't hold floating-point numbers. Give it a floating-point dtype:

    simplex = np.array(..., dtype=float)