English 中文(简体)
第32/64号轨道机器漂浮式矩阵表不正确?
原标题:Python 32/64-bit machine float summing of transposed matrix not correct?

首先,Im不是一种数学色彩,因此大量精度很少被过滤到我的日常工作中。 请说:

采用NumPy生成一个具有从1:

>>> m = numpy.matrix([(1.0 / 1000) for x in xrange(1000)]).T
>>> m
matrix[[ 0.001 ],
       [ 0.001 ],
       ...
       [ 0.001 ]])

在64轨Windows with 2.6, 传闻很少奏到1.0. math.fsum()与这一矩阵相吻合,如果我改变矩阵以使用较少的数字,则数字不变。

>>> numpy.sum(m)
1.0000000000000007
>>> math.fsum(m)
1.0
>>> sum(m)
matrix([[ 1.]])
>>> float(sum(m))
1.0000000000000007

On 32-bit Linux (Ubuntu) with Python 2.6, summing always works out to 1.0.

>>> numpy.sum(m)
1.0
>>> math.fsum(m)
1.0
>>> sum(m)
matrix([[ 1.]])
>>> float(sum(m))
1.0000000000000007

在评估矩阵表等于1时(如:化石和lt;总和(m)与lt;+epsilon)时,我可以补充一下我的代码,但我想首先理解,差异的原因是什么是沙捞越的,如果能够以更好的方式正确确定数额。

我的理解是,这笔钱正在处理数量(floats)的机组代表,其方式与重新显示的方式不同,在计算总和时,使用内部沉积。 如何看待我用来计算其数额的3种方法,这并不清楚为什么这些方法重复了所有不同的,或者平台之间的相同。

正确计算矩阵总和的最佳方式是什么?

如果你重新寻找一个更令人感兴趣的矩阵,这一简单变化将可减少矩阵数量:

>>> m = numpy.matrix([(1.0 / 999) for x in xrange(999)]).T

事先得到任何帮助!

Update I think I figured something out. If I correct the value being stored to a 32-bit float the results match the 32-bit Linux sum ing.

>>> m = numpy.matrix([(numpy.float32(1.0) / 1000) for x in xrange(1000)]).T
>>> m
matrix[[ 0.001 ],
       [ 0.001 ],
       ...
       [ 0.001 ]])
>>> numpy.sum(m)
1.0

这将确定矩阵机号,以代表32个轨道浮标,而不是我的视窗测试上的64个轨道,并且将正确地加以汇总。 为什么在32个轨道和64个轨道系统中的0.001号浮标不等于机器号? 如果我试图用大量精彩的地方储存极少数人,我会期望他们会有所不同。

没有人对此有任何想法? 我是否应该明确转向32个轨道浮动,或者是否有64个轨道总和的方法? 还是我回过来,增加了一根sil子? 如果我听起来,我会有兴趣发表意见。 感谢!

最佳回答

这是因为,正如你已经发现的那样,你将32个轨道浮动区与64个轨道浮动区进行了比较。

如果你在两台机器上注明了32轨或64轨制,你就会看到同样的结果。

假设式浮动点(假设阵列的数字类型)与机器精确度相同。 因此,你重新看到不同机器的不同结果。

E.g. The 32-bit version:

m = numpy.ones(1000, dtype=numpy.float32) / 1000
print repr(m.sum())

缩略语

m = numpy.ones(1000, dtype=numpy.float64) / 1000
print repr(m.sum())

由于精度不同,结果有所不同,但你看到不同机器的相同结果。 (然而,64个轨道运行将S在32台轨道机上放慢)

如果你仅具体说明numpy.float,这要么是float32,要么是float64<<<>>>,视机器的本地建筑而定。

问题回答

I d 说最准确的方式(不是最有效率的)是使用decimal模块:

>>> from decimal import Decimal
>>> m = numpy.matrix([(Decimal(1) / 1000) for x in xrange(1000)])
>>> numpy.sum(m)
Decimal( 1.000 )
>>> numpy.sum(m) == 1.0
True

首先,如果你使用 n来储存价值,那么如果提供,你就应当使用 n素方法与阵列/矩阵合作。 也就是说,如果你们想要信任那些把 n放在一起的极其有能力的人。

Now, the 64-bit answer of numpy s sum() can not sum up to exactly 1 for the reasons how floating point numbers are being handled in computers (murgatroid99 provided you with a link, there are hundred s more out there). Therefore, the only safe way, (and even very helpful in understanding your mathematical treatment of your code much better, and therefore your problem per se) is to use an epsilon value to cut off at a certain precision.

为什么我认为这是有益的? 由于计算科学需要像实验科学那样处理错误,并且故意处理(模拟:确定错误)的错误,因此,你在处理你的计算错误方面已经迈出了第一步。

因此,也许还有其他处理方法,但大多数时间,我将使用sil,确定我对某一问题所要求的精确性。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...