简体   繁体   中英

Improve a sliced function (numpy)

Slice is a powerful access index for elements of an array available in numpy. This solution is one of the most efficient. The image is initially create with zeros and then filled with those on even lines and then on odd lines, always scan the image every two pixels.

Implement an isccsym function using slice and handle complex arrays. Remember that it is enough to test half the array, since if F(a)==F(-a) , there is no need to compare F(-a) with F(a) again.

def isccsym(F):

    G = np.copy(F)
    G[0,0] = np.conjugate(F[0,0])   
    G[0,1:] = np.conjugate(F[0,:0:-1])
    G[1:,0] = np.conjugate(F[:0:-1,0])
    G[1:,1:] = np.conjugate(F[1:,1:][::-1,::-1])

    if G.dtype == np.complex:
        Gaux = np.sum(([G.imag]))

    return (abs(F-G)< 10E-4).all()

Can someone help me? I need to improve her processing time even more.

It is possible to generate the whole G matrix at once, not building it by parts. That should speed up your operation a bit at least. Further, the calculation of Gaux is not used for anything and could therefore be removed, leading to:

def isccsym(F):
    G = F.conj().T
    return (abs(F-G) < 10E-4).all()

However, this implementation does not use slice.

For a solution that is using slices and that is only checking the relevant elements, as per the given hint, it is possible to do:

def isccsym(F):
    row, col = F.shape
    sym = (row == col)
    while sym and row > 0:
        row -= 1
        sym = (abs(F[row, row:] - F[row:, row].conj()) < 10e-4).all()
    return sym

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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