TypeError:'numpy.complex128'对象不可迭代 np.arrays 基本问题没有意义

问题描述 投票:0回答:1

我对一些非常基本的问题有疑问,但我已经花了一个小时试图找出问题所在,而且不能不,所以我在这里寻求帮助。

我有一个数组

_
,它等于:

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

如果我综合运行以下列表:

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

一切都很好,我得到:

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

但是,如果我定义一个仅包含该列表的函数,则会出现一个巨大的错误:

功能定义

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)

代码行:

complex_mapping(_.ravel())

我明白了:

---------------------------------------------------------------------------
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

我希望通过许多数组运行此函数,这就是我创建该函数但它不起作用的原因。

python arrays function jupyter complex-numbers
1个回答
0
投票

我编写的代码我认为问题可能在于您传递的不是数组而是单个元素,因此编写了相同的检查。

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)

输出:

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

希望这能解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.