Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cat inconsistency with scalars and arrays #38019

Open
BioTurboNick opened this issue Oct 14, 2020 · 4 comments
Open

cat inconsistency with scalars and arrays #38019

BioTurboNick opened this issue Oct 14, 2020 · 4 comments
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@BioTurboNick
Copy link
Contributor

BioTurboNick commented Oct 14, 2020

I noticed an inconsistency in the behavior of cat when concatenating values with vectors and arrays.

cat(1, [2 3], dims = 1) # vcat of a scalar to a 1×2 array
#=
2×2 Array{Int64,2}:
 1  1
 2  3
=#

cat(1, [2;3], dims = 2) # hcat of scalar to a vector
#=
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 2)")
=#

cat(1, reshape([2;3], (2,1)), dims = 2) # hcat of scalar to a 2×1 array
#=
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 2)")
=#

cat(1, [2 3; 4 5], dims = 1)
#=
3×2 Matrix{Int64}:
 1  1
 2  3
 4  5
=#

cat(1, [2 3; 4 5], dims = 2)
#=
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 2)")
=#

It appears that, generally speaking, A) cat can concatenate with a scalar when dims is lower than the dimensionality of the array; B) if dims is equal to the dimensionality, it can only concatenate the scalar if the array is extended along only that dimension; and C) if dims is greater than the dimensionality of the array, it always fails.

  • Is case A an intentional feature or unintentional? (It doesn't appear to be documented?)
  • Should the only legal concatenation of a scalar with an array be case B, where the array is only extended in the one dimension being concatenated and all others illegal?
  • Should a scalar always be expanded to fill a 1-length extension of the array along the specified dimension, as in case A?
@dkarrasch dkarrasch added the arrays [a, r, r, a, y, s] label Oct 14, 2020
@strickek
Copy link
Contributor

#34395

@BioTurboNick
Copy link
Contributor Author

Coming from #34395, I think the following seem like valid uses of cat with scalars and should be preserved:

cat(1, 2, 3, dims = (1, 2, 3)) # [1 0 0; 0 0 0; 0 0 0;;  0 0 0; 0 2 0; 0 0 0;;  0 0 0; 0 0 0; 0 0 3]
cat(1, [2 3], dims = 2) # [1 2 3]
cat(1, reshape([2; 3], (2, 1)), dims = 1) # [1; 2; 3]

@mbauman
Copy link
Member

mbauman commented Nov 12, 2020

Another bugged case: [fill(1); rand(2, 2, 2)] hits some sort of non-terminating loop/recursion.

Edit: it's an infinite loop.

while true

@mbauman mbauman added the bug Indicates an unexpected problem or unintended behavior label Nov 12, 2020
@BioTurboNick
Copy link
Contributor Author

Another bugged case: [fill(1); rand(2, 2, 2)] hits some sort of non-terminating loop/recursion.

Edit: it's an infinite loop.

while true

Looks like an issue with 0-dimensional arrays?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants