1

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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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:\Python\Lib\site-packages\numpy\lib\function_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.

5
  • 1
    Please add your input as a complex_array = ... code line to your question so that we can reproduce your results. Even better: write a short but complete Python program, including the import ... lines.
    – pts
    Commented Jan 20, 2024 at 16:46
  • @pts that line is here: "code line: complex_mapping(_.ravel())" (complex_array is a function argument) as shown in the error itself too.
    – CherryDT
    Commented Jan 20, 2024 at 16:54
  • I get the precise message you see when the first argument I pass is a complex number rather than a complex array. Are you sure you're passing it a complex array? Commented Jan 20, 2024 at 16:56
  • 1
    At this point in your code complex_array is a single element, a single complex number, not a numpy array. Somehow you've iterated too deep.
    – hpaulj
    Commented Jan 20, 2024 at 16:56
  • Every time you evaluate an expression at the Python interactive prompt, the result is automatically assigned to the variable _. So it's unlikely that the name still referred to the original array, by the time you had defined this function to operate on it. Commented Jan 23, 2024 at 1:38

1 Answer 1

1

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        ])
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.