Refactor integrator API.
This commit is contained in:
parent
6f39b38a72
commit
d1e76175b7
@ -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<Double> = { 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)
|
||||
}
|
@ -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<Double> = { 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)
|
||||
}
|
||||
}
|
@ -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 }
|
||||
}
|
||||
|
@ -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 <T:Any> Field<T>.integrator: GaussIntegrator<T> 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 <T : Any> GaussIntegrator<T>.integrate(
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user