Arrays with custom indices examples?

145 views
Skip to first unread message

Angel de Vicente

unread,
Oct 24, 2016, 7:13:37 AM10/24/16
to julia-users
Hi,

I'm trying to figure out how to use arrays with custom indices, and I've
read http://docs.julialang.org/en/release-0.5/devdocs/offset-arrays/,
but I don't seem to see how I could actually access array positions
using arbitrary indices.

A basic example of what I'd like to do would be something like:
,----
| m = rand(1:10,(3,3))
| mg = zeros(Int,(0:4,0:4))
| mg[1:3,1:3]=m
`----

so that mg would be a copy of m but with a buffer zone around it, and
when I need to access any value in mg, I can use the same indexing as
for m (the buffer zone would represent ghost cells for PDEs).

But I don't seem to get very far, as the creation of mg already is
giving me troubles:
,----
| julia> mg = zeros(Int,(0:4,0:4))
| ERROR: MethodError: Cannot `convert` an object of type Tuple{UnitRange{Int64},UnitRange{Int64}} to an object of type
| Array{Int64,N}
| This may have arisen from a call to the constructor Array{Int64,N}(...),
| since type constructors fall back to convert methods.
| in Array{Int64,N}(::Tuple{UnitRange{Int64},UnitRange{Int64}}) at ./sysimg.jl:53
| in zeros(::Type{T}, ::Tuple{UnitRange{Int64},UnitRange{Int64}}, ::Vararg{Tuple{UnitRange{Int64},UnitRange{Int64}},N}
| ) at ./array.jl:169
`----

I had a tough night, so perhaps the answer is clear from
http://docs.julialang.org/en/release-0.5/devdocs/offset-arrays/, but I
can't see whether what I'm trying to do is doable and if so, how. Any
help appreciated.

Thanks,
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/

Isaiah Norton

unread,
Oct 24, 2016, 11:37:43 AM10/24/16
to julia...@googlegroups.com
mg = zeros(Int,(0:4,0:4))

This isn't related to indexing -- it doesn't work with `1:4` either. Use:

  zeros(Int, 4, 4)

(See also the help entry: `?zeros`)

That said, be aware of the warnings in the referenced document: this feature is still considered experimental, so if you hit other issues please ping back here again, and folks may suggest to file a bug report.

Angel de Vicente

unread,
Oct 24, 2016, 2:34:33 PM10/24/16
to julia...@googlegroups.com
Hi,

Isaiah Norton <isaiah...@gmail.com> writes:
> mg = zeros(Int,(0:4,0:4))
>
> This isn't related to indexing -- it doesn't work with `1:4`
> either.

But it doesn't complain if I do:
,----
| julia> mg=zeros(Int,(0:4))
| 5-element Array{Int64,1}:
| 0
| 0
| 0
| 0
| 0
`----

(though then I cannot access mg[0], the indices would go as for a
standard Julia array, 1 to 5.


> Use:
> zeros(Int, 4, 4)

Probably I'm missing something very basic, but then I would just have a
normal array, where the indices would go from 1:4, in each dimension.

In the page on "Arrays with custom indices" I don't see any clear
example, so I'm a bit lost as to how I could create an array wih
non-standard inidces and how to use it. Any basic example would be
greatly appreciated.

Thanks and sorry if I'm asking very basic questions, I just had a week
or so of exposure to Julia...

Mauro

unread,
Oct 24, 2016, 3:34:14 PM10/24/16
to julia...@googlegroups.com
I think you have to make your own type. You can't just request that
ordinary arrays use different indexing.

Tim Holy

unread,
Oct 24, 2016, 5:08:59 PM10/24/16
to julia...@googlegroups.com
That document is aimed at developers to tell them how to make their package ready for arrays that have custom indices. As a user, the key line is:

> Such array types are expected to be supplied through packages.

I recommend the OffsetArrays package.

Great to see someone interested in trying this. Definitely report any bugs you find; as Isaiah said, this is still experimental functionality, and many packages probably aren't fully ready yet. It will mostly take users who want that functionality to notice where it needs to be added.

Best,
--Tim

Angel de Vicente

unread,
Oct 24, 2016, 6:23:20 PM10/24/16
to julia...@googlegroups.com
Hi,

Tim Holy <tim....@gmail.com> writes:
> That document is aimed at developers to tell them how to make their package
> ready for arrays that have custom indices. As a user, the key line is:
>
>> Such array types are expected to be supplied through packages.
>
> I recommend the OffsetArrays package.
>
> Great to see someone interested in trying this. Definitely report any bugs you
> find; as Isaiah said, this is still experimental functionality, and many
> packages probably aren't fully ready yet. It will mostly take users who want
> that functionality to notice where it needs to be added.

OK, great, I just tried the example of my first mail, and things seem to
work OK. For my use case, obviously not as convenient as the native
support of Fortran, but great that it works. If I find things that don't
work I will let you know. For the moment, from my brief testing, the
only thing I found is that something like mg[:,:] = 0 raises an error,
as you can see below (due to its dependence on "size").

Thanks a lot (Julia looks like a great language with a great
community! :-) )

,----
| julia> m=rand(1:10,(3,3))
| 3×3 Array{Int64,2}:
| 4 1 3
| 4 10 9
| 8 6 6
|
| julia> mg=OffsetArray(Int,0:4,0:4)
| OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 0:4×0:4:
| 140473481981296 140473483139344 140473481980240 140473481980528 140473481980752
| 140473481981392 140473483138224 140473481980304 140473481980592 140473481980784
| 140473481981456 140473481979952 140473481980432 140473481980624 140473481980816
| 140473481981520 140473481979984 140473481980464 140473481980656 140473481980848
| 140473481981648 140473481980208 140473481980496 140473481980720 140473481980880
|
| julia> mg[:,:]=0
| ERROR: size not supported for arrays with indices (0:4,0:4); see http://docs.julialang.org/en/latest/devdocs/offset-arrays/
| in errmsg(::OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}}) at /home/angelv/.julia/v0.5/OffsetArrays/src/OffsetArrays.jl:47
| in trailingsize(::OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}}, ::Int64) at ./abstractarray.jl:201
| in macro expansion at ./multidimensional.jl:424 [inlined]
| in _unsafe_batchsetindex!(::OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}}, ::Base.Repeated{Int64}, ::Colon, ::Colon) at ./multidimensional.jl:421
| in setindex!(::OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}}, ::Int64, ::Colon, ::Colon) at ./abstractarray.jl:832
|
| julia> mg[indices(mg,1),indices(mg,2)]=0
| 0
|
| julia> mg[1:3,1:3]=m
| 3×3 Array{Int64,2}:
| 4 1 3
| 4 10 9
| 8 6 6
|
| julia> mg
| OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 0:4×0:4:
| 0 0 0 0 0
| 0 4 1 3 0
| 0 4 10 9 0
| 0 8 6 6 0
| 0 0 0 0 0
`----
Reply all
Reply to author
Forward
0 new messages