Compare commits

...

8 Commits

Author SHA1 Message Date
Iaroslav Postovalov
a888f24fad
Fix dependency notation 2020-09-13 17:12:57 +07:00
Iaroslav Postovalov
e00b1153b1
Add SLF4J simple binding to examples 2020-09-13 17:12:07 +07:00
Iaroslav Postovalov
e6fb8bfc52
Change rounding to match the one from Java 2020-09-13 17:05:34 +07:00
Iaroslav Postovalov
9c3330cb6d
Implement decimal benchmark, update field implementation 2020-09-13 17:02:30 +07:00
Iaroslav Postovalov
855bab258a
Add decimal wrapper 2020-09-13 16:31:18 +07:00
Iaroslav Postovalov
dc02d957b0
Fix benchmarks 2020-09-13 16:18:35 +07:00
Iaroslav Postovalov
3dc59ccd26
Update benchmark 2020-09-12 18:43:58 +07:00
Iaroslav Postovalov
3b52eb7ac1
Implement bignum module and write basic benchmark 2020-09-12 18:22:59 +07:00
8 changed files with 152 additions and 3 deletions

View File

@ -32,10 +32,13 @@ dependencies {
implementation(project(":kmath-koma")) implementation(project(":kmath-koma"))
implementation(project(":kmath-viktor")) implementation(project(":kmath-viktor"))
implementation(project(":kmath-dimensions")) implementation(project(":kmath-dimensions"))
implementation(project(":kmath-bignum"))
implementation(project(":kmath-memory"))
implementation("com.kyonifer:koma-core-ejml:0.12") 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-io-jvm:0.2.0-npm-dev-6")
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-8") implementation("org.slf4j:slf4j-simple:1.7.30")
"benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath "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 // Configure benchmark

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -9,8 +9,9 @@ import scientifik.kmath.operations.RealField
import kotlin.random.Random import kotlin.random.Random
import kotlin.system.measureTimeMillis import kotlin.system.measureTimeMillis
class ExpressionsInterpretersBenchmark { private class ExpressionsInterpretersBenchmark {
private val algebra: Field<Double> = RealField private val algebra: Field<Double> = RealField
fun functionalExpression() { fun functionalExpression() {
val expr = algebra.expressionInField { val expr = algebra.expressionInField {
variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0)

View 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"))
}
}
}

View File

@ -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()
}

View File

@ -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())
}

View File

@ -47,5 +47,6 @@ include(
":kmath-for-real", ":kmath-for-real",
":kmath-geometry", ":kmath-geometry",
":kmath-ast", ":kmath-ast",
":kmath-bignum",
":examples" ":examples"
) )