Skip to content

Commit e5f345c

Browse files
committed
Specialize lmul!/rmul! for strided triangular matrices
1 parent f781708 commit e5f345c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/diagonal.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,27 @@ function rmul!(T::Tridiagonal, D::Diagonal)
372372
end
373373
return T
374374
end
375+
for T in [:UpperTriangular, :UnitUpperTriangular,
376+
:LowerTriangular, :UnitLowerTriangular]
377+
@eval rmul!(A::$T{<:Any, <:StridedMatrix}, D::Diagonal) = _rmul!(A, D)
378+
@eval lmul!(D::Diagonal, A::$T{<:Any, <:StridedMatrix}) = _lmul!(D, A)
379+
end
380+
function _rmul!(A::UpperOrLowerTriangular, D::Diagonal)
381+
P = parent(A)
382+
isunit = A isa UnitUpperOrUnitLowerTriangular
383+
isupper = A isa UpperOrUnitUpperTriangular
384+
for col in axes(A,2)
385+
rowstart = isupper ? firstindex(A,1) : col+isunit
386+
rowstop = isupper ? col-isunit : lastindex(A,1)
387+
for row in rowstart:rowstop
388+
P[row, col] *= D.diag[col]
389+
end
390+
if isunit
391+
A[col, col] *= D.diag[col]
392+
end
393+
end
394+
return A
395+
end
375396

376397
function lmul!(D::Diagonal, B::AbstractVecOrMat)
377398
matmul_size_check(size(D), size(B))
@@ -395,6 +416,22 @@ function lmul!(D::Diagonal, T::Tridiagonal)
395416
end
396417
return T
397418
end
419+
function _lmul!(D::Diagonal, A::UpperOrLowerTriangular)
420+
P = parent(A)
421+
isunit = A isa UnitUpperOrUnitLowerTriangular
422+
isupper = A isa UpperOrUnitUpperTriangular
423+
for col in axes(A,2)
424+
rowstart = isupper ? firstindex(A,1) : col+isunit
425+
rowstop = isupper ? col-isunit : lastindex(A,1)
426+
for row in rowstart:rowstop
427+
P[row, col] = D.diag[col] * P[row, col]
428+
end
429+
if isunit
430+
A[col, col] = D.diag[col] * A[col, col]
431+
end
432+
end
433+
return A
434+
end
398435

399436
@inline function __muldiag_nonzeroalpha!(out, D::Diagonal, B, alpha::Number, beta::Number)
400437
@inbounds for j in axes(B, 2)

0 commit comments

Comments
 (0)