Fix after series merge

This commit is contained in:
Alexander Nozik 2022-09-26 16:47:38 +03:00
parent 4d1137659b
commit d70389d2e6
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
36 changed files with 62 additions and 57 deletions

View File

@ -12,6 +12,8 @@ import space.kscience.kmath.commons.optimization.CMOptimizer
import space.kscience.kmath.distributions.NormalDistribution
import space.kscience.kmath.expressions.chiSquaredExpression
import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.operations.asIterable
import space.kscience.kmath.operations.toList
import space.kscience.kmath.optimization.FunctionOptimizationTarget
import space.kscience.kmath.optimization.optimizeWith
import space.kscience.kmath.optimization.resultPoint
@ -20,8 +22,6 @@ import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.real.DoubleVector
import space.kscience.kmath.real.map
import space.kscience.kmath.real.step
import space.kscience.kmath.structures.asIterable
import space.kscience.kmath.structures.toList
import space.kscience.plotly.*
import space.kscience.plotly.models.ScatterMode
import space.kscience.plotly.models.TraceValues

View File

@ -13,6 +13,8 @@ import space.kscience.kmath.distributions.NormalDistribution
import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.binding
import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.operations.asIterable
import space.kscience.kmath.operations.toList
import space.kscience.kmath.optimization.QowOptimizer
import space.kscience.kmath.optimization.chiSquaredOrNull
import space.kscience.kmath.optimization.fitWith
@ -20,8 +22,6 @@ import space.kscience.kmath.optimization.resultPoint
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.real.map
import space.kscience.kmath.real.step
import space.kscience.kmath.structures.asIterable
import space.kscience.kmath.structures.toList
import space.kscience.plotly.*
import space.kscience.plotly.models.ScatterMode
import kotlin.math.abs

View File

@ -3,12 +3,13 @@ package space.kscience.kmath.series
import kotlinx.html.FlowContent
import kotlinx.html.h1
import space.kscience.kmath.operations.DoubleBufferOps
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.operations.bufferAlgebra
import space.kscience.kmath.operations.toList
import space.kscience.kmath.stat.ksComparisonStatistic
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.slice
import space.kscience.kmath.structures.toList
import space.kscience.plotly.*
import kotlin.math.PI
@ -33,7 +34,7 @@ fun main() = with(Double.algebra.bufferAlgebra.seriesAlgebra()) {
val s2 = s1.slice(20U..50U).moveTo(40)
val s3: Buffer<Double> = s1.zip(s2) { l, r -> l + r } //s1 + s2
val s4 = ln(s3)
val s4 = DoubleBufferOps.ln(s3)
val kmTest = ksComparisonStatistic(s1, s2)

View File

@ -37,7 +37,8 @@ class StreamDoubleFieldND(override val shape: IntArray) : FieldND<Double, Double
this@StreamDoubleFieldND.shape,
shape
)
this is BufferND && this.shapeIndices == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer
this is BufferND && indices == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer
else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) }
}

View File

@ -6,11 +6,10 @@
package space.kscience.kmath.commons.random
import kotlinx.coroutines.runBlocking
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.misc.toIntExact
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.samplers.GaussianSampler
import space.kscience.kmath.samplers.next
import space.kscience.kmath.stat.next
public class CMRandomGeneratorWrapper(

View File

@ -13,11 +13,11 @@ import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.Symbol.Companion.y
import space.kscience.kmath.expressions.chiSquaredExpression
import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.operations.map
import space.kscience.kmath.optimization.*
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.asBuffer
import space.kscience.kmath.structures.map
import kotlin.math.pow
import kotlin.test.Test

View File

@ -6,8 +6,8 @@
package space.kscience.kmath.expressions
import space.kscience.kmath.operations.ExtendedField
import space.kscience.kmath.operations.asIterable
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asIterable
import space.kscience.kmath.structures.indices
import kotlin.jvm.JvmName

View File

@ -15,7 +15,7 @@ import kotlin.math.pow
import kotlin.math.pow as kpow
public class DoubleBufferND(
indexes: ShapeIndices,
indexes: ShapeIndexer,
override val buffer: DoubleBuffer,
) : MutableBufferND<Double>(indexes, buffer)
@ -35,7 +35,7 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND<Double, DoubleField>(D
arg: DoubleBufferND,
transform: (Double) -> Double,
): DoubleBufferND {
val indexes = arg.shapeIndices
val indexes = arg.indices
val array = arg.buffer.array
return DoubleBufferND(indexes, DoubleBuffer(indexes.linearSize) { transform(array[it]) })
}
@ -45,8 +45,8 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND<Double, DoubleField>(D
r: DoubleBufferND,
block: (l: Double, r: Double) -> Double,
): DoubleBufferND {
require(l.shapeIndices == r.shapeIndices) { "Zip requires the same shapes, but found ${l.shape} on the left and ${r.shape} on the right" }
val indexes = l.shapeIndices
require(l.indices == r.indices) { "Zip requires the same shapes, but found ${l.shape} on the left and ${r.shape} on the right" }
val indexes = l.indices
val lArray = l.buffer.array
val rArray = r.buffer.array
return DoubleBufferND(indexes, DoubleBuffer(indexes.linearSize) { block(lArray[it], rArray[it]) })

View File

@ -6,10 +6,10 @@
package space.kscience.kmath.nd
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.operations.asSequence
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.MutableBuffer
import space.kscience.kmath.structures.asMutableBuffer
import space.kscience.kmath.structures.asSequence
import kotlin.jvm.JvmInline
/**

View File

@ -54,7 +54,7 @@ public interface StructureND<out T> : Featured<StructureFeature>, WithShape {
* @return the lazy sequence of pairs of indices to values.
*/
@PerformancePitfall
public fun elements(): Sequence<Pair<IntArray, T>> = shapeIndices.asSequence().map { it to get(it) }
public fun elements(): Sequence<Pair<IntArray, T>> = indices.asSequence().map { it to get(it) }
/**
* Feature is some additional structure information that allows to access it special properties or hints.
@ -71,7 +71,7 @@ public interface StructureND<out T> : Featured<StructureFeature>, WithShape {
if (st1 === st2) return true
// fast comparison of buffers if possible
if (st1 is BufferND && st2 is BufferND && st1.shapeIndices == st2.shapeIndices)
if (st1 is BufferND && st2 is BufferND && st1.indices == st2.indices)
return Buffer.contentEquals(st1.buffer, st2.buffer)
//element by element comparison if it could not be avoided
@ -87,7 +87,7 @@ public interface StructureND<out T> : Featured<StructureFeature>, WithShape {
if (st1 === st2) return true
// fast comparison of buffers if possible
if (st1 is BufferND && st2 is BufferND && st1.shapeIndices == st2.shapeIndices)
if (st1 is BufferND && st2 is BufferND && st1.indices == st2.indices)
return Buffer.contentEquals(st1.buffer, st2.buffer)
//element by element comparison if it could not be avoided

View File

@ -6,6 +6,7 @@
package space.kscience.kmath.structures
import space.kscience.kmath.operations.WithSize
import space.kscience.kmath.operations.asSequence
import kotlin.jvm.JvmInline
import kotlin.reflect.KClass

View File

@ -7,7 +7,7 @@ package space.kscience.kmath.streaming
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import space.kscience.kmath.structures.asSequence
import space.kscience.kmath.operations.asSequence
import kotlin.test.Test
import kotlin.test.assertEquals

View File

@ -13,9 +13,9 @@ import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.operations.asIterable
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.asIterable
import kotlin.math.pow
/*
@ -131,7 +131,7 @@ public fun RealMatrix.extractColumn(columnIndex: Int): RealMatrix =
extractColumns(columnIndex..columnIndex)
public fun RealMatrix.sumByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j ->
columns[j].asIterable().sum()
columns[j].sum()
}
public fun RealMatrix.minByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j ->

View File

@ -13,8 +13,8 @@ import space.kscience.kmath.structures.DoubleBuffer
* Map one [BufferND] using function without indices.
*/
public inline fun BufferND<Double>.mapInline(crossinline transform: DoubleField.(Double) -> Double): BufferND<Double> {
val array = DoubleArray(shapeIndices.linearSize) { offset -> DoubleField.transform(buffer[offset]) }
return BufferND(shapeIndices, DoubleBuffer(array))
val array = DoubleArray(indices.linearSize) { offset -> DoubleField.transform(buffer[offset]) }
return BufferND(indices, DoubleBuffer(array))
}
/**

View File

@ -5,10 +5,10 @@
package space.kscience.kmath.integration
import space.kscience.kmath.operations.map
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.asBuffer
import space.kscience.kmath.structures.map
import kotlin.jvm.Synchronized
import kotlin.math.ulp
import kotlin.native.concurrent.ThreadLocal
@ -30,7 +30,7 @@ public fun GaussIntegratorRuleFactory.build(
numPoints: Int,
range: ClosedRange<Double>,
): Pair<Buffer<Double>, Buffer<Double>> {
val normalized = build(numPoints)
val normalized: Pair<Buffer<Double>, Buffer<Double>> = build(numPoints)
val length = range.endInclusive - range.start
val points = normalized.first.map(::DoubleBuffer) {

View File

@ -12,14 +12,10 @@ import space.kscience.kmath.interpolation.SplineInterpolator
import space.kscience.kmath.interpolation.interpolatePolynomials
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.Field
import space.kscience.kmath.operations.invoke
import space.kscience.kmath.operations.sum
import space.kscience.kmath.operations.*
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.MutableBufferFactory
import space.kscience.kmath.structures.map
/**
* Compute analytical indefinite integral of this [PiecewisePolynomial], keeping all intervals intact

View File

@ -6,7 +6,7 @@
package space.kscience.kmath.geometry
import space.kscience.kmath.structures.toList
import space.kscience.kmath.operations.toList
import kotlin.test.Test
import kotlin.test.assertEquals

View File

@ -5,7 +5,7 @@
package space.kscience.kmath.geometry
import space.kscience.kmath.structures.toList
import space.kscience.kmath.operations.toList
import kotlin.test.Test
import kotlin.test.assertEquals

View File

@ -10,7 +10,7 @@ import kotlinx.coroutines.test.runTest
import space.kscience.kmath.distributions.NormalDistribution
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.stat.RandomGenerator
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.nextBuffer
import kotlin.native.concurrent.ThreadLocal
import kotlin.test.Test
@ -36,7 +36,7 @@ internal class UniformHistogram1DTest {
@Test
fun rebinDown() = runTest {
val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(generator.nextDoubleBuffer(10000))
val h2 = Histogram.uniform1D(DoubleField,0.03).produceFrom(h1)
val h2 = Histogram.uniform1D(DoubleField, 0.03).produceFrom(h1)
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
}
@ -44,13 +44,13 @@ internal class UniformHistogram1DTest {
@Test
fun rebinUp() = runTest {
val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(generator.nextDoubleBuffer(10000))
val h2 = Histogram.uniform1D(DoubleField,0.01).produceFrom(h1)
val h2 = Histogram.uniform1D(DoubleField, 0.01).produceFrom(h1)
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
}
@ThreadLocal
companion object{
companion object {
private val generator = RandomGenerator.default(123)
}
}

View File

@ -21,9 +21,9 @@ import space.kscience.kmath.expressions.MST
import space.kscience.kmath.expressions.MstRing
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.nd.Structure2D
import space.kscience.kmath.operations.asSequence
import space.kscience.kmath.operations.invoke
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asSequence
/**
* A function for conversion of number to MST for pretty print
@ -47,11 +47,13 @@ internal class KMathJupyter : JupyterIntegration() {
syntaxRender.renderPart(mathRender.render(MST.Numeric(it)), s)
+s.toString()
}
is MST -> {
val s = StringBuilder()
syntaxRender.renderPart(mathRender.render(it), s)
+s.toString()
}
else -> {
+"<ms>"
+it.toString()

View File

@ -7,7 +7,7 @@ package space.kscience.kmath.distributions
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.samplers.Sampler
import space.kscience.kmath.stat.Sampler
/**
* A distribution of typed objects.

View File

@ -3,12 +3,11 @@
* 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.stat
package space.kscience.kmath.distributions
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.chains.SimpleChain
import space.kscience.kmath.distributions.Distribution
import space.kscience.kmath.distributions.Distribution1D
import space.kscience.kmath.random.RandomGenerator
public class UniformDistribution(public val range: ClosedFloatingPointRange<Double>) : Distribution1D<Double> {
private val length: Double = range.endInclusive - range.start

View File

@ -7,6 +7,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.BlockingDoubleChain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.Sampler
import space.kscience.kmath.structures.DoubleBuffer
import kotlin.math.ln
import kotlin.math.pow

View File

@ -8,6 +8,8 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.random.chain
import space.kscience.kmath.stat.Sampler
import space.kscience.kmath.stat.next
import kotlin.math.*
/**

View File

@ -8,6 +8,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.random.chain
import space.kscience.kmath.stat.Sampler
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min

View File

@ -7,6 +7,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.BlockingIntChain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.Sampler
import space.kscience.kmath.structures.IntBuffer
import kotlin.math.exp

View File

@ -7,6 +7,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.BlockingDoubleChain
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.Sampler
public interface BlockingDoubleSampler : Sampler<Double> {
override fun sample(generator: RandomGenerator): BlockingDoubleChain

View File

@ -8,6 +8,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.BlockingIntChain
import space.kscience.kmath.misc.toIntExact
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.Sampler
import space.kscience.kmath.structures.IntBuffer
import kotlin.math.*

View File

@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.first
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.chains.combine
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.BufferFactory
import space.kscience.kmath.structures.DoubleBuffer

View File

@ -13,6 +13,7 @@ import space.kscience.kmath.operations.Group
import space.kscience.kmath.operations.ScaleOperations
import space.kscience.kmath.operations.invoke
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.stat.Sampler
/**
* Implements [Sampler] by sampling only certain [value].

View File

@ -97,7 +97,7 @@ public class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>, L>(
* Build a new series
*/
public fun series(size: Int, fromIndex: Int = 0, block: A.(label: L) -> T): Series<T> {
return bufferFactory(size) {
return elementAlgebra.bufferFactory(size) {
val index = it + fromIndex
elementAlgebra.block(labelResolver(index))
}.moveTo(fromIndex)
@ -122,7 +122,7 @@ public class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>, L>(
* Map a series to another series of the same size
*/
public inline fun Buffer<T>.map(crossinline transform: A.(T) -> T): Series<T> {
val buf = bufferFactory(size) {
val buf = elementAlgebra.bufferFactory(size) {
elementAlgebra.transform(getAbsolute(it))
}
return buf.moveTo(indices.first)
@ -133,7 +133,7 @@ public class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>, L>(
*/
public inline fun Buffer<T>.mapWithLabel(crossinline transform: A.(arg: T, label: L) -> T): Series<T> {
val labels = labels
val buf = bufferFactory(size) {
val buf = elementAlgebra.bufferFactory(size) {
elementAlgebra.transform(getAbsolute(it), labels[it])
}
return buf.moveTo(indices.first)
@ -161,7 +161,7 @@ public class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>, L>(
crossinline operation: A.(left: T, right: T) -> T,
): Series<T> {
val newRange = indices.intersect(other.indices)
return bufferFactory(newRange.size) {
return elementAlgebra.bufferFactory(newRange.size) {
elementAlgebra.operation(
getAbsolute(it),
other.getAbsolute(it)

View File

@ -5,8 +5,8 @@
package space.kscience.kmath.stat
import space.kscience.kmath.operations.asSequence
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asSequence
/**
* Non-composable median

View File

@ -1,7 +1,7 @@
package space.kscience.kmath.stat
import space.kscience.kmath.operations.asIterable
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asIterable
/**
* Rank statistics

View File

@ -1,16 +1,15 @@
package space.kscience.kmath.stat
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.misc.sorted
import space.kscience.kmath.operations.*
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.BufferFactory
import space.kscience.kmath.structures.asIterable
import space.kscience.kmath.structures.sorted
import space.kscience.kmath.structures.MutableBufferFactory
public interface StatisticalAlgebra<T, out A : Algebra<T>, out BA : BufferAlgebra<T, A>> : Algebra<Buffer<T>> {
public val bufferAlgebra: BA
public val elementAlgebra: A get() = bufferAlgebra.elementAlgebra
public val bufferFactory: BufferFactory<T> get() = bufferAlgebra.bufferFactory
override val bufferFactory: MutableBufferFactory<Buffer<T>> get() = bufferAlgebra.bufferFactory
}
/**
@ -41,8 +40,8 @@ public fun <T : Comparable<T>, A, BA : BufferAlgebra<T, A>> StatisticalAlgebra<T
val n = sx.size
val m = sy.size
var rankX = 0
var rankY = 0
var rankX: Int = 0
var rankY: Int = 0
var curD: T = zero
// Find the max difference between cdf_x and cdf_y

View File

@ -8,8 +8,6 @@ package space.kscience.kmath.stat
import kotlinx.coroutines.runBlocking
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.random.chain
import space.kscience.kmath.samplers.Sampler
import space.kscience.kmath.samplers.sampleBuffer
import kotlin.test.Test
class SamplerTest {

View File

@ -8,8 +8,8 @@ package space.kscience.kmath.tensors.core.internal
import space.kscience.kmath.nd.as1D
import space.kscience.kmath.operations.DoubleBufferOps.Companion.map
import space.kscience.kmath.operations.toMutableList
import space.kscience.kmath.random.RandomGenerator
import space.kscience.kmath.samplers.GaussianSampler
import space.kscience.kmath.stat.RandomGenerator
import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.tensors.core.BufferedTensor
import space.kscience.kmath.tensors.core.DoubleTensor