Skip to content

Commit 61e8df3

Browse files
committed
ldiv for vector-of-vectors RHS
1 parent 35a4427 commit 61e8df3

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/LinearAlgebra.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,26 @@ const LAPACKFactorizations{T,S} = Union{
700700
(\)(F::AdjointFactorization{<:Any,<:LAPACKFactorizations}, B::AbstractVecOrMat) = ldiv(F, B)
701701
(\)(F::TransposeFactorization{<:Any,<:LU}, B::AbstractVecOrMat) = ldiv(F, B)
702702

703+
# return the "scalar" type for vector fields, if possible
704+
_scalartype(::Type{T}) where {T<:Number} = T
705+
_scalartype(::Type{T}) where T = _scalartype(T, Base.IteratorEltype(T))
706+
_scalartype(::Type{T}, ::Base.HasEltype) where T = _scalartype(eltype(T))
707+
_scalartype(::Type{T}, ::Base.EltypeUnknown) where T = T
708+
703709
function ldiv(F::Factorization, B::AbstractVecOrMat)
704710
require_one_based_indexing(B)
705711
m, n = size(F)
706712
if m != size(B, 1)
707713
throw(DimensionMismatch("arguments must have the same number of rows"))
708714
end
709715

710-
TFB = typeof(oneunit(eltype(B)) / oneunit(eltype(F)))
716+
TFB = typeof(zero(_scalartype(eltype(B))) / oneunit(eltype(F)))
711717
FF = Factorization{TFB}(F)
712718

713719
# For wide problem we (often) compute a minimum norm solution. The solution
714720
# is larger than the right hand side so we use size(F, 2).
715-
BB = _zeros(TFB, B, n)
721+
TBB = typeof(zero(eltype(B)) / oneunit(eltype(F)))
722+
BB = _zeros(TBB, B, n)
716723

717724
if n > size(B, 1)
718725
# Underdetermined

src/factorization.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ end
135135

136136
function (\)(F::Factorization, B::AbstractVecOrMat)
137137
require_one_based_indexing(B)
138-
TFB = typeof(oneunit(eltype(F)) \ oneunit(eltype(B)))
138+
TFB = typeof(oneunit(eltype(F)) \ zero(eltype(B)))
139139
ldiv!(F, copy_similar(B, TFB))
140140
end
141141
(\)(F::TransposeFactorization, B::AbstractVecOrMat) = conj!(adjoint(F.parent) \ conj.(B))
@@ -179,7 +179,7 @@ end
179179

180180
function (/)(B::AbstractMatrix, F::Factorization)
181181
require_one_based_indexing(B)
182-
TFB = typeof(oneunit(eltype(B)) / oneunit(eltype(F)))
182+
TFB = typeof(zero(eltype(B)) / oneunit(eltype(F)))
183183
rdiv!(copy_similar(B, TFB), F)
184184
end
185185
# reinterpretation trick for complex lhs and real factorization

src/hessenberg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ TransUpperHessenberg{T,S<:UpperHessenberg{T}} = Transpose{T, S}
196196
AdjOrTransUpperHessenberg{T,S<:UpperHessenberg{T}} = AdjOrTrans{T, S}
197197

198198
function (\)(H::Union{UpperHessenberg,AdjOrTransUpperHessenberg}, B::AbstractVecOrMat)
199-
TFB = typeof(oneunit(eltype(H)) \ oneunit(eltype(B)))
199+
TFB = typeof(oneunit(eltype(H)) \ zero(eltype(B)))
200200
return ldiv!(H, copy_similar(B, TFB))
201201
end
202202

203203
(/)(B::AbstractMatrix, H::UpperHessenberg) = _rdiv(B, H)
204204
(/)(B::AbstractMatrix, H::AdjUpperHessenberg) = _rdiv(B, H)
205205
(/)(B::AbstractMatrix, H::TransUpperHessenberg) = _rdiv(B, H)
206206
function _rdiv(B, H)
207-
TFB = typeof(oneunit(eltype(B)) / oneunit(eltype(H)))
207+
TFB = typeof(zero(eltype(B)) / oneunit(eltype(H)))
208208
return rdiv!(copy_similar(B, TFB), H)
209209
end
210210

src/special.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ function mul(B::Bidiagonal, H::UpperHessenberg)
132132
end
133133

134134
function /(H::UpperHessenberg, B::Bidiagonal)
135-
T = typeof(oneunit(eltype(H))/oneunit(eltype(B)))
135+
T = typeof(oneunit(eltype(H))/zero(eltype(B)))
136136
A = _rdiv!(similar(H, T, size(H)), H, B)
137137
return B.uplo == 'U' ? UpperHessenberg(A) : A
138138
end
139139

140140
function \(B::Bidiagonal, H::UpperHessenberg)
141-
T = typeof(oneunit(eltype(B))\oneunit(eltype(H)))
141+
T = typeof(zero(eltype(B))\oneunit(eltype(H)))
142142
A = ldiv!(similar(H, T, size(H)), B, H)
143143
return B.uplo == 'U' ? UpperHessenberg(A) : A
144144
end

test/lu.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ dimg = randn(n)/2
127127
end
128128

129129
# Test whether Ax_ldiv_B!(y, LU, x) indeed overwrites y
130-
resultT = typeof(oneunit(eltyb) / oneunit(eltya))
130+
resultT = typeof(zero(eltyb) / oneunit(eltya))
131131

132132
b_dest = similar(b, resultT)
133133
c_dest = similar(c, resultT)

0 commit comments

Comments
 (0)