Skip to content

Commit c455213

Browse files
authored
More mul! overloads needed by BandedBlockBandedMatrix (#165)
* Update mul.jl * systematic overloading * Update test_layoutarray.jl * ambiguities with Fill multiplication * Update test_layoutarray.jl * v0.2.1 * Update mul.jl * Update test_layoutarray.jl
1 parent cd5452a commit c455213

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ArrayLayouts"
22
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
33
authors = ["Sheehan Olver <solver@mac.com>"]
4-
version = "1.2.0"
4+
version = "1.2.1"
55

66
[deps]
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"

src/ArrayLayouts.jl

+8
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ getindex(A::AdjOrTrans{<:Any,<:LayoutVector}, kr::Integer, jr::AbstractVector) =
203203
*(a::Adjoint{T, <:LayoutMatrix{T}} where T, b::Zeros{<:Any, 2}) = FillArrays.mult_zeros(a, b)
204204
*(A::Adjoint{<:Any, <:Zeros{<:Any,1}}, B::Diagonal{<:Any,<:LayoutVector}) = (B' * A')'
205205
*(A::Transpose{<:Any, <:Zeros{<:Any,1}}, B::Diagonal{<:Any,<:LayoutVector}) = transpose(transpose(B) * transpose(A))
206+
*(a::Adjoint{<:Number,<:LayoutVector}, b::Zeros{<:Number,1})= FillArrays._adjvec_mul_zeros(a, b)
207+
function *(a::Transpose{T, <:LayoutVector{T}}, b::Zeros{T, 1}) where T<:Real
208+
la, lb = length(a), length(b)
209+
if la lb
210+
throw(DimensionMismatch("dot product arguments have lengths $la and $lb"))
211+
end
212+
return zero(T)
213+
end
206214

207215
*(A::Diagonal{<:Any,<:LayoutVector}, B::Diagonal{<:Any,<:LayoutVector}) = mul(A, B)
208216
*(A::Diagonal{<:Any,<:LayoutVector}, B::AbstractMatrix) = mul(A, B)

src/mul.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,20 @@ end
317317

318318

319319
# mul! for subarray of layout matrix
320-
LinearAlgebra.mul!(C::SubArray{<:Any,2,<:LayoutMatrix}, A::SubArray{<:Any,2,<:LayoutMatrix}, B::SubArray{<:Any,2,<:LayoutMatrix}, α::Number, β::Number) =
320+
const SubLayoutMatrix = Union{SubArray{<:Any,2,<:LayoutMatrix}, SubArray{<:Any,2,<:AdjOrTrans{<:Any,<:LayoutMatrix}}}
321+
322+
LinearAlgebra.mul!(C::AbstractMatrix, A::SubLayoutMatrix, B::AbstractMatrix, α::Number, β::Number) =
323+
ArrayLayouts.mul!(C, A, B, α, β)
324+
LinearAlgebra.mul!(C::AbstractMatrix, A::AbstractMatrix, B::SubLayoutMatrix, α::Number, β::Number) =
325+
ArrayLayouts.mul!(C, A, B, α, β)
326+
LinearAlgebra.mul!(C::AbstractMatrix, A::SubLayoutMatrix, B::LayoutMatrix, α::Number, β::Number) =
321327
ArrayLayouts.mul!(C, A, B, α, β)
322-
LinearAlgebra.mul!(C::AbstractMatrix, A::SubArray{<:Any,2,<:LayoutMatrix}, B::SubArray{<:Any,2,<:LayoutMatrix}, α::Number, β::Number) =
328+
LinearAlgebra.mul!(C::AbstractMatrix, A::LayoutMatrix, B::SubLayoutMatrix, α::Number, β::Number) =
323329
ArrayLayouts.mul!(C, A, B, α, β)
330+
LinearAlgebra.mul!(C::AbstractMatrix, A::SubLayoutMatrix, B::SubLayoutMatrix, α::Number, β::Number) =
331+
ArrayLayouts.mul!(C, A, B, α, β)
332+
LinearAlgebra.mul!(C::AbstractVector, A::SubLayoutMatrix, B::AbstractVector, α::Number, β::Number) =
333+
ArrayLayouts.mul!(C, A, B, α, β)
324334

325335

326336
###

src/muladd.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ function _default_blasmul!(::IndexCartesian, α, A::AbstractMatrix, B::AbstractV
224224
z = zero(A[1,1]*B[1] + A[1,1]*B[1])
225225

226226
@inbounds for k in colsupport(B,1)
227-
b = B[k]
227+
b = α * B[k]
228228
for i = colsupport(A,k)
229-
C[i] += α * A[i,k] * b
229+
C[i] += A[i,k] * b
230230
end
231231
end
232232

test/test_layoutarray.jl

+27
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
361361

362362
b = MyVector(randn(5))
363363
@test A' * b A' * b.A
364+
365+
@test b'*Zeros(5) == 0
366+
@test transpose(b)*Zeros(5) == 0
367+
@test_throws DimensionMismatch b'*Zeros(6)
368+
@test_throws DimensionMismatch transpose(b)*Zeros(6)
364369
end
365370

366371
@testset "AbstractQ" begin
@@ -474,6 +479,28 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
474479
@test LinearAlgebra.copymutable_oftype(A, BigFloat) == A
475480
end
476481
end
482+
483+
@testset "mul! with subarrays" begin
484+
A = MyMatrix(randn(3,3))
485+
V = view(A, 1:3, 1:3)
486+
B = randn(3,3)
487+
x = randn(3)
488+
@test mul!(similar(B), V, B) A * B
489+
@test mul!(similar(B), B, V) B * A
490+
@test mul!(similar(B), V, V) A^2
491+
@test mul!(similar(B), V, A) A * A
492+
@test mul!(similar(B), A, V) A * A
493+
@test mul!(MyMatrix(randn(3,3)), A, V) A * A
494+
@test mul!(similar(x), V, x) V * x
495+
496+
@test mul!(copy(B), V, B, 2.0, 3.0) 2A * B + 3B
497+
@test mul!(copy(B), B, V, 2.0, 3.0) 2B * A + 3B
498+
@test mul!(copy(B), V, V, 2.0, 3.0) 2A^2 + 3B
499+
@test mul!(copy(B), V, A, 2.0, 3.0) 2A * A + 3B
500+
@test mul!(copy(B), A, V, 2.0, 3.0) 2A * A + 3B
501+
@test mul!(MyMatrix(copy(B)), A, V, 2.0, 3.0) 2A * A + 3B
502+
@test mul!(copy(x), V, x, 2.0, 3.0) 2A * x + 3x
503+
end
477504
end
478505

479506
struct MyUpperTriangular{T} <: AbstractMatrix{T}

0 commit comments

Comments
 (0)