0

How to modify different parts of a numpy array of complex numbers in parallel using python? This question seems to give an answer for numpy array with real coefficients: Is shared readonly data copied to different processes for Python multiprocessing? , but not for complex coefficients.

Community
  • 1
  • 1
lagoa
  • 119
  • 3

1 Answers1

1

You need to view the array containing twice the number of floats floats as complex numbers:

>>> shared_array_base = multiprocessing.Array(ctypes.c_double, 3*3*2)
>>> shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
>>> shared_array = shared_array.view(np.complex128).reshape(3, 3)

The complex number format is [re0, im0, re1, im1, re2, im2, ...]:

>>> shared_array[1,1] = 1+2j
>>> shared_array.base
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  2.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.])
>>> shared_array.base.base
<multiprocessing.sharedctypes.c_double_Array_18 object at 0x7f7c1b5d1f80>
pv.
  • 33,875
  • 8
  • 55
  • 49