From 07e569877035b1d2490684c92ad9794c865dca76 Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Mon, 11 Sep 2023 17:18:20 +0200 Subject: [PATCH 01/15] add IntegralDataProblem --- src/SciMLBase.jl | 2 +- src/problems/basic_problems.jl | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/SciMLBase.jl b/src/SciMLBase.jl index e4d1e371f..b583be5ab 100644 --- a/src/SciMLBase.jl +++ b/src/SciMLBase.jl @@ -762,7 +762,7 @@ export LinearProblem, NonlinearProblem, IntervalNonlinearProblem, IntegralProblem, OptimizationProblem -export IntegralProblem +export IntegralDataProblem export DiscreteProblem, ImplicitDiscreteProblem export SteadyStateProblem, SteadyStateSolution diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 6cb329290..90f5bfefa 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -386,6 +386,59 @@ function IntegralProblem(f, lb, ub, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end +@doc doc""" + +Defines a sampled integral problem. +Documentation Page: https://docs.sciml.ai/Integrals/stable/ + +## Mathematical Specification of a Sampled Integral Problem + +Integral problems define integrals over a set of data `y` +with corresponding sampling points `x`. + +`y` is an `AbstractArray` and `x` is a `AbstractVector`, +whose elements define the space being integrated along the dimension of `y` that is integrated. +This dimension is given by `dim`. + +## Problem Type + +### Constructors + +IntegralProblem(y,x; + dim=1, kwargs...) + +- `y`: The integrand +- `x`: The sampled integration domain +- `dim`: The dimension of `y`being integrated +- kwargs: Keyword arguments copied to the solvers. + +### Fields + +The fields match the names of the constructor arguments. +""" +struct IntegralDataProblem{Y<:AbstractArray, X<:AbstractVector, DIMVAL} <: AbstractIntegralProblem{false} + x::X + y::Y + dim::DIMVAL + kwargs::K + @add_kwonly function IntegralProblem(x, y; + dim = 1, + kwargs...) + @assert length(x) > 1 + @assert isfinite(first(x)) "Infinite limits are not supported" + @assert isfinite(last(x)) "Infinite limits are not supported" + @assert size(y, dim) == length(x) "Integrand and grid must be of equal length along the integrated dimension" + @assert axes(y, dim) == axes(x,1) "Grid and integrand array must use the same indexing along integrated dimension" + new{typeof(x), typeof(y), Val{dim}, typeof(kwargs)}(x, y, Val(dim), kwargs) + end +end + +TruncatedStacktraces.@truncate_stacktrace IntegralDataProblem 1 4 + +function IntegralProblem(x::AbstractArray, y::AbstractVector; dim=1, kwargs...) + IntegralDataProblem(x, y; dim=dim) +end + struct QuadratureProblem end @deprecate QuadratureProblem(args...; kwargs...) IntegralProblem(args...; kwargs...) From 181847d4c6a7d639ca4e51b3fe7291630914c026 Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Mon, 11 Sep 2023 17:55:06 +0200 Subject: [PATCH 02/15] fix mistake --- src/problems/basic_problems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 90f5bfefa..dd94f21a0 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -416,7 +416,7 @@ IntegralProblem(y,x; The fields match the names of the constructor arguments. """ -struct IntegralDataProblem{Y<:AbstractArray, X<:AbstractVector, DIMVAL} <: AbstractIntegralProblem{false} +struct IntegralDataProblem{Y<:AbstractArray, X<:AbstractVector, DIMVAL, K} <: AbstractIntegralProblem{false} x::X y::Y dim::DIMVAL From 2fcfbb549097ecd446304b127b4e0cb032b135b7 Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Mon, 11 Sep 2023 18:05:13 +0200 Subject: [PATCH 03/15] another small mistake --- src/problems/basic_problems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index dd94f21a0..24b955965 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -421,7 +421,7 @@ struct IntegralDataProblem{Y<:AbstractArray, X<:AbstractVector, DIMVAL, K} <: Ab y::Y dim::DIMVAL kwargs::K - @add_kwonly function IntegralProblem(x, y; + @add_kwonly function IntegralDataProblem(x, y; dim = 1, kwargs...) @assert length(x) > 1 From dfd917a38c9dd3d4acec6c804932e6601752ebee Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Wed, 13 Sep 2023 09:44:32 +0200 Subject: [PATCH 04/15] removed `IntegralDataProblem`, added x and dim field to `IntegralProblem` --- src/problems/basic_problems.jl | 85 ++++++++++++---------------------- 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 24b955965..877320ae5 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -334,10 +334,16 @@ which are `Number`s or `AbstractVector`s with the same geometry as `u`. ### Constructors +``` IntegralProblem{iip}(f,lb,ub,p=NullParameters(); nout=1, batch = 0, kwargs...) -- f: the integrand, `y = f(u,p)` for out-of-place or `f(y,u,p)` for in-place. +IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); + nout = 1, + batch = 0, kwargs...) +``` + +- f: the integrand, callable function `y = f(u,p)` for out-of-place or `f(y,u,p)` for in-place, or `AbstractArray` for sampled problems. - lb: Either a number or vector of lower bounds. - ub: Either a number or vector of upper bounds. - p: The parameters associated with the problem. @@ -353,6 +359,8 @@ IntegralProblem{iip}(f,lb,ub,p=NullParameters(); if `f` is a scalar valued function `y[i]` is the evaluation of `f` at a different point. Note that batch is a suggestion for the number of points, and it is not necessarily true that batch is the same as batchsize in all algorithms. +- x: Vector of gridpoints on which to evaluate the integrand for integrators that support this. Defaults to `nothing` +- dim: For integrals over sampled data (when `f isa AbstractArray`), this specifies the dimension along which is integrated. - kwargs: Keyword arguments copied to the solvers. Additionally, we can supply iip like IntegralProblem{iip}(...) as true or false to declare at @@ -362,21 +370,33 @@ compile time whether the integrator function is in-place. The fields match the names of the constructor arguments. """ -struct IntegralProblem{isinplace, P, F, B, K} <: AbstractIntegralProblem{isinplace} +struct IntegralProblem{isinplace, P, F, B, K, X, D} <: AbstractIntegralProblem{isinplace} f::F lb::B ub::B nout::Int p::P batch::Int + x::X + dim::D kwargs::K @add_kwonly function IntegralProblem{iip}(f, lb, ub, p = NullParameters(); nout = 1, - batch = 0, kwargs...) where {iip} + batch = 0, x=nothing, dim=Val(1), kwargs...) where {iip} @assert typeof(lb)==typeof(ub) "Type of lower and upper bound must match" warn_paramtype(p) - new{iip, typeof(p), typeof(f), typeof(lb), typeof(kwargs)}(f, lb, ub, nout, p, - batch, kwargs) + new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim) typeof(kwargs)}(f, lb, ub, nout, p, + batch, x, dim, kwargs) + end + @add_kwonly function IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); + dim=Val(1), + nout = 1, + batch = 0, kwargs...) where {iip} + lb = x[begin] + ub = x[end] + warn_paramtype(p) + new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, lb, ub, nout, p, + batch, x, dim, kwargs) end end @@ -385,58 +405,11 @@ TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 function IntegralProblem(f, lb, ub, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end - -@doc doc""" - -Defines a sampled integral problem. -Documentation Page: https://docs.sciml.ai/Integrals/stable/ - -## Mathematical Specification of a Sampled Integral Problem - -Integral problems define integrals over a set of data `y` -with corresponding sampling points `x`. - -`y` is an `AbstractArray` and `x` is a `AbstractVector`, -whose elements define the space being integrated along the dimension of `y` that is integrated. -This dimension is given by `dim`. - -## Problem Type - -### Constructors - -IntegralProblem(y,x; - dim=1, kwargs...) - -- `y`: The integrand -- `x`: The sampled integration domain -- `dim`: The dimension of `y`being integrated -- kwargs: Keyword arguments copied to the solvers. - -### Fields - -The fields match the names of the constructor arguments. -""" -struct IntegralDataProblem{Y<:AbstractArray, X<:AbstractVector, DIMVAL, K} <: AbstractIntegralProblem{false} - x::X - y::Y - dim::DIMVAL - kwargs::K - @add_kwonly function IntegralDataProblem(x, y; - dim = 1, - kwargs...) - @assert length(x) > 1 - @assert isfinite(first(x)) "Infinite limits are not supported" - @assert isfinite(last(x)) "Infinite limits are not supported" - @assert size(y, dim) == length(x) "Integrand and grid must be of equal length along the integrated dimension" - @assert axes(y, dim) == axes(x,1) "Grid and integrand array must use the same indexing along integrated dimension" - new{typeof(x), typeof(y), Val{dim}, typeof(kwargs)}(x, y, Val(dim), kwargs) - end +function IntegralProblem(f, x::AbstractVector{<:Number}, args...; kwargs...) + IntegralProblem{isinplace(f, 3)}(f, x, args...; kwargs...) end - -TruncatedStacktraces.@truncate_stacktrace IntegralDataProblem 1 4 - -function IntegralProblem(x::AbstractArray, y::AbstractVector; dim=1, kwargs...) - IntegralDataProblem(x, y; dim=dim) +function IntegralProblem(y::AbstractArray, x::AbstractVector{<:Number}, args...; dim::Int=1, kwargs...) + IntegralProblem{false}(y, x, args...; dim=Val(dim), kwargs...) end struct QuadratureProblem end From e60e921f5e7f2e538bbb6cc70275a41b492096ed Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Wed, 13 Sep 2023 09:45:37 +0200 Subject: [PATCH 05/15] Stop exporting IntegralDataProblem --- src/SciMLBase.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/SciMLBase.jl b/src/SciMLBase.jl index b583be5ab..f52d73b2c 100644 --- a/src/SciMLBase.jl +++ b/src/SciMLBase.jl @@ -762,8 +762,6 @@ export LinearProblem, NonlinearProblem, IntervalNonlinearProblem, IntegralProblem, OptimizationProblem -export IntegralDataProblem - export DiscreteProblem, ImplicitDiscreteProblem export SteadyStateProblem, SteadyStateSolution export NoiseProblem From a92e7f701ebd2814f2bcb1a4efdafb6d202abd5e Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Thu, 14 Sep 2023 14:18:50 +0200 Subject: [PATCH 06/15] fix typo in IntegralProblem --- src/problems/basic_problems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 877320ae5..cc106f3ec 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -385,7 +385,7 @@ struct IntegralProblem{isinplace, P, F, B, K, X, D} <: AbstractIntegralProblem{i batch = 0, x=nothing, dim=Val(1), kwargs...) where {iip} @assert typeof(lb)==typeof(ub) "Type of lower and upper bound must match" warn_paramtype(p) - new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim) typeof(kwargs)}(f, lb, ub, nout, p, + new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, lb, ub, nout, p, batch, x, dim, kwargs) end @add_kwonly function IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); From af4cfb8e328f6b60420d26fb24ba50697901c47a Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Thu, 14 Sep 2023 14:51:51 +0200 Subject: [PATCH 07/15] removed one unnecessary inner constructor --- src/problems/basic_problems.jl | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index cc106f3ec..7fe5a58fc 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -370,7 +370,7 @@ compile time whether the integrator function is in-place. The fields match the names of the constructor arguments. """ -struct IntegralProblem{isinplace, P, F, B, K, X, D} <: AbstractIntegralProblem{isinplace} +struct IntegralProblem{isinplace, P, F, B, X, D, K} <: AbstractIntegralProblem{isinplace} f::F lb::B ub::B @@ -382,22 +382,12 @@ struct IntegralProblem{isinplace, P, F, B, K, X, D} <: AbstractIntegralProblem{i kwargs::K @add_kwonly function IntegralProblem{iip}(f, lb, ub, p = NullParameters(); nout = 1, - batch = 0, x=nothing, dim=Val(1), kwargs...) where {iip} + batch = 0, x=nothing, dim=nothing, kwargs...) where {iip} @assert typeof(lb)==typeof(ub) "Type of lower and upper bound must match" warn_paramtype(p) new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, lb, ub, nout, p, batch, x, dim, kwargs) end - @add_kwonly function IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); - dim=Val(1), - nout = 1, - batch = 0, kwargs...) where {iip} - lb = x[begin] - ub = x[end] - warn_paramtype(p) - new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, lb, ub, nout, p, - batch, x, dim, kwargs) - end end TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 @@ -406,10 +396,10 @@ function IntegralProblem(f, lb, ub, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end function IntegralProblem(f, x::AbstractVector{<:Number}, args...; kwargs...) - IntegralProblem{isinplace(f, 3)}(f, x, args...; kwargs...) + IntegralProblem(f, x[begin], x[end], args...; x=x, kwargs...) end function IntegralProblem(y::AbstractArray, x::AbstractVector{<:Number}, args...; dim::Int=1, kwargs...) - IntegralProblem{false}(y, x, args...; dim=Val(dim), kwargs...) + IntegralProblem{false}(y, x[begin], x[end], args...; dim=Val(dim), kwargs...) end struct QuadratureProblem end From caedc7c5ad772a510ae23842420ec0262ad4176d Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Fri, 15 Sep 2023 09:43:35 +0200 Subject: [PATCH 08/15] remove method ambiguity --- src/problems/basic_problems.jl | 5 ++++- test/function_building_error_messages.jl | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 7fe5a58fc..1fde2ced6 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -392,7 +392,10 @@ end TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 -function IntegralProblem(f, lb, ub, args...; kwargs...) +function IntegralProblem(f, lb::AbstractVector{<:Number}, ub::AbstractVector{<:Number}, args...; kwargs...) + IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) +end +function IntegralProblem(f, lb::Number, ub::Number, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end function IntegralProblem(f, x::AbstractVector{<:Number}, args...; kwargs...) diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index a150371d8..8a1dbdf47 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -455,6 +455,7 @@ intf(u) = 1.0 intf(u, p) = 1.0 IntegralProblem(intf, 0.0, 1.0) + # Optimization optf(u) = 1.0 From 7cee282d8fc3a5c21638f2e1613c5db53a976e48 Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Fri, 15 Sep 2023 09:48:19 +0200 Subject: [PATCH 09/15] add problem building tests --- test/function_building_error_messages.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index 8a1dbdf47..ab8ddcbc4 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -453,8 +453,17 @@ NonlinearFunction(nfoop, vjp = nvjp) intf(u) = 1.0 @test_throws SciMLBase.TooFewArgumentsError IntegralProblem(intf, 0.0, 1.0) intf(u, p) = 1.0 +p = 2.0 +x = [1.0,2.0] +y = rand(2,2) IntegralProblem(intf, 0.0, 1.0) - +IntegralProblem(intf, 0.0, 1.0, p) +IntegralProblem(intf, [0.0], [1.0]) +IntegralProblem(intf, [0.0], [1.0], p) +IntegralProblem(intf, x) +IntegralProblem(intf, x, p) +IntegralProblem(y, x) +IntegralProblem(y, x, p) # Optimization From 6b21a0d8b29e1013319ad9f2b3fa7457a4b7a4cd Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Fri, 15 Sep 2023 09:58:47 +0200 Subject: [PATCH 10/15] ran the format_document.jl thing in VScode --- src/problems/basic_problems.jl | 21 +++++++++++++++------ test/function_building_error_messages.jl | 6 +++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 1fde2ced6..3cf8bb819 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -382,27 +382,36 @@ struct IntegralProblem{isinplace, P, F, B, X, D, K} <: AbstractIntegralProblem{i kwargs::K @add_kwonly function IntegralProblem{iip}(f, lb, ub, p = NullParameters(); nout = 1, - batch = 0, x=nothing, dim=nothing, kwargs...) where {iip} + batch = 0, x = nothing, dim = nothing, kwargs...) where {iip} @assert typeof(lb)==typeof(ub) "Type of lower and upper bound must match" warn_paramtype(p) - new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, lb, ub, nout, p, + new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, + lb, ub, nout, p, batch, x, dim, kwargs) end end TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 -function IntegralProblem(f, lb::AbstractVector{<:Number}, ub::AbstractVector{<:Number}, args...; kwargs...) +function IntegralProblem(f, + lb::AbstractVector{<:Number}, + ub::AbstractVector{<:Number}, + args...; + kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end function IntegralProblem(f, lb::Number, ub::Number, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end function IntegralProblem(f, x::AbstractVector{<:Number}, args...; kwargs...) - IntegralProblem(f, x[begin], x[end], args...; x=x, kwargs...) + IntegralProblem(f, x[begin], x[end], args...; x = x, kwargs...) end -function IntegralProblem(y::AbstractArray, x::AbstractVector{<:Number}, args...; dim::Int=1, kwargs...) - IntegralProblem{false}(y, x[begin], x[end], args...; dim=Val(dim), kwargs...) +function IntegralProblem(y::AbstractArray, + x::AbstractVector{<:Number}, + args...; + dim::Int = 1, + kwargs...) + IntegralProblem{false}(y, x[begin], x[end], args...; dim = Val(dim), kwargs...) end struct QuadratureProblem end diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index ab8ddcbc4..3a7b339d7 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -454,10 +454,10 @@ intf(u) = 1.0 @test_throws SciMLBase.TooFewArgumentsError IntegralProblem(intf, 0.0, 1.0) intf(u, p) = 1.0 p = 2.0 -x = [1.0,2.0] -y = rand(2,2) +x = [1.0, 2.0] +y = rand(2, 2) IntegralProblem(intf, 0.0, 1.0) -IntegralProblem(intf, 0.0, 1.0, p) +IntegralProblem(intf, 0.0, 1.0, p) IntegralProblem(intf, [0.0], [1.0]) IntegralProblem(intf, [0.0], [1.0], p) IntegralProblem(intf, x) From 09561b62e56c39a89b5dea18a5b86d404d6fb5e5 Mon Sep 17 00:00:00 2001 From: Ilian Pihlajamaa Date: Fri, 15 Sep 2023 11:33:42 +0200 Subject: [PATCH 11/15] Update basic_problems.jl --- src/problems/basic_problems.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 3cf8bb819..63ad114e0 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -393,17 +393,24 @@ end TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 -function IntegralProblem(f, +function IntegralProblem(f::Function, lb::AbstractVector{<:Number}, ub::AbstractVector{<:Number}, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end -function IntegralProblem(f, lb::Number, ub::Number, args...; kwargs...) +function IntegralProblem(f::Function, + lb::Number, + ub::Number, + args...; + kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end -function IntegralProblem(f, x::AbstractVector{<:Number}, args...; kwargs...) +function IntegralProblem(f::Function, + x::AbstractVector{<:Number}, + args...; + kwargs...) IntegralProblem(f, x[begin], x[end], args...; x = x, kwargs...) end function IntegralProblem(y::AbstractArray, @@ -411,7 +418,7 @@ function IntegralProblem(y::AbstractArray, args...; dim::Int = 1, kwargs...) - IntegralProblem{false}(y, x[begin], x[end], args...; dim = Val(dim), kwargs...) + IntegralProblem{false}(y, x[begin], x[end], p = nothing; dim = Val(dim), kwargs...) end struct QuadratureProblem end From cdc36635707177d6118aa560c4823ba6faa658b8 Mon Sep 17 00:00:00 2001 From: IlianPihlajamaa <73794090+IlianPihlajamaa@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:52:03 +0200 Subject: [PATCH 12/15] revert to DataIntegralProblem --- src/SciMLBase.jl | 2 +- src/problems/basic_problems.jl | 92 ++++++++++++++---------- test/function_building_error_messages.jl | 12 ++-- 3 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/SciMLBase.jl b/src/SciMLBase.jl index f52d73b2c..1de4703d6 100644 --- a/src/SciMLBase.jl +++ b/src/SciMLBase.jl @@ -760,7 +760,7 @@ export solve, solve!, init, discretize, symbolic_discretize export LinearProblem, NonlinearProblem, IntervalNonlinearProblem, - IntegralProblem, OptimizationProblem + IntegralProblem, DataIntegralProblem, OptimizationProblem export DiscreteProblem, ImplicitDiscreteProblem export SteadyStateProblem, SteadyStateSolution diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 63ad114e0..f794b0e16 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -337,13 +337,9 @@ which are `Number`s or `AbstractVector`s with the same geometry as `u`. ``` IntegralProblem{iip}(f,lb,ub,p=NullParameters(); nout=1, batch = 0, kwargs...) - -IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); - nout = 1, - batch = 0, kwargs...) ``` -- f: the integrand, callable function `y = f(u,p)` for out-of-place or `f(y,u,p)` for in-place, or `AbstractArray` for sampled problems. +- f: the integrand, callable function `y = f(u,p)` for out-of-place or `f(y,u,p)` for in-place. - lb: Either a number or vector of lower bounds. - ub: Either a number or vector of upper bounds. - p: The parameters associated with the problem. @@ -359,8 +355,6 @@ IntegralProblem{iip}(f, x::AbstractVector{<:Number}, p = NullParameters(); if `f` is a scalar valued function `y[i]` is the evaluation of `f` at a different point. Note that batch is a suggestion for the number of points, and it is not necessarily true that batch is the same as batchsize in all algorithms. -- x: Vector of gridpoints on which to evaluate the integrand for integrators that support this. Defaults to `nothing` -- dim: For integrals over sampled data (when `f isa AbstractArray`), this specifies the dimension along which is integrated. - kwargs: Keyword arguments copied to the solvers. Additionally, we can supply iip like IntegralProblem{iip}(...) as true or false to declare at @@ -370,62 +364,86 @@ compile time whether the integrator function is in-place. The fields match the names of the constructor arguments. """ -struct IntegralProblem{isinplace, P, F, B, X, D, K} <: AbstractIntegralProblem{isinplace} +struct IntegralProblem{isinplace, P, F, B, K} <: AbstractIntegralProblem{isinplace} f::F lb::B ub::B nout::Int p::P batch::Int - x::X - dim::D kwargs::K @add_kwonly function IntegralProblem{iip}(f, lb, ub, p = NullParameters(); nout = 1, - batch = 0, x = nothing, dim = nothing, kwargs...) where {iip} + batch = 0, kwargs...) where {iip} @assert typeof(lb)==typeof(ub) "Type of lower and upper bound must match" warn_paramtype(p) - new{iip, typeof(p), typeof(f), typeof(lb), typeof(x), typeof(dim), typeof(kwargs)}(f, + new{iip, typeof(p), typeof(f), typeof(lb), typeof(kwargs)}(f, lb, ub, nout, p, - batch, x, dim, kwargs) + batch, kwargs) end end TruncatedStacktraces.@truncate_stacktrace IntegralProblem 1 4 -function IntegralProblem(f::Function, - lb::AbstractVector{<:Number}, - ub::AbstractVector{<:Number}, - args...; - kwargs...) - IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) -end -function IntegralProblem(f::Function, - lb::Number, - ub::Number, - args...; +function IntegralProblem(f, lb, ub, args...; kwargs...) IntegralProblem{isinplace(f, 3)}(f, lb, ub, args...; kwargs...) end -function IntegralProblem(f::Function, - x::AbstractVector{<:Number}, - args...; - kwargs...) - IntegralProblem(f, x[begin], x[end], args...; x = x, kwargs...) -end -function IntegralProblem(y::AbstractArray, - x::AbstractVector{<:Number}, - args...; - dim::Int = 1, - kwargs...) - IntegralProblem{false}(y, x[begin], x[end], p = nothing; dim = Val(dim), kwargs...) -end struct QuadratureProblem end @deprecate QuadratureProblem(args...; kwargs...) IntegralProblem(args...; kwargs...) @doc doc""" +Defines a integral problem over pre-sampled data. +Documentation Page: https://docs.sciml.ai/Integrals/stable/ + +## Mathematical Specification of a data Integral Problem + +Data integral problems are defined as: + +```math +\sum_i w_i y_i +``` +where `y_i` are sampled values of the integrand, and `w_i` are weights +assigned by a quadrature rule, which depend on sampling points `x`. + +## Problem Type + +### Constructors + +``` +DataIntegralProblem(x::AbstractVector, y::AbstractArray; dim=1, kwargs...) +``` +- y: The sampled integrand, must be a subtype of `AbstractArray`. + It is assumed that the values of `y` along dimension `dim` + correspond to the integrand evaluated at sampling points `x` +- x: Sampling points, must be a subtype of `AbstractVector`. +- dim: Dimension along which to integrate. +- kwargs: Keyword arguments copied to the solvers. + +### Fields + +The fields match the names of the constructor arguments. +""" +struct DataIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} + y::Y + x::X + dim::D + kwargs::K + @add_kwonly function DataIntegralProblem(y::AbstractArray, x::AbstractVector; + dim = 1, + kwargs...) + @assert length(x)==size(y, dim) "The integrand `y` must have the same length as the sampling points `x` along the integrated dimension." + @assert axes(x, 1)==axes(y, dim) "The integrand `y` must obey the same indexing as the sampling points `x` along the integrated dimension." + new{typeof(y), typeof(x), Val{dim}, typeof(kwargs)}(y, x, Val(dim), kwargs) + end +end + +TruncatedStacktraces.@truncate_stacktrace DataIntegralProblem 1 4 + +@doc doc""" + Defines an optimization problem. Documentation Page: https://docs.sciml.ai/Optimization/stable/API/optimization_problem/ diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index 3a7b339d7..8fffc806b 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -454,16 +454,16 @@ intf(u) = 1.0 @test_throws SciMLBase.TooFewArgumentsError IntegralProblem(intf, 0.0, 1.0) intf(u, p) = 1.0 p = 2.0 -x = [1.0, 2.0] -y = rand(2, 2) + IntegralProblem(intf, 0.0, 1.0) IntegralProblem(intf, 0.0, 1.0, p) IntegralProblem(intf, [0.0], [1.0]) IntegralProblem(intf, [0.0], [1.0], p) -IntegralProblem(intf, x) -IntegralProblem(intf, x, p) -IntegralProblem(y, x) -IntegralProblem(y, x, p) + +x = [1.0, 2.0] +y = rand(2, 2) +DataIntegralProblem(y, x) +DataIntegralProblem(y, x; dim=2) # Optimization From d3619dba982275026aa1b0e91ecc3b627cd0599b Mon Sep 17 00:00:00 2001 From: IlianPihlajamaa <73794090+IlianPihlajamaa@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:57:18 +0200 Subject: [PATCH 13/15] add additional assert --- src/problems/basic_problems.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index f794b0e16..b2caeabe9 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -434,6 +434,7 @@ struct DataIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} @add_kwonly function DataIntegralProblem(y::AbstractArray, x::AbstractVector; dim = 1, kwargs...) + @assert dim <= ndims(y) "The integration dimension `dim` is larger than the number of dimensions of the integrand `y`" @assert length(x)==size(y, dim) "The integrand `y` must have the same length as the sampling points `x` along the integrated dimension." @assert axes(x, 1)==axes(y, dim) "The integrand `y` must obey the same indexing as the sampling points `x` along the integrated dimension." new{typeof(y), typeof(x), Val{dim}, typeof(kwargs)}(y, x, Val(dim), kwargs) From 124d3bec83466215bab456c3106e94be25e07481 Mon Sep 17 00:00:00 2001 From: IlianPihlajamaa <73794090+IlianPihlajamaa@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:45:01 +0200 Subject: [PATCH 14/15] rename `DataIntegralProblem` to `SampledIntegralProblem` --- src/SciMLBase.jl | 2 +- src/problems/basic_problems.jl | 10 +++++----- test/function_building_error_messages.jl | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SciMLBase.jl b/src/SciMLBase.jl index 1de4703d6..f0811160f 100644 --- a/src/SciMLBase.jl +++ b/src/SciMLBase.jl @@ -760,7 +760,7 @@ export solve, solve!, init, discretize, symbolic_discretize export LinearProblem, NonlinearProblem, IntervalNonlinearProblem, - IntegralProblem, DataIntegralProblem, OptimizationProblem + IntegralProblem, SampledIntegralProblem, OptimizationProblem export DiscreteProblem, ImplicitDiscreteProblem export SteadyStateProblem, SteadyStateSolution diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index b2caeabe9..0217c3f37 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -400,7 +400,7 @@ Documentation Page: https://docs.sciml.ai/Integrals/stable/ ## Mathematical Specification of a data Integral Problem -Data integral problems are defined as: +Sampled integral problems are defined as: ```math \sum_i w_i y_i @@ -413,7 +413,7 @@ assigned by a quadrature rule, which depend on sampling points `x`. ### Constructors ``` -DataIntegralProblem(x::AbstractVector, y::AbstractArray; dim=1, kwargs...) +SampledIntegralProblem(x::AbstractVector, y::AbstractArray; dim=1, kwargs...) ``` - y: The sampled integrand, must be a subtype of `AbstractArray`. It is assumed that the values of `y` along dimension `dim` @@ -426,12 +426,12 @@ DataIntegralProblem(x::AbstractVector, y::AbstractArray; dim=1, kwargs...) The fields match the names of the constructor arguments. """ -struct DataIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} +struct SampledIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} y::Y x::X dim::D kwargs::K - @add_kwonly function DataIntegralProblem(y::AbstractArray, x::AbstractVector; + @add_kwonly function SampledIntegralProblem(y::AbstractArray, x::AbstractVector; dim = 1, kwargs...) @assert dim <= ndims(y) "The integration dimension `dim` is larger than the number of dimensions of the integrand `y`" @@ -441,7 +441,7 @@ struct DataIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} end end -TruncatedStacktraces.@truncate_stacktrace DataIntegralProblem 1 4 +TruncatedStacktraces.@truncate_stacktrace SampledIntegralProblem 1 4 @doc doc""" diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index 8fffc806b..c424a6f84 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -462,8 +462,8 @@ IntegralProblem(intf, [0.0], [1.0], p) x = [1.0, 2.0] y = rand(2, 2) -DataIntegralProblem(y, x) -DataIntegralProblem(y, x; dim=2) +SampledIntegralProblem(y, x) +SampledIntegralProblem(y, x; dim=2) # Optimization From 1e960d185092f00a4fb460b0573f7e65d6cdcb31 Mon Sep 17 00:00:00 2001 From: IlianPihlajamaa <73794090+IlianPihlajamaa@users.noreply.github.com> Date: Sun, 17 Sep 2023 11:28:26 +0200 Subject: [PATCH 15/15] change default dimension to `ndims(y)` --- src/problems/basic_problems.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 0217c3f37..e0385140d 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -413,13 +413,13 @@ assigned by a quadrature rule, which depend on sampling points `x`. ### Constructors ``` -SampledIntegralProblem(x::AbstractVector, y::AbstractArray; dim=1, kwargs...) +SampledIntegralProblem(y::AbstractArray, x::AbstractVector; dim=ndims(y), kwargs...) ``` - y: The sampled integrand, must be a subtype of `AbstractArray`. It is assumed that the values of `y` along dimension `dim` correspond to the integrand evaluated at sampling points `x` - x: Sampling points, must be a subtype of `AbstractVector`. -- dim: Dimension along which to integrate. +- dim: Dimension along which to integrate. Defaults to the last dimension of `y`. - kwargs: Keyword arguments copied to the solvers. ### Fields @@ -432,7 +432,7 @@ struct SampledIntegralProblem{Y, X, D, K} <: AbstractIntegralProblem{false} dim::D kwargs::K @add_kwonly function SampledIntegralProblem(y::AbstractArray, x::AbstractVector; - dim = 1, + dim = ndims(y), kwargs...) @assert dim <= ndims(y) "The integration dimension `dim` is larger than the number of dimensions of the integrand `y`" @assert length(x)==size(y, dim) "The integrand `y` must have the same length as the sampling points `x` along the integrated dimension."