Skip to content

Commit 230e603

Browse files
Merge pull request #39 from JuliaDiffEq/myb/fact
Add `lu_instance`
2 parents e9c61fd + c8c90e6 commit 230e603

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.4.1"
3+
version = "2.5.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -16,7 +16,8 @@ BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
1616
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
1717
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
1818
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
19+
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
1920
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2021

2122
[targets]
22-
test = ["Test", "LabelledArrays", "StaticArrays", "BandedMatrices", "BlockBandedMatrices"]
23+
test = ["Test", "LabelledArrays", "StaticArrays", "BandedMatrices", "BlockBandedMatrices", "SuiteSparse"]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ A `getindex` which is always allowed.
6060

6161
A `setindex!` which is always allowed.
6262

63+
## lu_instance(A)
64+
65+
Return an instance of the LU factorization object with the correct type
66+
cheaply.
67+
6368
## List of things to add
6469

6570
- https://github.com/JuliaLang/julia/issues/22216

src/ArrayInterface.jl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ abstract type ColoringAlgorithm end
346346
"""
347347
fast_matrix_colors(A)
348348
349-
Query whether a matrix has a fast algorithm for getting the structural
350-
colors of the matrix.
349+
Query whether a matrix has a fast algorithm for getting the structural
350+
colors of the matrix.
351351
"""
352352
fast_matrix_colors(A) = false
353353
fast_matrix_colors(A::AbstractArray) = fast_matrix_colors(typeof(A))
@@ -356,8 +356,8 @@ fast_matrix_colors(A::Type{<:Union{Diagonal,Bidiagonal,Tridiagonal,SymTridiagona
356356
"""
357357
matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
358358
359-
The color vector for dense matrix and triangular matrix is simply
360-
`[1,2,3,...,size(A,2)]`
359+
The color vector for dense matrix and triangular matrix is simply
360+
`[1,2,3,...,size(A,2)]`
361361
"""
362362
function matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
363363
eachindex(1:size(A,2)) # Vector size matches number of rows
@@ -379,12 +379,48 @@ function matrix_colors(A::Union{Tridiagonal,SymTridiagonal})
379379
_cycle(1:3,size(A,2))
380380
end
381381

382+
"""
383+
lu_instance(A) -> lu_factorization_instance
384+
385+
Return an instance of the LU factorization object with the correct type
386+
cheaply.
387+
"""
388+
function lu_instance(A::Matrix{T}) where T
389+
noUnitT = typeof(zero(T))
390+
luT = LinearAlgebra.lutype(noUnitT)
391+
ipiv = Vector{LinearAlgebra.BlasInt}(undef, 0)
392+
info = zero(LinearAlgebra.BlasInt)
393+
return LU{luT}(similar(A, 0, 0), ipiv, info)
394+
end
395+
396+
"""
397+
lu_instance(a::Number) -> a
398+
399+
Return the number.
400+
"""
401+
lu_instance(a::Number) = a
402+
382403
function __init__()
383404

405+
@require SuiteSparse="4607b0f0-06f3-5cda-b6b1-a6196a1729e9" begin
406+
lu_instance(jac_prototype::SparseMatrixCSC) = SuiteSparse.UMFPACK.UmfpackLU(Ptr{Cvoid}(), Ptr{Cvoid}(), 1, 1,
407+
jac_prototype.colptr[1:1],
408+
jac_prototype.rowval[1:1],
409+
jac_prototype.nzval[1:1],
410+
0)
411+
end
412+
384413
@require StaticArrays="90137ffa-7385-5640-81b9-e52037218182" begin
385414
ismutable(::Type{<:StaticArrays.StaticArray}) = false
386415
can_setindex(::Type{<:StaticArrays.StaticArray}) = false
387416
ismutable(::Type{<:StaticArrays.MArray}) = true
417+
function lu_instance(_A::StaticArrays.StaticMatrix{N,N}) where {N}
418+
A = StaticArrays.SArray(_A)
419+
L = LowerTriangular(A)
420+
U = UpperTriangular(A)
421+
p = StaticArrays.SVector{N,Int}(1:N)
422+
return StaticArrays.LU(L, U, p)
423+
end
388424
end
389425

390426
@require LabelledArrays="2ee39098-c373-598a-b85f-a56591580800" begin

test/runtests.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ArrayInterface, Test
22
using Base: setindex
3-
import ArrayInterface: has_sparsestruct, findstructralnz, fast_scalar_indexing
3+
import ArrayInterface: has_sparsestruct, findstructralnz, fast_scalar_indexing, lu_instance
44
@test ArrayInterface.ismutable(rand(3))
55

66
using StaticArrays
@@ -127,3 +127,16 @@ rowind,colind=findstructralnz(BBB)
127127
@test all(isone, y2)
128128
end
129129
end
130+
131+
using SuiteSparse
132+
@testset "lu_instance" begin
133+
for A in [
134+
randn(5, 5),
135+
@SMatrix(randn(5, 5)),
136+
@MMatrix(randn(5, 5)),
137+
sprand(50, 50, 0.5)
138+
]
139+
@test lu_instance(A) isa typeof(lu(A))
140+
end
141+
@test lu_instance(1) === 1
142+
end

0 commit comments

Comments
 (0)