Karatsuba added, 2 bugs are fixed #328
@ -10,16 +10,19 @@ import kotlinx.benchmark.Blackhole
|
|||||||
import org.openjdk.jmh.annotations.Benchmark
|
import org.openjdk.jmh.annotations.Benchmark
|
||||||
import org.openjdk.jmh.annotations.Scope
|
import org.openjdk.jmh.annotations.Scope
|
||||||
import org.openjdk.jmh.annotations.State
|
import org.openjdk.jmh.annotations.State
|
||||||
|
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||||
import space.kscience.kmath.operations.*
|
import space.kscience.kmath.operations.*
|
||||||
|
import java.math.BigInteger
|
||||||
|
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
internal class BigIntBenchmark {
|
internal class BigIntBenchmark {
|
||||||
|
|
||||||
val kmNumber = BigIntField.number(Int.MAX_VALUE)
|
val kmNumber = BigIntField.number(Int.MAX_VALUE)
|
||||||
val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE)
|
val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE)
|
||||||
val largeKmNumber = BigIntField { number(11).pow(100_000UL) }
|
val largeKmNumber = BigIntField { number(11).pow(100_000UL) }
|
||||||
val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) }
|
val largeJvmNumber: BigInteger = JBigIntegerField { number(11).pow(100_000) }
|
||||||
val bigExponent = 50_000
|
val bigExponent = 50_000
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
package space.kscience.kmath.operations
|
package space.kscience.kmath.operations
|
||||||
|
|
||||||
import space.kscience.kmath.misc.Symbol
|
import space.kscience.kmath.misc.Symbol
|
||||||
|
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub for DSL the [Algebra] is.
|
* Stub for DSL the [Algebra] is.
|
||||||
@ -247,11 +248,12 @@ public interface RingOperations<T> : GroupOperations<T> {
|
|||||||
*/
|
*/
|
||||||
public interface Ring<T> : Group<T>, RingOperations<T> {
|
public interface Ring<T> : Group<T>, RingOperations<T> {
|
||||||
/**
|
/**
|
||||||
* neutral operation for multiplication
|
* The neutral element of multiplication
|
||||||
*/
|
*/
|
||||||
public val one: T
|
public val one: T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
|
public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
|
||||||
this == zero && exponent > 0UL -> zero
|
this == zero && exponent > 0UL -> zero
|
||||||
this == one -> base
|
this == one -> base
|
||||||
@ -259,6 +261,7 @@ public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
|
|||||||
else -> powWithoutOptimization(base, exponent)
|
else -> powWithoutOptimization(base, exponent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
public fun <T> Ring<T>.pow(base: T, exponent: UInt): T = pow(base, exponent.toULong())
|
public fun <T> Ring<T>.pow(base: T, exponent: UInt): T = pow(base, exponent.toULong())
|
||||||
|
|
||||||
private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
|
private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
|
||||||
|
@ -98,7 +98,10 @@ public class BigInt internal constructor(
|
|||||||
else -> BigInt(sign, multiplyMagnitudeByUInt(magnitude, other))
|
else -> BigInt(sign, multiplyMagnitudeByUInt(magnitude, other))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
|
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
public fun pow(other: UInt): BigInt = BigIntField { pow(this@BigInt, other) }
|
public fun pow(other: UInt): BigInt = BigIntField { pow(this@BigInt, other) }
|
||||||
|
|
||||||
public operator fun times(other: Int): BigInt = when {
|
public operator fun times(other: Int): BigInt = when {
|
||||||
|
@ -147,7 +147,7 @@ public interface PowerOperations<T> : Algebra<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raises this element to the power [pow].
|
* Raises this element to the power [power].
|
||||||
*
|
*
|
||||||
* @receiver the base.
|
* @receiver the base.
|
||||||
* @param power the exponent.
|
* @param power the exponent.
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package space.kscience.kmath.operations
|
package space.kscience.kmath.operations
|
||||||
|
|
||||||
|
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||||
import space.kscience.kmath.testutils.RingVerifier
|
import space.kscience.kmath.testutils.RingVerifier
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
@ -22,23 +23,22 @@ internal class BigIntAlgebraTest {
|
|||||||
assertEquals(res, 1_000_000.toBigInt())
|
assertEquals(res, 1_000_000.toBigInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UnstableKMathAPI
|
||||||
@Test
|
@Test
|
||||||
fun testKBigIntegerRingPow() {
|
fun testKBigIntegerRingPow() {
|
||||||
BigIntField {
|
for (num in -5..5)
|
||||||
for (num in -5..5)
|
for (exponent in 0U..10U) {
|
||||||
for (exponent in 0U..10U) {
|
assertEquals(
|
||||||
assertEquals(
|
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
|
||||||
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
|
num.toBigInt().pow(exponent.toULong()),
|
||||||
num.toBigInt().pow(exponent.toULong()),
|
"$num ^ $exponent"
|
||||||
"$num ^ $exponent"
|
)
|
||||||
)
|
assertEquals(
|
||||||
assertEquals(
|
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
|
||||||
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
|
num.toBigInt().pow(exponent),
|
||||||
num.toBigInt().pow(exponent),
|
"$num ^ $exponent"
|
||||||
"$num ^ $exponent"
|
)
|
||||||
)
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user