NumPy vs SymPy Row 操作不同?
NumPy vs SymPy Row operations different?
我这辈子都无法理解为什么使用 NumPy 进行的行运算显然会导致错误的答案。正确答案在 SymPy 矩阵中。谁能告诉我为什么 NumPy 无法执行正确的计算?我要疯了。谢谢!
# 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
麻木:
[[ 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]]
同情:
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]])
您的 NumPy 数组具有整数数据类型。它实际上不能容纳浮点数。给它一个浮点数据类型:
simplex = np.array(..., dtype=float)
我这辈子都无法理解为什么使用 NumPy 进行的行运算显然会导致错误的答案。正确答案在 SymPy 矩阵中。谁能告诉我为什么 NumPy 无法执行正确的计算?我要疯了。谢谢!
# 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
麻木:
[[ 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]]
同情:
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]])
您的 NumPy 数组具有整数数据类型。它实际上不能容纳浮点数。给它一个浮点数据类型:
simplex = np.array(..., dtype=float)