From 415d11749aee1caa8aefbf102b0b89baa8976195 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sun, 28 Jun 2020 22:06:50 +0700 Subject: [PATCH 1/4] 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 } +} From 1614eef4526066dcab71220db2929f19f32fe5f9 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sun, 28 Jun 2020 22:10:39 +0700 Subject: [PATCH 2/4] Revert change of JBigIntegerField to Ring, delete unused psvm function --- .../kotlin/scientifik/kmath/operations/BigNumbers.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 1c1c0a5bf..32feeb0d1 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt @@ -7,13 +7,14 @@ import java.math.MathContext /** * A field wrapper for Java [BigInteger] */ -object JBigIntegerRing : Ring, PowerOperations { +object JBigIntegerField : Field, PowerOperations { override val zero: BigInteger get() = BigInteger.ZERO override val one: BigInteger get() = BigInteger.ONE + override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b) 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()) @@ -54,7 +55,3 @@ abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathCo class JBigDecimalField(mathContext: MathContext = MathContext.DECIMAL64) : JBigDecimalFieldBase(mathContext) { companion object : JBigDecimalFieldBase() } - -fun main() { - JBigDecimalField { one pow 2 } -} From e96ecaddcfab5c8ab659c2b302d8ff7630b8cb95 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sun, 28 Jun 2020 22:21:18 +0700 Subject: [PATCH 3/4] Revert implementing PowerOperations for JBigIntegerField --- .../jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 32feeb0d1..48d3c8c59 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt @@ -7,7 +7,7 @@ import java.math.MathContext /** * A field wrapper for Java [BigInteger] */ -object JBigIntegerField : Field, PowerOperations { +object JBigIntegerField : Field { override val zero: BigInteger get() = BigInteger.ZERO @@ -19,8 +19,6 @@ object JBigIntegerField : Field, PowerOperations { 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 power(arg: BigInteger, pow: Number): BigInteger = arg.pow(pow.toInt()) - override fun sqrt(arg: BigInteger): BigInteger = arg.sqrt() override fun BigInteger.unaryMinus(): BigInteger = negate() } From 186575d8b335128eabbe701bbcaf237b883f9c8d Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sun, 26 Jul 2020 11:28:19 +0700 Subject: [PATCH 4/4] Override number function for JBigIntegerField and JBigDecimalField --- .../kotlin/scientifik/kmath/operations/BigNumbers.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 48d3c8c59..eb268bb5e 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/BigNumbers.kt @@ -14,6 +14,7 @@ object JBigIntegerField : Field { override val one: BigInteger get() = BigInteger.ONE + override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong()) override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b) override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b) override fun BigInteger.minus(b: BigInteger): BigInteger = this.subtract(b) @@ -35,9 +36,8 @@ abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathCo 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 BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b) + override fun number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble()) override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.multiply(k.toDouble().toBigDecimal(mathContext), mathContext)