简体   繁体   English

为什么数组的价值与预期不同?

[英]Why array have different value than anticipated?

I changed attributes of a class from:我将 class 的属性从:

self.firstBase = np.array([1, 0])
self.secondBase = np.array([0, 1])

to

self.base = np.array([[1, 0], [0, 1]])

Since then operation of multiplying these two vectors by Hadamard matrix returns wrong value.从那时起,将这两个向量乘以 Hadamard 矩阵的操作返回错误值。

def H(self):
    H = np.array([[1 / math.sqrt(2), 1 / math.sqrt(2)],
                  [1 / math.sqrt(2), - 1 / math.sqrt(2)]])

    topValue = H[0][0] * self.base[0][0] + H[0][1] * self.base[0][1]
    bottomValue = H[1][0] * self.base[0][0] + H[1][1] * self.base[0][1]
    self.base[0] = np.array([topValue, bottomValue])

    topValue = H[0][0] * self.base[1][0] + H[0][1] * self.base[1][1]
    bottomValue = H[1][0] * self.base[1][0] + H[1][1] * self.base[1][1]
    self.base[1] = np.array([topValue, bottomValue])

    return self

I did some testing and find out that calculations are still correct but when I initialize我做了一些测试,发现计算仍然正确,但是当我初始化时

 self.base[0] = np.array([topValue, bottomValue])

and

self.base[1] = np.array([topValue, bottomValue])

it gives me [0, 0] and [0, 0] as a result.结果它给了我 [0, 0] 和 [0, 0] 。

Why is that?这是为什么? What is the fix?解决方法是什么?

Change self.base datatype to float self.base数据类型更改为float

self.base = np.array([[1, 0], [0, 1]], dtype=float)

This is happening because you are trying to assign float values to an integer array.发生这种情况是因为您试图将浮点值分配给 integer 数组。 Hence, need to initialize self.base using float values instead as follows:因此,需要使用浮点值来初始化self.base ,如下所示:

  1. use self.base = np.array([1.0, 0.0], [0.0, 1.0])使用self.base = np.array([1.0, 0.0], [0.0, 1.0])
  2. or self.base = np.array([[1, 0], [0, 1]], dtype=float)self.base = np.array([[1, 0], [0, 1]], dtype=float)

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

相关问题 在Python中,为什么最低(最小)浮点值具有与最大值不同的有效数字? - In Python, why does the lowest (smallest) floating point value have different significant-digits than the largest? 为什么subprocess.call()无法按预期运行命令? - why is subprocess.call() not running command as anticipated? 了解为什么Bitcoinlib生成的地址与我拥有的地址不同 - Understand why Bitcoinlib is generating different addresses than what I have 为什么SimpleNamespace的大小与空类的大小不同? - Why does SimpleNamespace have a different size than that of an empty class? 建议使用一个getter返回一个与setter设置不同的值吗? - Is it advisable to have a getter that return a different value than that set by the setter? 为什么Maya返回的值与3ds Max不同? - Why does maya return a different value than 3ds max? 为数组设置新值会得到与应有的结果不同的结果 - Setting a new value to an array gives different results than it should 为什么具有相同值的可变对象在Python中具有不同的ID - why mutable objects having same value have different id in Python 为什么.append()比在预先分配的数组中设置值慢? - Why is .append() slower than setting the value in a pre-allocated array? Python类,在通过print或format调用时具有与分配给value时不同的值 - Python class, have different value when called by print or format than when assigned to value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM