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

julia resizing to n-1 dimension array in singleton dimensions #15621

Closed
silgon opened this issue Mar 25, 2016 · 3 comments
Closed

julia resizing to n-1 dimension array in singleton dimensions #15621

silgon opened this issue Mar 25, 2016 · 3 comments

Comments

@silgon
Copy link

silgon commented Mar 25, 2016

Hello, I would like to know why julia doesn't treat an array of N dimensions with a singleton in one of the dimensions as an array of (N-1) dimensions.
To explain that a little bit let's say I have the following code.

julia> a=ones(3,3,3);
julia> a[:,:,1]
3x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
julia> a[1,:,:]
1x3x3 Array{Float64,3}:
[:, :, 1] =
 1.0  1.0  1.0
[:, :, 2] =
 1.0  1.0  1.0
[:, :, 3] =
 1.0  1.0  1.0

In the second line you can see that the singleton dimension a[:,:,1] is converted to Array{Float64,2} reducing the dimensions. Nonetheless this effect doesn't happen with a[1,:,:] because it keeps the Array{Float64,3} dimension. So in the first case I'm able directly to do matrix operations because I have Array{Float64,2} but it's not the same in the second case because I have to transform Array{Float64,3} to Array{Float64,2} by reducing my singleton dimension.

I have the following options to resize it:

julia> reshape(a[1,:,:],size(a,2),size(a,3))
3x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
julia> slice(a,1,:,:)
3x3 SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Colon,Colon},3}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

My problem now is that this conversion I have to make it a lot of times in my case, because I use it in an iterative process. I fear that this can lead to performance problems just because of this conversion will slow down the process, any thoughts or recommendations?

@Jutho
Copy link
Contributor

Jutho commented Mar 25, 2016

This has already been changed in julia v0.5, see PR #13612 . Note however that a[1,:,:] still copies the data, so this could still be costly in an iterative process if you don't actually need a copy and are satisfied with just a view. Then slice(a,1,:,:) is the way to go.

@Jutho Jutho closed this as completed Mar 25, 2016
@silgon
Copy link
Author

silgon commented Mar 25, 2016

do you mean v0.45? I don't see any v0.5 in the tags on github. I'm asking in order to change to that version, because now I have the version provided by the ppa:staticfloat/juliareleases repositories on ubuntu 14.04

@Jutho
Copy link
Contributor

Jutho commented Mar 25, 2016

v0.5 is still in development, so I guess I should have said, this is changed on the master, which you can get e.g. as nightly built ( http://julialang.org/downloads/ ) or by cloning the github repository and building it yourself. But unless you explicitly need a copy, I think you want to use slice which works just fine on the current release version of julia.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants