Skip to content

Commit

Permalink
Implement multiplication for OffsetMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Sep 14, 2020
2 parents 945ef97 + b2fd31a commit fd542d8
Show file tree
Hide file tree
Showing 15 changed files with 1,133 additions and 297 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: TagBot
on:
schedule:
- cron: 0 * * * *
jobs:
TagBot:
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
17 changes: 16 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ os:

julia:
- 1.0
- 1.3
- 1
- nightly

notifications:
email: false

after_success:
# push coverage results to Codecov
- julia -e 'using Pkg, OffsetArrays; cd(joinpath(dirname(pathof(OffsetArrays)), "..")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

jobs:
include:
- stage: "Documentation"
julia: 1
os: linux
script:
- julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd()));
Pkg.instantiate()'
- julia --project=docs/ docs/make.jl
after_success: skip
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name = "OffsetArrays"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
version = "0.11.4"
version = "1.2.0"

[compat]
julia = "0.7, 1"

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

[targets]
test = ["CatIndices", "DelimitedFiles", "Test"]
test = ["Aqua", "CatIndices", "DelimitedFiles", "Test", "LinearAlgebra"]
94 changes: 36 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
# OffsetArrays.jl

[![](https://img.shields.io/badge/docs-stable-blue.svg)](https://JuliaArrays.github.io/OffsetArrays.jl/stable)
[![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.

```julia
julia> using OffsetArrays

julia> y = OffsetArray{Float64}(undef, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);
## Usage

julia> summary(y)
"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"
You can construct such arrays as follows:

julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
14

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

## 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
```julia
julia> using OffsetArrays

julia> r = [:x, :y, :z];

julia> x = OffsetVector([:t, r...], 0:3)
OffsetArray(::Array{Symbol,1}, 0:3) with eltype Symbol with indices 0:3:
:t
:x
:y
:z

julia> x[0]
:t

julia> x[1:3]
3-element Array{Symbol,1}:
:x
:y
:z
OA = OffsetArray(A, axis1, axis2, ...)
```

## Example: Polynomials
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
(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
where you want `OA` to have axes `(axis1, axis2, ...)` and be indexed by values that
fall within these axis ranges. Example:

julia> coeffs = OffsetVector([6, 5, -2, 3, 1], -1:3)
OffsetArray(::Array{Int64,1}, -1:3) with eltype Int64 with indices -1:3:
6
5
-2
3
1
```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]
```

julia> polynomial(x, coeffs) = sum(coeffs[n]*x^n for n in eachindex(coeffs))
polynomial (generic function with 1 method)
which prints out

julia> polynomial(2.0, coeffs)
24.0
```
Notice our use of the `eachindex` function which does not assume that the given array starts at `1`.

## Notes on supporting OffsetArrays
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
```

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/).
[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg
[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/report.html
63 changes: 0 additions & 63 deletions READMEOLD.md

This file was deleted.

2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- julia_version: 1.0
- julia_version: 1.3
- julia_version: 1
- julia_version: nightly

platform:
Expand Down
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
site/
5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

[compat]
Documenter = "0.24"
14 changes: 14 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Documenter
using OffsetArrays

makedocs(
sitename = "OffsetArrays",
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
pages = ["index.md", "internals.md", "reference.md"],
modules = [OffsetArrays],
doctestfilters = [r"at \./.*", r"at /home.*", r"top-level scope.*", r"\[\d*\]\s*$"], # for backtraces
)

deploydocs(
repo = "github.com:JuliaArrays/OffsetArrays.jl.git"
)
121 changes: 121 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# OffsetArrays.jl

OffsetArrays provides Julia users with arrays that have arbitrary
indices, similar to those found in some other programming languages
like Fortran. Below is the basic usage found in the README, followed
by a couple of short examples illustrating circumstances in which
OffsetArrays can be useful. For a lengthier discussion, see
[this blog post](https://julialang.org/blog/2017/04/offset-arrays/).

## 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 = Float64.(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]
```

gives the output

```julia
here is A:
3×5 Array{Float64,2}:
1.0 4.0 7.0 10.0 13.0
2.0 5.0 8.0 11.0 14.0
3.0 6.0 9.0 12.0 15.0
here is OA:
3×5 OffsetArray(::Array{Float64,2}, -1:1, 0:4) with eltype Float64 with indices -1:1×0:4:
1.0 4.0 7.0 10.0 13.0
2.0 5.0 8.0 11.0 14.0
3.0 6.0 9.0 12.0 15.0
OA[-1, 0] = 1.0
OA[1, 4] = 15.0
```

OffsetArrays works for arbitrary dimensionality:

```julia
julia> using OffsetArrays

julia> y = OffsetArray{Float64}(undef, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1);

julia> summary(y)
"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"

julia> y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
14

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

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.

## 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

```jldoctest
julia> using OffsetArrays
julia> r = [:x, :y, :z];
julia> x = OffsetVector([:t, r...], 0:3)
4-element OffsetArray(::Array{Symbol,1}, 0:3) with eltype Symbol with indices 0:3:
:t
:x
:y
:z
julia> x[0]
:t
julia> x[1:3]
3-element Array{Symbol,1}:
:x
:y
:z
```

## Example: Polynomials

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

```jldoctest
julia> using OffsetArrays
julia> coeffs = OffsetVector([6, 5, -2, 3, 1], -1:3)
5-element OffsetArray(::Array{Int64,1}, -1:3) with eltype Int64 with indices -1:3:
6
5
-2
3
1
julia> polynomial(x, coeffs) = sum(coeffs[n]*x^n for n in eachindex(coeffs))
polynomial (generic function with 1 method)
julia> polynomial(2.0, coeffs)
24.0
```

Notice our use of the `eachindex` function which does not assume that the given array starts at `1`.
Loading

0 comments on commit fd542d8

Please sign in to comment.