testing if a numpy array is symmetric?

后端 未结 2 2057
清歌不尽
清歌不尽 2021-02-13 10:03

Is there a better pythonic way of checking if a ndarray is diagonally symmetric in a particular dimension? i.e for all of x

(arr[:,:,x].T==arr[:,:,x]).all()


        
相关标签:
2条回答
  • 2021-02-13 10:29

    If I understand you correctly, you want to do the check

    all((arr[:,:,x].T==arr[:,:,x]).all() for x in range(arr.shape[2]))
    

    without the Python loop. Here is how to do it:

    (arr.transpose(1, 0, 2) == arr).all()
    
    0 讨论(0)
  • 2021-02-13 10:31

    If your array contains floats (especially if they're the result of a computation), use allclose

    np.allclose(arr.transpose(1, 0, 2), arr)
    

    If some of your values might be NaN, set those to a marker value before the test.

    arr[np.isnan(arr)] = 0
    
    0 讨论(0)
提交回复
热议问题