TypeError: 'numpy.complex128' object is not iterable np.arrays basic question does not make sense

Question:

I have problem with something very basic but I am already one hour trying to find out what’s wrong and can’t not, so I am here asking for help.

I have an array _ which is equal to:

array([-0.44723986+0.j        , -0.95965416+0.j        ,
       -0.45242139+0.27410111j, -0.45242139-0.27410111j,
       -0.01292882+0.j        ])

If I run the following list comprehensive:

mapping_1=np.array(
    [
        cmath.sqrt(
            complex_num**2+1
        ) for complex_num in _.ravel()
    ]
)
mapping_1

Everything is ok and I get:

array([1.09545584+0.j        , 1.3859784 +0.j        ,
       1.06911548-0.11599234j, 1.06911548+0.11599234j,
       1.00008357+0.j        ])

But, if I define a function with only that list comprehensive I get a huge error:

Function definiton

def complex_mapping(complex_array,curvature='+',number=1):
    mapping_1=np.array(
        [
            cmath.sqrt(
                complex_num**2+number if curvature=='+' else complex_num**2-number
            ) for complex_num in complex_array
        ]
    )
    return(mapping_1)

code line: complex_mapping(_.ravel())

I get this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[87], line 1
----> 1 complex_mapping(_.ravel())

File D:PythonLibsite-packagesnumpylibfunction_base.py:2328, in vectorize.__call__(self, *args, **kwargs)
   2325     vargs = [args[_i] for _i in inds]
   2326     vargs.extend([kwargs[_n] for _n in names])
-> 2328 return self._vectorize_call(func=func, args=vargs)

File D:PythonLibsite-packagesnumpylibfunction_base.py:2406, in vectorize._vectorize_call(self, func, args)
   2404     res = func()
   2405 else:
-> 2406     ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2408     # Convert args to object arrays first
   2409     inputs = [asanyarray(a, dtype=object) for a in args]

File D:PythonLibsite-packagesnumpylibfunction_base.py:2366, in vectorize._get_ufunc_and_otypes(self, func, args)
   2362     raise ValueError('cannot call `vectorize` on size 0 inputs '
   2363                      'unless `otypes` is set')
   2365 inputs = [arg.flat[0] for arg in args]
-> 2366 outputs = func(*inputs)
   2368 # Performance note: profiling indicates that -- for simple
   2369 # functions at least -- this wrapping can almost double the
   2370 # execution time.
   2371 # Hence we make it optional.
   2372 if self.cache:

File D:PythonLibsite-packagesnumpylibfunction_base.py:2328, in vectorize.__call__(self, *args, **kwargs)
   2325     vargs = [args[_i] for _i in inds]
   2326     vargs.extend([kwargs[_n] for _n in names])
-> 2328 return self._vectorize_call(func=func, args=vargs)

File D:PythonLibsite-packagesnumpylibfunction_base.py:2406, in vectorize._vectorize_call(self, func, args)
   2404     res = func()
   2405 else:
-> 2406     ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2408     # Convert args to object arrays first
   2409     inputs = [asanyarray(a, dtype=object) for a in args]

File D:PythonLibsite-packagesnumpylibfunction_base.py:2366, in vectorize._get_ufunc_and_otypes(self, func, args)
   2362     raise ValueError('cannot call `vectorize` on size 0 inputs '
   2363                      'unless `otypes` is set')
   2365 inputs = [arg.flat[0] for arg in args]
-> 2366 outputs = func(*inputs)
   2368 # Performance note: profiling indicates that -- for simple
   2369 # functions at least -- this wrapping can almost double the
   2370 # execution time.
   2371 # Hence we make it optional.
   2372 if self.cache:

    [... skipping similar frames: vectorize.__call__ at line 2328 (3 times), vectorize._get_ufunc_and_otypes at line 2366 (3 times), vectorize._vectorize_call at line 2406 (3 times)]

File D:PythonLibsite-packagesnumpylibfunction_base.py:2328, in vectorize.__call__(self, *args, **kwargs)
   2325     vargs = [args[_i] for _i in inds]
   2326     vargs.extend([kwargs[_n] for _n in names])
-> 2328 return self._vectorize_call(func=func, args=vargs)

File D:PythonLibsite-packagesnumpylibfunction_base.py:2406, in vectorize._vectorize_call(self, func, args)
   2404     res = func()
   2405 else:
-> 2406     ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2408     # Convert args to object arrays first
   2409     inputs = [asanyarray(a, dtype=object) for a in args]

File D:PythonLibsite-packagesnumpylibfunction_base.py:2366, in vectorize._get_ufunc_and_otypes(self, func, args)
   2362     raise ValueError('cannot call `vectorize` on size 0 inputs '
   2363                      'unless `otypes` is set')
   2365 inputs = [arg.flat[0] for arg in args]
-> 2366 outputs = func(*inputs)
   2368 # Performance note: profiling indicates that -- for simple
   2369 # functions at least -- this wrapping can almost double the
   2370 # execution time.
   2371 # Hence we make it optional.
   2372 if self.cache:

Cell In[5], line 14, in complex_mapping(complex_array, curvature, number)
     12 def complex_mapping(complex_array,curvature='+',number=1):
     13     mapping_1=np.array(
---> 14         [
     15             cmath.sqrt(
     16                 complex_num**2+number if curvature=='+' else complex_num**2-number
     17             ) for complex_num in complex_array
     18         ]
     19     )
     20     return(mapping_1)

TypeError: 'numpy.complex128' object is not iterable

I am expecting to run this function through many arrays, that is why I created the function but it is not working.

Asked By: Alejandro Romero

||

Answers:

The code I wrote it I think the issue might be in in what you are passing is not an array but a single element so wrote a check for the same.

import numpy as np
import cmath

def complex_mapping(complex_array, curvature='+', number=1):
    if isinstance(complex_array, np.ndarray):
        mapping_1 = np.array([cmath.sqrt(complex_num**2 + number if curvature=='+' else complex_num**2 - number) 
                              for complex_num in complex_array])
        return mapping_1
    else:
        raise TypeError("Input must be an array of complex numbers")

complex_array = np.array([-0.44723986+0.j, -0.95965416+0.j, -0.45242139+0.27410111j, -0.45242139-0.27410111j, -0.01292882+0.j])
result = complex_mapping(complex_array)
print(result)

Output:

array([1.09545584+0.j        , 1.38597839+0.j        ,
       1.06911548-0.11599234j, 1.06911548+0.11599234j,
       1.00008357+0.j        ])
Answered By: Arunbh Yashaswi