Improve big arithmetics algebra in JVM module of kmath-core

This commit is contained in:
Iaroslav 2020-06-28 22:06:50 +07:00
parent 3d85c22497
commit 415d11749a
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
2 changed files with 35 additions and 12 deletions

View File

@ -340,7 +340,7 @@ internal class AsmBuilder<T> internal constructor(
checkcast(type)
}
fun loadNumeric(value: Number) {
internal fun loadNumeric(value: Number) {
if (expectationStack.peek() == NUMBER_TYPE) {
loadNumberConstant(value, true)
expectationStack.pop()

View File

@ -7,31 +7,54 @@ import java.math.MathContext
/**
* A field wrapper for Java [BigInteger]
*/
object JBigIntegerField : Field<BigInteger> {
override val zero: BigInteger = BigInteger.ZERO
override val one: BigInteger = BigInteger.ONE
object JBigIntegerRing : Ring<BigInteger>, PowerOperations<BigInteger> {
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<BigDecimal> {
override val zero: BigDecimal = BigDecimal.ZERO
override val one: BigDecimal = BigDecimal.ONE
abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathContext = MathContext.DECIMAL64) :
Field<BigDecimal>,
PowerOperations<BigDecimal> {
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)
}
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 }
}