From ca1a14b276f37026842c5c891dec1bdc401ad1e8 Mon Sep 17 00:00:00 2001 From: Greg Vernon Date: Mon, 7 Aug 2023 21:35:04 -0600 Subject: [PATCH 1/2] add support for definite integration --- src/integral.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/integral.jl b/src/integral.jl index 93eaf21..4f8c467 100644 --- a/src/integral.jl +++ b/src/integral.jl @@ -1,5 +1,6 @@ using LinearAlgebra using Statistics: mean, std +using Symbolics Base.signbit(z::Complex{T}) where {T <: Number} = signbit(real(z)) Base.signbit(x::SymbolicUtils.Sym{Number}) = false @@ -21,6 +22,7 @@ Arguments: ---------- - `eq`: a univariate expression - `x`: the independent variable (optional) +- `domain`: the domain upon which to evaluate a definite integral (optional) Keyword Arguments: ------------------ @@ -44,7 +46,7 @@ Output: - `unsolved`: the residual unsolved portion of the input - `err`: the numerical error in reaching the solution """ -function integrate(eq, x = nothing; abstol = 1e-6, num_steps = 2, num_trials = 10, +function integrate(eq, x = nothing, domain::Vector{<:Number} = nothing; abstol = 1e-6, num_steps = 2, num_trials = 10, radius = 1.0, show_basis = false, opt = STLSQ(exp.(-10:1:0)), bypass = false, symbolic = true, max_basis = 100, verbose = false, complex_plane = true, @@ -71,6 +73,9 @@ function integrate(eq, x = nothing; abstol = 1e-6, num_steps = 2, num_trials = 1 s, u, ϵ = integrate_sum(eq, x, l; bypass, abstol, num_trials, num_steps, radius, show_basis, opt, symbolic, max_basis, verbose, complex_plane, use_optim) + if domain != nothing + s = substitute( s, Dict( [ x=>domain[1] ] ) ) - substitute( s, Dict( [ x=>domain[2] ] ) ) + end # return simplify(s), u, ϵ return s, u, ϵ end From 9aaa42e1781358b04b634f93c22001c476580e4f Mon Sep 17 00:00:00 2001 From: Greg Vernon Date: Tue, 8 Aug 2023 21:50:33 -0600 Subject: [PATCH 2/2] add simple tests for definite integration --- src/integral.jl | 4 ++-- test/runtests.jl | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/integral.jl b/src/integral.jl index 4f8c467..8470738 100644 --- a/src/integral.jl +++ b/src/integral.jl @@ -46,7 +46,7 @@ Output: - `unsolved`: the residual unsolved portion of the input - `err`: the numerical error in reaching the solution """ -function integrate(eq, x = nothing, domain::Vector{<:Number} = nothing; abstol = 1e-6, num_steps = 2, num_trials = 10, +function integrate(eq, x = nothing, domain::Vector{<:Number} = [NaN]; abstol = 1e-6, num_steps = 2, num_trials = 10, radius = 1.0, show_basis = false, opt = STLSQ(exp.(-10:1:0)), bypass = false, symbolic = true, max_basis = 100, verbose = false, complex_plane = true, @@ -73,7 +73,7 @@ function integrate(eq, x = nothing, domain::Vector{<:Number} = nothing; abstol = s, u, ϵ = integrate_sum(eq, x, l; bypass, abstol, num_trials, num_steps, radius, show_basis, opt, symbolic, max_basis, verbose, complex_plane, use_optim) - if domain != nothing + if !all( domain .=== NaN ) s = substitute( s, Dict( [ x=>domain[1] ] ) ) - substitute( s, Dict( [ x=>domain[2] ] ) ) end # return simplify(s), u, ϵ diff --git a/test/runtests.jl b/test/runtests.jl index 9c94f76..aae091f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -240,8 +240,27 @@ function test_integrals(; kw...) return n end +function test_definite_integrals() + miss_count = 0 + eqn = x + val = SymbolicNumericIntegration.integrate( eqn, x, [ 0, 1 ] )[1] + miss_count += ( isapprox( val, 0.5 ) ) && ( typeof( val ) == typeof( 1 ) ) + val = SymbolicNumericIntegration.integrate( eqn, x, [ 0.0, 1.0 ] )[1] + miss_count += ( isapprox( val, 0.5 ) ) && ( typeof( val ) == typeof( 1.0 ) ) + val = SymbolicNumericIntegration.integrate( eqn, x, [ 0//1, 1//1 ] )[1] + miss_count += ( isapprox( val, 0.5 ) ) && ( typeof( val ) == typeof( 1//1 ) ) + val = SymbolicNumericIntegration.integrate( eqn, x, [ Num(π), Num(2π) ] )[1] + miss_count += ( isequal( val, 3*Num(π)^2/2 ) ) && ( typeof( val ) == typeof( Num(π) ) ) + return miss_count +end + @testset "integral" begin n = test_integrals(; symbolic = false, verbose = false, homotopy = true, num_steps = 2, num_trials = 10) @test n > 0 end + +@testset "definite_integral" begin + n = test_definite_integrals() + @test n == 0 +end