3

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]])
  • 1
    The numpy `simplex` matrix is an array of integers (check `simplex.dtype` for the exact data type). You can't change the data type of a numpy array in-place, so `simplex[1,:] = simplex[1,:] - (1/4)*simplex[0,:]` casts the values to integers. – Warren Weckesser Oct 14 '18 at 20:15

1 Answers1

2

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)
user2357112
  • 260,549
  • 28
  • 431
  • 505