Karatsuba added, 2 bugs are fixed #328

Merged
zhelenskiy merged 15 commits from dev into dev 2021-05-14 09:12:26 +03:00
5 changed files with 27 additions and 18 deletions
Showing only changes of commit 1e94538931 - Show all commits

View File

@ -10,16 +10,19 @@ import kotlinx.benchmark.Blackhole
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.*
import java.math.BigInteger
@UnstableKMathAPI
@State(Scope.Benchmark)
internal class BigIntBenchmark {
val kmNumber = BigIntField.number(Int.MAX_VALUE)
val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE)
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
@Benchmark

View File

@ -6,6 +6,7 @@
package space.kscience.kmath.operations
import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
/**
* Stub for DSL the [Algebra] is.
@ -247,11 +248,12 @@ public interface RingOperations<T> : GroupOperations<T> {
*/
public interface Ring<T> : Group<T>, RingOperations<T> {
/**
* neutral operation for multiplication
* The neutral element of multiplication
*/
public val one: T
}
@UnstableKMathAPI
public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
this == zero && exponent > 0UL -> zero
this == one -> base
@ -259,6 +261,7 @@ public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
else -> powWithoutOptimization(base, exponent)
}
@UnstableKMathAPI
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) {

View File

@ -98,7 +98,10 @@ public class BigInt internal constructor(
else -> BigInt(sign, multiplyMagnitudeByUInt(magnitude, other))
}
@UnstableKMathAPI
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
@UnstableKMathAPI
public fun pow(other: UInt): BigInt = BigIntField { pow(this@BigInt, other) }
public operator fun times(other: Int): BigInt = when {

View File

@ -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.
* @param power the exponent.

View File

@ -5,6 +5,7 @@
package space.kscience.kmath.operations
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.testutils.RingVerifier
import kotlin.math.pow
import kotlin.test.Test
@ -22,23 +23,22 @@ internal class BigIntAlgebraTest {
assertEquals(res, 1_000_000.toBigInt())
}
@UnstableKMathAPI
@Test
fun testKBigIntegerRingPow() {
BigIntField {
for (num in -5..5)
for (exponent in 0U..10U) {
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent.toULong()),
"$num ^ $exponent"
)
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent),
"$num ^ $exponent"
)
}
}
for (num in -5..5)
for (exponent in 0U..10U) {
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent.toULong()),
"$num ^ $exponent"
)
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent),
"$num ^ $exponent"
)
}
}
@Test