简体   繁体   English

循环中添加矩阵的次数不能超过20次

[英]Cannot add matrices more than 20 times in loop

I'm trying to create a sum of 2x2 matrices in a for loop, but when I loop the summation more than 21 times (when my n > 20, as shown below) it gives me the following error message: 我试图在for循环中创建2x2矩阵的总和,但是当我将总和循环超过21次(当我的n> 20时,如下所示)时,它会给我以下错误消息:

TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output parameter (typecode 'd') according to the casting rule ''same_kind'' TypeError:根据转换规则``same_kind'',ufunc'add'输出(typecode'O')不能被强制为提供的输出参数(typecode'd')

This is my code: 这是我的代码:

k = 2
n = 21

A2 = np.matrix('0.5 -0.5; 0.5 0.5')
SumA2 = np.zeros((k,k))

for i in range(0, n+1):
    SumA2 += np.linalg.matrix_power(A2, i)/np.math.factorial(i)

print(A2)
print("\n", SumA2)

I'm suspecting it has something to do with the factorial becoming too big, but should that really be an issue? 我怀疑这与阶乘过大有关,但是这真的是一个问题吗? In Matlab I can loop it 1000 times without issue. 在Matlab中,我可以循环1000次而不会出现问题。

At 21, it switches the array type to object: 在21,它将数组类型切换为object:

In [776]: np.linalg.matrix_power(A2,20)/np.math.factorial(20)
Out[776]: 
matrix([[-4.01398205e-22,  0.00000000e+00],
        [ 0.00000000e+00, -4.01398205e-22]])
In [777]: np.linalg.matrix_power(A2,21)/np.math.factorial(21)
Out[777]: 
matrix([[-9.557100128609015e-24, 9.557100128609015e-24],
        [-9.557100128609015e-24, -9.557100128609015e-24]], dtype=object)

more specifically, it's the factorial that's switched: 更具体地说,它是转换的factorial

In [778]: np.array(np.math.factorial(20))
Out[778]: array(2432902008176640000)
In [779]: np.array(np.math.factorial(21))
Out[779]: array(51090942171709440000, dtype=object)

Python3 is using integers for the factorial . Python3将整数用于factorial Those can be any length. 这些可以是任何长度。 But at this point the value becomes too large to represent with np.int64 . 但是在这一点上,该值变得太大而无法用np.int64表示。 So it switches to using an object dtype array that holds the long Python integer. 因此,它切换为使用包含长Python整数的对象dtype数组。 That switch propagates to the power calculation. 该开关传播到power计算。

The error arises when it tries to convert this array to a dtype compatible with SumA2 . 当它尝试将此数组转换为与SumA2兼容的SumA2时,将出现错误。

In [782]: SumA2 = np.zeros((k,k))
In [783]: SumA2 += Out[777]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-783-53cbd27f9514> in <module>()
----> 1 SumA2 += Out[777]

TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output parameter (typecode 'd') according to the casting rule ''same_kind''
In [784]: SumA2 = np.zeros((k,k), object)
In [785]: SumA2 += Out[777]
In [786]: SumA2
Out[786]: 
array([[-9.557100128609015e-24, 9.557100128609015e-24],
       [-9.557100128609015e-24, -9.557100128609015e-24]], dtype=object)

At 170 it starts having problems converting integer to float 在170,它开始遇到将整数转换为浮点数的问题

Do a 1/factorial(...) first seems to help. 首先做1/factorial(...)似乎有帮助。 And changing the dtype of A2 to a higher precision float may help: 并将A2的dtype更改为更高精度的float可能会有所帮助:

In [812]: np.linalg.matrix_power(A2.astype('float128'),171)*(1/np.math.factorial(171))
Out[812]: 
matrix([[-1.04145922e-335, -1.04145922e-335],
        [ 1.04145922e-335, -1.04145922e-335]], dtype=float128)

With a 2x2 matrix, this really doesn't make special use of numpy . 对于2x2矩阵,这实际上并没有特别使用numpy The repeated power could almost as easily be calculated with lists and 'raw' Python numbers. 使用列表和“原始” Python数字几乎可以很容易地计算出重复功效。 But even those aren't designed for infinite precision math. 但是,即使那些不是为无限精度数学而设计的。 Integers can be long, but I don't think Python floats are that flexible. 整数可能很长,但我认为Python浮点数没有那么灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 While Not Loop 运行次数超出预期 - While Not Loop running more times than intended 将“nan”添加到 numpy 数组 20 次而不循环 - Add “nan” to numpy array 20 times without loop 无法在 selenium chromedriver 上添加超过 20 个扩展 - Unable to add more than 20 extensions on selenium chromedriver Python:如何使用for循环执行的次数比文件中包含项目的次数多 - Python: How to loop using a for loop more times than there are items in the file 循环使用100多个不同的正则表达式时,Python re模块变得慢20倍 - Python re module becomes 20 times slower when looping on more than 100 different regex Python,使用超过 20 次 mock.patch.object() 和太多静态嵌套块错误 - Python, using more than 20 times mock.patch.object() and too many statically nested blocks error 为什么我的for循环运行的次数超过我指定的次数? - Why does my for loop run more times than I specify? asio无法将消息写入服务器两次以上 - Cannot write the message to the server more than two times by asio 正则表达式:一个字符在一个序列中不能出现超过 4 次 - Regular expressions: a character cannot show more than 4 times in a sequence Python - 更有效的遍历矩阵的方法 - Python - More efficient way to loop through matrices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM