Skip to content

Defining neutral_element for xor (⊻) and fixing it for and (&). #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Hamiltonian-Action
Copy link

The ⊻ operation is associative, commutative, and has a well-defined neutral element. Surprisingly, this was missing from the pre-existing list.

@vchuravy
Copy link
Member

vchuravy commented Jul 1, 2025

Could you also add a test?

@Hamiltonian-Action
Copy link
Author

Hamiltonian-Action commented Jul 1, 2025

Uncovered another issue with neutral_element(::typeof(Base.:(&)), T) = one(T), which should actually read ~zero(T). Modified accordingly. Test have been augmented and ran successfully on my machine.

Edit: I would disregard the style complaints below. Better to keep the file uniform as it originally were.

Copy link
Contributor

github-actions bot commented Jul 1, 2025

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic master) to apply these changes.

Click here to view the suggested changes.
diff --git a/test/testsuite/reductions.jl b/test/testsuite/reductions.jl
index a1bdc83..cf705b8 100644
--- a/test/testsuite/reductions.jl
+++ b/test/testsuite/reductions.jl
@@ -67,9 +67,9 @@ end
             @test compare(A->reduce(+, A; dims=dims, init=zero(ET)), AT, rand(range, sz))
             @test compare(A->reduce(*, A; dims=dims, init=one(ET)), AT, rand(range, sz))
             if ET <: Integer
-                @test compare(A->reduce(&, A; dims=dims, init=~zero(ET)), AT, rand(range, sz))
-                @test compare(A->reduce(|, A; dims=dims, init=zero(ET)), AT, rand(range, sz))
-                @test compare(A->reduce(⊻, A; dims=dims, init=zero(ET)), AT, rand(range, sz))
+                @test compare(A -> reduce(&, A; dims = dims, init = ~zero(ET)), AT, rand(range, sz))
+                @test compare(A -> reduce(|, A; dims = dims, init = zero(ET)), AT, rand(range, sz))
+                @test compare(A -> reduce(⊻, A; dims = dims, init = zero(ET)), AT, rand(range, sz))
             end
         end
     end

@Hamiltonian-Action Hamiltonian-Action changed the title Defining neutral_element for xor (⊻). Defining neutral_element for xor (⊻) and fixing it for and (&). Jul 1, 2025
@maleadt
Copy link
Member

maleadt commented Jul 2, 2025

Yeah don't worry about the formatter, they're just a suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants