@@ -15,10 +15,21 @@ factors are stored with layout SLAY and τ stored with layout TLAY
15
15
"""
16
16
struct QRPackedLayout{SLAY,TLAY} <: AbstractQRLayout end
17
17
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
+
18
27
MemoryLayout (:: Type{<:LinearAlgebra.QRCompactWY{<:Any,MAT}} ) where MAT =
19
28
QRCompactWYLayout {typeof(MemoryLayout(MAT)),DenseColumnMajor} ()
20
29
MemoryLayout (:: Type{<:LinearAlgebra.QR{<:Any,MAT}} ) where MAT =
21
30
QRPackedLayout {typeof(MemoryLayout(MAT)),DenseColumnMajor} ()
31
+ MemoryLayout (:: Type{<:LinearAlgebra.LU{<:Any,MAT}} ) where MAT =
32
+ LULayout {typeof(MemoryLayout(MAT))} ()
22
33
23
34
function materialize! (L:: Ldiv{<:QRCompactWYLayout,<:Any,<:Any,<:AbstractVector} )
24
35
A,b = L. A, L. B
@@ -32,6 +43,15 @@ function materialize!(L::Ldiv{<:QRCompactWYLayout,<:Any,<:Any,<:AbstractMatrix})
32
43
B
33
44
end
34
45
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
+
35
55
# Julia implementation similar to xgelsy
36
56
function materialize! (Ldv:: Ldiv{<:QRPackedLayout,<:Any,<:Any,<:AbstractMatrix{T}} ) where T
37
57
A,B = Ldv. A,Ldv. B
@@ -287,16 +307,69 @@ _lu(layout, axes, A, pivot::P; kwds...) where P = Base.invoke(lu, Tuple{Abstract
287
307
_lu! (layout, axes, A, args... ; kwds... ) = error (" Overload _lu!(::$(typeof (layout)) , axes, A)" )
288
308
_cholesky (layout, axes, A, :: Val{false} = Val (false ); check:: Bool = true ) = cholesky! (cholcopy (A); check = check)
289
309
_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... )
291
310
_factorize (layout, axes, A) = qr (A) # Default to QR
292
311
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
+
293
343
# 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)
296
360
check && LinearAlgebra. checkpositivedefinite (info)
297
361
return Cholesky (C. data, A. uplo, info)
298
362
end
299
363
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
+
300
373
_inv_eye (_, :: Type{T} , axs:: NTuple{2,Base.OneTo{Int}} ) where T = Matrix {T} (I, map (length,axs)... )
301
374
function _inv_eye (A, :: Type{T} , (rows,cols)) where T
302
375
dest = zero! (similar (A, T, (cols,rows)))
@@ -318,14 +391,16 @@ end
318
391
macro _layoutfactorizations (Typ)
319
392
esc (quote
320
393
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)
322
396
LinearAlgebra. qr (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _qr (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
323
397
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... )
325
399
LinearAlgebra. lu (A:: $Typ{T} ; kwds... ) where T = ArrayLayouts. _lu (ArrayLayouts. MemoryLayout (A), axes (A), A; kwds... )
326
400
LinearAlgebra. lu! (A:: $Typ , args... ; kwds... ) = ArrayLayouts. _lu! (ArrayLayouts. MemoryLayout (A), axes (A), A, args... ; kwds... )
327
401
LinearAlgebra. factorize (A:: $Typ ) = ArrayLayouts. _factorize (ArrayLayouts. MemoryLayout (A), axes (A), A)
328
402
LinearAlgebra. inv (A:: $Typ ) = ArrayLayouts. _inv (ArrayLayouts. MemoryLayout (A), axes (A), A)
403
+ LinearAlgebra. ldiv! (L:: LU{<:Any,<:$Typ} , B) = ArrayLayouts. ldiv! (L, B)
329
404
end )
330
405
end
331
406
0 commit comments