Compare commits
8 Commits
dev
...
commandert
Author | SHA1 | Date | |
---|---|---|---|
|
a888f24fad | ||
|
e00b1153b1 | ||
|
e6fb8bfc52 | ||
|
9c3330cb6d | ||
|
855bab258a | ||
|
dc02d957b0 | ||
|
3dc59ccd26 | ||
|
3b52eb7ac1 |
@ -32,10 +32,13 @@ dependencies {
|
||||
implementation(project(":kmath-koma"))
|
||||
implementation(project(":kmath-viktor"))
|
||||
implementation(project(":kmath-dimensions"))
|
||||
implementation(project(":kmath-bignum"))
|
||||
implementation(project(":kmath-memory"))
|
||||
implementation("com.kyonifer:koma-core-ejml:0.12")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.2.0-npm-dev-6")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-8")
|
||||
"benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath
|
||||
implementation("org.slf4j:slf4j-simple:1.7.30")
|
||||
"benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8")
|
||||
"benchmarksImplementation"(sourceSets.main.get().output.plus(sourceSets.main.get().runtimeClasspath))
|
||||
}
|
||||
|
||||
// Configure benchmark
|
||||
|
@ -0,0 +1,41 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark
|
||||
import org.openjdk.jmh.annotations.Scope
|
||||
import org.openjdk.jmh.annotations.State
|
||||
import scientifik.kmath.operations.JBigDecimalField
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.random.Random
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
class BigDecimalBenchmark {
|
||||
final var times: Int = 10000
|
||||
|
||||
@Benchmark
|
||||
fun java() {
|
||||
val random = Random(0)
|
||||
var sum = JBigDecimalField.zero
|
||||
|
||||
repeat(times) {
|
||||
sum += JBigDecimalField {
|
||||
number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0)
|
||||
}
|
||||
}
|
||||
|
||||
println("java:$sum")
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
fun bignum() {
|
||||
val random = Random(0)
|
||||
var sum = BigDecimalField.zero
|
||||
|
||||
repeat(times) {
|
||||
sum += BigDecimalField {
|
||||
number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0)
|
||||
}
|
||||
}
|
||||
|
||||
println("bignum:$sum")
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark
|
||||
import org.openjdk.jmh.annotations.Scope
|
||||
import org.openjdk.jmh.annotations.State
|
||||
import scientifik.kmath.operations.BigIntField
|
||||
import scientifik.kmath.operations.JBigIntegerField
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.random.Random
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
class BigIntegerBenchmark {
|
||||
final var times: Int = 1000000
|
||||
|
||||
@Benchmark
|
||||
fun java() {
|
||||
val random = Random(0)
|
||||
var sum = JBigIntegerField.zero
|
||||
repeat(times) { sum += JBigIntegerField { number(random.nextInt()) * random.nextInt() } }
|
||||
println("java:$sum")
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
fun bignum() {
|
||||
val random = Random(0)
|
||||
var sum = BigIntegerRing.zero
|
||||
repeat(times) { sum += BigIntegerRing { number(random.nextInt()) * random.nextInt() } }
|
||||
println("bignum:$sum")
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
fun bigint() {
|
||||
val random = Random(0)
|
||||
var sum = BigIntField.zero
|
||||
repeat(times) { sum += BigIntField { number(random.nextInt()) * random.nextInt() } }
|
||||
println("bigint:$sum")
|
||||
}
|
||||
}
|
@ -9,8 +9,9 @@ import scientifik.kmath.operations.RealField
|
||||
import kotlin.random.Random
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
class ExpressionsInterpretersBenchmark {
|
||||
private class ExpressionsInterpretersBenchmark {
|
||||
private val algebra: Field<Double> = RealField
|
||||
|
||||
fun functionalExpression() {
|
||||
val expr = algebra.expressionInField {
|
||||
variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0)
|
||||
|
10
kmath-bignum/build.gradle.kts
Normal file
10
kmath-bignum/build.gradle.kts
Normal file
@ -0,0 +1,10 @@
|
||||
plugins { id("scientifik.mpp") }
|
||||
|
||||
kotlin.sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api("com.ionspin.kotlin:bignum:0.1.5")
|
||||
api(project(":kmath-core"))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import com.ionspin.kotlin.bignum.decimal.BigDecimal
|
||||
import com.ionspin.kotlin.bignum.decimal.DecimalMode
|
||||
import com.ionspin.kotlin.bignum.decimal.RoundingMode
|
||||
import scientifik.kmath.operations.Field
|
||||
|
||||
abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DEFAULT_MODE) :
|
||||
Field<BigDecimal> {
|
||||
override val zero: BigDecimal
|
||||
get() = BigDecimal.ZERO
|
||||
|
||||
override val one: BigDecimal
|
||||
get() = BigDecimal.ONE
|
||||
|
||||
override fun number(value: Number): BigDecimal = BigDecimal.fromDouble(value.toDouble(), mode)
|
||||
override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b, mode)
|
||||
override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.times(number(k))
|
||||
override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.times(b)
|
||||
override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b)
|
||||
override fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b, mode)
|
||||
override fun BigDecimal.times(k: Number): BigDecimal = multiply(number(k), mode)
|
||||
override fun BigDecimal.plus(b: Number): BigDecimal = add(number(b), mode)
|
||||
override fun BigDecimal.minus(b: Number): BigDecimal = subtract(number(b), mode)
|
||||
override fun BigDecimal.div(k: Number): BigDecimal = divide(number(k), mode)
|
||||
|
||||
companion object {
|
||||
internal val DEFAULT_MODE = DecimalMode(16, RoundingMode.ROUND_HALF_TOWARDS_ZERO)
|
||||
}
|
||||
}
|
||||
|
||||
class BigDecimalField(mode: DecimalMode = DEFAULT_MODE) : BigDecimalFieldBase(mode) {
|
||||
companion object : BigDecimalFieldBase()
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import scientifik.kmath.operations.Ring
|
||||
|
||||
object BigIntegerRing : Ring<BigInteger> {
|
||||
override val zero: BigInteger
|
||||
get() = BigInteger.ZERO
|
||||
|
||||
override val one: BigInteger
|
||||
get() = BigInteger.ONE
|
||||
|
||||
override fun number(value: Number): BigInteger = BigInteger.fromLong(value.toLong())
|
||||
override fun add(a: BigInteger, b: BigInteger): BigInteger = a + b
|
||||
override fun multiply(a: BigInteger, k: Number): BigInteger = a * k.toLong()
|
||||
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a * b
|
||||
override fun BigInteger.plus(b: Number): BigInteger = plus(b.toLong())
|
||||
override fun BigInteger.minus(b: Number): BigInteger = minus(b.toLong())
|
||||
override fun BigInteger.div(k: Number): BigInteger = div(k.toLong())
|
||||
override fun BigInteger.times(k: Number): BigInteger = times(k.toLong())
|
||||
}
|
@ -47,5 +47,6 @@ include(
|
||||
":kmath-for-real",
|
||||
":kmath-geometry",
|
||||
":kmath-ast",
|
||||
":kmath-bignum",
|
||||
":examples"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user