Skip to content

Commit

Permalink
sum for diagonal (and related) matrices (#32184)
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio authored and andreasnoack committed Jun 27, 2019
1 parent 042504d commit 8ff014f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,5 @@ function eigvecs(M::Bidiagonal{T}) where T
Q #Actually Triangular
end
eigen(M::Bidiagonal) = Eigen(eigvals(M), eigvecs(M))

Base._sum(A::Bidiagonal, ::Colon) = sum(A.dv) + sum(A.ev)
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,5 @@ end

cholesky(A::Diagonal, ::Val{false} = Val(false); check::Bool = true) =
cholesky!(cholcopy(A), Val(false); check = check)

Base._sum(A::Diagonal, ::Colon) = sum(A.diag)
3 changes: 3 additions & 0 deletions stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,6 @@ function SymTridiagonal{T}(M::Tridiagonal) where T
throw(ArgumentError("Tridiagonal is not symmetric, cannot convert to SymTridiagonal"))
end
end

Base._sum(A::Tridiagonal, ::Colon) = sum(A.d) + sum(A.dl) + sum(A.du)
Base._sum(A::SymTridiagonal, ::Colon) = sum(A.dv) + 2sum(A.ev)
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,9 @@ end
@test vcat((Aub\bb)...) UpperTriangular(A)\b
end

@testset "sum" begin
@test sum(Bidiagonal([1,2,3], [1,2], :U)) == 9
@test sum(Bidiagonal([1,2,3], [1,2], :L)) == 9
end

end # module TestBidiagonal
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,8 @@ end
@test E.vectors == [0 1 0; 1 0 0; 0 0 1]
end

@testset "sum" begin
@test sum(Diagonal([1,2,3])) == 6
end

end # module TestDiagonal
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,9 @@ end
@test cond(SymTridiagonal([1,2,3], [0,0])) 3
end

@testset "sum" begin
@test sum(Tridiagonal([1,2], [1,2,3], [7,8])) == 24
@test sum(SymTridiagonal([1,2,3], [1,2])) == 12
end

end # module TestTridiagonal

0 comments on commit 8ff014f

Please sign in to comment.