Skip to content

Commit 9342543

Browse files
authored
Work on QuasiKron (#84)
* Work on QuasiKron * v0.9.3 * Update ci.yml
1 parent 1417255 commit 9342543

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
version:
1313
- '1.6'
1414
- '1'
15-
- '^1.8.0-0'
1615
os:
1716
- ubuntu-latest
1817
- macOS-latest

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuasiArrays"
22
uuid = "c4ea9172-b204-11e9-377d-29865faadc5c"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "0.9.2"
4+
version = "0.9.3"
55

66
[deps]
77
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/quasikron.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,39 @@ QuasiKron{T}(a::AbstractQuasiOrMatrix...) where T = QuasiKron{mapreduce(eltype,p
1818
QuasiKron(a...) = QuasiKron{mapreduce(eltype,promote_type,a)}(a...)
1919

2020

21-
quasikron(a::Inclusion...) = Inclusion(ProductDomain(map(domain,a)...))
21+
quasikron(a::Union{Inclusion,OneTo}...) = Inclusion(ProductDomain(map(domain,a)...))
2222
quasikron(a...) = QuasiKron(a...)
2323

2424
_kronaxes(::Tuple{}...) = ()
2525
_kronaxes(a::Tuple...) = (quasikron(map(first,a)...), _kronaxes(map(tail,a)...)...)
2626
axes(K::QuasiKron) = _kronaxes(map(axes,K.args)...)
2727

28-
function _getindex(::Type{IND}, K::QuasiKron{<:Any,1}, i::IND) where IND
28+
function _getindex(::Type{IND}, K::QuasiKron, i::IND) where IND
2929
@boundscheck checkbounds(K, i...)
3030
prod(getindex.(K.args,i...))
31+
end
32+
33+
34+
MemoryLayout(::Type{<:QuasiKron{<:Any, <:Any, <:Args}}) where Args = kronlayout(tuple_type_memorylayouts(Args)...)
35+
kronlayout(_...) = QuasiLazyLayout()
36+
cardinality(K::QuasiKron) = prod(cardinality, K.args)
37+
38+
struct ArrayQuasiVector{T, Arr} <: AbstractQuasiVector{T}
39+
array::Arr
40+
end
41+
42+
ArrayQuasiVector(A::AbstractArray{T}) where T = ArrayQuasiVector{T, typeof(A)}(A)
43+
44+
axes(a::ArrayQuasiVector) = (quasikron(axes(a.array)...),)
45+
46+
function _getindex(::Type{IND}, K::ArrayQuasiVector, i::IND) where IND
47+
@boundscheck checkbounds(K, i...)
48+
K.array[first(i)...]
49+
end
50+
51+
function _getindex(::Type{IND}, f::ApplyQuasiVector{<:Any, typeof(*), <:Tuple{QuasiKron,ArrayQuasiVector}}, ij::IND) where IND
52+
K,c = f.args
53+
A,B = K.args
54+
i,j = ij[1]
55+
transpose(A[i,:]) * c.array * B[j,:]
3156
end

test/test_quasikron.jl

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using QuasiArrays, DomainSets, StaticArrays, Test
2+
import QuasiArrays: ArrayQuasiVector
23

34
@testset "Kron" begin
45
@testset "InclusionKron" begin
@@ -12,12 +13,53 @@ using QuasiArrays, DomainSets, StaticArrays, Test
1213
end
1314

1415
@testset "QuasiKron" begin
15-
a = QuasiVector(randn(5), 0:0.5:2)
16-
b = QuasiVector([5,6,8], [1,3,4])
17-
K = quasikron(a, b)
18-
@test axes(K) == (quasikron(axes(a,1),axes(b,1)),)
19-
@test K[SVector(0.5,3)] == a[0.5]b[3]
20-
@test_throws BoundsError K[SVector(0.6,3)]
21-
@test_throws BoundsError K[SVector(0.5,2)]
16+
@testset "vec" begin
17+
a = QuasiVector(randn(5), 0:0.5:2)
18+
b = QuasiVector([5,6,8], [1,3,4])
19+
K = quasikron(a, b)
20+
@test axes(K) == (quasikron(axes(a,1),axes(b,1)),)
21+
@test K[SVector(0.5,3)] == a[0.5]b[3]
22+
@test_throws BoundsError K[SVector(0.6,3)]
23+
@test_throws BoundsError K[SVector(0.5,2)]
24+
end
25+
@testset "mat" begin
26+
a = QuasiMatrix(randn(5,3), 0:0.5:2, Base.OneTo(3))
27+
b = QuasiMatrix(randn(3,2), [1,3,4], Base.OneTo(2))
28+
K = QuasiKron(a,b)
29+
@test SVector(0.5,3) in axes(K,1)
30+
@test SVector(3,2) in axes(K,2)
31+
@test K[SVector(0.5,3), SVector(3,2)] a[0.5,3]*b[3,2]
32+
end
33+
end
34+
35+
@testset "ArrayQuasiVector" begin
36+
A = randn(2,3)
37+
a = ArrayQuasiVector(A)
38+
for k in axes(A,1), j in axes(A,2)
39+
@test a[(k,j)] == A[k,j]
40+
end
41+
@test_throws BoundsError a[(3,1)]
42+
@test_throws BoundsError a[(1,4)]
43+
44+
A = randn(2,3,4)
45+
a = ArrayQuasiVector(A)
46+
for k in axes(A,1), j in axes(A,2), l in axes(A,3)
47+
@test a[(k,j,l)] == A[k,j,l]
48+
end
49+
@test_throws BoundsError a[(3,1,1)]
50+
@test_throws BoundsError a[(1,4,1)]
51+
@test_throws BoundsError a[(1,1,5)]
52+
53+
@testset "mul" begin
54+
a = QuasiMatrix(randn(5,3), 0:0.5:2, Base.OneTo(3))
55+
b = QuasiMatrix(randn(3,2), [1,3,4], Base.OneTo(2))
56+
K = QuasiKron(a,b)
57+
58+
C = randn(3,2)
59+
c = ArrayQuasiVector(C)
60+
f = K*c
61+
@test axes(f,1) == axes(K,1)
62+
@test f[SVector(0.5,3)] a[0.5,:]'*C*b[3,:]
63+
end
2264
end
2365
end

0 commit comments

Comments
 (0)