Skip to content

Commit

Permalink
Merge pull request #90 from JuliaArrays/teh/redesign
Browse files Browse the repository at this point in the history
Redesign around IdOffsetRange
  • Loading branch information
timholy authored Jan 18, 2020
2 parents e22f56e + 71aa870 commit 5c46d0e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OffsetArrays"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
version = "0.11.4"
version = "1.0.0"

[compat]
julia = "0.7, 1"
Expand Down
56 changes: 53 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
# OffsetArrays.jl

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


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

## Usage

You can construct such arrays as follows:

```julia
OA = OffsetArray(A, axis1, axis2, ...)
```

where you want `OA` to have axes `(axis1, axis2, ...)` and be indexed by values that
fall within these axis ranges. Example:

```julia
using OffsetArrays
A = reshape(1:15, 3, 5)
println("here is A:")
display(A)
OA = OffsetArray(A, -1:1, 0:4) # OA will have axes (-1:1, 0:4)
println("here is OA:")
display(OA)
@show OA[-1,0] OA[1,4]
```

which prints out

```
here is A:
3×5 reshape(::UnitRange{Int64}, 3, 5) with eltype Int64:
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
here is OA:
OffsetArray(reshape(::UnitRange{Int64}, 3, 5), -1:1, 0:4) with eltype Int64 with indices -1:1×0:4:
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
OA[-1, 0] = 1
OA[1, 4] = 15
```

OffsetArrays works for arbitrary dimensionality:

```julia
julia> using OffsetArrays

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

## Example: Relativistic Notation
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
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

```julia
julia> using OffsetArrays

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

## Notes on supporting OffsetArrays

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/).
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/).


[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg
[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/report.html
Loading

2 comments on commit 5c46d0e

@timholy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/8131

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" 5c46d0e6365dd39235923144337dc8f3401f373a
git push origin v1.0.0

Please sign in to comment.