From ded01251d1e94af4edf315c3d5ad70dce6b06ee0 Mon Sep 17 00:00:00 2001 From: zhelenskiy Date: Wed, 12 May 2021 11:05:42 +0300 Subject: [PATCH] Karatsuba enhancement --- .../kotlin/space/kscience/kmath/operations/BigInt.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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 88c952928..be73e274a 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 @@ -324,10 +324,8 @@ public class BigInt internal constructor( } internal fun multiplyMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude = when { - mag1.size + mag2.size < KARATSUBA_THRESHOLD || mag1.isEmpty() || mag2.isEmpty() -> naiveMultiplyMagnitudes( - mag1, - mag2 - ) + mag1.size + mag2.size < KARATSUBA_THRESHOLD || mag1.isEmpty() || mag2.isEmpty() -> + naiveMultiplyMagnitudes(mag1, mag2) // TODO implement Fourier else -> karatsubaMultiplyMagnitudes(mag1, mag2) } @@ -361,7 +359,7 @@ public class BigInt internal constructor( val z0 = x0 * y0 val z2 = x1 * y1 - val z1 = (x0 + x1) * (y1 + y0) - z0 - z2 + val z1 = (x0 - x1) * (y1 - y0) + z0 + z2 return (z2.shl(2 * halfSize * BASE_SIZE) + z1.shl(halfSize * BASE_SIZE) + z0).magnitude }