Skip to content

Possibly undocumented behavior of indexing assignment to arrays #40018

Open

Description

This is based on a post on the forum.

The documentation for indexing assignment says:

If any index I_k selects more than one location, then the right hand side X must be an array with the same shape as the result of indexing A[I_1, I_2, ..., I_n] or a vector with the same number of elements. The value in location I_1[i_1], I_2[i_2], ..., I_n[i_n] of A is overwritten with the value X[I_1, I_2, ..., I_n] , converting if necessary.

I am not sure if this is a bug, but the behavior in 1.5.4 (and also 1.5.3) seems to slightly different:

A = zeros(10, 10); #10 x 10 Array{T, 2}
                                                                   
b = ones(100); #100 element Array{T, 1}
                                                                   
s = zeros(1, 100); #1 x 100 Array{T, 2}
                                                                   
t = zeros(100, 1); # 100 x 1 Array{T, 2}

Now as documented,

 A[:, :] = b # works as documented

fills A with ones. The behavior with s and t, however, is a bit puzzling. Neither of them are vectors. However,

 A[:, :] = s

fills A with zeros, even though this behavior does not seem to be documented in the quote from the “Indexing assignment” section reproduced above, as s neither has the same dimensions as A, nor is it a “vector”. Indeed,

 A[:, :] = t

raises precisely this error:

ERROR: DimensionMismatch("tried to assign 100×1 array to 10×10 destination")

A comment on the forum by Tamas Papp also suggests that this might be undocumented behavior. More precisely, I am not sure if it is a documented feature that the assignment with s above should work, while the one with t should fail. (The quote from the documentation seems to suggest both should fail with an error).

julia> versioninfo()
Julia Version 1.5.4
Commit 69fcb5745b (2021-03-11 19:13 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions