Skip to content

Commit

Permalink
Added global D4 variable holding all group elements and function for …
Browse files Browse the repository at this point in the history
…getting all symmetries at once.
  • Loading branch information
icetube23 committed Feb 3, 2022
1 parent 64bd24d commit d9ecc47
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ julia> my_alg(m) = ... # some super clever matrix transformation

julia> m = rand(10, 10); # our test matrix

julia> for g in D4
julia> for g in SquareSymmetries.D4
@assert inv(g)(my_alg(g(m))) == my_alg(m)
end
```
Expand Down
2 changes: 1 addition & 1 deletion src/SquareSymmetries.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SquareSymmetries

export unit, rotate90, rotate180, rotate270, flipdiag, flipadiag, flipx, flipy
export unit, rotate90, rotate180, rotate270, flipdiag, flipadiag, flipx, flipy, symmetries

include("symmetry.jl")

Expand Down
5 changes: 5 additions & 0 deletions src/symmetry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ rotate270(x::AbstractMatrix) = rotl90(x)
# no base function directly returns this, but we can easily compose it
flipadiag(x::AbstractMatrix) = rotate180(flipdiag(x))

const D4 = (unit, rotate90, rotate180, rotate270, flipx, flipy, flipdiag, flipadiag)

# convenient function for getting all symmetries of a given matrix at once
symmetries(x::AbstractMatrix) = map(g -> g(x), D4)

# binary group operation for elements of the symmetry group
Base.:(::typeof(unit), ::typeof(unit)) = unit
Base.:(::typeof(unit), ::typeof(flipx)) = flipx
Expand Down
25 changes: 19 additions & 6 deletions test/symmetry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,39 @@ end
# the '∘' function acts as function composition and group operation at the same time
# it can be used prevent unneeded computations, e.g., (rotate180 ∘ rotate180)(m) does
# not actually rotate m twice by 180 degrees but directly returns unit(m)
elements = [unit, rotate90, rotate180, rotate270, flipx, flipy, flipdiag, flipadiag]
m = rand(10, 10)

for g1 in elements
for g2 in elements
for g1 in SquareSymmetries.D4
for g2 in SquareSymmetries.D4
@test (g1 g2)(m) == g1(g2(m))
end
end
end

@testset "Inverse elements" begin
elements = [unit, rotate90, rotate180, rotate270, flipx, flipy, flipdiag, flipadiag]
m = rand(10, 10)

# for all group elements g and all matrices m, g(g^(-1)(m)) = g^(-1)(g(m)) = m
for g in elements
for g in SquareSymmetries.D4
@test g(inv(g)(m)) == inv(g)(g(m)) == m
end

for g in elements
for g in SquareSymmetries.D4
@test g inv(g) == inv(g) g == unit
end
end

@testset "Symmetries" begin
m = rand(10, 10)

syms = symmetries(m)
@test length(syms) == 8
@test syms[1] == unit(m)
@test syms[2] == rotate90(m)
@test syms[3] == rotate180(m)
@test syms[4] == rotate270(m)
@test syms[5] == flipx(m)
@test syms[6] == flipy(m)
@test syms[7] == flipdiag(m)
@test syms[8] == flipadiag(m)
end

2 comments on commit d9ecc47

@icetube23
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Publish the SquareSymmetries mini package.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53769

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" d9ecc47b26e2e4855d11773f891e41e64d33c9f3
git push origin v0.1.0

Please sign in to comment.