2014-01-23 41 views
1

我有一个数组表示“x”的值为0到9的100个值。我必须在用于查找组件“sigma”的公式中使用这个“x”。但有三个条件查找 “西格马”,它们是,而X < 1,标准差=式,而X = 1,标准差=式而X> 1,标准差=式数组中的条件,Python

我需要编写一个Python代码,将所有值x < 1并将其应用于“sigma”,并将所有值x = 1等等应用到“sigma”以获得“sigma”(an数组长度相同,即100个数值

我该怎么办?

我尝试到目前为止是:

a1 = (2*delta_c*rho_0*r_s)/(x**2-1) 
b1 = (2/(np.sqrt(1-x**2))) 
c1 = np.arctanh(np.sqrt((1-x)/(1+x))) 

a2 = (2*delta_c*rho_0*r_s)/(1-x**2) 
b2 = 2/(np.sqrt(x**2-1)) 
c2 = np.arctan(np.sqrt((x-1)/(1+x))) 


#finding sigma(x) 

for i in x: 
    if i<1: 
     sigma = a1*(1-(b1*c1)) 
    elif i == 1: 
     sigma = (2*delta_c*rho_0*r_s)/3 
    elif i>1: 
     sigma = a2*(1-(b2*c2)) 

的问题是,如果你可以在我的公式中看到A1,B1,C1和A2,B2,C2,有X参与。所以,我需要应用条件X这里还有

+0

这是蟒蛇.. **列表**。另外,你能告诉我们你迄今为止的尝试吗?输入和输出? – aIKid

+1

你可以用一个例子来解释你的问题吗?或者这个问题可能因为不清楚而关闭。 – thefourtheye

+0

@thefourtheye我编辑了我的问题 – ThePredator

回答

2

你可以用布尔项目分配(numpy的阵列)做到这一点很容易:

sigma = np.empty(x.shape) 
sigma[x<1] = a1*(1-(b1*c1)) 
sigma[x==1] = (2*delta_c*rho_0*r_s)/3 
sigma[x>1] = a2*(1-(b2*c2)) 
+1

+1使用numpy。 – aIKid

+0

另一种习惯用法是'sigma = np.empty_like(x)' – heltonbiker

+0

@heltonbiker - 啊,是的,我从来没有记得那一个;-),尽管如果原始数组是整数,*可能会给出错误的类型。 – mgilson

1

如何:

sigma = x 
for i, xi in enumerate(x): 
    a1 = (2*delta_c*rho_0*r_s)/(xi**2-1) 
    b1 = (2/(np.sqrt(1-xi**2))) 
    c1 = np.arctanh(np.sqrt((1-xi)/(1+xi))) 

    a2 = (2*delta_c*rho_0*r_s)/(1-xi**2) 
    b2 = 2/(np.sqrt(xi**2-1)) 
    c2 = np.arctan(np.sqrt((xi-1)/(1+xi))) 

    if xi<1: 
     sigma[i] = a1*(1-(b1*c1)) 
    elif xi == 1: 
     sigma[i] = (2*delta_c*rho_0*r_s)/3 
    elif xi>1: 
     sigma[i] = a2*(1-(b2*c2)) 
+0

问题是,如果你可以在我的公式中看到a1,b1和c1,就有x。所以我需要在这里应用x的条件以及 – ThePredator

+0

因此,您要以n x 3数组开始,并且您想要以另一个n x 3数组结束? –

+0

不,我只是将公式拆分为a1,b1和c1以方便使用。如果你可以看到西格玛的最终公式,根据x的条件,它只是a1 *(1-(b1 * c1)),a2 *(1-(b2 * c2)) – ThePredator