简体   繁体   English

确定两个复数是否相等

[英]Determine whether two complex numbers are equal

The following code causes the print statements to be executed: 以下代码将导致执行打印语句:

import numpy as np
import math

foo = np.array([1/math.sqrt(2), 1/math.sqrt(2)], dtype=np.complex_)

total = complex(0, 0)
one = complex(1, 0)
for f in foo:
   total = total + pow(np.abs(f), 2)
   if(total != one):
      print str(total) + " vs " + str(one)
      print "NOT EQUAL"

However, my input of [1/math.sqrt(2), 1/math.sqrt(2)] results in the total being one : 但是,我的输入[1/math.sqrt(2), 1/math.sqrt(2)]导致totalone

(1+0j) vs (1+0j) NOT EQUAL

Is it something to do with mixing NumPy with Python's complex type? 与将NumPy与Python的复杂类型混合在一起有关系吗?

When using floating point numbers it is important to keep in mind that working with these numbers is never accurate and thus computations are every time subject to rounding errors. 使用浮点数时,请务必牢记,使用这些数字永远都不准确,因此每次都会出现舍入误差。 This is caused by the design of floating point arithmetic and currently the most practicable way to do high arbitrary precision mathematics on computers with limited resources. 这是浮点算法的设计引起的,并且是目前在资源有限的计算机上进行高精度任意数学运算的最可行方法。 You can't compute exactly using floats (means you have practically no alternative), as your numbers have to be cut off somewhere to fit in a reasonable amount of memory (in most cases at maximum 64 bits), this cut-off is done by rounding it (see below for an example). 您无法使用浮点数精确地计算(这意味着您几乎没有其他选择),因为必须将数字切掉一定的位置才能容纳合理的内存量(大多数情况下,最大为64位),因此已完成通过四舍五入(请参见下面的示例)。

To deal correctly with these shortcomings you should never compare to floats for equality, but for closeness. 为了正确处理这些缺点,您永远不应该将其与浮点数相提并论,而要保持亲密关系。 Numpy provides 2 functions for that: np.isclose for comparison of single values (or a item-wise comparison for arrays) and np.allclose for whole arrays. Numpy为此提供了2个函数: np.isclose用于单个值的比较(或数组的np.allclose比较)和np.allclose用于整个数组。 The latter is a np.all(np.isclose(a, b)) , so you get a single value for an array. 后者是np.all(np.isclose(a, b)) ,因此您将获得数组的单个值。

>>> np.isclose(np.float32('1.000001'), np.float32('0.999999'))
True

But sometimes the rounding is very practicable and matches with our analytical expectation, see for example: 但是有时四舍五入非常可行,并且符合我们的分析预期,例如:

>>> np.float(1) == np.square(np.sqrt(1))
True

After squaring the value will be reduced in size to fit in the given memory, so in this case it's rounded to what we would expect. 平方后,值的大小将减小以适合给定的内存,因此在这种情况下,它会四舍五入到我们期望的值。

These two functions have built-in absolute and relative tolerances (you can also give then as parameter) that are use to compare two values. 这两个函数具有内置的绝对和相对公差(您也可以将其作为参数),用于比较两个值。 By default they are rtol=1e-05 and atol=1e-08 . 默认情况下,它们是rtol=1e-05atol=1e-08


Also, don't mix different packages with their types. 另外,请勿将不同的包装与其类型混合使用。 If you use Numpy, use Numpy-Types and Numpy-Functions. 如果您使用Numpy,请使用Numpy-Types和Numpy-Functions。 This will also reduce your rounding errors. 这也将减少您的舍入错误。

Btw: Rounding errors have even more impact when working with numbers which differ in their exponent widely. 顺便说一句:当使用指数差异很大的数字时,舍入误差的影响更大。

I guess, the same considerations as for real numbers are applicable: never assume they can be equal, but rather close enough: 我猜想,与实数相同的考虑也适用:永远不要假设它们可以相等,但是要足够接近:

eps = 0.000001
if abs(a - b) < eps:
    print "Equal"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM