Skip to content

Commit 5c46d0e

Browse files
authored
Merge pull request #90 from JuliaArrays/teh/redesign
Redesign around IdOffsetRange
2 parents e22f56e + 71aa870 commit 5c46d0e

File tree

4 files changed

+220
-112
lines changed

4 files changed

+220
-112
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "0.11.4"
3+
version = "1.0.0"
44

55
[compat]
66
julia = "0.7, 1"

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
# OffsetArrays.jl
22

3+
[![Build Status](https://travis-ci.org/JuliaArrays/OffsetArrays.jl.svg?branch=master)](https://travis-ci.org/JuliaArrays/OffsetArrays.jl)
4+
[![codecov.io](http://codecov.io/github/JuliaArrays/OffsetArrays.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaArrays/OffsetArrays.jl?branch=master)
5+
[![PkgEval][pkgeval-img]][pkgeval-url]
6+
37

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

12+
## Usage
13+
14+
You can construct such arrays as follows:
15+
16+
```julia
17+
OA = OffsetArray(A, axis1, axis2, ...)
18+
```
19+
20+
where you want `OA` to have axes `(axis1, axis2, ...)` and be indexed by values that
21+
fall within these axis ranges. Example:
22+
23+
```julia
24+
using OffsetArrays
25+
A = reshape(1:15, 3, 5)
26+
println("here is A:")
27+
display(A)
28+
OA = OffsetArray(A, -1:1, 0:4) # OA will have axes (-1:1, 0:4)
29+
println("here is OA:")
30+
display(OA)
31+
@show OA[-1,0] OA[1,4]
32+
```
33+
34+
which prints out
35+
36+
```
37+
here is A:
38+
3×5 reshape(::UnitRange{Int64}, 3, 5) with eltype Int64:
39+
1 4 7 10 13
40+
2 5 8 11 14
41+
3 6 9 12 15
42+
here is OA:
43+
OffsetArray(reshape(::UnitRange{Int64}, 3, 5), -1:1, 0:4) with eltype Int64 with indices -1:1×0:4:
44+
1 4 7 10 13
45+
2 5 8 11 14
46+
3 6 9 12 15
47+
OA[-1, 0] = 1
48+
OA[1, 4] = 15
49+
```
50+
51+
OffsetArrays works for arbitrary dimensionality:
52+
853
```julia
954
julia> using OffsetArrays
1055

@@ -21,7 +66,8 @@ julia> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
2166
```
2267

2368
## 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
69+
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
70+
2571
```julia
2672
julia> using OffsetArrays
2773

@@ -49,7 +95,7 @@ Suppose one wants to represent the Laurent polynomial
4995
```
5096
6/x + 5 - 2*x + 3*x^2 + x^3
5197
```
52-
in julia. The coefficients of this polynomial are a naturally `-1` based list, since the `n`th element of the list
98+
in julia. The coefficients of this polynomial are a naturally `-1` based list, since the `n`th element of the list
5399
(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.
54100
```julia
55101
julia> using OffsetArrays
@@ -72,4 +118,8 @@ Notice our use of the `eachindex` function which does not assume that the given
72118

73119
## Notes on supporting OffsetArrays
74120

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/).
121+
There are several "tricks" that make it easier to support arrays with general indexes, see the [documentation](http://docs.julialang.org/en/latest/devdocs/offset-arrays/).
122+
123+
124+
[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg
125+
[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/report.html

0 commit comments

Comments
 (0)