提高numpy.dot的精度(python) [英] Increasing precision of numpy.dot (python)

查看:110
本文介绍了提高numpy.dot的精度(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟某个物理系统.为了传播解决方案,我需要能够乘以行列式= 1的矩阵,这些矩阵描述了系统的每个部分.在下面的代码中,T(变量)是一个带有det(T)= 1的二维矩阵,我只表示区域号,其余的都不相关.

I'm attempting to simulate a certain physical system. In order to propagate solutions I need to be able to multiply matrices of determinant = 1 which describe each part of the system. In the code below T(variables) is a 2-dimensional matrix with det(T) = 1, i just indicates the region number and the rest is irrelevant.

当我为30多个区域的系统运行此代码时,最终的Msys不再具有行列式=1.在整个计算过程中,我检查了Msys的行列式的值,在前几次迭代中为1,但随后它开始与此不同.我曾尝试在创建数组T时放入dtype = float64,以查看这是否会提高精度并阻止其崩溃,但是我没有看到任何改善.

When I run this code for systems with more than 30 regions, the final Msys no longer has determinant = 1. I checked the value of the determinant of Msys throughout the calculation and it's 1 for the first few iterations but then it starts diverging from that. I've tried putting dtype = float64 when creating the array T to see if this would improve the precision and stop it from breaking down but I saw no improvement.

有什么办法可以编写代码来避免错误的累积,或者可以增加numpy存储的小数位数的数量,从而使该错误对于具有100多个区域的系统而言可以忽略不计.

Is there any way I could write the code to avoid the error accumulating or a way I can increase the amount of decimal places numpy stores so to make the error negligible for systems with 100+ regions.

for i in range(n):                                  
    if i == 0:                                      
        Msys = T(L[i],i,k)
    else:                                           
        Msys = numpy.dot(T(L[i]-L[i-1],i,k), Msys)
return Msys

推荐答案

所有浮点运算的精度有限,并且会累积错误.您需要确定多少精度足够好"或多少错误累积可以忽略".如果float64不够精确,请尝试float128.您可以像下面这样找出浮点类型的精度:

All Floating point operations have limited precision and errors do accumulate. You need to decide how much precision is "good enough" or how much error accumulation is "negligible". If float64 is not precise enough for you, try float128. You can find out the precision of the float types like this:

In [83]: np.finfo(np.float32).eps
Out[83]: 1.1920929e-07

In [84]: np.finfo(np.float64).eps
Out[84]: 2.2204460492503131e-16

In [85]: np.finfo(np.float128).eps
Out[85]: 1.084202172485504434e-19

有关浮点算法的更多信息:什么每个计算机科学家都应该了解浮点算法

Here is a lot more info about floating point arithmetic: What Every Computer Scientist Should Know About Floating-Point Arithmetic

这篇关于提高numpy.dot的精度(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆