Skip to content

Commit 3cf10c6

Browse files
authored
Merge pull request #3979 from armanbilge/topic/algebra/semifield
Add `Semifield` and `CommutativeSemifield` to algebra
2 parents 8e98f0b + 9c7ab12 commit 3cf10c6

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package algebra
2+
package ring
3+
4+
import scala.{specialized => sp}
5+
6+
/**
7+
* CommutativeSemifield is a Semifield that is commutative under multiplication.
8+
*/
9+
trait CommutativeSemifield[@sp(Int, Long, Float, Double) A]
10+
extends Any
11+
with Semifield[A]
12+
with CommutativeRig[A]
13+
with MultiplicativeCommutativeGroup[A]
14+
15+
object CommutativeSemifield
16+
extends AdditiveMonoidFunctions[CommutativeSemifield]
17+
with MultiplicativeGroupFunctions[CommutativeSemifield] {
18+
@inline final def apply[A](implicit r: CommutativeSemifield[A]): CommutativeSemifield[A] = r
19+
}

algebra-core/src/main/scala/algebra/ring/DivisionRing.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ring
33

44
import scala.{specialized => sp}
55

6-
trait DivisionRing[@sp(Byte, Short, Int, Long, Float, Double) A] extends Any with Ring[A] with MultiplicativeGroup[A] {
6+
trait DivisionRing[@sp(Byte, Short, Int, Long, Float, Double) A] extends Any with Ring[A] with Semifield[A] {
77
self =>
88

99
/**

algebra-core/src/main/scala/algebra/ring/Field.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Field[@sp(Int, Long, Float, Double) A]
77
extends Any
88
with EuclideanRing[A]
99
with DivisionRing[A]
10-
with MultiplicativeCommutativeGroup[A] {
10+
with CommutativeSemifield[A] {
1111
self =>
1212

1313
// default implementations for GCD
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package algebra
2+
package ring
3+
4+
import scala.{specialized => sp}
5+
6+
/**
7+
* Semifield consists of:
8+
*
9+
* - a commutative monoid for addition (+)
10+
* - a group for multiplication (*)
11+
*
12+
* Alternately, a Semifield can be thought of as a DivisionRing without an additive inverse.
13+
*/
14+
trait Semifield[@sp(Int, Long, Float, Double) A] extends Any with Rig[A] with MultiplicativeGroup[A]
15+
16+
object Semifield extends AdditiveMonoidFunctions[Semifield] with MultiplicativeGroupFunctions[Semifield] {
17+
@inline final def apply[A](implicit ev: Semifield[A]): Semifield[A] = ev
18+
}

algebra-laws/shared/src/main/scala/algebra/laws/RingLaws.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,25 @@ trait RingLaws[A] extends GroupLaws[A] { self =>
259259
}
260260
)
261261

262+
def semifield(implicit A: Semifield[A]) = new RingProperties(
263+
name = "semifield",
264+
al = additiveCommutativeMonoid,
265+
ml = multiplicativeGroup,
266+
parents = Seq(rig)
267+
)
268+
269+
def commutativeSemifield(implicit A: CommutativeSemifield[A]) = new RingProperties(
270+
name = "semifield",
271+
al = additiveCommutativeMonoid,
272+
ml = multiplicativeCommutativeGroup,
273+
parents = Seq(semifield, commutativeRig)
274+
)
275+
262276
def divisionRing(implicit A: DivisionRing[A]) = new RingProperties(
263277
name = "division ring",
264278
al = additiveCommutativeGroup,
265279
ml = multiplicativeGroup,
266-
parents = Seq(ring),
280+
parents = Seq(ring, semifield),
267281
"fromDouble" -> forAll { (n: Double) =>
268282
if (Platform.isJvm) {
269283
// TODO: BigDecimal(n) is busted in scalajs, so we skip this test.
@@ -311,7 +325,7 @@ trait RingLaws[A] extends GroupLaws[A] { self =>
311325
name = "field",
312326
al = additiveCommutativeGroup,
313327
ml = multiplicativeCommutativeGroup,
314-
parents = Seq(euclideanRing, divisionRing)
328+
parents = Seq(euclideanRing, divisionRing, commutativeSemifield)
315329
)
316330

317331
// Approximate fields such a Float or Double, even through filtered using FPFilter, do not work well with

0 commit comments

Comments
 (0)