JuliaArrays/OffsetArrays.jl

Name: OffsetArrays.jl

Owner: JuliaArrays

Description: Fortran-like arrays with arbitrary, zero or negative starting indices.

Created: 2014-01-31 11:13:19.0

Updated: 2018-01-17 00:26:46.0

Pushed: 2018-01-16 08:21:31.0

Homepage:

Size: 132

Language: Julia

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

OffsetArrays.jl

OffsetArrays provides Julia users with arrays that have arbitrary indices, similar to those found in some other programming languages like Fortran.

a> using OffsetArrays

a> y = OffsetArray{Float64}(uninitialized, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);

a> summary(y)
setArrays.OffsetArray{Float64,8,Array{Float64,8}} with indices -1:1×-7:7×-128:512×-5:5×-1:1×-3:3×-2:2×-1:1"

a> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14


a> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5

Support for such arrays is based on new functionality in Julia v0.5, and the modern package is not usable on earlier Julia versions.

Special note for Julia 0.5

During the transition during Julia 0.5 to allowing arrays with arbitrary indices, certain operations (like size and length) are deliberately unsupported; see the documentation describing the reasoning behind this decision. The general recommendation is to use indices and linearindices for most operations where size and length would have formerly been used.

If your package makes use of OffsetArrays, you can also add the following internal convenience definitions:

e(A::AbstractArray) = map(length, indices(A))
e(A) = size(A)

gth(A::AbstractArray) = length(linearindices(A))
gth(A) = length(A)

These versions should work for all types.

Performance optimization with @unsafe

Also during Julia 0.5, @inbounds will not remove the internal bounds-checking that happens when using an OffsetArray. Until this changes, you can often accomplish the same task with @unsafe:

OffsetArray(rand(1000), 0:999)
afe for i in indices(v, 1)
v[i] += 1

With such annotation, OffsetArrays are as performant as Arrays.

@unsafe is not as powerful as @inbounds, and it is possible to set up situations where it fails to remove bounds checks. However, it suffices for many uses.

Comparison with Fortran

The original purpose of the package was to simplify translation of Fortran codes, say

see also translation to Julia of Fortran codes from ClawPack

The directory examples of hyperbolic_PDE contains at the moment a translation of explicit upwind finite difference scheme for scalar law advection equation from the book Numerical Solution of Hyperbolic Partial Differential Equations by prof. John A. Trangenstein.

That is 7.2s for Julia script vs. 4.8s for Fortran.

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.