Skip to content

Commit e3f4568

Browse files
Merge pull request #401 from JuliaArrays/cholesky
Add Cholesky Instance
2 parents cc35f7b + de0eef5 commit e3f4568

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

docs/src/linearalgebra.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ running the full factorization. This thus allows for building types to hold the
2929
without having to perform expensive extra computations.
3030

3131
```@docs
32+
ArrayInterface.bunchkaufman_instance
33+
ArrayInterface.cholesky_instance
34+
ArrayInterface.ldlt_instance
3235
ArrayInterface.lu_instance
3336
ArrayInterface.qr_instance
3437
ArrayInterface.svd_instance

src/ArrayInterface.jl

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,89 @@ matrix_colors(A::Bidiagonal) = _cycle(1:2, Base.size(A, 2))
440440
matrix_colors(A::Union{Tridiagonal, SymTridiagonal}) = _cycle(1:3, Base.size(A, 2))
441441
_cycle(repetend, len) = repeat(repetend, div(len, length(repetend)) + 1)[1:len]
442442

443+
"""
444+
bunchkaufman_instance(A, pivot = LinearAlgebra.RowMaximum()) -> bunchkaufman_factorization_instance
445+
446+
Returns an instance of the Cholesky factorization object with the correct type
447+
cheaply.
448+
"""
449+
function bunchkaufman_instance(A::Matrix{T}) where T
450+
return bunchkaufman(similar(A, 0, 0), check = false)
451+
end
452+
function bunchkaufman_instance(A::SparseMatrixCSC)
453+
bunchkaufman(sparse(similar(A, 1, 1)), check = false)
454+
end
455+
456+
"""
457+
bunchkaufman_instance(a::Number) -> a
458+
459+
Returns the number.
460+
"""
461+
bunchkaufman_instance(a::Number) = a
462+
463+
"""
464+
bunchkaufman_instance(a::Any) -> cholesky(a, check=false)
465+
466+
Returns the number.
467+
"""
468+
bunchkaufman_instance(a::Any) = bunchkaufman(a, check = false)
469+
470+
"""
471+
cholesky_instance(A, pivot = LinearAlgebra.RowMaximum()) -> cholesky_factorization_instance
472+
473+
Returns an instance of the Cholesky factorization object with the correct type
474+
cheaply.
475+
"""
476+
function cholesky_instance(A::Matrix{T}, pivot = LinearAlgebra.RowMaximum()) where {T}
477+
return cholesky(similar(A, 0, 0), pivot, check = false)
478+
end
479+
function cholesky_instance(A::SparseMatrixCSC)
480+
cholesky(sparse(similar(A, 1, 1)), check = false)
481+
end
482+
483+
"""
484+
cholesky_instance(a::Number, pivot = LinearAlgebra.RowMaximum()) -> a
485+
486+
Returns the number.
487+
"""
488+
cholesky_instance(a::Number, pivot = LinearAlgebra.RowMaximum()) = a
489+
490+
"""
491+
cholesky_instance(a::Any, pivot = LinearAlgebra.RowMaximum()) -> cholesky(a, check=false)
492+
493+
Slow fallback which gets the instance via factorization. Should get
494+
specialized for new matrix types.
495+
"""
496+
cholesky_instance(a::Any, pivot = LinearAlgebra.RowMaximum()) = cholesky(a, pivot, check = false)
497+
498+
"""
499+
ldlt_instance(A) -> ldlt_factorization_instance
500+
501+
Returns an instance of the LDLT factorization object with the correct type
502+
cheaply.
503+
"""
504+
function ldlt_instance(A::Matrix{T}) where {T}
505+
return ldlt(SymTridiagonal(similar(A, 0, 0)), check = false)
506+
end
507+
function ldlt_instance(A::SparseMatrixCSC)
508+
ldlt(sparse(similar(A, 1, 1)), check = false)
509+
end
510+
511+
"""
512+
ldlt_instance(a::Number) -> a
513+
514+
Returns the number.
515+
"""
516+
ldlt_instance(a::Number) = a
517+
518+
"""
519+
ldlt_instance(a::Any) -> ldlt(a, check=false)
520+
521+
Slow fallback which gets the instance via factorization. Should get
522+
specialized for new matrix types.
523+
"""
524+
ldlt_instance(a::Any) = ldlt(a)
525+
443526
"""
444527
lu_instance(A) -> lu_factorization_instance
445528
@@ -478,7 +561,8 @@ lu_instance(a::Number) = a
478561
"""
479562
lu_instance(a::Any) -> lu(a, check=false)
480563
481-
Returns the number.
564+
Slow fallback which gets the instance via factorization. Should get
565+
specialized for new matrix types.
482566
"""
483567
lu_instance(a::Any) = lu(a, check = false)
484568

@@ -507,7 +591,8 @@ qr_instance(a::Number) = a
507591
"""
508592
qr_instance(a::Any) -> qr(a)
509593
510-
Returns the number.
594+
Slow fallback which gets the instance via factorization. Should get
595+
specialized for new matrix types.
511596
"""
512597
qr_instance(a::Any) = qr(a)# check = false)
513598

@@ -531,7 +616,8 @@ svd_instance(a::Number) = a
531616
"""
532617
svd_instance(a::Any) -> svd(a)
533618
534-
Returns the number.
619+
Slow fallback which gets the instance via factorization. Should get
620+
specialized for new matrix types.
535621
"""
536622
svd_instance(a::Any) = svd(a) #check = false)
537623

0 commit comments

Comments
 (0)