Skip to content

Commit c6fae67

Browse files
authored
Add OffsetMatrix type alias (#131)
1 parent e907809 commit c6fae67

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/OffsetArrays.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ else
77
using Base: IdentityUnitRange
88
end
99

10-
export OffsetArray, OffsetVector
10+
export OffsetArray, OffsetMatrix, OffsetVector
1111

1212
include("axes.jl")
1313

@@ -17,6 +17,7 @@ struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
1717
offsets::NTuple{N,Int}
1818
end
1919
OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
20+
OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}
2021

2122
## OffsetArray constructors
2223

@@ -44,6 +45,10 @@ OffsetArray{T}(init::ArrayInitializer, inds::Vararg{AbstractUnitRange,N}) where
4445
OffsetVector(A::AbstractVector, offset) = OffsetArray(A, offset)
4546
OffsetVector{T}(init::ArrayInitializer, inds::AbstractUnitRange) where {T} = OffsetArray{T}(init, inds)
4647

48+
# OffsetMatrix constructors
49+
OffsetMatrix(A::AbstractMatrix, offset1, offset2) = OffsetArray(A, offset1, offset2)
50+
OffsetMatrix{T}(init::ArrayInitializer, inds1::AbstractUnitRange, inds2::AbstractUnitRange) where {T} = OffsetArray{T}(init, inds1, inds2)
51+
4752
"""
4853
OffsetArray(A, indices...)
4954

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ end
108108
@test typeof(OffsetVector{Float64}(undef, -2:2)) == typeof(OffsetArray{Float64}(undef, -2:2))
109109
end
110110

111+
@testset "OffsetMatrix constructors" begin
112+
local v = rand(5, 3)
113+
@test OffsetMatrix(v, -2, -1) == OffsetArray(v, -2, -1)
114+
@test OffsetMatrix(v, -2:2, -1:1) == OffsetArray(v, -2:2, -1:1)
115+
@test typeof(OffsetMatrix{Float64}(undef, -2:2, -1:1)) == typeof(OffsetArray{Float64}(undef, -2:2, -1:1))
116+
end
117+
111118
@testset "undef, missing, and nothing constructors" begin
112119
y = OffsetArray{Float32}(undef, (IdentityUnitRange(-1:1),))
113120
@test axes(y) == (IdentityUnitRange(-1:1),)

0 commit comments

Comments
 (0)