Skip to content

Commit f41df82

Browse files
authored
Merge branch 'master' into SEBAtest
2 parents afb4894 + 5d3ef46 commit f41df82

File tree

12 files changed

+101
-10
lines changed

12 files changed

+101
-10
lines changed

src/LinearAlgebra.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
1313
copy, copyto!, copymutable, cos, cosh, cot, coth, csc, csch, eltype, exp, fill!, floor,
1414
getindex, hcat, getproperty, imag, inv, invpermuterows!, isapprox, isequal, isone, iszero,
1515
IndexStyle, kron, kron!, length, log, map, ndims, one, oneunit, parent, permutecols!,
16-
permutedims, permuterows!, power_by_squaring, promote_rule, real, sec, sech, setindex!,
16+
permutedims, permuterows!, power_by_squaring, promote_rule, real, isreal, sec, sech, setindex!,
1717
show, similar, sin, sincos, sinh, size, sqrt, strides, stride, tan, tanh, transpose, trunc,
1818
typed_hcat, vec, view, zero
1919
import Base: AbstractArray, AbstractMatrix, Array, Matrix

src/bidiag.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ axes(M::Bidiagonal) = (ax = axes(M.dv, 1); (ax, ax))
286286
for func in (:conj, :copy, :real, :imag)
287287
@eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.uplo)
288288
end
289+
isreal(M::Bidiagonal) = isreal(M.dv) && isreal(M.ev)
289290

290291
adjoint(B::Bidiagonal{<:Number}) = Bidiagonal(vec(adjoint(B.dv)), vec(adjoint(B.ev)), B.uplo == 'U' ? :L : :U)
291292
adjoint(B::Bidiagonal{<:Number, <:Base.ReshapedArray{<:Number,1,<:Adjoint}}) =

src/diagonal.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ factorize(D::Diagonal) = D
256256
real(D::Diagonal) = Diagonal(real(D.diag))
257257
imag(D::Diagonal) = Diagonal(imag(D.diag))
258258

259+
isreal(D::Diagonal) = isreal(D.diag)
260+
259261
iszero(D::Diagonal) = all(iszero, D.diag)
260262
isone(D::Diagonal) = all(isone, D.diag)
261263
isdiag(D::Diagonal) = all(isdiag, D.diag)

src/generic.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,15 @@ ishermitian(x::Number) = (x == conj(x))
13581358
_iszero(V) = iszero(V)
13591359
# A Base.FastContiguousSubArray view of a StridedArray
13601360
FastContiguousSubArrayStrided{T,N,P<:StridedArray,I<:Tuple{AbstractUnitRange, Vararg{Any}}} = Base.SubArray{T,N,P,I,true}
1361-
# using mapreduce instead of all permits vectorization
1362-
_iszero(V::FastContiguousSubArrayStrided) = mapreduce(iszero, &, V, init=true)
1361+
# Reducing over the entire array instead of calling `all` within `iszero` permits vectorization
1362+
# The loop is equivalent to a mapreduce, but is faster to compile
1363+
function _iszero(V::FastContiguousSubArrayStrided)
1364+
ret = true
1365+
for i in eachindex(V)
1366+
ret &= iszero(@inbounds V[i])
1367+
end
1368+
ret
1369+
end
13631370

13641371
"""
13651372
istriu(A::AbstractMatrix, k::Integer = 0) -> Bool

src/symmetric.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ issymmetric(A::Hermitian{<:Real}) = true
441441
issymmetric(A::Hermitian{<:Complex}) = isreal(A)
442442
issymmetric(A::Symmetric) = true
443443

444+
# check if the symmetry is known from the type
445+
_issymmetric(::Union{SymSymTri, Hermitian{<:Real}}) = true
446+
_issymmetric(::Any) = false
447+
444448
adjoint(A::Hermitian) = A
445449
transpose(A::Symmetric) = A
446450
adjoint(A::Symmetric{<:Real}) = A

src/triangular.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ istril(A::Transpose, k::Integer=0) = istriu(A.parent, -k)
376376
istriu(A::Adjoint, k::Integer=0) = istril(A.parent, -k)
377377
istriu(A::Transpose, k::Integer=0) = istril(A.parent, -k)
378378

379+
istril(U::UpperTriangular, k::Integer=0) = istril(parent(U), max(-1, k))
380+
istril(U::UnitUpperTriangular, k::Integer=0) = k < 0 ? false : istril(parent(U), k)
381+
istriu(U::LowerTriangular, k::Integer=0) = istriu(parent(U), min(1, k))
382+
istriu(U::UnitLowerTriangular, k::Integer=0) = k > 0 ? false : istriu(parent(U), k)
383+
379384
function tril!(A::UpperTriangular{T}, k::Integer=0) where {T}
380385
if k < 0
381386
fill!(A.data, zero(T))

src/tridiag.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,14 @@ function (::Type{SymTri})(A::AbstractMatrix) where {SymTri <: SymTridiagonal}
111111
checksquare(A)
112112
du = diag(A, 1)
113113
d = diag(A)
114-
dl = diag(A, -1)
115-
if _checksymmetric(d, du, dl)
116-
SymTri(d, du)
117-
else
114+
if !(_issymmetric(A) || _checksymmetric(d, du, diag(A, -1)))
118115
throw(ArgumentError("matrix is not symmetric; cannot convert to SymTridiagonal"))
119116
end
117+
return SymTri(d, du)
120118
end
121119

122120
_checksymmetric(d, du, dl) = all(((x, y),) -> x == transpose(y), zip(du, dl)) && all(issymmetric, d)
123-
_checksymmetric(A::AbstractMatrix) = _checksymmetric(diagview(A), diagview(A, 1), diagview(A, -1))
121+
_checksymmetric(A::AbstractMatrix) = _issymmetric(A) || _checksymmetric(diagview(A), diagview(A, 1), diagview(A, -1))
124122

125123
SymTridiagonal{T,V}(S::SymTridiagonal{T,V}) where {T,V<:AbstractVector{T}} = S
126124
SymTridiagonal{T,V}(S::SymTridiagonal) where {T,V<:AbstractVector{T}} =
@@ -173,6 +171,7 @@ _copyto_banded!(dest::SymTridiagonal, src::SymTridiagonal) =
173171
for func in (:conj, :copy, :real, :imag)
174172
@eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev))
175173
end
174+
isreal(S::SymTridiagonal) = isreal(S.dv) && isreal(S.ev)
176175

177176
transpose(S::SymTridiagonal) = S
178177
adjoint(S::SymTridiagonal{<:Number}) = SymTridiagonal(vec(adjoint(S.dv)), vec(adjoint(S.ev)))
@@ -667,6 +666,7 @@ for func in (:conj, :copy, :real, :imag)
667666
Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du))
668667
end
669668
end
669+
isreal(T::Tridiagonal) = isreal(T.dl) && isreal(T.d) && isreal(T.du)
670670

671671
adjoint(S::Tridiagonal{<:Number}) = Tridiagonal(vec(adjoint(S.du)), vec(adjoint(S.d)), vec(adjoint(S.dl)))
672672
adjoint(S::Tridiagonal{<:Number, <:Base.ReshapedArray{<:Number,1,<:Adjoint}}) =

test/bidiag.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,4 +1172,12 @@ end
11721172
@test_throws InexactError convert(Bidiagonal, M)
11731173
end
11741174

1175+
@testset "isreal" begin
1176+
M = Bidiagonal(ones(2), ones(1), :U)
1177+
@test @inferred((M -> Val(isreal(M)))(M)) == Val(true)
1178+
M = complex.(M)
1179+
@test isreal(M)
1180+
@test !isreal(im*M)
1181+
end
1182+
11751183
end # module TestBidiagonal

test/diagonal.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,4 +1481,12 @@ end
14811481
@test ishermitian(D) == ishermitian(A)
14821482
end
14831483

1484+
@testset "isreal" begin
1485+
D = Diagonal(ones(2))
1486+
@test @inferred((D -> Val(isreal(D)))(D)) == Val(true)
1487+
D = complex.(D)
1488+
@test isreal(D)
1489+
@test !isreal(im*D)
1490+
end
1491+
14841492
end # module TestDiagonal

test/generic.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ end
167167
@test_throws DimensionMismatch mul!(similar(a), a, Diagonal(Vector{Float64}(undef, an+1)))
168168
@test mul!(similar(a), a, Diagonal(1.:an)) == a.*Vector(1:an)'
169169
@test mul!(similar(a), a, Diagonal(1:an)) == a.*Vector(1:an)'
170+
171+
@testset "different axes" begin
172+
O = OffsetArray(similar(a), ntuple(_->2, ndims(a)))
173+
@test mul!(O, a, 2) == OffsetArray(2a, axes(O))
174+
@test mul!(O, 2, a) == OffsetArray(2a, axes(O))
175+
end
170176
end
171177

172178
@testset "Scaling with 5-argument mul!" begin
@@ -180,6 +186,20 @@ end
180186
@test mul!(copy(a), Diagonal([1; 2]), a, 10, 100) == 10a.*[1; 2] .+ 100a
181187
@test mul!(copy(a), a, Diagonal(1.:an), 10, 100) == 10a.*Vector(1:an)' .+ 100a
182188
@test mul!(copy(a), a, Diagonal(1:an), 10, 100) == 10a.*Vector(1:an)' .+ 100a
189+
190+
@testset "different axes" begin
191+
if eltype(a) <: Number
192+
O = OffsetArray(ones(size(a)), ntuple(_->2, ndims(a)))
193+
@test mul!(copy(O), a, 2, 3, 4) == OffsetArray(6a .+ 4, axes(O))
194+
@test mul!(copy(O), 2, a, 3, 4) == OffsetArray(6a .+ 4, axes(O))
195+
@test mul!(copy(O), a, 2, 3, 0) == OffsetArray(6a, axes(O))
196+
@test mul!(copy(O), 2, a, 3, 0) == OffsetArray(6a, axes(O))
197+
@test mul!(copy(O), a, 2, 1, 4) == OffsetArray(2a .+ 4, axes(O))
198+
@test mul!(copy(O), 2, a, 1, 4) == OffsetArray(2a .+ 4, axes(O))
199+
@test mul!(copy(O), a, 2, 1, 0) == OffsetArray(2a, axes(O))
200+
@test mul!(copy(O), 2, a, 1, 0) == OffsetArray(2a, axes(O))
201+
end
202+
end
183203
end
184204
end
185205
end

0 commit comments

Comments
 (0)