1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Multidimensional matrix permutation Julia vs. Python disagreement

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 04:22.

  1. Stack

    Stack Membro Participativo

    I have noticed a difference in behavior between python's numpy.permute_dims and julia's Base.permutedims.

    On an input 3x3x3 matrix containing elements 0:26, inclusive in both languages, they agree for the axes argument (1,2,0) but disagree for (0,2,1).

    As far as I can tell from the docs, these functions should be equivalent. There's a note about permutedims being non-recursive, but I don't see why that should have behavioral consequences.

    Julia is also column-major order, but again I don't see why that should effect the overall behavior.

    Python code:

    arr = np.array([[[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]],
    [[9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],
    [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

    arr_perm = np.permute_dims(arr, axes=[0,2,1])


    Output:

    array([[[ 0, 3, 6],
    [ 1, 4, 7],
    [ 2, 5, 8]],

    [[ 9, 12, 15],
    [10, 13, 16],
    [11, 14, 17]],

    [[18, 21, 24],
    [19, 22, 25],
    [20, 23, 26]]])


    Julia code:

    arr = [
    0 1 2
    3 4 5
    6 7 8;;;
    9 10 11
    12 13 14
    15 16 17;;;
    18 19 20
    21 22 23
    24 25 26
    ]

    arr_perm = permutedims(arr, [1,3,2])


    Output:

    3×3×3 Array{Int64, 3}:
    [:, :, 1] =
    0 9 18
    3 12 21
    6 15 24

    [:, :, 2] =
    1 10 19
    4 13 22
    7 16 25

    [:, :, 3] =
    2 11 20
    5 14 23
    8 17 26

    Continue reading...

Compartilhe esta Página