-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DoubleBuffer generator was moved to DoubleBuffer file and factory fun… #485
base: dev
Are you sure you want to change the base?
Changes from all commits
5bb895a
19cd740
b698f2d
7e4ece8
2144c63
d9b719f
2cdaa69
0e8eb44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
name: Dokka publication | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
workflow_dispatch: | ||
release: | ||
types: [ created ] | ||
|
||
jobs: | ||
build: | ||
|
@@ -24,7 +25,7 @@ jobs: | |
- uses: gradle/[email protected] | ||
with: | ||
arguments: dokkaHtmlMultiModule --no-parallel | ||
- uses: JamesIves/github-pages-deploy-action@4.2.5 | ||
- uses: JamesIves/github-pages-deploy-action@v4.3.0 | ||
with: | ||
branch: gh-pages | ||
folder: build/dokka/htmlMultiModule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2018-2021 KMath contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package space.kscience.kmath.operations | ||
|
||
import space.kscience.kmath.expressions.Symbol | ||
import space.kscience.kmath.expressions.symbol | ||
|
||
interface BinaryLogic<T : Any> { | ||
/** | ||
* Logic 'not' | ||
*/ | ||
public operator fun T.not(): T | ||
|
||
/** | ||
* Logic 'and' | ||
*/ | ||
public infix fun T.and(other: T): T | ||
|
||
/** | ||
* Logic 'or' | ||
*/ | ||
public infix fun T.or(other: T): T | ||
|
||
/** | ||
* Logic 'xor' | ||
*/ | ||
public infix fun T.xor(other: T): T | ||
|
||
companion object { | ||
public val TRUE: Symbol by symbol | ||
public val FALSE: Symbol by symbol | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package space.kscience.kmath.structures | ||
|
||
import kotlin.jvm.JvmInline | ||
import kotlin.random.Random.Default.nextDouble | ||
|
||
/** | ||
* Specialized [MutableBuffer] implementation over [DoubleArray]. | ||
|
@@ -42,6 +43,11 @@ public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer<D | |
*/ | ||
public inline fun DoubleBuffer(size: Int, init: (Int) -> Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) }) | ||
|
||
/** | ||
* A chunk of doubles of given [size]. | ||
*/ | ||
public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any reasons to add this to the core? |
||
|
||
/** | ||
* Returns a new [DoubleBuffer] of given elements. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI | |
import space.kscience.kmath.operations.DoubleField | ||
import space.kscience.kmath.stat.RandomGenerator | ||
import space.kscience.kmath.stat.nextBuffer | ||
import space.kscience.kmath.structures.nextDoubleBuffer | ||
import kotlin.native.concurrent.ThreadLocal | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
@@ -35,15 +36,15 @@ internal class UniformHistogram1DTest { | |
|
||
@Test | ||
fun rebinDown() = runTest { | ||
val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(generator.nextDoubleBuffer(10000)) | ||
val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(nextDoubleBuffer(10000)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see how it helps |
||
val h2 = Histogram.uniform1D(DoubleField,0.03).produceFrom(h1) | ||
|
||
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) | ||
} | ||
|
||
@Test | ||
fun rebinUp() = runTest { | ||
val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(generator.nextDoubleBuffer(10000)) | ||
val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(nextDoubleBuffer(10000)) | ||
val h2 = Histogram.uniform1D(DoubleField,0.01).produceFrom(h1) | ||
|
||
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,13 @@ package space.kscience.kmath.nd4j | |
import space.kscience.kmath.misc.toIntExact | ||
|
||
internal fun LongArray.toIntArray(): IntArray = IntArray(size) { this[it].toIntExact() } | ||
|
||
internal fun LongArray.linspace(start: Long, stop: Long) = Array(this.size) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try not to use Arrays in mathematics because they have some default overrides in Kotling StdLib. You should use Buffer and there are already BufferAlgebra.zero producers for that. There is a DoubleBuffer producer that uses step, we can add a similar one for Longs, but we need to be careful about naming. |
||
start + it * ((stop - start) / (this.size - 1)) | ||
} | ||
|
||
internal fun LongArray.zeros() = Array(this.size) { 0 } | ||
|
||
internal fun LongArray.ones() = Array(this.size) { 1 } | ||
|
||
internal fun repeat(number: Long, size: Int) = LongArray(size) { number } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2018-2021 KMath contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package space.kscience.kmath.nd4j | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ArraysTest { | ||
@Test | ||
fun checkLinspaceBuilder() { | ||
val array = LongArray(3).linspace(15, 22) | ||
assertEquals(array[0], 15) | ||
assertEquals(array[1], 18) | ||
assertEquals(array[2], 21) | ||
} | ||
|
||
@Test | ||
fun checkZerosBuilder() { | ||
val array = LongArray(3).zeros() | ||
assertEquals(array[0], 0) | ||
assertEquals(array[1], 0) | ||
assertEquals(array[2], 0) | ||
} | ||
|
||
@Test | ||
fun checkOnesBuilder() { | ||
val array = LongArray(3).ones() | ||
assertEquals(array[0], 1) | ||
assertEquals(array[1], 1) | ||
assertEquals(array[2], 1) | ||
} | ||
|
||
@Test | ||
fun checkRepeatBuilder() { | ||
val array = repeat(5, 3) | ||
assertEquals(array[0], 5) | ||
assertEquals(array[1], 5) | ||
assertEquals(array[2], 5) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package space.kscience.kmath.samplers | |
import space.kscience.kmath.chains.BlockingDoubleChain | ||
import space.kscience.kmath.stat.RandomGenerator | ||
import space.kscience.kmath.structures.DoubleBuffer | ||
import space.kscience.kmath.structures.nextDoubleBuffer | ||
import kotlin.math.* | ||
|
||
/** | ||
|
@@ -23,8 +24,8 @@ public object BoxMullerSampler : NormalizedGaussianSampler { | |
var state = Double.NaN | ||
|
||
override fun nextBufferBlocking(size: Int): DoubleBuffer { | ||
val xs = generator.nextDoubleBuffer(size) | ||
val ys = generator.nextDoubleBuffer(size) | ||
val xs = nextDoubleBuffer(size) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a mistake. You are using the default generator instead of provided one. It will give wrong results. |
||
val ys = nextDoubleBuffer(size) | ||
|
||
return DoubleBuffer(size) { index -> | ||
if (state.isNaN()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default generator should never be used outside of tests