Is there a typealias for Arrays of dimension less than N?

I am trying to compactly represent Union{Array{T,1}, Array{T,2}, Array{T,3}, … Array{T,N}}, where N is known. Is there a way to do that?

One could write a macro for creating such an expression

macro UnionForArraysOfRankLessThanN(T,N)
    args = [:(Array{$T,$n}) for n ∈ 1:N]
    esc(Expr(:curly, :Union, args...))
end

Though it’s probably best to ask yourself some serious questions about whether this is really the right way to do things before you implement something like this.

3 Likes

Interesting. I will see if I can avoid it without having to make too many types.

Probably what you want to use is simply Array{T} which is an array with element type T of any rank. I’m having a very hard time imagining any situation in which it would help you to define the union you described.

2 Likes

I know about Array{T}, not quite as limiting as what I was hoping for, but then I can set a constraint on N in the constructor/function and throw an error if the given array exceeds the threshold value. My use case is representing scalar and vector data defined over 2D and 3D structured meshes, represented by the same type. Taking all combinations of 1, 2 or 3 components of the data field, and 2 or 3 mesh dimensions, this could be thrown at me as a 2, 3 or 4 dimensional array. The geometry itself can be 2 or 3 dimensional arrays.

Hm, I’m rather doubtful that there can be any performance advantage of using the type system to check for this rather than doing @assert 2 ≤ N ≤ 6 as there is some significant overhead in dispatching on complicated types, but if I’m wrong hopefully someone more knowledgeable will come along and correct me.

So, my suggestion is that you do

function _core_functionality(A::Array)
    # what you are actually doing lives here
    # you may have multiple such functions for arrays of different rank
end

function userfacingfunction{T,N}(A::Array{T,N})
    # stuff that may change the rank of the array goes here
    @assert 2 ≤ N ≤ 6
    _core_functionality(A)
end

The function boundary ensures that the compiler knows how to specialize for arrays of different rank.

2 Likes

@mohamed82008, by any chance, are you working on VTK-like meshes?

Yep! I am working on the type system for VTK mesh data, file readers and writers for most geometric file formats, and converters between VTK mesh types, e.g. structured → unstructured.

2 Likes

No macros required:

julia> ArrayN(T, N) = Union{[Array{T,n} for n = 1:N]...}

julia> ArrayN(Int, 5)
Union{Array{Int64,1}, Array{Int64,2}, Array{Int64,3}, Array{Int64,4}, Array{Int64,5}}

julia> ArrayN(T, 5) where T
Union{Array{T,1}, Array{T,2}, Array{T,3}, Array{T,4}, Array{T,5}} where T
7 Likes

@mohamed82008 where is this development happening, is it open source? It may be really useful for a package that I am working on: Announcement: GeoStats.jl I don’t want to reinvent the wheel if you have something working already.

I am still working on it, but it’s kind of an early stage of development, probably 20%. I got the data type heirarchy and some basic functions pretty much finalized. Still working on the readers and converters, then come the writing, testing and debugging.

Integration with GeometryTypes is another task down the road probably after release.

I can share the type hierarchy if you want, because that is probably final.

Already shared some of my code in another topic. Feel free to use or change it.

So this is not in a package? Bad news.