Skip to content

Commit fd542d8

Browse files
committed
Implement multiplication for OffsetMatrix
2 parents 945ef97 + b2fd31a commit fd542d8

File tree

15 files changed

+1133
-297
lines changed

15 files changed

+1133
-297
lines changed

.github/workflows/TagBot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: TagBot
2+
on:
3+
schedule:
4+
- cron: 0 * * * *
5+
jobs:
6+
TagBot:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: JuliaRegistries/TagBot@v1
10+
with:
11+
token: ${{ secrets.GITHUB_TOKEN }}

.travis.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@ os:
66

77
julia:
88
- 1.0
9-
- 1.3
9+
- 1
1010
- nightly
1111

1212
notifications:
1313
email: false
14+
15+
after_success:
16+
# push coverage results to Codecov
17+
- julia -e 'using Pkg, OffsetArrays; cd(joinpath(dirname(pathof(OffsetArrays)), "..")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
18+
19+
jobs:
20+
include:
21+
- stage: "Documentation"
22+
julia: 1
23+
os: linux
24+
script:
25+
- julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd()));
26+
Pkg.instantiate()'
27+
- julia --project=docs/ docs/make.jl
28+
after_success: skip

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "0.11.4"
3+
version = "1.2.0"
44

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

88
[extras]
9+
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
910
CatIndices = "aafaddc9-749c-510e-ac4f-586e18779b91"
1011
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
12+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1113
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1214

1315
[targets]
14-
test = ["CatIndices", "DelimitedFiles", "Test"]
16+
test = ["Aqua", "CatIndices", "DelimitedFiles", "Test", "LinearAlgebra"]

README.md

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,53 @@
11
# OffsetArrays.jl
22

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

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

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
1214

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:
1516

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
2517
```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, ...)
4519
```
4620

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:
5623

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+
```
6434

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
6736

68-
julia> polynomial(2.0, coeffs)
69-
24.0
7037
```
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+
```
7451

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

READMEOLD.md

Lines changed: 0 additions & 63 deletions
This file was deleted.

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
33
- julia_version: 1.0
4-
- julia_version: 1.3
4+
- julia_version: 1
55
- julia_version: nightly
66

77
platform:

docs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
site/

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
4+
[compat]
5+
Documenter = "0.24"

docs/make.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Documenter
2+
using OffsetArrays
3+
4+
makedocs(
5+
sitename = "OffsetArrays",
6+
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
7+
pages = ["index.md", "internals.md", "reference.md"],
8+
modules = [OffsetArrays],
9+
doctestfilters = [r"at \./.*", r"at /home.*", r"top-level scope.*", r"\[\d*\]\s*$"], # for backtraces
10+
)
11+
12+
deploydocs(
13+
repo = "github.com:JuliaArrays/OffsetArrays.jl.git"
14+
)

docs/src/index.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# OffsetArrays.jl
2+
3+
OffsetArrays provides Julia users with arrays that have arbitrary
4+
indices, similar to those found in some other programming languages
5+
like Fortran. Below is the basic usage found in the README, followed
6+
by a couple of short examples illustrating circumstances in which
7+
OffsetArrays can be useful. For a lengthier discussion, see
8+
[this blog post](https://julialang.org/blog/2017/04/offset-arrays/).
9+
10+
## Usage
11+
12+
You can construct such arrays as follows:
13+
14+
```julia
15+
OA = OffsetArray(A, axis1, axis2, ...)
16+
```
17+
18+
where you want `OA` to have axes `(axis1, axis2, ...)` and be indexed by values that
19+
fall within these axis ranges. Example:
20+
21+
```julia
22+
using OffsetArrays
23+
A = Float64.(reshape(1:15, 3, 5))
24+
println("Here is A:")
25+
display(A)
26+
OA = OffsetArray(A, -1:1, 0:4) # OA will have axes (-1:1, 0:4)
27+
println("Here is OA:")
28+
display(OA)
29+
@show OA[-1,0] OA[1,4]
30+
```
31+
32+
gives the output
33+
34+
```julia
35+
here is A:
36+
3×5 Array{Float64,2}:
37+
1.0 4.0 7.0 10.0 13.0
38+
2.0 5.0 8.0 11.0 14.0
39+
3.0 6.0 9.0 12.0 15.0
40+
here is OA:
41+
3×5 OffsetArray(::Array{Float64,2}, -1:1, 0:4) with eltype Float64 with indices -1:1×0:4:
42+
1.0 4.0 7.0 10.0 13.0
43+
2.0 5.0 8.0 11.0 14.0
44+
3.0 6.0 9.0 12.0 15.0
45+
OA[-1, 0] = 1.0
46+
OA[1, 4] = 15.0
47+
```
48+
49+
OffsetArrays works for arbitrary dimensionality:
50+
51+
```julia
52+
julia> using OffsetArrays
53+
54+
julia> y = OffsetArray{Float64}(undef, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);
55+
56+
julia> summary(y)
57+
"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"
58+
59+
julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
60+
14
61+
62+
julia> y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
63+
19.0
64+
```
65+
66+
You can use `OffsetArrays.no_offset_view(A)` if you want to return a view of the data in `A` but where indexing starts at 1.
67+
68+
## Example: Relativistic Notation
69+
70+
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
71+
72+
```jldoctest
73+
julia> using OffsetArrays
74+
75+
julia> r = [:x, :y, :z];
76+
77+
julia> x = OffsetVector([:t, r...], 0:3)
78+
4-element OffsetArray(::Array{Symbol,1}, 0:3) with eltype Symbol with indices 0:3:
79+
:t
80+
:x
81+
:y
82+
:z
83+
84+
julia> x[0]
85+
:t
86+
87+
julia> x[1:3]
88+
3-element Array{Symbol,1}:
89+
:x
90+
:y
91+
:z
92+
```
93+
94+
## Example: Polynomials
95+
96+
Suppose one wants to represent the Laurent polynomial
97+
```
98+
6/x + 5 - 2*x + 3*x^2 + x^3
99+
```
100+
in julia. The coefficients of this polynomial are a naturally `-1` based list, since the `n`th element of the list
101+
(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.
102+
103+
```jldoctest
104+
julia> using OffsetArrays
105+
106+
julia> coeffs = OffsetVector([6, 5, -2, 3, 1], -1:3)
107+
5-element OffsetArray(::Array{Int64,1}, -1:3) with eltype Int64 with indices -1:3:
108+
6
109+
5
110+
-2
111+
3
112+
1
113+
114+
julia> polynomial(x, coeffs) = sum(coeffs[n]*x^n for n in eachindex(coeffs))
115+
polynomial (generic function with 1 method)
116+
117+
julia> polynomial(2.0, coeffs)
118+
24.0
119+
```
120+
121+
Notice our use of the `eachindex` function which does not assume that the given array starts at `1`.

0 commit comments

Comments
 (0)