From 415d11749aee1caa8aefbf102b0b89baa8976195 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sun, 28 Jun 2020 22:06:50 +0700 Subject: [PATCH] Improve big arithmetics algebra in JVM module of kmath-core --- .../kmath/asm/internal/AsmBuilder.kt | 2 +- .../scientifik/kmath/operations/BigNumbers.kt | 45 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt index 5531fd5dc..f3ee81c1c 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt @@ -340,7 +340,7 @@ internal class AsmBuilder internal constructor( checkcast(type) } - fun loadNumeric(value: Number) { + internal fun loadNumeric(value: Number) { if (expectationStack.peek() == NUMBER_TYPE) { loadNumberConstant(value, true) expectationStack.pop() diff --git a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt index e6f09c040..1c1c0a5bf 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt @@ -7,31 +7,54 @@ import java.math.MathContext /** * A field wrapper for Java [BigInteger] */ -object JBigIntegerField : Field { - override val zero: BigInteger = BigInteger.ZERO - override val one: BigInteger = BigInteger.ONE +object JBigIntegerRing : Ring, PowerOperations { + override val zero: BigInteger + get() = BigInteger.ZERO + + override val one: BigInteger + get() = BigInteger.ONE override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b) - + override fun BigInteger.minus(b: BigInteger): BigInteger = this.subtract(b) override fun multiply(a: BigInteger, k: Number): BigInteger = a.multiply(k.toInt().toBigInteger()) - override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b) - - override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b) + override fun power(arg: BigInteger, pow: Number): BigInteger = arg.pow(pow.toInt()) + override fun sqrt(arg: BigInteger): BigInteger = arg.sqrt() + override fun BigInteger.unaryMinus(): BigInteger = negate() } /** * A Field wrapper for Java [BigDecimal] */ -class JBigDecimalField(val mathContext: MathContext = MathContext.DECIMAL64) : Field { - override val zero: BigDecimal = BigDecimal.ZERO - override val one: BigDecimal = BigDecimal.ONE +abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathContext = MathContext.DECIMAL64) : + Field, + PowerOperations { + override val zero: BigDecimal + get() = BigDecimal.ZERO + + override val one: BigDecimal + get() = BigDecimal.ONE override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b) + override fun BigDecimal.minus(b: BigDecimal): BigDecimal { + return subtract(b) + } override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.multiply(k.toDouble().toBigDecimal(mathContext), mathContext) override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.multiply(b, mathContext) override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b, mathContext) -} \ No newline at end of file + override fun power(arg: BigDecimal, pow: Number): BigDecimal = arg.pow(pow.toInt(), mathContext) + override fun sqrt(arg: BigDecimal): BigDecimal = arg.sqrt(mathContext) + override fun BigDecimal.unaryMinus(): BigDecimal = negate(mathContext) + +} + +class JBigDecimalField(mathContext: MathContext = MathContext.DECIMAL64) : JBigDecimalFieldBase(mathContext) { + companion object : JBigDecimalFieldBase() +} + +fun main() { + JBigDecimalField { one pow 2 } +}