Python 中矩阵逆元素求和中的虚假值
Spurious values in summation of inverse elements of a matrix in Python
我有一个矩阵,R2
。我总结了每一行中非零元素的倒数。但是,如电流输出所示,会出现虚假值(第 1 个和第 5 个值)。我怎样才能避免这种情况?
R2=array([[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[1.11453512e+10, 0.00000000e+00, 2.82017381e+11, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[1.11453512e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 1.87125503e+09, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 2.82017381e+11, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 1.87125503e+09, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 1.20130268e+10, 3.53068596e+10, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 5.22617991e+08],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 5.22617991e+08],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 1.20130268e+10, 3.53068596e+10, 0.00000000e+00]])
R3=np.divide(1, R2, where=R2 != 0).sum(1)
当前输出为
array([8.94213159e+130, 1.20671688e-010, 2.76413345e-010, 6.51526508e-010,
7.71108578e+140, 3.69806090e-011, 1.77173260e-010, 5.67835429e-010,
3.58863402e-010, 1.91947593e-009, 1.91947593e-009, 5.22617991e+008])
为此,您可以通过两种方式做到这一点,一种是在您的 np.devide
中使用 out=R2
作为:
np.divide(1, R2, where=R2!=0, out=R2).sum(1)
# R2:
# [2.0628927806128987e-10 1.2067168750757737e-10 2.7641334517657591e-10
# 6.5152650788289563e-10 4.5358660476653588e-10 3.6980608990547905e-11
# 1.7717325958995994e-10 5.6783542936586616e-10 3.5886340231599305e-10
# 1.9194759328959448e-09 1.9194759328959448e-09 1.1156607561074707e-10]
或将 np.devide
结果放入另一个名为 R3
的变量中,然后将其求和为:
R3 = np.divide(1, R2, where=R2!=0)
result = R3.sum(1)
我有一个矩阵,R2
。我总结了每一行中非零元素的倒数。但是,如电流输出所示,会出现虚假值(第 1 个和第 5 个值)。我怎样才能避免这种情况?
R2=array([[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[1.11453512e+10, 0.00000000e+00, 2.82017381e+11, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[1.11453512e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 1.87125503e+09, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 1.86647223e+10, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 2.82017381e+11, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 6.54826235e+09,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
3.64932878e+10, 0.00000000e+00, 1.87125503e+09, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 4.48759847e+09, 0.00000000e+00, 4.08814184e+10,
0.00000000e+00, 1.20130268e+10, 3.53068596e+10, 0.00000000e+00],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 5.22617991e+08],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.65770709e+11, 0.00000000e+00, 0.00000000e+00, 5.22617991e+08],
[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 1.20130268e+10, 3.53068596e+10, 0.00000000e+00]])
R3=np.divide(1, R2, where=R2 != 0).sum(1)
当前输出为
array([8.94213159e+130, 1.20671688e-010, 2.76413345e-010, 6.51526508e-010,
7.71108578e+140, 3.69806090e-011, 1.77173260e-010, 5.67835429e-010,
3.58863402e-010, 1.91947593e-009, 1.91947593e-009, 5.22617991e+008])
为此,您可以通过两种方式做到这一点,一种是在您的 np.devide
中使用 out=R2
作为:
np.divide(1, R2, where=R2!=0, out=R2).sum(1)
# R2:
# [2.0628927806128987e-10 1.2067168750757737e-10 2.7641334517657591e-10
# 6.5152650788289563e-10 4.5358660476653588e-10 3.6980608990547905e-11
# 1.7717325958995994e-10 5.6783542936586616e-10 3.5886340231599305e-10
# 1.9194759328959448e-09 1.9194759328959448e-09 1.1156607561074707e-10]
或将 np.devide
结果放入另一个名为 R3
的变量中,然后将其求和为:
R3 = np.divide(1, R2, where=R2!=0)
result = R3.sum(1)