2016-12-01 49 views
-1

所以我有這樣的代碼:陣列和地圖輸出乘法

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

沒有錯誤?

感謝您的幫助!

+0

'map'。你需要:array(list(map(lambda x:x if x <1 else 1,Ci *(g ** 3.0 - 1.0)/ ei)))' – Goodies

+0

Thanks! :)它的工作原理應該如此。 – Jakub

回答

1

你必須在Python 3 明確評估map結果例如:不立即執行

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