Skip to content

Commit 8e68d82

Browse files
committed
Higher-order functions in factorize to obtain structure
1 parent 8ab7e09 commit 8e68d82

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/dense.jl

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,38 +1541,8 @@ function factorize(A::AbstractMatrix{T}) where T
15411541
m, n = size(A)
15421542
if m == n
15431543
if m == 1 return A[1] end
1544-
utri = true
1545-
utri1 = true
1546-
herm = true
1547-
sym = true
1548-
for j = 1:n-1, i = j+1:m
1549-
if utri1
1550-
if A[i,j] != 0
1551-
utri1 = i == j + 1
1552-
utri = false
1553-
end
1554-
end
1555-
if sym
1556-
sym &= A[i,j] == A[j,i]
1557-
end
1558-
if herm
1559-
herm &= A[i,j] == conj(A[j,i])
1560-
end
1561-
if !(utri1|herm|sym) break end
1562-
end
1563-
ltri = true
1564-
ltri1 = true
1565-
for j = 3:n, i = 1:j-2
1566-
ltri1 &= A[i,j] == 0
1567-
if !ltri1 break end
1568-
end
1544+
utri, utri1, ltri, ltri1, sym, herm = getstructure(A)
15691545
if ltri1
1570-
for i = 1:n-1
1571-
if A[i,i+1] != 0
1572-
ltri &= false
1573-
break
1574-
end
1575-
end
15761546
if ltri
15771547
if utri
15781548
return Diagonal(A)
@@ -1610,6 +1580,54 @@ factorize(A::Adjoint) = adjoint(factorize(parent(A)))
16101580
factorize(A::Transpose) = transpose(factorize(parent(A)))
16111581
factorize(a::Number) = a # same as how factorize behaves on Diagonal types
16121582

1583+
function getstructure(A::StridedMatrix)
1584+
m, n = size(A)
1585+
if m == 1 return A[1] end
1586+
utri = true
1587+
utri1 = true
1588+
herm = true
1589+
sym = true
1590+
for j = 1:n-1, i = j+1:m
1591+
if utri1
1592+
if A[i,j] != 0
1593+
utri1 = i == j + 1
1594+
utri = false
1595+
end
1596+
end
1597+
if sym
1598+
sym &= A[i,j] == A[j,i]
1599+
end
1600+
if herm
1601+
herm &= A[i,j] == conj(A[j,i])
1602+
end
1603+
if !(utri1|herm|sym) break end
1604+
end
1605+
ltri = true
1606+
ltri1 = true
1607+
for j = 3:n, i = 1:j-2
1608+
ltri1 &= A[i,j] == 0
1609+
if !ltri1 break end
1610+
end
1611+
if ltri1
1612+
for i = 1:n-1
1613+
if A[i,i+1] != 0
1614+
ltri &= false
1615+
break
1616+
end
1617+
end
1618+
end
1619+
return (utri, utri1, ltri, ltri1, sym, herm)
1620+
end
1621+
function getstructure(A::AbstractMatrix)
1622+
utri = istriu(A)
1623+
utri1 = istriu(A,-1)
1624+
ltri = istril(A)
1625+
ltri1 = istril(A,1)
1626+
sym = issymmetric(A)
1627+
herm = ishermitian(A)
1628+
return (utri, utri1, ltri, ltri1, sym, herm)
1629+
end
1630+
16131631
## Moore-Penrose pseudoinverse
16141632

16151633
"""

test/dense.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module TestDense
44

55
using Test, LinearAlgebra, Random
66
using LinearAlgebra: BlasComplex, BlasFloat, BlasReal
7+
using Test: GenericArray
78

89
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
910
isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
@@ -191,6 +192,8 @@ bimg = randn(n,2)/2
191192
f = rand(eltya,n-2)
192193
A = diagm(0 => d)
193194
@test factorize(A) == Diagonal(d)
195+
# test that the generic structure-evaluation method works
196+
@test factorize(A) == factorize(GenericArray(A))
194197
A += diagm(-1 => e)
195198
@test factorize(A) == Bidiagonal(d,e,:L)
196199
A += diagm(-2 => f)

0 commit comments

Comments
 (0)