|
1 | 1 | # OffsetArrays.jl |
2 | 2 |
|
| 3 | +[](https://JuliaArrays.github.io/OffsetArrays.jl/stable) |
| 4 | +[](https://travis-ci.org/JuliaArrays/OffsetArrays.jl) |
| 5 | +[](http://codecov.io/github/JuliaArrays/OffsetArrays.jl?branch=master) |
| 6 | +[![PkgEval][pkgeval-img]][pkgeval-url] |
| 7 | + |
3 | 8 |
|
4 | 9 | OffsetArrays provides Julia users with arrays that have arbitrary |
5 | 10 | indices, similar to those found in some other programming languages |
6 | 11 | like Fortran. |
7 | 12 |
|
8 | | -```julia |
9 | | -julia> using OffsetArrays |
10 | | - |
11 | | -julia> y = OffsetArray{Float64}(undef, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1); |
| 13 | +## Usage |
12 | 14 |
|
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 | +You can construct such arrays as follows: |
15 | 16 |
|
16 | | -julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14 |
17 | | -14 |
18 | | - |
19 | | -julia> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5 |
20 | | -19.0 |
21 | | -``` |
22 | | - |
23 | | -## Example: Relativistic Notation |
24 | | -Suppose we have a position vector `r = [:x, :y, :z]` which is naturally one-based, ie. `r[1] == :x`, `r[2] == :y`, `r[3] == :z` and we also want to construct a relativistic position vector which includes time as the 0th component. This can be done with OffsetArrays like |
25 | 17 | ```julia |
26 | | -julia> using OffsetArrays |
27 | | - |
28 | | -julia> r = [:x, :y, :z]; |
29 | | - |
30 | | -julia> x = OffsetVector([:t, r...], 0:3) |
31 | | -OffsetArray(::Array{Symbol,1}, 0:3) with eltype Symbol with indices 0:3: |
32 | | - :t |
33 | | - :x |
34 | | - :y |
35 | | - :z |
36 | | - |
37 | | -julia> x[0] |
38 | | -:t |
39 | | - |
40 | | -julia> x[1:3] |
41 | | -3-element Array{Symbol,1}: |
42 | | - :x |
43 | | - :y |
44 | | - :z |
| 18 | +OA = OffsetArray(A, axis1, axis2, ...) |
45 | 19 | ``` |
46 | 20 |
|
47 | | -## Example: Polynomials |
48 | | -Suppose one wants to represent the Laurent polynomial |
49 | | -``` |
50 | | -6/x + 5 - 2*x + 3*x^2 + x^3 |
51 | | -``` |
52 | | -in julia. The coefficients of this polynomial are a naturally `-1` based list, since the `n`th element of the list |
53 | | -(counting from `-1`) `6, 5, -2, 3, 1` is the coefficient corresponding to the `n`th power of `x`. This Laurent polynomial can be evaluated at say `x = 2` as follows. |
54 | | -```julia |
55 | | -julia> using OffsetArrays |
| 21 | +where you want `OA` to have axes `(axis1, axis2, ...)` and be indexed by values that |
| 22 | +fall within these axis ranges. Example: |
56 | 23 |
|
57 | | -julia> coeffs = OffsetVector([6, 5, -2, 3, 1], -1:3) |
58 | | -OffsetArray(::Array{Int64,1}, -1:3) with eltype Int64 with indices -1:3: |
59 | | - 6 |
60 | | - 5 |
61 | | - -2 |
62 | | - 3 |
63 | | - 1 |
| 24 | +```julia |
| 25 | +using OffsetArrays |
| 26 | +A = reshape(1:15, 3, 5) |
| 27 | +println("here is A:") |
| 28 | +display(A) |
| 29 | +OA = OffsetArray(A, -1:1, 0:4) # OA will have axes (-1:1, 0:4) |
| 30 | +println("here is OA:") |
| 31 | +display(OA) |
| 32 | +@show OA[-1,0] OA[1,4] |
| 33 | +``` |
64 | 34 |
|
65 | | -julia> polynomial(x, coeffs) = sum(coeffs[n]*x^n for n in eachindex(coeffs)) |
66 | | -polynomial (generic function with 1 method) |
| 35 | +which prints out |
67 | 36 |
|
68 | | -julia> polynomial(2.0, coeffs) |
69 | | -24.0 |
70 | 37 | ``` |
71 | | -Notice our use of the `eachindex` function which does not assume that the given array starts at `1`. |
72 | | - |
73 | | -## Notes on supporting OffsetArrays |
| 38 | +here is A: |
| 39 | +3×5 reshape(::UnitRange{Int64}, 3, 5) with eltype Int64: |
| 40 | + 1 4 7 10 13 |
| 41 | + 2 5 8 11 14 |
| 42 | + 3 6 9 12 15 |
| 43 | +here is OA: |
| 44 | +OffsetArray(reshape(::UnitRange{Int64}, 3, 5), -1:1, 0:4) with eltype Int64 with indices -1:1×0:4: |
| 45 | + 1 4 7 10 13 |
| 46 | + 2 5 8 11 14 |
| 47 | + 3 6 9 12 15 |
| 48 | +OA[-1, 0] = 1 |
| 49 | +OA[1, 4] = 15 |
| 50 | +``` |
74 | 51 |
|
75 | | -Julia supports generic programming with arrays that doesn't require you to assume that indices start with 1, see the [documentation](http://docs.julialang.org/en/latest/devdocs/offset-arrays/). |
| 52 | +[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg |
| 53 | +[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/report.html |
0 commit comments