简体   繁体   English

将不同的 if 语句应用于 numpy 数组的每个元素时出错

[英]Error in applying different if statements to each element of a numpy array

Good morning, I have a simple question about applying a different if statement to every element of a numpy array.早上好,我有一个简单的问题,关于对 numpy 数组的每个元素应用不同的 if 语句。 I have written a function that take as input a numpy array made of 12 elements, checks if the element is 0 or 1 and, if it's 1, acts on another array.我写了一个 function ,它以一个由 12 个元素组成的 numpy 数组作为输入,检查元素是 0 还是 1,如果是 1,则作用于另一个数组。 The function is the following: function如下:

def symmetry_test(determinant):
    print(determinant)
    ag=np.array([1,1,1,1])
    bg=np.array([1,-1,-1,1])
    au=np.array([1,1,-1,-1])
    bu=np.array([1,-1,1,-1])
    representations=np.zeros((4,12))
    print(determinant[0])
    if int(determinant[0])==1:
       representations[:,0]=au
   
    print(determinant[1])
    if int(determinant[1])==1:
       representations[:,1]=au
   
    print(determinant[2])
    if determinant[2]==1:
       representations[:,2]=ag

    print(determinant[3])
    if determinant[3]==1:
       representations[:,3]=ag
  
    if determinant[4]==1:
       representations[:,4]=bg

    if determinant[5]==1:
       representations[:,5]=bg
 
    if determinant[6]==1:
       representations[:,6]=ag
   
    if determinant[7]==1:
       representations[:,7]=ag
 
    if determinant[8]==1:
       representations[:,1]=bu

    if determinant[9]==1:
       representations[:,9]=bu
 
    if determinant[10]==1:
       representations[:,10]=au
  
    if determinant[11]==1:
       representations[:,11]=au

    idx = np.argwhere(np.all(representations[..., :] == 0, axis=0))
    representations = np.delete(representations, idx, axis=1)
    return representations

The function takes determinant as input, which is a numpy array, generates an array called representations and fills it. function 将determinant作为输入,它是一个 numpy 数组,生成一个称为representations的数组并填充它。 I put print(determinant[0]) and int(determinant[0]) in the definition to check if the function reads the array properly.我将print(determinant[0])int(determinant[0])放在定义中以检查 function 是否正确读取数组。 The problem is the following: if I give as input an array defined as test=np.array([1,1,1,1,1,1,0,0,0,0,0,0]) the function works fine and returns and array like问题如下:如果我将定义为test=np.array([1,1,1,1,1,1,0,0,0,0,0,0])的数组作为输入,则 function 有效很好,返回和数组一样

 1  1  1  1  1  1
 1  1  1  1 -1 -1
-1 -1  1  1  1  1
-1 -1  1  1 -1 -1

which is exactly what I want.这正是我想要的。 Now, if I give to the function the array test=np.array([1,1,1,1,0,0,0,0,1,1,0,0]) and use it as a=symmetry_test(test) ,the output is现在,如果我给 function 数组test=np.array([1,1,1,1,0,0,0,0,1,1,0,0])并将其用作a=symmetry_test(test) ,output 是

 1  1  1  1  1
 1 -1  1  1 -1
-1  1  1  1  1
-1 -1  1  1 -1

(yes, it only has 5 columns) instead of (是的,它只有 5 列)而不是

 1  1  1  1  1  1
 1  1  1  1 -1 -1
-1 -1  1  1 -1 -1
-1 -1  1  1  1  1

Honestly I have no idea of the reason why it doesn't work and what puzzles me the most is the fact that for one array it works and for another it fails completely.老实说,我不知道它为什么不起作用的原因,最让我困惑的是,对于一个阵列它可以工作,而对于另一个阵列它完全失败了。 I tried to punt the else condition我试图取消其他条件

else:
    representations[:,0]=np.zeros(4)

after each if statement without success;在每个 if 语句之后没有成功; I also tried to put determinant=np.asarray(determinant) at the beginning of the function but, also in this case, it didn't solve the problem.我还尝试将determinant=np.asarray(determinant)放在 function 的开头,但是在这种情况下,它也没有解决问题。 Any suggestion will be greatly appreciated.任何建议将不胜感激。 Thanks in advance and sorry for the easy question.在此先感谢,很抱歉这个简单的问题。

It's a bug in your code.这是您的代码中的错误。

if determinant[8] == 1:
    representations[:, 1] = bu

Should be应该

if determinant[8] == 1:
    representations[:, 8] = bu

And if you want a more concise way of implementing that function, consider this -如果您想要一种更简洁的方式来实现 function,请考虑这一点 -

def symmetry_test(determinant):
    
    ag = np.array([1, 1, 1, 1])
    bg = np.array([1, -1, -1, 1])
    au = np.array([1, 1, -1, -1])
    bu = np.array([1, -1, 1, -1])

    representations = np.array([au, au, ag, ag, bg, bg, ag, ag, bu, bu, au, au])

    determinant = np.array(determinant, dtype=np.bool)

    return representations[determinant]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM