@@ -15,10 +15,21 @@ factors are stored with layout SLAY and τ stored with layout TLAY
1515"""
1616struct QRPackedLayout{SLAY,TLAY} <: AbstractQRLayout end
1717
18+
19+ """
20+ LULayout{SLAY}()
21+
22+ represents a Packed QR factorization whose
23+ factors are stored with layout SLAY and τ stored with layout TLAY
24+ """
25+ struct LULayout{SLAY} <: AbstractQRLayout end
26+
1827MemoryLayout (:: Type{<:LinearAlgebra.QRCompactWY{<:Any,MAT}} ) where MAT =
1928 QRCompactWYLayout {typeof(MemoryLayout(MAT)),DenseColumnMajor} ()
2029MemoryLayout (:: Type{<:LinearAlgebra.QR{<:Any,MAT}} ) where MAT =
2130 QRPackedLayout {typeof(MemoryLayout(MAT)),DenseColumnMajor} ()
31+ MemoryLayout (:: Type{<:LinearAlgebra.LU{<:Any,MAT}} ) where MAT =
32+ LULayout {typeof(MemoryLayout(MAT))} ()
2233
2334function materialize! (L:: Ldiv{<:QRCompactWYLayout,<:Any,<:Any,<:AbstractVector} )
2435 A,b = L. A, L. B
@@ -32,6 +43,15 @@ function materialize!(L::Ldiv{<:QRCompactWYLayout,<:Any,<:Any,<:AbstractMatrix})
3243 B
3344end
3445
46+ materialize! (L:: Ldiv{<:LULayout{<:AbstractColumnMajor},<:AbstractColumnMajor,<:LU{T},<:AbstractVecOrMat{T}} ) where {T<: BlasFloat } =
47+ LAPACK. getrs! (' N' , L. A. factors, L. A. ipiv, L. B)
48+
49+ function materialize! (L:: Ldiv{<:LULayout} )
50+ A,B = L. A,L. B
51+ _apply_ipiv_rows! (A, B)
52+ ldiv! (UpperTriangular (A. factors), ldiv! (UnitLowerTriangular (A. factors), B))
53+ end
54+
3555# Julia implementation similar to xgelsy
3656function materialize! (Ldv:: Ldiv{<:QRPackedLayout,<:Any,<:Any,<:AbstractMatrix{T}} ) where T
3757 A,B = Ldv. A,Ldv. B
@@ -287,16 +307,69 @@ _lu(layout, axes, A, pivot::P; kwds...) where P = Base.invoke(lu, Tuple{Abstract
287307_lu! (layout, axes, A, args... ; kwds... ) = error (" Overload _lu!(::$(typeof (layout)) , axes, A)" )
288308_cholesky (layout, axes, A, :: Val{false} = Val (false ); check:: Bool = true ) = cholesky! (cholcopy (A); check = check)
289309_cholesky (layout, axes, A, :: Val{true} ; tol = 0.0 , check:: Bool = true ) = cholesky! (cholcopy (A), Val (true ); tol = tol, check = check)
290- _cholesky! (layout, axes, A, v:: Val{tf} ; kwds... ) where tf = Base. invoke (cholesky!, Tuple{LinearAlgebra. RealHermSymComplexHerm,Val{tf}}, A, v; kwds... )
291310_factorize (layout, axes, A) = qr (A) # Default to QR
292311
312+
313+ _factorize (:: AbstractStridedLayout , axes, A) = lu (A)
314+ function _lu! (:: AbstractColumnMajor , axes, A:: AbstractMatrix{T} , pivot:: Union{NoPivot, RowMaximum} = RowMaximum ();
315+ check:: Bool = true ) where T<: BlasFloat
316+ if pivot === NoPivot ()
317+ return generic_lufact! (A, pivot; check = check)
318+ end
319+ lpt = LAPACK. getrf! (A)
320+ check && checknonsingular (lpt[3 ])
321+ return LU {T,typeof(A)} (lpt[1 ], lpt[2 ], lpt[3 ])
322+ end
323+
324+ # for some reason only defined for StridedMatrix in LinearAlgebra
325+ function getproperty (F:: LU{T,<:LayoutMatrix} , d:: Symbol ) where T
326+ m, n = size (F)
327+ if d === :L
328+ L = tril! (getfield (F, :factors )[1 : m, 1 : min (m,n)])
329+ for i = 1 : min (m,n); L[i,i] = one (T); end
330+ return L
331+ elseif d === :U
332+ return triu! (getfield (F, :factors )[1 : min (m,n), 1 : n])
333+ elseif d === :p
334+ return ipiv2perm (getfield (F, :ipiv ), m)
335+ elseif d === :P
336+ return Matrix {T} (I, m, m)[:,invperm (F. p)]
337+ else
338+ getfield (F, d)
339+ end
340+ end
341+
342+
293343# Cholesky factorization without pivoting (copied from stdlib/LinearAlgebra).
294- function _cholesky! (layout, axes, A:: LinearAlgebra.RealHermSymComplexHerm , :: Val{false} ; check:: Bool = true )
295- C, info = LinearAlgebra. _chol! (A. data, A. uplo == ' U' ? UpperTriangular : LowerTriangular)
344+
345+ # _chol!. Internal methods for calling unpivoted Cholesky
346+ # # BLAS/LAPACK element types
347+ function _chol! (:: SymmetricLayout{<:AbstractColumnMajor} , A:: AbstractMatrix{<:BlasFloat} , :: Type{UpperTriangular} )
348+ C, info = LAPACK. potrf! (' U' , A)
349+ return UpperTriangular (C), info
350+ end
351+ function _chol! (:: SymmetricLayout{<:AbstractColumnMajor} , A:: AbstractMatrix{<:BlasFloat} , :: Type{LowerTriangular} )
352+ C, info = LAPACK. potrf! (' L' , A)
353+ return LowerTriangular (C), info
354+ end
355+
356+ _chol! (_, A, UL) = LinearAlgebra. _chol! (A, UL)
357+
358+ function _cholesky! (layout, axes, A:: RealHermSymComplexHerm , :: Val{false} ; check:: Bool = true )
359+ C, info = _chol! (layout, A. data, A. uplo == ' U' ? UpperTriangular : LowerTriangular)
296360 check && LinearAlgebra. checkpositivedefinite (info)
297361 return Cholesky (C. data, A. uplo, info)
298362end
299363
364+ function _cholesky! (:: SymmetricLayout{<:AbstractColumnMajor} , axes, A:: AbstractMatrix{<:BlasReal} ,
365+ :: Val{true} ; tol = 0.0 , check:: Bool = true )
366+ AA, piv, rank, info = LAPACK. pstrf! (A. uplo, A. data, tol)
367+ C = CholeskyPivoted {eltype(AA),typeof(AA)} (AA, A. uplo, piv, rank, tol, info)
368+ check && chkfullrank (C)
369+ return C
370+ end
371+
372+
300373_inv_eye (_, :: Type{T} , axs:: NTuple{2,Base.OneTo{Int}} ) where T = Matrix {T} (I, map (length,axs)... )
301374function _inv_eye (A, :: Type{T} , (rows,cols)) where T
302375 dest = zero! (similar (A, T, (cols,rows)))
@@ -318,14 +391,16 @@ end
318391macro _layoutfactorizations (Typ)
319392 esc (quote
320393 LinearAlgebra. cholesky (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _cholesky (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
321- LinearAlgebra. cholesky! (A:: $Typ , v:: Val{false} = Val (false ); check:: Bool = true ) = ArrayLayouts. _cholesky! (ArrayLayouts. MemoryLayout (A), axes (A), A, v; check= check)
394+ LinearAlgebra. cholesky! (A:: RealHermSymComplexHerm{<:Real,<:$Typ} , v:: Val{false} = Val (false ); check:: Bool = true ) = ArrayLayouts. _cholesky! (ArrayLayouts. MemoryLayout (A), axes (A), A, v; check= check)
395+ LinearAlgebra. cholesky! (A:: RealHermSymComplexHerm{<:Real,<:$Typ} , v:: Val{true} ; check:: Bool = true , tol = 0.0 ) = ArrayLayouts. _cholesky! (ArrayLayouts. MemoryLayout (A), axes (A), A, v; check= check, tol= tol)
322396 LinearAlgebra. qr (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _qr (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
323397 LinearAlgebra. qr! (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _qr! (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
324- LinearAlgebra. lu (A:: $Typ , pivot:: Union{Val{false}, Val{true} } ; kwds... ) = ArrayLayouts. _lu (ArrayLayouts. MemoryLayout (A), axes (A), A, pivot; kwds... )
398+ LinearAlgebra. lu (A:: $Typ , pivot:: Union{NoPivot,RowMaximum } ; kwds... ) = ArrayLayouts. _lu (ArrayLayouts. MemoryLayout (A), axes (A), A, pivot; kwds... )
325399 LinearAlgebra. lu (A:: $Typ{T} ; kwds... ) where T = ArrayLayouts. _lu (ArrayLayouts. MemoryLayout (A), axes (A), A; kwds... )
326400 LinearAlgebra. lu! (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _lu! (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
327401 LinearAlgebra. factorize (A:: $Typ ) = ArrayLayouts. _factorize (ArrayLayouts. MemoryLayout (A), axes (A), A)
328402 LinearAlgebra. inv (A:: $Typ ) = ArrayLayouts. _inv (ArrayLayouts. MemoryLayout (A), axes (A), A)
403+ LinearAlgebra. ldiv! (L:: LU{<:Any,<:$Typ} , B) = ArrayLayouts. ldiv! (L, B)
329404 end )
330405end
331406
0 commit comments