{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/18494","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/18494/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/18494/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/18494/events","html_url":"https://github.com/JuliaLang/julia/pull/18494","id":176762432,"number":18494,"title":"Add support for views with arrays of CartesianIndex","user":{"login":"mbauman","id":154641,"avatar_url":"https://avatars.githubusercontent.com/u/154641?v=3","gravatar_id":"","url":"https://api.github.com/users/mbauman","html_url":"https://github.com/mbauman","followers_url":"https://api.github.com/users/mbauman/followers","following_url":"https://api.github.com/users/mbauman/following{/other_user}","gists_url":"https://api.github.com/users/mbauman/gists{/gist_id}","starred_url":"https://api.github.com/users/mbauman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbauman/subscriptions","organizations_url":"https://api.github.com/users/mbauman/orgs","repos_url":"https://api.github.com/users/mbauman/repos","events_url":"https://api.github.com/users/mbauman/events{/privacy}","received_events_url":"https://api.github.com/users/mbauman/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/multilinear%20algebra","name":"multilinear algebra","color":"006b75"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-13T22:04:12Z","updated_at":"2016-09-15T14:40:50Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/JuliaLang/julia/pulls/18494","html_url":"https://github.com/JuliaLang/julia/pull/18494","diff_url":"https://github.com/JuliaLang/julia/pull/18494.diff","patch_url":"https://github.com/JuliaLang/julia/pull/18494.patch"},"body":"The first commit here simply allows using arrays of `CartesianIndex{0}` as trailing indices.  Whereas they previously would throw an error during bounds checking, this commit enables their use.\r\n\r\nAllowing arrays of CartesianIndex for SubArray, however, is more challenging.  Enabling the creation of views with an index of type `AbstractArray{CartesianIndex{N}}` is relatively simple. The disastrously difficult part, however, is reindexing a view with arrays of CartesianIndex.  Here's a good example of why that's hard:\r\n\r\n```jl\r\njulia> A = copy(reshape(1:16, 4, 4))\r\n       V = view(A, [1 2; 3 4], [4 2; 3 1])\r\n       view(V, :, map(CartesianIndex, [(1,1), (2, 2)]), [CartesianIndex()], :)\r\n2×2×1×2 SubArray{Int64,4,SubArray{Int64,4,Array{Int64,2},Tuple{Array{Int64,2},Array{Int64,2}},false},Tuple{Colon,Array{CartesianIndex{2},1},Array{CartesianIndex{0},1},Colon},false}:\r\n[:, :, 1, 1] =\r\n 13  10\r\n 15  12\r\n\r\n[:, :, 1, 2] =\r\n 5  2\r\n 7  4\r\n```\r\n\r\nThe 2-dimensional array of CartesianIndex{2} spans the two parent indices, and the second parent index gets further split up by the addition of a dimension right in the middle.  I believe the only index type this could possibly be recomputed to is a `Array{CartesianIndex{2}, 4}`.  Doing that sort of indexing arithmetic is crazy-making, and it's likely to allocate large arrays.  So I punt and simply rely on a second level of indirection, just like subarrays of reshaped arrays do.\r\n\r\nThe fun part that led to this rabbit hole is that we now have a completely first-class equivalent to [`np.newaxis`](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#numpy.newaxis):\r\n\r\n```jl\r\njulia> const newaxis = [CartesianIndex()];\r\n\r\njulia> A = 1:3\r\n       A[:, newaxis] * A[newaxis, :]\r\n3×3 Array{Int64,2}:\r\n 1  2  3\r\n 2  4  6\r\n 3  6  9\r\n\r\njulia> A = copy(reshape(1:6, 3, 2))\r\n       A[:, newaxis, :]\r\n3×1×2 Array{Int64,3}:\r\n[:, :, 1] =\r\n 1\r\n 2\r\n 3\r\n\r\n[:, :, 2] =\r\n 4\r\n 5\r\n 6\r\n```"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/247348015","html_url":"https://github.com/JuliaLang/julia/pull/18494#issuecomment-247348015","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/18494","id":247348015,"user":{"login":"timholy","id":1525481,"avatar_url":"https://avatars.githubusercontent.com/u/1525481?v=3","gravatar_id":"","url":"https://api.github.com/users/timholy","html_url":"https://github.com/timholy","followers_url":"https://api.github.com/users/timholy/followers","following_url":"https://api.github.com/users/timholy/following{/other_user}","gists_url":"https://api.github.com/users/timholy/gists{/gist_id}","starred_url":"https://api.github.com/users/timholy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timholy/subscriptions","organizations_url":"https://api.github.com/users/timholy/orgs","repos_url":"https://api.github.com/users/timholy/repos","events_url":"https://api.github.com/users/timholy/events{/privacy}","received_events_url":"https://api.github.com/users/timholy/received_events","type":"User","site_admin":false},"created_at":"2016-09-15T14:40:50Z","updated_at":"2016-09-15T14:40:50Z","body":"I assume you mean \"**1**-dimensional array of `CartesianIndex{2}`\" above?\r\n\r\nThis is really great. `newaxis` definitely seems worth adding as an export."}}
{"actor":{"display_login":"timholy"}}