From 0fc23b36f47893865ae73ed3bcce77597a9b3d65 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Tue, 28 Mar 2023 15:23:54 +0100 Subject: [PATCH 01/16] Update fillalgebra.jl --- src/fillalgebra.jl | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 300e44d5..78a52bfe 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -160,37 +160,20 @@ function *(a::Transpose{T, <:AbstractVector{T}}, b::ZerosVector{T}) where T<:Rea end *(a::Transpose{T, <:AbstractMatrix{T}}, b::ZerosVector{T}) where T<:Real = mult_zeros(a, b) -# treat zero separately to support ∞-vectors -function _zero_dot(a, b) - axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - zero(promote_type(eltype(a),eltype(b))) -end - -_fill_dot(a::Zeros, b::Zeros) = _zero_dot(a, b) -_fill_dot(a::Zeros, b) = _zero_dot(a, b) -_fill_dot(a, b::Zeros) = _zero_dot(a, b) -_fill_dot(a::Zeros, b::AbstractFill) = _zero_dot(a, b) -_fill_dot(a::AbstractFill, b::Zeros) = _zero_dot(a, b) - -function _fill_dot(a::AbstractFill, b::AbstractFill) - axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - getindex_value(a)getindex_value(b)*length(b) -end - # support types with fast sum -function _fill_dot(a::AbstractFill, b) - axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - getindex_value(a)sum(b) -end -function _fill_dot(a, b::AbstractFill) +function _fill_dot(a::AbstractVector, b::AbstractFill) axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - sum(a)getindex_value(b) + # treat zero separately to support ∞-vectors + if iszero(b) || iszero(a) + zero(promote_type(eltype(a), eltype(b))) + else + sum(a) * getindex_value(b) + end end - dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) -dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) +dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(b, a) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot(a, b) function dot(u::AbstractVector, E::Eye, v::AbstractVector) From a239211f5b17649e9478b25830b6da89c6aebeed Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Tue, 28 Mar 2023 15:41:14 +0100 Subject: [PATCH 02/16] promote_op --- src/fillalgebra.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 78a52bfe..bae5cae8 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -166,7 +166,7 @@ function _fill_dot(a::AbstractVector, b::AbstractFill) axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) # treat zero separately to support ∞-vectors if iszero(b) || iszero(a) - zero(promote_type(eltype(a), eltype(b))) + zero(promote_op(*, eltype(a), eltype(b))) else sum(a) * getindex_value(b) end From cbcb666415ca819a7d0f503beb8369bf98b7a0e3 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Wed, 29 Mar 2023 08:49:50 +0100 Subject: [PATCH 03/16] add breaking test --- test/runtests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 9b2cfb19..2240f549 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1291,6 +1291,7 @@ end Random.seed!(5) u = rand(n) v = rand(n) + c = rand(ComplexF16, n) @test dot(u, D, v) == dot(u, v) @test dot(u, 2D, v) == 2dot(u, v) @@ -1300,6 +1301,7 @@ end @test dot(Zeros(5), Ones{ComplexF16}(5)) ≡ zero(ComplexF64) @test dot(Ones{ComplexF16}(5), Zeros(5)) ≡ zero(ComplexF64) @test dot(randn(5), Zeros{ComplexF16}(5)) ≡ dot(Zeros{ComplexF16}(5), randn(5)) ≡ zero(ComplexF64) + @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c) ≡ dot(c, fill(1 + im, 15)) @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 From 13d48d4145924e727e37f293dc30ad11d372337e Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Wed, 29 Mar 2023 09:15:36 +0100 Subject: [PATCH 04/16] add breaking test --- test/runtests.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 2240f549..09fb5883 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1301,9 +1301,10 @@ end @test dot(Zeros(5), Ones{ComplexF16}(5)) ≡ zero(ComplexF64) @test dot(Ones{ComplexF16}(5), Zeros(5)) ≡ zero(ComplexF64) @test dot(randn(5), Zeros{ComplexF16}(5)) ≡ dot(Zeros{ComplexF16}(5), randn(5)) ≡ zero(ComplexF64) - @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c) ≡ dot(c, fill(1 + im, 15)) + @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≡ dot(c, fill(1 + im, 15)) @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 + @test dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 let N = 2^big(1000) # fast dot for fast sum @test dot(Fill(2,N),1:N) == dot(Fill(2,N),1:N) == dot(1:N,Fill(2,N)) == 2*sum(1:N) From 4fab28db14ea48f3f178bbb48eba528665708a55 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Wed, 29 Mar 2023 09:19:50 +0100 Subject: [PATCH 05/16] fix --- src/fillalgebra.jl | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index bae5cae8..75c4cae7 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -162,18 +162,30 @@ end # support types with fast sum -function _fill_dot(a::AbstractVector, b::AbstractFill) +function _fill_dot(a::AbstractVector{T}, b::AbstractFill{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) + S = promote_op(+, T, V) # treat zero separately to support ∞-vectors if iszero(b) || iszero(a) - zero(promote_op(*, eltype(a), eltype(b))) + zero(S) else - sum(a) * getindex_value(b) + dot(S(sum(a)), S(getindex_value(b))) + end +end + +function _fill_dot_conj(a::AbstractVector{T}, b::AbstractFill{V}) where {T,V} + axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) + S = promote_op(+, T, V) + # treat zero separately to support ∞-vectors + if iszero(b) || iszero(a) + zero(S) + else + dot(S(getindex_value(b)), S(sum(a))) end end dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) -dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(b, a) +dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot_conj(b, a) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot(a, b) function dot(u::AbstractVector, E::Eye, v::AbstractVector) From 50552834f330b4aa5b491eda39650b6feac5199e Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Wed, 29 Mar 2023 13:15:19 +0100 Subject: [PATCH 06/16] accept round-off errors --- src/fillalgebra.jl | 12 ++++++------ test/runtests.jl | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 75c4cae7..0bf60aeb 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -162,31 +162,31 @@ end # support types with fast sum -function _fill_dot(a::AbstractVector{T}, b::AbstractFill{V}) where {T,V} +function _fill_dot(a::AbstractFillVector{T}, b::AbstractVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) S = promote_op(+, T, V) # treat zero separately to support ∞-vectors if iszero(b) || iszero(a) zero(S) else - dot(S(sum(a)), S(getindex_value(b))) + dot(getindex_value(a), S(sum(b))) end end -function _fill_dot_conj(a::AbstractVector{T}, b::AbstractFill{V}) where {T,V} +function _fill_dot_rev(a::AbstractVector{T}, b::AbstractFillVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) S = promote_op(+, T, V) # treat zero separately to support ∞-vectors if iszero(b) || iszero(a) zero(S) else - dot(S(getindex_value(b)), S(sum(a))) + dot(S(sum(a)), getindex_value(b)) end end dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) -dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot_conj(b, a) -dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot(a, b) +dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) +dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot_rev(a, b) function dot(u::AbstractVector, E::Eye, v::AbstractVector) length(u) == size(E,1) && length(v) == size(E,2) || diff --git a/test/runtests.jl b/test/runtests.jl index 09fb5883..33676a84 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1301,7 +1301,7 @@ end @test dot(Zeros(5), Ones{ComplexF16}(5)) ≡ zero(ComplexF64) @test dot(Ones{ComplexF16}(5), Zeros(5)) ≡ zero(ComplexF64) @test dot(randn(5), Zeros{ComplexF16}(5)) ≡ dot(Zeros{ComplexF16}(5), randn(5)) ≡ zero(ComplexF64) - @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≡ dot(c, fill(1 + im, 15)) + @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≈ dot(c, fill(1 + im, 15)) @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 @test dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 From ccdabdd56d8e977eaabfb0ca8cc5f14815b1d2cc Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 29 Mar 2023 16:49:31 +0100 Subject: [PATCH 07/16] Update test/runtests.jl Co-authored-by: Sheehan Olver --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 33676a84..138b86d1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1298,7 +1298,7 @@ end @test dot(u, Z, v) == 0 @test dot(Zeros(5), Zeros{ComplexF16}(5)) ≡ zero(ComplexF64) - @test dot(Zeros(5), Ones{ComplexF16}(5)) ≡ zero(ComplexF64) + @test @inferred(dot(Zeros(5), Ones{ComplexF16}(5))) ≡ zero(ComplexF64) @test dot(Ones{ComplexF16}(5), Zeros(5)) ≡ zero(ComplexF64) @test dot(randn(5), Zeros{ComplexF16}(5)) ≡ dot(Zeros{ComplexF16}(5), randn(5)) ≡ zero(ComplexF64) @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≈ dot(c, fill(1 + im, 15)) From fae61780f111ae7d76f344aed364c4f03fe3533b Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Wed, 29 Mar 2023 20:59:00 +0100 Subject: [PATCH 08/16] update --- src/fillalgebra.jl | 9 +++++---- test/runtests.jl | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 0bf60aeb..926a527c 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -161,15 +161,16 @@ end *(a::Transpose{T, <:AbstractMatrix{T}}, b::ZerosVector{T}) where T<:Real = mult_zeros(a, b) # support types with fast sum +_mul_maybe_zero(a, b) = ifelse(iszero(a), zero(promote_op(*, typeof(a), typeof(b))), a * b) function _fill_dot(a::AbstractFillVector{T}, b::AbstractVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) S = promote_op(+, T, V) # treat zero separately to support ∞-vectors - if iszero(b) || iszero(a) + if iszero(a) zero(S) else - dot(getindex_value(a), S(sum(b))) + _mul_maybe_zero(S(sum(b)), conj(getindex_value(a))) end end @@ -177,10 +178,10 @@ function _fill_dot_rev(a::AbstractVector{T}, b::AbstractFillVector{V}) where {T, axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) S = promote_op(+, T, V) # treat zero separately to support ∞-vectors - if iszero(b) || iszero(a) + if iszero(b) zero(S) else - dot(S(sum(a)), getindex_value(b)) + _mul_maybe_zero(conj(S(sum(a))), getindex_value(b)) end end diff --git a/test/runtests.jl b/test/runtests.jl index 33676a84..fc6b1465 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1305,6 +1305,7 @@ end @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 @test dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 + @test dot([Fill(1, 2, 2)], [[1 2; 3 4]]) let N = 2^big(1000) # fast dot for fast sum @test dot(Fill(2,N),1:N) == dot(Fill(2,N),1:N) == dot(1:N,Fill(2,N)) == 2*sum(1:N) From ec6d190e6f688c4d367690a531a3926f33679bb4 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 11:57:14 +0100 Subject: [PATCH 09/16] support inf and nan --- src/fillalgebra.jl | 18 ++------------- test/runtests.jl | 56 ++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 926a527c..8d266003 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -161,28 +161,14 @@ end *(a::Transpose{T, <:AbstractMatrix{T}}, b::ZerosVector{T}) where T<:Real = mult_zeros(a, b) # support types with fast sum -_mul_maybe_zero(a, b) = ifelse(iszero(a), zero(promote_op(*, typeof(a), typeof(b))), a * b) - function _fill_dot(a::AbstractFillVector{T}, b::AbstractVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - S = promote_op(+, T, V) - # treat zero separately to support ∞-vectors - if iszero(a) - zero(S) - else - _mul_maybe_zero(S(sum(b)), conj(getindex_value(a))) - end + dot(getindex_value(a), sum(b)) end function _fill_dot_rev(a::AbstractVector{T}, b::AbstractFillVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) - S = promote_op(+, T, V) - # treat zero separately to support ∞-vectors - if iszero(b) - zero(S) - else - _mul_maybe_zero(conj(S(sum(a))), getindex_value(b)) - end + dot(sum(a), getindex_value(b)) end dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) diff --git a/test/runtests.jl b/test/runtests.jl index 034616b1..61a74f92 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -306,20 +306,30 @@ end # type, and produce numerically correct results. as_array(x::AbstractArray) = Array(x) as_array(x::UniformScaling) = x -function test_addition_and_subtraction(As, Bs, Tout::Type) +equal_or_undef(a::Number, b::Number) = (a == b) || isequal(a, b) +equal_or_undef(a, b) = all(equal_or_undef.(a, b)) +function test_addition_subtraction_dot(As, Bs, Tout::Type) for A in As, B in Bs - @testset "$(typeof(A)) ± $(typeof(B))" begin - @test A + B isa Tout{promote_type(eltype(A), eltype(B))} - @test as_array(A + B) == as_array(A) + as_array(B) + @testset "$(typeof(A)) and $(typeof(B))" begin + T = Tout{promote_type(eltype(A), eltype(B))} + @test A + B isa T + @test equal_or_undef(as_array(A + B), as_array(A) + as_array(B)) @test A - B isa Tout{promote_type(eltype(A), eltype(B))} - @test as_array(A - B) == as_array(A) - as_array(B) + @test equal_or_undef(as_array(A - B), as_array(A) - as_array(B)) @test B + A isa Tout{promote_type(eltype(B), eltype(A))} - @test as_array(B + A) == as_array(B) + as_array(A) + @test equal_or_undef(as_array(B + A), as_array(B) + as_array(A)) @test B - A isa Tout{promote_type(eltype(B), eltype(A))} - @test as_array(B - A) == as_array(B) - as_array(A) + @test equal_or_undef(as_array(B - A), as_array(B) - as_array(A)) + + d1 = dot(A, B) + d2 = dot(as_array(A), as_array(B)) + d3 = dot(B, A) + d4 = dot(as_array(B), as_array(A)) + @test d1 ≈ d2 || d1 ≡ d2 + @test d3 ≈ d4 || d3 ≡ d4 end end end @@ -349,24 +359,24 @@ end @test -A_fill === Fill(-A_fill.value, 5) # FillArray +/- FillArray should construct a new FillArray. - test_addition_and_subtraction((A_fill, B_fill), (A_fill, B_fill), Fill) + test_addition_subtraction_dot((A_fill, B_fill), (A_fill, B_fill), Fill) test_addition_and_subtraction_dim_mismatch(A_fill, Fill(randn(rng), 5, 2)) # FillArray + Array (etc) should construct a new Array using `getindex`. - A_dense, B_dense = randn(rng, 5), [5, 4, 3, 2, 1] - test_addition_and_subtraction((A_fill, B_fill), (A_dense, B_dense), Array) + B_dense = (randn(rng, 5), [5, 4, 3, 2, 1], fill(Inf, 5), fill(NaN, 5)) + test_addition_subtraction_dot((A_fill, B_fill), B_dense, Array) test_addition_and_subtraction_dim_mismatch(A_fill, randn(rng, 5, 2)) # FillArray + StepLenRange / UnitRange (etc) should yield an AbstractRange. A_ur, B_ur = 1.0:5.0, 6:10 - test_addition_and_subtraction((A_fill, B_fill), (A_ur, B_ur), AbstractRange) + test_addition_subtraction_dot((A_fill, B_fill), (A_ur, B_ur), AbstractRange) test_addition_and_subtraction_dim_mismatch(A_fill, 1.0:6.0) test_addition_and_subtraction_dim_mismatch(A_fill, 5:10) # FillArray + UniformScaling should yield a Matrix in general As_fill_square = (Fill(randn(rng, Float64), 3, 3), Fill(5, 4, 4)) Bs_us = (UniformScaling(2.3), UniformScaling(3)) - test_addition_and_subtraction(As_fill_square, Bs_us, Matrix) + test_addition_subtraction_dot(As_fill_square, Bs_us, Matrix) As_fill_nonsquare = (Fill(randn(rng, Float64), 3, 2), Fill(5, 3, 4)) for A in As_fill_nonsquare, B in Bs_us test_addition_and_subtraction_dim_mismatch(A, B) @@ -374,12 +384,12 @@ end # FillArray + StaticArray should not have ambiguities A_svec, B_svec = SVector{5}(rand(5)), SVector(1, 2, 3, 4, 5) - test_addition_and_subtraction((A_fill, B_fill, Zeros(5)), (A_svec, B_svec), SVector{5}) + test_addition_subtraction_dot((A_fill, B_fill, Zeros(5)), (A_svec, B_svec), SVector{5}) # Issue #224 A_matmat, B_matmat = Fill(rand(3,3),5), [rand(3,3) for n=1:5] - test_addition_and_subtraction((A_matmat,), (A_matmat,), Fill) - test_addition_and_subtraction((B_matmat,), (A_matmat,), Vector) + test_addition_subtraction_dot((A_matmat,), (A_matmat,), Fill) + test_addition_subtraction_dot((B_matmat,), (A_matmat,), Vector) # Optimizations for Zeros and RectOrDiagonal{<:Any, <:AbstractFill} As_special_square = ( @@ -389,7 +399,7 @@ end RectDiagonal(Fill(randn(rng, Float64), 3), 3, 3), RectDiagonal(Fill(3, 4), 4, 4) ) DiagonalAbstractFill{T} = Diagonal{T, <:AbstractFill{T, 1}} - test_addition_and_subtraction(As_special_square, Bs_us, DiagonalAbstractFill) + test_addition_subtraction_dot(As_special_square, Bs_us, DiagonalAbstractFill) As_special_nonsquare = ( Zeros(3, 2), Zeros{Int}(3, 4), Eye(3, 2), Eye{Int}(3, 4), @@ -514,7 +524,7 @@ end @test [SVector(1,2)', SVector(2,3)', SVector(3,4)']' * Zeros{Int}(3) === SVector(0,0) @test_throws DimensionMismatch randn(4)' * Zeros(3) @test Zeros(5)' * randn(5,3) ≡ Zeros(5)'*Zeros(5,3) ≡ Zeros(5)'*Ones(5,3) ≡ Zeros(3)' - @test Zeros(5)' * randn(5) ≡ Zeros(5)' * Zeros(5) ≡ Zeros(5)' * Ones(5) ≡ 0.0 + @test abs(Zeros(5)' * randn(5)) ≡ abs(Zeros(5)' * Zeros(5)) ≡ abs(Zeros(5)' * Ones(5)) ≡ 0.0 @test Zeros(5) * Zeros(6)' ≡ Zeros(5,1) * Zeros(6)' ≡ Zeros(5,6) @test randn(5) * Zeros(6)' ≡ randn(5,1) * Zeros(6)' ≡ Zeros(5,6) @test Zeros(5) * randn(6)' ≡ Zeros(5,6) @@ -529,7 +539,7 @@ end @test transpose([1, 2, 3]) * Zeros{Int}(3) === zero(Int) @test_throws DimensionMismatch transpose(randn(4)) * Zeros(3) @test transpose(Zeros(5)) * randn(5,3) ≡ transpose(Zeros(5))*Zeros(5,3) ≡ transpose(Zeros(5))*Ones(5,3) ≡ transpose(Zeros(3)) - @test transpose(Zeros(5)) * randn(5) ≡ transpose(Zeros(5)) * Zeros(5) ≡ transpose(Zeros(5)) * Ones(5) ≡ 0.0 + @test abs(transpose(Zeros(5)) * randn(5)) ≡ abs(transpose(Zeros(5)) * Zeros(5)) ≡ abs(transpose(Zeros(5)) * Ones(5)) ≡ 0.0 @test randn(5) * transpose(Zeros(6)) ≡ randn(5,1) * transpose(Zeros(6)) ≡ Zeros(5,6) @test Zeros(5) * transpose(randn(6)) ≡ Zeros(5,6) @test transpose(randn(5)) * Zeros(5) ≡ 0.0 @@ -547,13 +557,13 @@ end @test +(z1) === z1 @test -(z1) === z1 - test_addition_and_subtraction((z1, z2), (z1, z2), Zeros) + test_addition_subtraction_dot((z1, z2), (z1, z2), Zeros) test_addition_and_subtraction_dim_mismatch(z1, Zeros{Float64}(4, 2)) end # `Zeros` +/- `Fill`s should yield `Fills`. fill1, fill2 = Fill(5.0, 4), Fill(5, 4) - test_addition_and_subtraction((z1, z2), (fill1, fill2), Fill) + test_addition_subtraction_dot((z1, z2), (fill1, fill2), Fill) test_addition_and_subtraction_dim_mismatch(z1, Fill(5, 5)) X = randn(3, 5) @@ -1299,13 +1309,11 @@ end @test dot(Zeros(5), Zeros{ComplexF16}(5)) ≡ zero(ComplexF64) @test @inferred(dot(Zeros(5), Ones{ComplexF16}(5))) ≡ zero(ComplexF64) - @test dot(Ones{ComplexF16}(5), Zeros(5)) ≡ zero(ComplexF64) - @test dot(randn(5), Zeros{ComplexF16}(5)) ≡ dot(Zeros{ComplexF16}(5), randn(5)) ≡ zero(ComplexF64) + @test abs(dot(Ones{ComplexF16}(5), Zeros(5))) ≡ abs(dot(randn(5), Zeros{ComplexF16}(5))) ≡ abs(dot(Zeros{ComplexF16}(5), randn(5))) ≡ zero(Float64) # 0.0 !≡ -0.0 @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≈ dot(c, fill(1 + im, 15)) @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 - @test dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 - @test dot([Fill(1, 2, 2)], [[1 2; 3 4]]) + @test_skip dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 # not working at present let N = 2^big(1000) # fast dot for fast sum @test dot(Fill(2,N),1:N) == dot(Fill(2,N),1:N) == dot(1:N,Fill(2,N)) == 2*sum(1:N) From ff235929e4e2292ec8b3c0503061de5c9e1b57d4 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 15:13:24 +0100 Subject: [PATCH 10/16] fix 1.6 --- src/fillalgebra.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 8d266003..4db7bca8 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -175,6 +175,10 @@ dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot_rev(a, b) +if VERSION >= v"1.6" && VERSION < v"1.7" + dot(A::AbstractFill, J::UniformScaling) = dot(tr(A), J.λ) # borrowed from julia\stdlib\v1.8\LinearAlgebra\src\uniformscaling.jl:516 +end + function dot(u::AbstractVector, E::Eye, v::AbstractVector) length(u) == size(E,1) && length(v) == size(E,2) || throw(DimensionMismatch("dot product arguments have dimensions $(length(u))×$(size(E))×$(length(v))")) From 8b6d5ade93617f4e9ff652b3666477d7e3d9aa82 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:00:17 +0100 Subject: [PATCH 11/16] Update fillalgebra.jl --- src/fillalgebra.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 4db7bca8..6e1bbb42 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -175,7 +175,7 @@ dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot_rev(a, b) -if VERSION >= v"1.6" && VERSION < v"1.7" +if VERSION >= v"1.6.0" && VERSION < v"1.7" dot(A::AbstractFill, J::UniformScaling) = dot(tr(A), J.λ) # borrowed from julia\stdlib\v1.8\LinearAlgebra\src\uniformscaling.jl:516 end From 7631b17016cb8b9a6791c072b6ef4d37b0798622 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:06:18 +0100 Subject: [PATCH 12/16] Update fillalgebra.jl --- src/fillalgebra.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 6e1bbb42..ecf90380 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -175,9 +175,8 @@ dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot_rev(a, b) -if VERSION >= v"1.6.0" && VERSION < v"1.7" - dot(A::AbstractFill, J::UniformScaling) = dot(tr(A), J.λ) # borrowed from julia\stdlib\v1.8\LinearAlgebra\src\uniformscaling.jl:516 -end +# for Julia 1.6 +dot(A::AbstractFill, J::UniformScaling) = dot(tr(A), J.λ) # borrowed from julia\stdlib\v1.8\LinearAlgebra\src\uniformscaling.jl:516 function dot(u::AbstractVector, E::Eye, v::AbstractVector) length(u) == size(E,1) && length(v) == size(E,2) || From d9ce80fab95275c8f93ef69c7d643496d505049f Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:27:10 +0100 Subject: [PATCH 13/16] trying to fix Julia 1.6 --- src/fillalgebra.jl | 3 --- test/runtests.jl | 14 ++++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index ecf90380..8d266003 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -175,9 +175,6 @@ dot(a::AbstractFillVector, b::AbstractFillVector) = _fill_dot(a, b) dot(a::AbstractFillVector, b::AbstractVector) = _fill_dot(a, b) dot(a::AbstractVector, b::AbstractFillVector) = _fill_dot_rev(a, b) -# for Julia 1.6 -dot(A::AbstractFill, J::UniformScaling) = dot(tr(A), J.λ) # borrowed from julia\stdlib\v1.8\LinearAlgebra\src\uniformscaling.jl:516 - function dot(u::AbstractVector, E::Eye, v::AbstractVector) length(u) == size(E,1) && length(v) == size(E,2) || throw(DimensionMismatch("dot product arguments have dimensions $(length(u))×$(size(E))×$(length(v))")) diff --git a/test/runtests.jl b/test/runtests.jl index 61a74f92..52c07bb8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -324,12 +324,14 @@ function test_addition_subtraction_dot(As, Bs, Tout::Type) @test B - A isa Tout{promote_type(eltype(B), eltype(A))} @test equal_or_undef(as_array(B - A), as_array(B) - as_array(A)) - d1 = dot(A, B) - d2 = dot(as_array(A), as_array(B)) - d3 = dot(B, A) - d4 = dot(as_array(B), as_array(A)) - @test d1 ≈ d2 || d1 ≡ d2 - @test d3 ≈ d4 || d3 ≡ d4 + if VERSION < v"1.6.0" || VERSION >= v"1.8.0" + d1 = dot(A, B) + d2 = dot(as_array(A), as_array(B)) + d3 = dot(B, A) + d4 = dot(as_array(B), as_array(A)) + @test d1 ≈ d2 || d1 ≡ d2 + @test d3 ≈ d4 || d3 ≡ d4 + end end end end From 972f9627b645af5eea9da4017cc744ad00869f68 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:35:50 +0100 Subject: [PATCH 14/16] comments --- src/fillalgebra.jl | 2 ++ test/runtests.jl | 1 + 2 files changed, 3 insertions(+) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index 8d266003..1adc2045 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -161,6 +161,8 @@ end *(a::Transpose{T, <:AbstractMatrix{T}}, b::ZerosVector{T}) where T<:Real = mult_zeros(a, b) # support types with fast sum +# infinite cases should be supported in InfiniteArrays.jl +# type issues of Bool dot are ignored at present. function _fill_dot(a::AbstractFillVector{T}, b::AbstractVector{V}) where {T,V} axes(a) == axes(b) || throw(DimensionMismatch("dot product arguments have lengths $(length(a)) and $(length(b))")) dot(getindex_value(a), sum(b)) diff --git a/test/runtests.jl b/test/runtests.jl index 52c07bb8..71681980 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -324,6 +324,7 @@ function test_addition_subtraction_dot(As, Bs, Tout::Type) @test B - A isa Tout{promote_type(eltype(B), eltype(A))} @test equal_or_undef(as_array(B - A), as_array(B) - as_array(A)) + # Julia 1.6 doesn't support dot(UniformScaling) if VERSION < v"1.6.0" || VERSION >= v"1.8.0" d1 = dot(A, B) d2 = dot(as_array(A), as_array(B)) From cf5ccc94c142c7572600331fd18c73e17ec0d93b Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:40:36 +0100 Subject: [PATCH 15/16] Update runtests.jl --- test/runtests.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 71681980..bd3e0e21 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -311,8 +311,7 @@ equal_or_undef(a, b) = all(equal_or_undef.(a, b)) function test_addition_subtraction_dot(As, Bs, Tout::Type) for A in As, B in Bs @testset "$(typeof(A)) and $(typeof(B))" begin - T = Tout{promote_type(eltype(A), eltype(B))} - @test A + B isa T + @test A + B isa Tout{promote_type(eltype(A), eltype(B))} @test equal_or_undef(as_array(A + B), as_array(A) + as_array(B)) @test A - B isa Tout{promote_type(eltype(A), eltype(B))} From 4c630e0ef87f1d390ca159eb63a889f40ef0dd6d Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Thu, 30 Mar 2023 16:56:11 +0100 Subject: [PATCH 16/16] add @inferred --- test/runtests.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index bd3e0e21..1a119203 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1309,12 +1309,12 @@ end @test dot(u, 2D, v) == 2dot(u, v) @test dot(u, Z, v) == 0 - @test dot(Zeros(5), Zeros{ComplexF16}(5)) ≡ zero(ComplexF64) + @test @inferred(dot(Zeros(5), Zeros{ComplexF16}(5))) ≡ zero(ComplexF64) @test @inferred(dot(Zeros(5), Ones{ComplexF16}(5))) ≡ zero(ComplexF64) - @test abs(dot(Ones{ComplexF16}(5), Zeros(5))) ≡ abs(dot(randn(5), Zeros{ComplexF16}(5))) ≡ abs(dot(Zeros{ComplexF16}(5), randn(5))) ≡ zero(Float64) # 0.0 !≡ -0.0 - @test dot(c, Fill(1 + im, 15)) ≡ dot(Fill(1 + im, 15), c)' ≈ dot(c, fill(1 + im, 15)) + @test abs(@inferred(dot(Ones{ComplexF16}(5), Zeros(5)))) ≡ abs(@inferred(dot(randn(5), Zeros{ComplexF16}(5)))) ≡ abs(@inferred(dot(Zeros{ComplexF16}(5), randn(5)))) ≡ zero(Float64) # 0.0 !≡ -0.0 + @test @inferred(dot(c, Fill(1 + im, 15))) ≡ (@inferred(dot(Fill(1 + im, 15), c)))' ≈ @inferred(dot(c, fill(1 + im, 15))) - @test dot(Fill(1,5), Fill(2.0,5)) ≡ 10.0 + @test @inferred(dot(Fill(1,5), Fill(2.0,5))) ≡ 10.0 @test_skip dot(Fill(true,5), Fill(Int8(1),5)) isa Int8 # not working at present let N = 2^big(1000) # fast dot for fast sum