Skip to content

Commit 973a709

Browse files
judoberc42f
authored andcommitted
/-Operator for LU (#583)
* Added rdiv for LU type. Added a test too.
1 parent d3440a9 commit 973a709

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/lu.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,5 @@ end
141141

142142
\(F::LU, v::AbstractVector) = F.U \ (F.L \ v[F.p])
143143
\(F::LU, B::AbstractMatrix) = F.U \ (F.L \ B[F.p,:])
144+
145+
/(B::AbstractMatrix, F::LU) = @inbounds ((B/F.U)/F.L)[:,invperm(F.p)]

src/util.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,10 @@ TrivialView(a::AbstractArray{T,N}) where {T,N} = TrivialView{typeof(a),T,N}(a)
101101
# """
102102
@inline drop_sdims(a::StaticArrayLike) = TrivialView(a)
103103
@inline drop_sdims(a) = a
104+
105+
Base.@propagate_inbounds function invperm(p::StaticVector)
106+
# in difference to base, this does not check if p is a permutation (every value unique)
107+
ip = similar(p)
108+
ip[p] = 1:length(p)
109+
similar_type(p)(ip)
110+
end

test/lu.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ using StaticArrays, Test, LinearAlgebra
3838
l_u = l*u
3939
@test l*u a[p,:]
4040
end
41+
42+
@testset "LU division ($m×$n)" for m in [1:4..., 15], n in [1:4..., 15]
43+
a = SMatrix{m,m,Float64}(rand(Float64,m,m))
44+
a_lu = lu(a)
45+
b_col = SMatrix{m,n,Float64}(rand(Float64,m,n))
46+
b_line = SMatrix{n,m,Float64}(rand(Float64,n,m))
47+
48+
# test if / and \ work with lu:
49+
@test a\b_col a_lu\b_col
50+
@test b_line/a b_line/a_lu
51+
52+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Random.seed!(44); include("eigen.jl")
4545
include("expm.jl")
4646
include("sqrtm.jl")
4747
include("lyap.jl")
48-
include("lu.jl")
48+
Random.seed!(42); include("lu.jl")
4949
Random.seed!(42); include("qr.jl")
5050
Random.seed!(42); include("chol.jl") # hermitian_type(::Type{Any}) for block algorithm
5151
include("deque.jl")

0 commit comments

Comments
 (0)