且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

长度为 1 的数组可以转换为 python 标量错误吗?Python

更新时间:2022-01-03 18:28:51

你先导入numpy.cos,然后再导入math.cos.后者掩盖了前者,并且不知道如何处理 NumPy 数组.因此出现错误.

You first import numpy.cos, and then import math.cos. The latter shadows the former, and doesn't know how to handle NumPy arrays. Hence the error.

要修复,请尝试:

import numpy

def CosineMap(a,x):
    return a*numpy.cos(x/(2.*pi))

这类问题是避免 from X import * 样式导入的一个很好的理由.

Problems of this sort are a good reason to avoid from X import *-style imports.

对于 TentMap,这里是一种正确矢量化的方法:

As to TentMap, here is one way to vectorize it correctly:

def TentMap(a,x):
    return 2.*a*numpy.minimum(x, 1.-x)