Skip to content

Commit 65382c7

Browse files
authored
Do not use zero(...) in generic_lu! algorithm to support Unitful (#38659)
* make generic_lu! accept unitful matrices * Add Furlong test for lu
1 parent 8629e21 commit 65382c7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

stdlib/LinearAlgebra/src/lu.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ function generic_lufact!(A::StridedMatrix{T}, ::Val{Pivot} = Val(true);
139139
for k = 1:minmn
140140
# find index max
141141
kp = k
142-
if Pivot
143-
amax = abs(zero(T))
144-
for i = k:m
142+
if Pivot && k < m
143+
amax = abs(A[k, k])
144+
for i = k+1:m
145145
absi = abs(A[i,k])
146146
if absi > amax
147147
kp = i

stdlib/LinearAlgebra/test/lu.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,22 @@ include("trickyarithmetic.jl")
324324
@test B isa LinearAlgebra.LU{ElT,Matrix{ElT}}
325325
end
326326

327+
# dimensional correctness:
328+
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
329+
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
330+
using .Main.Furlongs
331+
332+
@testset "lu factorization with dimension type" begin
333+
n = 4
334+
A = Matrix(Furlong(1.0) * I, n, n)
335+
F = lu(A).factors
336+
@test Diagonal(F) == Diagonal(A)
337+
# upper triangular part has a unit Furlong{1}
338+
@test all(x -> typeof(x) == Furlong{1, Float64}, F[i,j] for j=1:n for i=1:j)
339+
# lower triangular part is unitless Furlong{0}
340+
@test all(x -> typeof(x) == Furlong{0, Float64}, F[i,j] for j=1:n for i=j+1:n)
341+
end
342+
327343
@testset "Issue #30917. Determinant of integer matrix" begin
328344
@test det([1 1 0 0 1 0 0 0
329345
1 0 1 0 0 1 0 0

0 commit comments

Comments
 (0)