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]])
4

1 回答 1

2

您的 NumPy 数组具有整数 dtype。它实际上不能保存浮点数。给它一个浮点数据类型:

simplex = np.array(..., dtype=float)
于 2018-10-14T20:15:11.280 回答