Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
permutedims, permuterows!, power_by_squaring, promote_rule, real, sec, sech, setindex!,
show, similar, sin, sincos, sinh, size, sqrt, strides, stride, tan, tanh, transpose, trunc,
typed_hcat, vec, view, zero
import Base: AbstractArray, AbstractMatrix, Array, Matrix
using Base: IndexLinear, promote_eltype, promote_op, print_matrix,
@propagate_inbounds, reduce, typed_hvcat, typed_vcat, require_one_based_indexing,
splat, BitInteger
Expand Down
4 changes: 2 additions & 2 deletions src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ const AdjointAbsVec{T} = Adjoint{T,<:AbstractVector}
const AdjointAbsMat{T} = Adjoint{T,<:AbstractMatrix}
const TransposeAbsVec{T} = Transpose{T,<:AbstractVector}
const TransposeAbsMat{T} = Transpose{T,<:AbstractMatrix}
const AdjOrTransAbsVec{T} = AdjOrTrans{T,<:AbstractVector}
const AdjOrTransAbsMat{T} = AdjOrTrans{T,<:AbstractMatrix}
const AdjOrTransAbsVec{T,V<:AbstractVector} = AdjOrTrans{T,V}
const AdjOrTransAbsMat{T,M<:AbstractMatrix} = AdjOrTrans{T,M}

# for internal use below
wrapperop(_) = identity
Expand Down
13 changes: 12 additions & 1 deletion src/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export
trsm!,
trsm

using ..LinearAlgebra: libblastrampoline, BlasReal, BlasComplex, BlasFloat, BlasInt, DimensionMismatch, checksquare, chkstride1
using ..LinearAlgebra: libblastrampoline, BlasReal, BlasComplex, BlasFloat, BlasInt,
DimensionMismatch, checksquare, chkstride1, SingularException

include("lbt.jl")

Expand Down Expand Up @@ -1369,6 +1370,11 @@ for (fname, elty) in ((:dtrsv_,:Float64),
throw(DimensionMismatch(lazy"size of A is $n != length(x) = $(length(x))"))
end
chkstride1(A)
if diag == 'N'
for i in 1:n
iszero(A[i,i]) && throw(SingularException(i))
end
end
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
GC.@preserve x ccall((@blasfunc($fname), libblastrampoline), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ref{UInt8}, Ref{BlasInt},
Expand Down Expand Up @@ -2217,6 +2223,11 @@ for (mmname, smname, elty) in
end
chkstride1(A)
chkstride1(B)
if diag == 'N'
for i in 1:k
iszero(A[i,i]) && throw(SingularException(i))
end
end
ccall((@blasfunc($smname), libblastrampoline), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ref{UInt8}, Ref{UInt8},
Ref{BlasInt}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
Expand Down
3 changes: 2 additions & 1 deletion src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,8 @@ sqrt(::AbstractMatrix)
function sqrt(A::AbstractMatrix{T}) where {T<:Union{Real,Complex}}
if checksquare(A) == 0
return copy(float(A))
elseif isdiag(A)
elseif isdiag(A) && (T <: Complex || all(x -> x ≥ zero(x), diagview(A)))
# Real Diagonal sqrt requires each diagonal element to be positive
return applydiagonal(sqrt, A)
elseif ishermitian(A)
sqrtHermA = sqrt(Hermitian(A))
Expand Down
52 changes: 38 additions & 14 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,28 @@ function (*)(D::Diagonal, V::AbstractVector)
return D.diag .* V
end

function _diag_adj_mul(A::AdjOrTransAbsMat, D::Diagonal)
adj = wrapperop(A)
copy(adj(adj(D) * adj(A)))
end
function _diag_adj_mul(A::AdjOrTransAbsMat{<:Number, <:StridedMatrix}, D::Diagonal{<:Number})
@invoke *(A::AbstractMatrix, D::AbstractMatrix)
end
function _diag_adj_mul(D::Diagonal, A::AdjOrTransAbsMat)
adj = wrapperop(A)
copy(adj(adj(A) * adj(D)))
end
function _diag_adj_mul(D::Diagonal{<:Number}, A::AdjOrTransAbsMat{<:Number, <:StridedMatrix})
@invoke *(D::AbstractMatrix, A::AbstractMatrix)
end

function (*)(A::AdjOrTransAbsMat, D::Diagonal)
_diag_adj_mul(A, D)
end
function (*)(D::Diagonal, A::AdjOrTransAbsMat)
_diag_adj_mul(D, A)
end

function rmul!(A::AbstractMatrix, D::Diagonal)
matmul_size_check(size(A), size(D))
for I in CartesianIndices(A)
Expand Down Expand Up @@ -671,22 +693,24 @@ end
for Tri in (:UpperTriangular, :LowerTriangular)
UTri = Symbol(:Unit, Tri)
# 2 args
for (fun, f) in zip((:*, :rmul!, :rdiv!, :/), (:identity, :identity, :inv, :inv))
@eval $fun(A::$Tri, D::Diagonal) = $Tri($fun(A.data, D))
@eval $fun(A::$UTri, D::Diagonal) = $Tri(_setdiag!($fun(A.data, D), $f, D.diag))
for (fun, f) in zip((:mul, :rmul!, :rdiv!, :/), (:identity, :identity, :inv, :inv))
g = fun == :mul ? :* : fun
@eval $fun(A::$Tri, D::Diagonal) = $Tri($g(A.data, D))
@eval $fun(A::$UTri, D::Diagonal) = $Tri(_setdiag!($g(A.data, D), $f, D.diag))
end
@eval *(A::$Tri{<:Any, <:StridedMaybeAdjOrTransMat}, D::Diagonal) =
@invoke *(A::AbstractMatrix, D::Diagonal)
@eval *(A::$UTri{<:Any, <:StridedMaybeAdjOrTransMat}, D::Diagonal) =
@invoke *(A::AbstractMatrix, D::Diagonal)
for (fun, f) in zip((:*, :lmul!, :ldiv!, :\), (:identity, :identity, :inv, :inv))
@eval $fun(D::Diagonal, A::$Tri) = $Tri($fun(D, A.data))
@eval $fun(D::Diagonal, A::$UTri) = $Tri(_setdiag!($fun(D, A.data), $f, D.diag))
@eval mul(A::$Tri{<:Any, <:StridedMaybeAdjOrTransMat}, D::Diagonal) =
@invoke mul(A::AbstractMatrix, D::Diagonal)
@eval mul(A::$UTri{<:Any, <:StridedMaybeAdjOrTransMat}, D::Diagonal) =
@invoke mul(A::AbstractMatrix, D::Diagonal)
for (fun, f) in zip((:mul, :lmul!, :ldiv!, :\), (:identity, :identity, :inv, :inv))
g = fun == :mul ? :* : fun
@eval $fun(D::Diagonal, A::$Tri) = $Tri($g(D, A.data))
@eval $fun(D::Diagonal, A::$UTri) = $Tri(_setdiag!($g(D, A.data), $f, D.diag))
end
@eval *(D::Diagonal, A::$Tri{<:Any, <:StridedMaybeAdjOrTransMat}) =
@invoke *(D::Diagonal, A::AbstractMatrix)
@eval *(D::Diagonal, A::$UTri{<:Any, <:StridedMaybeAdjOrTransMat}) =
@invoke *(D::Diagonal, A::AbstractMatrix)
@eval mul(D::Diagonal, A::$Tri{<:Any, <:StridedMaybeAdjOrTransMat}) =
@invoke mul(D::Diagonal, A::AbstractMatrix)
@eval mul(D::Diagonal, A::$UTri{<:Any, <:StridedMaybeAdjOrTransMat}) =
@invoke mul(D::Diagonal, A::AbstractMatrix)
# 3-arg ldiv!
@eval ldiv!(C::$Tri, D::Diagonal, A::$Tri) = $Tri(ldiv!(C.data, D, A.data))
@eval ldiv!(C::$Tri, D::Diagonal, A::$UTri) = $Tri(_setdiag!(ldiv!(C.data, D, A.data), inv, D.diag))
Expand Down
5 changes: 4 additions & 1 deletion src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ julia> [1 1; 0 1] * [1 0; 1 1]
1 1
```
"""
function (*)(A::AbstractMatrix, B::AbstractMatrix)
(*)(A::AbstractMatrix, B::AbstractMatrix) = mul(A, B)
# we add an extra level of indirection to avoid ambiguities in *
function mul(A::AbstractMatrix, B::AbstractMatrix)
TS = promote_op(matprod, eltype(A), eltype(B))
mul!(matprod_dest(A, B, TS), A, B)
end
Expand Down Expand Up @@ -1021,6 +1023,7 @@ function _generic_matmatmul_nonadjtrans!(C, A, B, alpha, beta)
@inbounds for n in axes(B, 2), k in axes(B, 1)
# Balpha = B[k,n] * alpha, but we skip the multiplication in case isone(alpha)
Balpha = @stable_muladdmul MulAddMul(alpha, false)(B[k,n])
!ismissing(Balpha) && iszero(Balpha) && continue
@simd for m in axes(A, 1)
C[m,n] = muladd(A[m,k], Balpha, C[m,n])
end
Expand Down
8 changes: 5 additions & 3 deletions src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1223,11 +1223,13 @@ function generic_mattrimul!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function,
end
end
# division
function generic_trimatdiv!(C::StridedVecOrMat{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractVecOrMat{T}) where {T<:BlasFloat}
generic_trimatdiv!(C::StridedVector{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractVector{T}) where {T<:BlasFloat} =
BLAS.trsv!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : copyto!(C, B))
function generic_trimatdiv!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractMatrix{T}) where {T<:BlasFloat}
if stride(C,1) == stride(A,1) == 1
LAPACK.trtrs!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : copyto!(C, B))
BLAS.trsm!('L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), A, C === B ? C : copyto!(C, B))
else # incompatible with LAPACK
@invoke generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, tfun::Function, A::AbstractMatrix, B::AbstractVecOrMat)
@invoke generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, tfun::Function, A::AbstractMatrix, B::AbstractMatrix)
end
end
function generic_mattridiv!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::AbstractMatrix{T}, B::StridedMatrix{T}) where {T<:BlasFloat}
Expand Down
6 changes: 6 additions & 0 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,12 @@ end
@testset "sqrt for diagonal" begin
A = diagm(0 => [1, 2, 3])
@test sqrt(A)^2 ≈ A

A = diagm(0 => [1.0, -1.0])
@test sqrt(A) == diagm(0 => [1.0, 1.0im])
@test sqrt(A)^2 ≈ A
B = im*A
@test sqrt(B)^2 ≈ B
end

@testset "issue #40141" begin
Expand Down
1 change: 1 addition & 0 deletions test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ import LinearAlgebra: Adjoint, Transpose
(*)(x::RootInt, y::Integer) = x.i * y
adjoint(x::RootInt) = x
transpose(x::RootInt) = x
Base.zero(::RootInt) = RootInt(0)

@test Base.promote_op(*, RootInt, RootInt) === Int

Expand Down
2 changes: 2 additions & 0 deletions test/testtriag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ function test_triangular(elty1_types)
@test_throws DimensionMismatch transpose(Ann) \ bm
if t1 == UpperTriangular || t1 == LowerTriangular
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n))
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n, 2))
@test_throws SingularException rdiv!(fill(eltyB(1), n, n), t1(zeros(elty1, n, n)))
end
@test B / A1 ≈ B / M1
@test B / transpose(A1) ≈ B / transpose(M1)
Expand Down
7 changes: 6 additions & 1 deletion test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,13 @@ end
end
end

@testset "(l/r)mul! and (l/r)div! for non-contiguous matrices" begin
@testset "(l/r)mul! and (l/r)div! for non-contiguous arrays" begin
U = UpperTriangular(reshape(collect(3:27.0),5,5))
b = float.(1:10)
b2 = copy(b); b2v = view(b2, 1:2:9); b2vc = copy(b2v)
@test lmul!(U, b2v) == lmul!(U, b2vc)
b2 = copy(b); b2v = view(b2, 1:2:9); b2vc = copy(b2v)
@test ldiv!(U, b2v) ≈ ldiv!(U, b2vc)
B = float.(collect(reshape(1:100, 10,10)))
B2 = copy(B); B2v = view(B2, 1:2:9, 1:5); B2vc = copy(B2v)
@test lmul!(U, B2v) == lmul!(U, B2vc)
Expand Down