Skip to content

Commit

Permalink
kron for AbstractFill (#268)
Browse files Browse the repository at this point in the history
* kron for AbstractFill

* Aggressively promote to Zeros

* test for Eye kron type on v1.9+

* matrix of matrices

* Test for String

* revert convert to Zeros aggressively

* kron for rectangular Eye
  • Loading branch information
jishnub authored Jul 3, 2023
1 parent ac19566 commit e3c8eb9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,

import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
dot, norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AdjointAbsVec, TransposeAbsVec,
issymmetric, ishermitian, AdjOrTransAbsVec, checksquare, mul!
issymmetric, ishermitian, AdjOrTransAbsVec, checksquare, mul!, kron


import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
Expand Down Expand Up @@ -411,6 +411,7 @@ Base.replace_in_print_matrix(A::RectDiagonal, i::Integer, j::Integer, s::Abstrac


const RectOrDiagonal{T,V,Axes} = Union{RectDiagonal{T,V,Axes}, Diagonal{T,V}}
const RectDiagonalEye{T} = RectDiagonal{T,<:Ones{T,1}}
const SquareEye{T,Axes} = Diagonal{T,Ones{T,1,Tuple{Axes}}}
const Eye{T,Axes} = RectOrDiagonal{T,Ones{T,1,Tuple{Axes}}}

Expand Down
20 changes: 20 additions & 0 deletions src/fillalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,23 @@ fillzero(::Type{Zeros{T,N,AXIS}}, n, m) where {T,N,AXIS} = Zeros{T,N,AXIS}((n, m
fillzero(::Type{F}, n, m) where F = throw(ArgumentError("Cannot create a zero array of type $F"))

diagzero(D::Diagonal{F}, i, j) where F<:AbstractFill = fillzero(F, axes(D.diag[i], 1), axes(D.diag[j], 2))

# kron

_kronsize(f::AbstractFillVector, g::AbstractFillVector) = (size(f,1)*size(g,1),)
_kronsize(f::AbstractFillVecOrMat, g::AbstractFillVecOrMat) = (size(f,1)*size(g,1), size(f,2)*size(g,2))
function _kron(f::AbstractFill, g::AbstractFill, sz)
v = getindex_value(f)*getindex_value(g)
Fill(v, sz)
end
function _kron(f::Zeros, g::Zeros, sz)
Zeros{promote_type(eltype(f), eltype(g))}(sz)
end
function _kron(f::Ones, g::Ones, sz)
Ones{promote_type(eltype(f), eltype(g))}(sz)
end
function kron(f::AbstractFillVecOrMat, g::AbstractFillVecOrMat)
sz = _kronsize(f, g)
_kron(f, g, sz)
end
kron(E1::RectDiagonalEye, E2::RectDiagonalEye) = kron(sparse(E1), sparse(E2))
47 changes: 47 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,53 @@ end
end
end

@testset "kron" begin
for T in (Fill, Zeros, Ones), sz in ((2,), (2,2))
f = T{Int}((T == Fill ? (3,sz...) : sz)...)
g = Ones{Int}(2)
z = Zeros{Int}(2)
fc = collect(f)
gc = collect(g)
zc = collect(z)
@test kron(f, f) == kron(fc, fc)
@test kron(f, f) isa T{Int,length(sz)}
@test kron(f, g) == kron(fc, gc)
@test kron(f, g) isa AbstractFill{Int,length(sz)}
@test kron(g, f) == kron(gc, fc)
@test kron(g, f) isa AbstractFill{Int,length(sz)}
@test kron(f, z) == kron(fc, zc)
@test kron(f, z) isa AbstractFill{Int,length(sz)}
@test kron(z, f) == kron(zc, fc)
@test kron(z, f) isa AbstractFill{Int,length(sz)}
@test kron(f, f .+ 0.5) == kron(fc, fc .+ 0.5)
@test kron(f, f .+ 0.5) isa AbstractFill{Float64,length(sz)}
@test kron(f, g .+ 0.5) isa AbstractFill{Float64,length(sz)}
end

for m in (Fill(2,2,2), "a"), sz in ((2,2), (2,))
f = Fill(m, sz)
g = fill(m, sz)
@test kron(f, f) == kron(g, g)
end

@test_throws MethodError kron(Fill("a",2), Zeros(1)) # can't multiply String and Float64

E = Eye(2)
K = kron(E, E)
@test K isa Diagonal
if VERSION >= v"1.9"
@test K isa typeof(E)
end
C = collect(E)
@test K == kron(C, C)

E = Eye(2,3)
K = kron(E, E)
C = collect(E)
@test K == kron(C, C)
@test issparse(kron(E,E))
end

@testset "dot products" begin
n = 15
o = Ones(1:n)
Expand Down

2 comments on commit e3c8eb9

@jishnub
Copy link
Member Author

@jishnub jishnub commented on e3c8eb9 Jul 3, 2023

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/86781

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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" e3c8eb9ddd5ddcbb9e3a4729519733cade0b4d62
git push origin v1.3.0

Please sign in to comment.