Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More general division by triangular matrices #837

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 46 additions & 179 deletions src/triangular.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
@inline transpose(A::LinearAlgebra.LowerTriangular{<:Any,<:StaticMatrix}) =
LinearAlgebra.UpperTriangular(transpose(A.data))
@inline adjoint(A::LinearAlgebra.LowerTriangular{<:Any,<:StaticMatrix}) =
LinearAlgebra.UpperTriangular(adjoint(A.data))
@inline transpose(A::LinearAlgebra.UpperTriangular{<:Any,<:StaticMatrix}) =
LinearAlgebra.LowerTriangular(transpose(A.data))
@inline adjoint(A::LinearAlgebra.UpperTriangular{<:Any,<:StaticMatrix}) =
LinearAlgebra.LowerTriangular(adjoint(A.data))
@inline transpose(A::LowerTriangular{<:Any,<:StaticMatrix}) =
UpperTriangular(transpose(A.data))
@inline adjoint(A::LowerTriangular{<:Any,<:StaticMatrix}) =
UpperTriangular(adjoint(A.data))
@inline transpose(A::UnitLowerTriangular{<:Any,<:StaticMatrix}) =
UnitUpperTriangular(transpose(A.data))
@inline adjoint(A::UnitLowerTriangular{<:Any,<:StaticMatrix}) =
UnitUpperTriangular(adjoint(A.data))
@inline transpose(A::UpperTriangular{<:Any,<:StaticMatrix}) =
LowerTriangular(transpose(A.data))
@inline adjoint(A::UpperTriangular{<:Any,<:StaticMatrix}) =
LowerTriangular(adjoint(A.data))
@inline transpose(A::UnitUpperTriangular{<:Any,<:StaticMatrix}) =
UnitLowerTriangular(transpose(A.data))
@inline adjoint(A::UnitUpperTriangular{<:Any,<:StaticMatrix}) =
UnitLowerTriangular(adjoint(A.data))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's surprising that Base doesn't seem to include these rules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth opening an issue in Julia repository?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I can't think of a reason these shouldn't be structure-preserving.

@inline Base.:*(A::Adjoint{<:Any,<:StaticVector}, B::LinearAlgebra.AbstractTriangular{<:Any,<:StaticMatrix}) =
adjoint(adjoint(B) * adjoint(A))
@inline Base.:*(A::Transpose{<:Any,<:StaticVector}, B::LinearAlgebra.AbstractTriangular{<:Any,<:StaticMatrix}) =
Expand All @@ -15,193 +23,52 @@
@inline Base.:*(A::LinearAlgebra.AbstractTriangular{<:Any,<:StaticMatrix}, B::Transpose{<:Any,<:StaticVector}) =
transpose(transpose(B) * transpose(A))

const StaticULT = Union{UpperTriangular{<:Any,<:StaticMatrix},LowerTriangular{<:Any,<:StaticMatrix}}
const StaticULT{TA} = Union{UpperTriangular{TA,<:StaticMatrix},LowerTriangular{TA,<:StaticMatrix},UnitUpperTriangular{TA,<:StaticMatrix},UnitLowerTriangular{TA,<:StaticMatrix}}

@inline Base.:\(A::StaticULT, B::StaticVecOrMat) = _A_ldiv_B(Size(A), Size(B), A, B)
@inline Base.:\(A::StaticULT, B::StaticVecOrMatLike) = _A_ldiv_B(Size(A), Size(B), A, B)
@inline Base.:/(A::StaticVecOrMatLike, B::StaticULT) = transpose(transpose(B) \ transpose(A))

@generated function _A_ldiv_B(::Size{sa}, ::Size{sb}, A::UpperTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
@generated function _A_ldiv_B(::Size{sa}, ::Size{sb}, A::StaticULT{TA}, B::StaticVecOrMatLike{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]
init = [:($(X[i,j]) = B[$(LinearIndices(sb)[i, j])]) for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = m:-1:1
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
isunitdiag = A <: Union{UnitUpperTriangular, UnitLowerTriangular}
isupper = A <: Union{UnitUpperTriangular, UpperTriangular}

j_range = isupper ? (m:-1:1) : (1:m)

init = gen_by_access(B, :B) do access_b
init_exprs = [:($(X[i,j]) = $(uplo_access(sb, :b, i, j, access_b))) for i = 1:m, j = 1:n]
code = Expr(:block, init_exprs...)
for k = 1:n
for j = j_range
if !isunitdiag && k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
if isunitdiag
push!(code.args, :($(X[j,k]) = oneunit(TA) \ $(X[j,k])))
else
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])] \ $(X[j,k])))
end
i_range = isupper ? (j-1:-1:1) : (j+1:m)
for i = i_range
push!(code.args, :($(X[i,k]) -= A.data[$(LinearIndices(sa)[i, j])]*$(X[j,k])))
end
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])] \ $(X[j,k])))
for i = j-1:-1:1
push!(code.args, :($(X[i,k]) -= A.data[$(LinearIndices(sa)[i, j])]*$(X[j,k])))
end
end
end

return quote
@_inline_meta
@inbounds $(Expr(:block, init...))
@inbounds $code
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
end

@generated function _A_ldiv_B(::Size{sa}, ::Size{sb}, A::LowerTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]
init = [:($(X[i,j]) = B[$(LinearIndices(sb)[i, j])]) for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = 1:m
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])] \ $(X[j,k])))
for i = j+1:m
push!(code.args, :($(X[i,k]) -= A.data[$(LinearIndices(sa)[i, j])]*$(X[j,k])))
end
end
end

return quote
@_inline_meta
@inbounds $(Expr(:block, init...))
@inbounds $code
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
end

@generated function _Ac_ldiv_B(::Size{sa}, ::Size{sb}, A::UpperTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = 1:m
ex = :(B[$(LinearIndices(sb)[j, k])])
for i = 1:j-1
ex = :($ex - A.data[$(LinearIndices(sa)[i, j])]'*$(X[i,k]))
end
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])]' \ $ex))
end
end

return quote
@_inline_meta
@inbounds $code
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
end

@generated function _At_ldiv_B(::Size{sa}, ::Size{sb}, A::UpperTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = 1:m
ex = :(B[$(LinearIndices(sb)[j, k])])
for i = 1:j-1
ex = :($ex - A.data[$(LinearIndices(sa)[i, j])]*$(X[i,k]))
end
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])] \ $ex))
end
end

return quote
@_inline_meta
@inbounds $code
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
end

@generated function _Ac_ldiv_B(::Size{sa}, ::Size{sb}, A::LowerTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = m:-1:1
ex = :(B[$(LinearIndices(sb)[j, k])])
for i = m:-1:j+1
ex = :($ex - A.data[$(LinearIndices(sa)[i, j])]'*$(X[i,k]))
end
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])]' \ $ex))
end
end

return quote
@_inline_meta
@inbounds $code
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
end

@generated function _At_ldiv_B(::Size{sa}, ::Size{sb}, A::LowerTriangular{<:TA,<:StaticMatrix}, B::StaticVecOrMat{TB}) where {sa,sb,TA,TB}
m = sb[1]
n = length(sb) > 1 ? sb[2] : 1
if m != sa[1]
throw(DimensionMismatch("right hand side B needs first dimension of size $(sa[1]), has size $m"))
end

X = [Symbol("X_$(i)_$(j)") for i = 1:m, j = 1:n]

code = Expr(:block)
for k = 1:n
for j = m:-1:1
ex = :(B[$(LinearIndices(sb)[j, k])])
for i = m:-1:j+1
ex = :($ex - A.data[$(LinearIndices(sa)[i, j])]*$(X[i,k]))
end
if k == 1
push!(code.args, :(A.data[$(LinearIndices(sa)[j, j])] == zero(A.data[$(LinearIndices(sa)[j, j])]) && throw(LinearAlgebra.SingularException($j))))
end
push!(code.args, :($(X[j,k]) = A.data[$(LinearIndices(sa)[j, j])] \ $ex))
end
return code
end

return quote
@_inline_meta
@inbounds $code
b = mul_parent(B)
Tb = TB
@inbounds $init
TAB = typeof((zero(TA)*zero(TB) + zero(TA)*zero(TB))/one(TA))
@inbounds return similar_type(B, TAB)(tuple($(X...)))
end
Expand Down
29 changes: 19 additions & 10 deletions test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,36 @@ end
@testset "Triangular-matrix division" begin
for n in (1, 2, 3, 4),
eltyA in (Float64, ComplexF64, Int),
(t, uplo) in ((UpperTriangular, :U), (LowerTriangular, :L)),
eltyB in (Float64, ComplexF64)
(t, uplo) in ((UpperTriangular, :U), (LowerTriangular, :L), (UnitUpperTriangular, :U)),
eltyB in (Float64, ComplexF64),
tb in (identity, LowerTriangular, Symmetric)

A = t(eltyA == Int ? rand(1:7, n, n) : convert(Matrix{eltyA}, (eltyA <: Complex ? complex.(randn(n, n), randn(n, n)) : randn(n, n)) |> t -> cholesky(t't).U |> t -> uplo == :U ? t : adjoint(t)))
B = convert(Matrix{eltyB}, eltyA <: Complex ? real(A)*ones(n, n) : A*ones(n, n))
B = tb(convert(Matrix{eltyB}, eltyA <: Complex ? real(A)*ones(n, n) : A*ones(n, n)))
SA = t(SMatrix{n,n}(A.data))
SB = SMatrix{n,n}(B)
SB = tb(SMatrix{n,n}(parent(B)))

@test (SA\SB[:,1])::SVector{n} ≈ A\B[:,1]
if tb === identity
@test (SA\SB[:,1])::SVector{n} ≈ A\B[:,1]
@test (transpose(SA)\SB[:,1])::SVector{n} ≈ transpose(A)\B[:,1]
@test (SA'\SB[:,1])::SVector{n} ≈ A'\B[:,1]
end
@test (SA\SB)::SMatrix{n,n} ≈ A\B
@test (transpose(SA)\SB[:,1])::SVector{n} ≈ transpose(A)\B[:,1]
@test (transpose(SA)\SB)::SMatrix{n,n} ≈ transpose(A)\B
@test (SA'\SB[:,1])::SVector{n} ≈ A'\B[:,1]
@test (SA'\SB)::SMatrix{n,n} ≈ A'\B

@test_throws DimensionMismatch SA\ones(SVector{n+2,eltyB})
@test_throws DimensionMismatch transpose(SA)\ones(SVector{n+2,eltyB})
@test_throws DimensionMismatch SA'\ones(SVector{n+2,eltyB})

@test_throws LinearAlgebra.SingularException t(zeros(SMatrix{n,n,eltyA}))\ones(SVector{n,eltyB})
@test_throws LinearAlgebra.SingularException t(transpose(zeros(SMatrix{n,n,eltyA})))\ones(SVector{n,eltyB})
@test_throws LinearAlgebra.SingularException t(zeros(SMatrix{n,n,eltyA}))'\ones(SVector{n,eltyB})
if t != UnitUpperTriangular
@test_throws LinearAlgebra.SingularException t(zeros(SMatrix{n,n,eltyA}))\ones(SVector{n,eltyB})
@test_throws LinearAlgebra.SingularException t(transpose(zeros(SMatrix{n,n,eltyA})))\ones(SVector{n,eltyB})
@test_throws LinearAlgebra.SingularException t(zeros(SMatrix{n,n,eltyA}))'\ones(SVector{n,eltyB})
end

@test (SB/SA)::SMatrix{n,n} ≈ B/A
@test (SB/transpose(SA))::SMatrix{n,n} ≈ B/transpose(A)
@test (SB/SA')::SMatrix{n,n} ≈ B/A'
end
end