From b90c9c597c54a85bdf733bb98c3e4cb36b28cb8d Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 20 Jul 2020 11:07:43 +0700 Subject: [PATCH] Implement optional interface for "division with remainder" as well as its implementation of integer rings --- .../kmath/operations/NumberAlgebra.kt | 20 +++++++++++++++---- .../kmath/operations/OptionalOperations.kt | 15 ++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt index 953c5a112..8e4bc01ba 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt @@ -126,7 +126,7 @@ object FloatField : ExtendedField, Norm { * A field for [Int] without boxing. Does not produce corresponding field element */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object IntRing : Ring, Norm { +object IntRing : Ring, Norm, RemainderDivisionOperations { override val zero: Int = 0 override inline fun add(a: Int, b: Int) = a + b override inline fun multiply(a: Int, b: Int) = a * b @@ -142,13 +142,16 @@ object IntRing : Ring, Norm { override inline fun Int.minus(b: Int): Int = this - b override inline fun Int.times(b: Int): Int = this * b + + override fun Int.rem(arg: Int): Int = this % arg + override fun Int.div(arg: Int): Int = this / arg } /** * A field for [Short] without boxing. Does not produce appropriate field element */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object ShortRing : Ring, Norm { +object ShortRing : Ring, Norm, RemainderDivisionOperations { override val zero: Short = 0 override inline fun add(a: Short, b: Short) = (a + b).toShort() override inline fun multiply(a: Short, b: Short) = (a * b).toShort() @@ -164,13 +167,16 @@ object ShortRing : Ring, Norm { override inline fun Short.minus(b: Short) = (this - b).toShort() override inline fun Short.times(b: Short) = (this * b).toShort() + + override fun Short.rem(arg: Short): Short = (this % arg).toShort() + override fun Short.div(arg: Short): Short = (this / arg).toShort() } /** * A field for [Byte] values */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object ByteRing : Ring, Norm { +object ByteRing : Ring, Norm, RemainderDivisionOperations { override val zero: Byte = 0 override inline fun add(a: Byte, b: Byte) = (a + b).toByte() override inline fun multiply(a: Byte, b: Byte) = (a * b).toByte() @@ -186,13 +192,16 @@ object ByteRing : Ring, Norm { override inline fun Byte.minus(b: Byte) = (this - b).toByte() override inline fun Byte.times(b: Byte) = (this * b).toByte() + + override fun Byte.rem(arg: Byte): Byte = (this % arg).toByte() + override fun Byte.div(arg: Byte): Byte = (this / arg).toByte() } /** * A field for [Long] values */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object LongRing : Ring, Norm { +object LongRing : Ring, Norm, RemainderDivisionOperations { override val zero: Long = 0 override inline fun add(a: Long, b: Long) = (a + b) override inline fun multiply(a: Long, b: Long) = (a * b) @@ -208,4 +217,7 @@ object LongRing : Ring, Norm { override inline fun Long.minus(b: Long) = (this - b) override inline fun Long.times(b: Long) = (this * b) + + override fun Long.rem(arg: Long): Long = this % arg + override fun Long.div(arg: Long): Long = this / arg } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt index 709f0260f..66e938343 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt @@ -82,3 +82,18 @@ interface Norm { } fun >, R> norm(arg: T): R = arg.context.norm(arg) + +interface RemainderDivisionOperations : Ring { + /** + * Calculates the remainder of dividing this value by [arg]. + */ + infix fun T.rem(arg: T): T + + /** + * Performs the floored division of this value by [arg]. + */ + infix fun T.div(arg: T): T +} + +infix fun >> T.rem(arg: T): T = arg.context { this@rem rem arg } +infix fun >> T.div(arg: T): T = arg.context { this@div div arg }