|
1 |
| -OffsetArrays.jl |
2 |
| -=============== |
| 1 | +# OffsetArrays.jl |
3 | 2 |
|
4 |
| -Fortran-like arrays with arbitrary, that is zero or negative starting indices. |
| 3 | + |
| 4 | +OffsetArrays provides Julia users with arrays that have arbitrary |
| 5 | +indices, similar to those found in some other programming languages |
| 6 | +like Fortran. |
5 | 7 |
|
6 | 8 | ```julia
|
7 | 9 | julia> using OffsetArrays
|
8 | 10 |
|
9 | 11 | julia> y = OffsetArray(Float64, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);
|
10 | 12 |
|
| 13 | +julia> summary(y) |
| 14 | +"OffsetArrays.OffsetArray{Float64,8,Array{Float64,8}} with indices -1:1×-7:7×-128:512×-5:5×-1:1×-3:3×-2:2×-1:1" |
| 15 | + |
11 | 16 | julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
|
12 | 17 | 14
|
13 | 18 |
|
14 | 19 | julia> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
|
15 | 20 | 19.0
|
16 | 21 | ```
|
17 | 22 |
|
18 |
| -The main purpose of the package is to simplify translation of Fortran codes, say |
| 23 | +Support for such arrays is based on new functionality in Julia v0.5, |
| 24 | +and the modern package is not usable on earlier Julia versions. |
| 25 | + |
| 26 | +## Special note for Julia 0.5 |
| 27 | + |
| 28 | +During the transition during Julia 0.5 to allowing arrays with |
| 29 | +arbitrary indices, certain operations (like `size` and `length`) are |
| 30 | +deliberately unsupported; see the |
| 31 | +[documentation](http://docs.julialang.org/en/latest/devdocs/offset-arrays/) |
| 32 | +describing the reasoning behind this decision. The general |
| 33 | +recommendation is to use `indices` and `linearindices` for most |
| 34 | +operations where `size` and `length` would have formerly been used. |
| 35 | + |
| 36 | +If your package makes use of OffsetArrays, you can also add the |
| 37 | +following internal convenience definitions: |
| 38 | + |
| 39 | +```jl |
| 40 | +_size(A::AbstractArray) = map(length, indices(A)) |
| 41 | +_size(A) = size(A) |
| 42 | + |
| 43 | +_length(A::AbstractArray) = length(linearindices(A)) |
| 44 | +_length(A) = length(A) |
| 45 | +``` |
| 46 | + |
| 47 | +These versions should work for all types. |
| 48 | + |
| 49 | +## Performance optimization with @unsafe |
| 50 | + |
| 51 | +Also during Julia 0.5, `@inbounds` will not remove the internal |
| 52 | +bounds-checking that happens when using an OffsetArray. Until this |
| 53 | +changes, you can often accomplish the same task with `@unsafe`: |
| 54 | + |
| 55 | +```jl |
| 56 | +v = OffsetArray(rand(1000), 0:999) |
| 57 | +@unsafe for i in indices(v, 1) |
| 58 | + v[i] += 1 |
| 59 | +end |
| 60 | +``` |
| 61 | + |
| 62 | +With such annotation, `OffsetArray`s are as performant as `Array`s. |
| 63 | + |
| 64 | +`@unsafe` is not as powerful as `@inbounds`, and it is possible to set |
| 65 | +up situations where it fails to remove bounds checks. However, it |
| 66 | +suffices for many uses. |
| 67 | + |
| 68 | +# Comparison with Fortran |
| 69 | + |
| 70 | +The original purpose of the package was to simplify translation of Fortran codes, say |
19 | 71 | * the codes accompanying the book _Numerical Solution of Hyperbolic Partial Differential Equations_ by prof. John A. Trangenstein
|
20 | 72 | Please refer [here](http://www.math.duke.edu/~johnt/) and [here](http://www.cambridge.org/us/academic/subjects/mathematics/differential-and-integral-equations-dynamical-systems-and-co/numerical-solution-hyperbolic-partial-differential-equations)
|
21 | 73 | * Clawpack (stands for *Conservation Laws Package*) by prof. Randall J. LeVeque
|
@@ -43,8 +95,8 @@ the book _Numerical Solution of Hyperbolic Partial Differential Equations_ by pr
|
43 | 95 | flux = OffsetArray(Float64, 0:ncells)
|
44 | 96 | ```
|
45 | 97 |
|
46 |
| -+ UPDATE: |
47 |
| - Added |
| 98 | ++ UPDATE: |
| 99 | + Added |
48 | 100 | + examples/scalar_law/PROGRAM0/main_sub.jl
|
49 | 101 |
|
50 | 102 | see more details here
|
|
0 commit comments