From aa48b2c5057133589b3faa8fee6a34de543bd4af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 May 2024 16:41:51 +0200 Subject: [PATCH 1/9] Fix zeroing of sparsevector when coefficient is MOI.ScalarAffineFunction --- src/coefficients.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/coefficients.jl b/src/coefficients.jl index 02f40db..8daf5bf 100644 --- a/src/coefficients.jl +++ b/src/coefficients.jl @@ -119,7 +119,11 @@ end # general mutable API # why here? -MA.operate!(::typeof(zero), v::SparseVector) = (v .= 0; v) +function MA.operate!(::typeof(zero), v::SparseVector) + empty!(SparseArrays.nonzeroinds(v)) + empty!(SparseArrays.nonzeros(v)) + return v +end Base.zero(X::AbstractCoefficients) = MA.operate!(zero, similar(X)) Base.:-(X::AbstractCoefficients) = MA.operate_to!(__prealloc(X, -1, *), -, X) From 7128e8afe6b4c2736bd883b1b07b4564ed5f5512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 May 2024 17:05:24 +0200 Subject: [PATCH 2/9] Needed fixes --- src/arithmetic.jl | 4 ++-- src/types.jl | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/arithmetic.jl b/src/arithmetic.jl index 0ae8e1b..1b783d2 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -91,7 +91,7 @@ function MA.operate_to!( X::AlgebraElement, Y::AlgebraElement, ) - @assert parent(res) === parent(X) === parent(Y) + @assert parent(res) == parent(X) == parent(Y) MA.operate_to!(coeffs(res), -, coeffs(X), coeffs(Y)) return res end @@ -102,7 +102,7 @@ function MA.operate_to!( X::AlgebraElement, Y::AlgebraElement, ) - @assert parent(res) === parent(X) === parent(Y) + @assert parent(res) == parent(X) == parent(Y) mstr = mstructure(basis(parent(res))) MA.operate_to!(coeffs(res), mstr, coeffs(X), coeffs(Y)) return res diff --git a/src/types.jl b/src/types.jl index 11a37d5..16718d3 100644 --- a/src/types.jl +++ b/src/types.jl @@ -26,6 +26,9 @@ end basis(A::StarAlgebra) = A.basis object(A::StarAlgebra) = A.object +function Base.:(==)(a::StarAlgebra, b::StarAlgebra) + return a.basis == b.basis +end struct AlgebraElement{A,T,V} <: MA.AbstractMutable coeffs::V From 76e132398cd29ab712dac26b83318fe25523887c Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Wed, 29 May 2024 09:44:43 +0200 Subject: [PATCH 3/9] don't implement == for StarAlgebras --- src/types.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/types.jl b/src/types.jl index 16718d3..11a37d5 100644 --- a/src/types.jl +++ b/src/types.jl @@ -26,9 +26,6 @@ end basis(A::StarAlgebra) = A.basis object(A::StarAlgebra) = A.object -function Base.:(==)(a::StarAlgebra, b::StarAlgebra) - return a.basis == b.basis -end struct AlgebraElement{A,T,V} <: MA.AbstractMutable coeffs::V From dd292c69c5d3e451b829e29d9f038cf137f2b4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 30 May 2024 10:04:01 +0200 Subject: [PATCH 4/9] up --- src/types.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types.jl b/src/types.jl index 11a37d5..aa8919b 100644 --- a/src/types.jl +++ b/src/types.jl @@ -36,8 +36,9 @@ Base.parent(a::AlgebraElement) = a.parent Base.eltype(a::AlgebraElement) = valtype(coeffs(a)) coeffs(a::AlgebraElement) = a.coeffs function coeffs(x::AlgebraElement, b::AbstractBasis) - return coeffs(coeffs(x), basis(parent(x)), b) + return coeffs(coeffs(x), basis(x), b) end +basis(a::AlgebraElement) = basis(parent(a)) function AlgebraElement(coeffs, A::AbstractStarAlgebra) _sanity_checks(coeffs, A) From f5473ac52f9085ebb8d0493a688837ba54efa461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 30 May 2024 11:31:03 +0200 Subject: [PATCH 5/9] WIP --- src/arithmetic.jl | 11 +++++++++++ src/types.jl | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/arithmetic.jl b/src/arithmetic.jl index 1b783d2..d25c3a5 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -107,3 +107,14 @@ function MA.operate_to!( MA.operate_to!(coeffs(res), mstr, coeffs(X), coeffs(Y)) return res end + +# TODO just push to internal vectors once canonical is implemented for SparseVector +function unsafe_push!(a::SparseArrays.SparseVector, k, v) + a[k] = MA.add!!(a[k], v) + return v +end + +function unsafe_push!(a::AlgebraElement, k, v) + unsafe_push!(coeffs(a), basis(a)[k], v) + return a +end diff --git a/src/types.jl b/src/types.jl index aa8919b..231dc2d 100644 --- a/src/types.jl +++ b/src/types.jl @@ -27,6 +27,8 @@ end basis(A::StarAlgebra) = A.basis object(A::StarAlgebra) = A.object +function algebra end + struct AlgebraElement{A,T,V} <: MA.AbstractMutable coeffs::V parent::A From 45ef71f18d0f07cada75115d631be28b20309bdf Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Thu, 30 May 2024 16:02:30 +0200 Subject: [PATCH 6/9] =?UTF-8?q?replace=20further=20uses=20of=20basis(paren?= =?UTF-8?q?t(=E2=80=A6))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/algebra_elts.jl | 6 +++--- src/arithmetic.jl | 2 +- src/star.jl | 2 +- src/types.jl | 2 +- test/group_algebra.jl | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/algebra_elts.jl b/src/algebra_elts.jl index e2e160b..44954df 100644 --- a/src/algebra_elts.jl +++ b/src/algebra_elts.jl @@ -15,13 +15,13 @@ function Base.deepcopy_internal(a::AlgebraElement, id::IdDict) end # call overload: -(a::AlgebraElement)(x) = coeffs(a)[basis(parent(a))[x]] -Base.setindex!(a::AlgebraElement, v, idx) = a.coeffs[basis(parent(a))[idx]] = v +(a::AlgebraElement)(x) = coeffs(a)[basis(a)[x]] +Base.setindex!(a::AlgebraElement, v, idx) = a.coeffs[basis(a)[idx]] = v # AlgebraElement specific functions function supp(a::AlgebraElement) - b = basis(parent(a)) + b = basis(a) return [b[i] for (i, _) in nonzero_pairs(coeffs(a))] end diff --git a/src/arithmetic.jl b/src/arithmetic.jl index d25c3a5..39fe8cb 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -103,7 +103,7 @@ function MA.operate_to!( Y::AlgebraElement, ) @assert parent(res) == parent(X) == parent(Y) - mstr = mstructure(basis(parent(res))) + mstr = mstructure(basis(res)) MA.operate_to!(coeffs(res), mstr, coeffs(X), coeffs(Y)) return res end diff --git a/src/star.jl b/src/star.jl index b0e971d..100f707 100644 --- a/src/star.jl +++ b/src/star.jl @@ -2,7 +2,7 @@ Base.adjoint(a::AlgebraElement) = star(a) star(x::Any) = x' function star(X::AlgebraElement) - res = star(basis(parent(X)), coeffs(X)) + res = star(basis(X), coeffs(X)) return AlgebraElement(res, parent(X)) end diff --git a/src/types.jl b/src/types.jl index 231dc2d..88443d8 100644 --- a/src/types.jl +++ b/src/types.jl @@ -90,7 +90,7 @@ function Base.isone(a::AlgebraElement) if basis(A) isa DiracBasis return c == cfs1 else - dc = coeffs(c, basis(parent(a)), DiracBasis{UInt}(object(parent(a)))) + dc = coeffs(c, basis(a), DiracBasis{UInt}(object(parent(a)))) return dc == cfs1 end end diff --git a/test/group_algebra.jl b/test/group_algebra.jl index 0dc2c25..6804b6b 100644 --- a/test/group_algebra.jl +++ b/test/group_algebra.jl @@ -115,7 +115,7 @@ z = sum((one(RG) - RG(g)) * star(one(RG) - RG(g)) for g in G) @test SA.aug(z) == 0 - @test SA.supp(z) == sort(collect(basis(parent(z)))) + @test SA.supp(z) == sort(collect(basis(z))) @test SA.supp(RG(1) + RG(g)) == [one(G), g] @test SA.supp(a) == [one(G), h, g] From e74ecabe80e1f6c20ea618ee0156a08a6d8e46c9 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Thu, 30 May 2024 16:11:49 +0200 Subject: [PATCH 7/9] turn allocation in cached mul test into @test_broken --- test/caching_allocations.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/caching_allocations.jl b/test/caching_allocations.jl index 027dc0c..aed8800 100644 --- a/test/caching_allocations.jl +++ b/test/caching_allocations.jl @@ -52,6 +52,7 @@ end Y * Y k2 = @allocated Y * Y @test k2 / k1 < 0.7 + @test_broken k2 / k1 < 0.12 end @test all(!iszero, SA.mstructure(fRG).table) From 9db5760bbc4b56256f2aa6be203737154371e5f4 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Thu, 30 May 2024 16:13:00 +0200 Subject: [PATCH 8/9] SparseCoefficients based on Tuples are truly immutable --- src/sparse_coeffs.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sparse_coeffs.jl b/src/sparse_coeffs.jl index 5f07251..2f6a39a 100644 --- a/src/sparse_coeffs.jl +++ b/src/sparse_coeffs.jl @@ -101,6 +101,13 @@ function MA.operate!(::typeof(canonical), res::SparseCoefficients, cmp::C) where return res end +function MA.operate!( + ::typeof(canonical), + res::SparseCoefficients{T,I,Tuple{T},Tuple{I}}, +) where {T,I} + return res +end + # arithmetic on coefficients; performance overloads function MA.operate!(::typeof(zero), s::SparseCoefficients) From 57a74ab7204e27011482330e43f1c53b19509b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 31 May 2024 08:22:30 +0200 Subject: [PATCH 9/9] Unbreak test --- test/caching_allocations.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/caching_allocations.jl b/test/caching_allocations.jl index aed8800..025efbf 100644 --- a/test/caching_allocations.jl +++ b/test/caching_allocations.jl @@ -51,8 +51,7 @@ end @test Y * Y isa AlgebraElement Y * Y k2 = @allocated Y * Y - @test k2 / k1 < 0.7 - @test_broken k2 / k1 < 0.12 + @test k2 / k1 < 0.12 end @test all(!iszero, SA.mstructure(fRG).table)