diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt index f7c021998..645e60c29 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt @@ -250,21 +250,21 @@ public interface Ring : Group, RingOperations { * neutral operation for multiplication */ public val one: T +} - public fun T.pow(exponent: ULong): T = when { - this == zero && exponent > 0UL -> zero - this == one -> this - this == -one -> powWithoutOptimization(exponent % 2UL) - else -> powWithoutOptimization(exponent) - } +public fun Ring.pow(base: T, exponent: ULong): T = when { + this == zero && exponent > 0UL -> zero + this == one -> base + this == -one -> powWithoutOptimization(base, exponent % 2UL) + else -> powWithoutOptimization(base, exponent) +} - private fun T.powWithoutOptimization(exponent: ULong): T = when (exponent) { - 0UL -> one - 1UL -> this - else -> { - val pre = powWithoutOptimization(exponent shr 1).let { it * it } - if (exponent and 1UL == 0UL) pre else pre * this - } +private fun Ring.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) { + 0UL -> one + 1UL -> base + else -> { + val pre = powWithoutOptimization(base, exponent shr 1).let { it * it } + if (exponent and 1UL == 0UL) pre else pre * base } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt index 780a4591d..88c952928 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt @@ -98,6 +98,8 @@ public class BigInt internal constructor( else -> BigInt(sign, multiplyMagnitudeByUInt(magnitude, other)) } + public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) } + public operator fun times(other: Int): BigInt = when { other > 0 -> this * kotlin.math.abs(other).toUInt() other != Int.MIN_VALUE -> -this * kotlin.math.abs(other).toUInt()