diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt index 73770726b..8d5349bce 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt @@ -5,6 +5,9 @@ package space.kscience.kmath.functions +import space.kscience.kmath.integration.integrate +import space.kscience.kmath.integration.integrator +import space.kscience.kmath.integration.value import space.kscience.kmath.operations.DoubleField import kotlin.math.pow @@ -13,8 +16,8 @@ fun main() { val function: UnivariateFunction = { x -> 3 * x.pow(2) + 2 * x + 1 } //get the result of the integration - val result = DoubleField.integrate(0.0..10.0, function = function) + val result = DoubleField.integrator.integrate(0.0..10.0, function = function) //the value is nullable because in some cases the integration could not succeed - println(result.valueOrNull) + println(result.value) } \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt index 184672044..601a0e3c4 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt @@ -5,6 +5,9 @@ package space.kscience.kmath.functions +import space.kscience.kmath.integration.integrate +import space.kscience.kmath.integration.integrator +import space.kscience.kmath.integration.value import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.nd import space.kscience.kmath.operations.DoubleField @@ -22,9 +25,9 @@ fun main(): Unit = DoubleField { val function: (Double) -> StructureND = { x: Double -> 3 * number(x).pow(2) + 2 * diagonal(x) + 1 } //get the result of the integration - val result = integrate(0.0..10.0, function = function) + val result = integrator.integrate(0.0..10.0, function = function) //the value is nullable because in some cases the integration could not succeed - println(result.valueOrNull) + println(result.value) } } \ No newline at end of file diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt index 97dc94d1d..c5573fef1 100644 --- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt +++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt @@ -7,6 +7,7 @@ package space.kscience.kmath.commons.integration import org.junit.jupiter.api.Test import space.kscience.kmath.integration.integrate +import space.kscience.kmath.integration.value import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.DoubleField.sin import kotlin.math.PI @@ -19,7 +20,7 @@ internal class IntegrationTest { @Test fun simpson() { - val res = CMIntegrator.simpson().integrate(0.0..2 * PI, function = function) + val res = CMIntegrator.simpson().integrate(0.0..2 * PI, function = function).value assertTrue { abs(res) < 1e-3 } } @@ -28,7 +29,7 @@ internal class IntegrationTest { val res = CMIntegrator.simpson().integrate(0.0..PI, { targetRelativeAccuracy = 1e-4 targetAbsoluteAccuracy = 1e-4 - }, function) + }, function).value assertTrue { abs(res - 2) < 1e-3 } assertTrue { abs(res - 2) > 1e-12 } } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt index d6e716371..0038ca0f2 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt @@ -6,7 +6,9 @@ package space.kscience.kmath.integration import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.Field -import space.kscience.kmath.structures.* +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.asBuffer +import space.kscience.kmath.structures.indices /** * Set of univariate integration ranges. First components correspond to ranges themselves, second components to number of @@ -81,7 +83,7 @@ public val Field.integrator: GaussIntegrator get() = GaussIntegrat /** - * Use [integrate] to integrate the function in the current algebra with given [range] and [numPoints] + * Integrate using [intervals] segments with Gauss-Legendre rule of [order] order */ @UnstableKMathAPI public fun GaussIntegrator.integrate( diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt index b611b3f65..1f9f7aedf 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt @@ -16,7 +16,7 @@ import kotlin.test.assertEquals class GaussIntegralTest { @Test fun gaussSin() { - val res = DoubleField.integrate(0.0..2 * PI) { x -> + val res = DoubleField.integrator.integrate(0.0..2 * PI) { x -> sin(x) } assertEquals(0.0, res.valueOrNull!!, 1e-2) @@ -24,7 +24,7 @@ class GaussIntegralTest { @Test fun gaussUniform() { - val res = DoubleField.integrate(0.0..100.0) { x -> + val res = DoubleField.integrator.integrate(0.0..100.0) { x -> if(x in 30.0..50.0){ 1.0 } else {