admin管理员组文章数量:1406182
I have a numba jitclass with an instance attribute that is a 1d-array of floats, initialized to be zeros (in the MRE as [0.,0.]).
I have a jitted function that creates an instance of said class and changes the first element of this array (in the MRE to 1, such that the array should become [1.,0.]).
This change is not performed if the function also creates another array by copying elements from the attribute array. This happens even if the created array is empty, and even if it is created AFTER the attribute array should be changed.
MRE:
from numba import jit
from numba.core import types
from numba.experimental import jitclass
import numpy as np
@jitclass(spec={'attribute': types.Array(dtype=types.float64,ndim=1,layout='A')})
class TestClass():
def __init__(self):
self.attribute = np.zeros(2)
@jit
def test_func():
test_instance = TestClass()
test_instance.attribute[0] = 1
print(test_instance.attribute)
@jit
def test_func_bugged():
test_instance = TestClass()
test_instance.attribute[0] = 1
print(test_instance.attribute)
print(np.array([test_instance.attribute[1] for _ in range(0)]))
test_func()
test_func_bugged()
Expected printed outcome:
[1. 0.]
[1. 0.]
[]
Actually printed outcome:
[1. 0.]
[0. 0.]
[]
I am on python-3.13.0, numba-0.61.0, and numpy-2.1.3.
Why is this happening?
How can one create arrays from elements of the attribute array without making its elements unchangeable?
本文标签: pythonNumba jitclass instance array element cannot be changed based on later codeStack Overflow
版权声明:本文标题:python - Numba jitclass instance array element cannot be changed based on later code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744962104a2634718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论