Skip to content

Commit

Permalink
DiHedral -> Dihedral
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Oct 22, 2024
1 parent c1c6150 commit 96b498a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 129 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.meeuw.math.abstractalgebra.dihedral;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.text.TextUtils;

/**
* @since 0.14
* @see org.meeuw.math.abstractalgebra.dihedral
*/
public class DihedralGroup implements Group<DihedralSymmetry>, Streamable<DihedralSymmetry> {

public static Map<Integer, DihedralGroup> CACHE = new ConcurrentHashMap<>();
final int n;

private DihedralGroup(int n) {
this.n = n;
}
public static DihedralGroup of(int n) {
return CACHE.computeIfAbsent(n, DihedralGroup::new);
}


public DihedralSymmetry r(int k) {
return DihedralSymmetry.r(k, this);
}

public DihedralSymmetry s(int k) {
return DihedralSymmetry.s(k, this);
}

@Override
public DihedralSymmetry unity() {
return DihedralSymmetry.r(0, this);
}

@Override
public Cardinality getCardinality() {
return Cardinality.of(2L * n);
}

@Override
public Class<DihedralSymmetry> getElementClass() {
return DihedralSymmetry.class;
}

@Override
public Stream<DihedralSymmetry> stream() {
return Stream.concat(
IntStream.range(0, n)
.mapToObj(i -> DihedralSymmetry.r(i, this)),
IntStream.range(0, n)
.mapToObj(i -> DihedralSymmetry.s(i, this))
);
}

@Override
public boolean operationIsCommutative() {
return n <= 2;
}

@Override
public String toString() {
return "D" + TextUtils.subscript(n);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,33 @@

/**
* @since 0.14
* see org.meeuw.math.abstractalgebra.dihedral
*/
@EqualsAndHashCode
public class DiHedralSymmetry implements GroupElement<DiHedralSymmetry>, UnaryOperator<Vector2> {
public class DihedralSymmetry implements GroupElement<DihedralSymmetry>, UnaryOperator<Vector2> {

private final Symmetry symmetry;
private final int k;

private final DiHedralGroup group;
private final DihedralGroup group;

static DiHedralSymmetry r(int k,DiHedralGroup diHedralGroup) {
return new DiHedralSymmetry(r, k, diHedralGroup);
static DihedralSymmetry r(int k, DihedralGroup diHedralGroup) {
return new DihedralSymmetry(r, k, diHedralGroup);
}
static DiHedralSymmetry s(int k,DiHedralGroup diHedralGroup) {
return new DiHedralSymmetry(s, k, diHedralGroup);
static DihedralSymmetry s(int k, DihedralGroup diHedralGroup) {
return new DihedralSymmetry(s, k, diHedralGroup);
}

public static DiHedralSymmetry r(int k, int n) {
return DiHedralGroup.of(n).r(k);
public static DihedralSymmetry r(int k, int n) {
return DihedralGroup.of(n).r(k);
}

public static DiHedralSymmetry s(int k, int n) {
return DiHedralGroup.of(n).s(k);
public static DihedralSymmetry s(int k, int n) {
return DihedralGroup.of(n).s(k);
}


private DiHedralSymmetry(Symmetry symmetry, int k, DiHedralGroup diHedralGroup) {
private DihedralSymmetry(Symmetry symmetry, int k, DihedralGroup diHedralGroup) {
this.symmetry = symmetry;
if (k < 0) {
throw new InvalidElementCreationException(symmetry.name() + k + " is impossible");
Expand All @@ -53,38 +54,38 @@ private DiHedralSymmetry(Symmetry symmetry, int k, DiHedralGroup diHedralGroup)
this.group = diHedralGroup;
}

private DiHedralSymmetry(Symmetry symmetry, int k, int n) {
this(symmetry, k, DiHedralGroup.of(n));
private DihedralSymmetry(Symmetry symmetry, int k, int n) {
this(symmetry, k, DihedralGroup.of(n));
}

@Override
public DiHedralGroup getStructure() {
public DihedralGroup getStructure() {
return group;
}

@Override
public DiHedralSymmetry operate(DiHedralSymmetry operand) {
public DihedralSymmetry operate(DihedralSymmetry operand) {
if (symmetry == r) {
if (operand.symmetry == r) {
return new DiHedralSymmetry(r, (k + operand.k) % group.n, group);
return new DihedralSymmetry(r, (k + operand.k) % group.n, group);
} else {
return new DiHedralSymmetry(s, (k + operand.k) % group.n, group);
return new DihedralSymmetry(s, (k + operand.k) % group.n, group);
}
} else {
if (operand.symmetry == r) {
return new DiHedralSymmetry(s, (group.n + k - operand.k) % group.n, group);
return new DihedralSymmetry(s, (group.n + k - operand.k) % group.n, group);
} else {
return new DiHedralSymmetry(r, (group.n + k - operand.k) % group.n, group);
return new DihedralSymmetry(r, (group.n + k - operand.k) % group.n, group);
}
}
}

@Override
public DiHedralSymmetry inverse() {
public DihedralSymmetry inverse() {
if (symmetry == r) {
return new DiHedralSymmetry(r, (group.n - k) % group.n, group);
return new DihedralSymmetry(r, (group.n - k) % group.n, group);
} else {
return new DiHedralSymmetry(s, k % group.n, group);
return new DihedralSymmetry(s, k % group.n, group);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* The <a href="https://en.wikipedia.org/wiki/Dihedral_group"></a>dihedral symmetry groups</a>
* @since 0.14
*/
package org.meeuw.math.abstractalgebra.dihedral;
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,14 @@ public boolean equals(Object o) {
Vector2 vector3 = (Vector2) o;

if (Math.abs(vector3.x - x) > 2 * uncertaintyForDouble(x)) return false;
if (Math.abs(vector3.y - y) > 2 * uncertaintyForDouble(y)) return false;
return true;
return !(Math.abs(vector3.y - y) > 2 * uncertaintyForDouble(y));
}

@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(x);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = Double.hashCode(x);
result = 31 * result + Double.hashCode(y);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(x);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(z);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = Double.hashCode(x);
result = 31 * result + Double.hashCode(y);
result = 31 * result + Double.hashCode(z);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import net.jqwik.api.Arbitrary;
import org.junit.jupiter.api.Test;

import org.meeuw.math.abstractalgebra.dihedral.DiHedralGroup;
import org.meeuw.math.abstractalgebra.dihedral.DiHedralSymmetry;
import org.meeuw.math.abstractalgebra.dihedral.DihedralGroup;
import org.meeuw.math.abstractalgebra.dihedral.DihedralSymmetry;
import org.meeuw.math.abstractalgebra.dim2.Vector2;
import org.meeuw.math.exceptions.InvalidElementCreationException;
import org.meeuw.theories.abstractalgebra.GroupTheory;
Expand All @@ -17,26 +17,26 @@


@Log4j2
public class DiHedralGroupTest {
public class DihedralGroupTest {

@Test
public void apply() {
DiHedralSymmetry r1 = DiHedralSymmetry.r(1, 4);
DihedralSymmetry r1 = DihedralSymmetry.r(1, 4);
Vector2 v2 = r1.apply(Vector2.of(0.1, 1));
assertThat(v2.toString()).isEqualTo("(-1, 0.1)");
}

@Test
public void s() {
DiHedralSymmetry s1 = DiHedralSymmetry.s(1, 4);
DihedralSymmetry s1 = DihedralSymmetry.s(1, 4);
Vector2 v2 = s1.apply(Vector2.of(0.1, 1));
log.info("{}", v2);
assertThat(v2.toString()).isEqualTo("(1, 0.1)");

}
@Test
public void illegal() {
DiHedralGroup group = DiHedralGroup.of(4);
DihedralGroup group = DihedralGroup.of(4);
assertThatThrownBy(() -> {
group.s(4);
}).isInstanceOf(InvalidElementCreationException.class);
Expand All @@ -50,47 +50,47 @@ public void illegal() {
}


public static class D1Test implements GroupTheory<DiHedralSymmetry> {
public static class D1Test implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(1).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(1).stream().toList());
}
}

public static class D2Test implements GroupTheory<DiHedralSymmetry> {
public static class D2Test implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(2).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(2).stream().toList());
}
}


public static class TriangleSymmetryTest implements GroupTheory<DiHedralSymmetry> {
public static class TriangleSymmetryTest implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(3).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(3).stream().toList());
}
}

public static class SquareSymmetryTest implements GroupTheory<DiHedralSymmetry> {
public static class SquareSymmetryTest implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(4).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(4).stream().toList());
}
}


public static class HeptagonSymmetryTests implements GroupTheory<DiHedralSymmetry> {
public static class HeptagonSymmetryTests implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(5).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(5).stream().toList());
}
}

public static class HexagonSymmetryTests implements GroupTheory<DiHedralSymmetry> {
public static class HexagonSymmetryTests implements GroupTheory<DihedralSymmetry> {
@Override
public Arbitrary<? extends DiHedralSymmetry> elements() {
return Arbitraries.of(DiHedralGroup.of(6).stream().toList());
public Arbitrary<? extends DihedralSymmetry> elements() {
return Arbitraries.of(DihedralGroup.of(6).stream().toList());
}
}
}

0 comments on commit 96b498a

Please sign in to comment.