【发布时间】:2016-12-01 08:41:55
【问题描述】:
所以我有这个代码:
# 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)
没有错误?
感谢您的帮助!
【问题讨论】:
-
map不会立即执行。你需要:array(list(map(lambda x: x if x < 1 else 1, Ci*(g**3.0 - 1.0)/ei))) -
谢谢! :) 应该可以。
标签: arrays python-3.x dictionary