Skip to content

Commit

Permalink
commutative rings and division rings; type punning.
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jan 14, 2024
1 parent 146a987 commit af84658
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package rocks.palaiologos.maja.structure;

/**
* A division ring is a set R equipped with two binary operations + and ·, where (R, +) and (R, ·) are abelian groups.
* The multiplication distributes over addition (left and right distributivity).
*/
public interface CommutativeRing<T> extends AdditiveAbelianGroup<T>, MultiplicativeAbelianGroup<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package rocks.palaiologos.maja.structure;

/**
* A division ring is a set R equipped with two binary operations + and ·, where (R, +) is an abelian group and (R, ·) is a group.
* The multiplication distributes over addition (left and right distributivity).
*/
public interface DivisionRing<T> extends AdditiveAbelianGroup<T>, MultiplicativeGroup<T> {
}
86 changes: 86 additions & 0 deletions src/main/java/rocks/palaiologos/maja/structure/Ring.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,90 @@
* The multiplication distributes over addition (left and right distributivity).
*/
public interface Ring<T> extends AdditiveAbelianGroup<T>, MultiplicativeMonoid<T> {
static <T> Ring<T> of(DivisionRing<T> divisionRing) {
return new Ring<T>() {
@Override
public T addInv(T a) {
return divisionRing.addInv(a);
}

@Override
public T zero() {
return divisionRing.zero();
}

@Override
public T plus(T a, T b) {
return divisionRing.plus(a, b);
}

@Override
public T one() {
return divisionRing.one();
}

@Override
public T dot(T a, T b) {
return divisionRing.dot(a, b);
}
};
}

static <T> Ring<T> of(CommutativeRing<T> commutativeRing) {
return new Ring<T>() {
@Override
public T addInv(T a) {
return commutativeRing.addInv(a);
}

@Override
public T zero() {
return commutativeRing.zero();
}

@Override
public T plus(T a, T b) {
return commutativeRing.plus(a, b);
}

@Override
public T one() {
return commutativeRing.one();
}

@Override
public T dot(T a, T b) {
return commutativeRing.dot(a, b);
}
};
}

static <T> Ring<T> of(AdditiveAbelianGroup<T> additiveAbelianGroup, MultiplicativeMonoid<T> multiplicativeMonoid) {
return new Ring<T>() {
@Override
public T addInv(T a) {
return additiveAbelianGroup.addInv(a);
}

@Override
public T zero() {
return additiveAbelianGroup.zero();
}

@Override
public T plus(T a, T b) {
return additiveAbelianGroup.plus(a, b);
}

@Override
public T one() {
return multiplicativeMonoid.one();
}

@Override
public T dot(T a, T b) {
return multiplicativeMonoid.dot(a, b);
}
};
}
}

0 comments on commit af84658

Please sign in to comment.