数组和地图输出乘法

时间:2016-12-01 08:41:56

标签: arrays python-3.x dictionary

所以我有这段代码:

# include all functions from the math and numpy library
from math import *
from numpy import *
from functools import reduce

# definition of the function Sc see Table 2 for details
def Sc(Dd, ki, ei, Ci, T=298.15, sigma=0.072, gmax=10, g=1+1e-11, inc=1.01):
    # calculate the A parameter in equation 9
    A = 8.69251e-6*sigma/T
    # returns H(xi) as defined in equation 10
    xi = map(lambda x: x if x < 1 else 1, Ci*(g**3.0 - 1.0)/ei)
    #    xi = Ci*(g**3.0 - 1.0)/ei
    # calculates the dot product of the hygroscopicity and solub. vectors in Eq. 10
    k = dot(ki, ei*xi)
    # defines the function given by equation 9
    S = lambda D, Dd, k, A: (D**3.0-Dd**3.0)/(D**3.0-Dd**3.0*(1.0-k))*exp(A/D)
    # implementation of a pairwise max function; f(2,3) returns 3
    f = lambda x,y: x if x > y else y
    # returns 1 when g > gmax otherwise return the larger value S(g*Dd) or S(g*Dd*inc)
    return 1 if g > gmax else reduce(f,[S(g*Dd,Dd,k,A), \
Sc(Dd,ki,ei,Ci,T=T,sigma=sigma,gmax=gmax,inc=inc,g=g*inc)])

当我使用以下参数执行定义的函数时:

Sc(100e-9, array([0.6,0.2]), array([0.5,0.5]), array([inf,0.1]))

我收到了这个错误:

k = dot(ki, ei*xi)
TypeError: unsupported operand type(s) for *: 'float' and 'map'

我知道这是自我规划,但如何避免呢?有没有办法执行

k = dot(ki, ei*xi)

没有错误?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您必须在Python 3中明确评估map的结果。 例如:

xi = np.array(map(lambda x: x if x < 1 else 1, Ci*(g**3.0 - 1.0)/ei))