Skip to content

Commit 30ef2d1

Browse files
authored
Merge pull request #15 from alsam/teh/readme
Update the README
2 parents c9aa4e3 + 4fa403c commit 30ef2d1

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

README.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,73 @@
1-
OffsetArrays.jl
2-
===============
1+
# OffsetArrays.jl
32

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.
57

68
```julia
79
julia> using OffsetArrays
810

911
julia> y = OffsetArray(Float64, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);
1012

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+
1116
julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
1217
14
1318

1419
julia> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
1520
19.0
1621
```
1722

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
1971
* the codes accompanying the book _Numerical Solution of Hyperbolic Partial Differential Equations_ by prof. John A. Trangenstein
2072
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)
2173
* 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
4395
flux = OffsetArray(Float64, 0:ncells)
4496
```
4597

46-
+ UPDATE:
47-
Added
98+
+ UPDATE:
99+
Added
48100
+ examples/scalar_law/PROGRAM0/main_sub.jl
49101

50102
see more details here

0 commit comments

Comments
 (0)