fix segmented integral

This commit is contained in:
Alexander Nozik 2021-04-19 11:26:06 +03:00
parent 86818f6864
commit fd4108b74f
3 changed files with 4 additions and 5 deletions

View File

@ -102,7 +102,7 @@ public fun <T : Any> Field<T>.integrate(
require(intervals > 0) { "Number of intervals must be positive" } require(intervals > 0) { "Number of intervals must be positive" }
val rangeSize = (range.endInclusive - range.start) / intervals val rangeSize = (range.endInclusive - range.start) / intervals
val ranges = UnivariateIntegrandRanges( val ranges = UnivariateIntegrandRanges(
(0 until intervals).map { i -> (rangeSize * i)..(rangeSize * i + 1) to order } (0 until intervals).map { i -> (rangeSize * i)..(rangeSize * (i + 1)) to order }
) )
return GaussIntegrator(this).integrate( return GaussIntegrator(this).integrate(
UnivariateIntegrand( UnivariateIntegrand(

View File

@ -24,14 +24,14 @@ class GaussIntegralTest {
@Test @Test
fun gaussUniform() { fun gaussUniform() {
val res = DoubleField.integrate(0.0..100.0,300) { x -> val res = DoubleField.integrate(0.0..100.0) { x ->
if(x in 30.0..50.0){ if(x in 30.0..50.0){
1.0 1.0
} else { } else {
0.0 0.0
} }
} }
assertEquals(20.0, res.value!!, 1.0) assertEquals(20.0, res.value!!, 0.5)
} }

View File

@ -20,8 +20,7 @@ public sealed class Nd4jArrayStructure<T> : MutableStructureND<T> {
*/ */
public abstract val ndArray: INDArray public abstract val ndArray: INDArray
public override val shape: IntArray public override val shape: IntArray get() = ndArray.shape().toIntArray()
get() = ndArray.shape().toIntArray()
internal abstract fun elementsIterator(): Iterator<Pair<IntArray, T>> internal abstract fun elementsIterator(): Iterator<Pair<IntArray, T>>
internal fun indicesIterator(): Iterator<IntArray> = ndArray.indicesIterator() internal fun indicesIterator(): Iterator<IntArray> = ndArray.indicesIterator()