Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add symbolic integral #240

Merged
merged 22 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Reexport = "0.2, 1"
ReferenceTests = "0.9"
Requires = "1.1"
RuntimeGeneratedFunctions = "0.4.3, 0.5"
SafeTestsets = "0"
SciMLBase = "1.8"
Setfield = "0.7"
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
Expand Down
6 changes: 6 additions & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ using LinearAlgebra

using Reexport

using DomainSets

import DomainSets: Domain
@reexport using SymbolicUtils

import SymbolicUtils: Term, Add, Mul, Pow, Sym, symtype,
Expand Down Expand Up @@ -57,6 +60,9 @@ export Differential, expand_derivatives

include("diff.jl")

export Integral
include("integral.jl")

include("array-lib.jl")

include("linear_algebra.jl")
Expand Down
22 changes: 22 additions & 0 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ function expand_derivatives(O::Symbolic, simplify=false; occurances=nothing)
return expand_derivatives(operation(arg)(inner), simplify)
end
end
elseif isa(operation(arg), Integral)
if isa(operation(arg).domain, AbstractInterval)
domain = operation(arg).domain
a, b = DomainSets.endpoints(domain)
c = 0
inner_function = expand_derivatives(arguments(arg)[1])
if istree(value(a))
t1 = SymbolicUtils.substitute(inner_function, Dict(operation(arg).x => value(a)))
t2 = D(a)
c -= t1*t2
end
if istree(value(b))
t1 = SymbolicUtils.substitute(inner_function, Dict(operation(arg).x => value(b)))
t2 = D(b)
c += t1*t2
end
inner = expand_derivatives(D(arguments(arg)[1]))
c += operation(arg)(inner)
return value(c)
end
end

l = length(arguments(arg))
Expand Down Expand Up @@ -151,6 +171,8 @@ function expand_derivatives(O::Symbolic, simplify=false; occurances=nothing)
x = +((!_iszero(c) ? vcat(c, exprs) : exprs)...)
return simplify ? SymbolicUtils.simplify(x) : x
end
elseif istree(O) && isa(operation(O), Integral)
return operation(O)(expand_derivatives(arguments(O)[1]))
elseif !hasderiv(O)
return O
else
Expand Down
16 changes: 16 additions & 0 deletions src/integral.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# basic integral struct with upper bound and lower bound.
struct Integral{X, T <: Domain} <: Function
x
domain::T
Integral(x,domain) = new{typeof(x),typeof(domain)}(Symbolics.value(x), domain)
end

(I::Integral)(x) = Term{SymbolicUtils.symtype(x)}(I, [x])
(I::Integral)(x::Num) = Num(I(Symbolics.value(x)))
SymbolicUtils.promote_symtype(::Integral, x) = x

function Base.show(io::IO, I::Integral)
print(io, "Integral(", I.x, ", ", I.domain, ")")
end

Base.:(==)(I1::Integral, I2::Integral) = convert(Bool, simplify(isequal(I1.x, I2.x) && isequal(I1.domain, I2.domain)))
35 changes: 35 additions & 0 deletions test/integral.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Symbolics, Test
using DomainSets
@variables x y

I1 = Integral(x, ClosedInterval(1, 5))
I2 = Integral(x, ClosedInterval(1, 5))
@test I1 == I2

@variables v(..) u(..) x y
D = Differential(x)
Dxx = Differential(x)^2
I = Integral(y, ClosedInterval(1, 5))
eq = D(I(u(x,y))) ~ 0
eq_test = I(D(u(x,y)))
@test isequal(expand_derivatives(eq.lhs), Symbolics.value(eq_test))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just using Symbolics.derivative could be fine.


eq = Dxx((I(u(x,y)))) + I(D(u(x,y))) ~ 0
eq_test = I(D(u(x,y))) + I(D(D(u(x,y))))
@test isequal(expand_derivatives(eq.lhs), Symbolics.value(eq_test))

I = Integral(y, ClosedInterval(1, v(x)))
eq = D((I(u(x,y)))) ~ 0
eq_test = I(D(u(x,y))) + D(v(x))*u(x, v(x))
@test isequal(expand_derivatives(eq.lhs), Symbolics.value(eq_test))

I = Integral(y, ClosedInterval(1, v(x)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you test an integral with the integrand u^2? Does it expand the derivative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does expand. Should I add this as a test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, in the last commit.

eq = Dxx((I(u(x,y)))) ~ 0
eq_test = D(I(D(u(x,y))) + D(v(x))*u(x, v(x)))
eq_test_ = I(D(D(u(x,y)))) + D(D(v(x)))*u(x, v(x)) + 2D(u(x,v(x)))*D(v(x))
@test isequal(expand_derivatives(eq.lhs), Symbolics.value(eq_test_))
@test isequal(expand_derivatives(eq.lhs), expand_derivatives(eq_test))

eq = D((I(u(x,y)^2))) ~ 0
eq_test = I(2D(u(x,y))*u(x,y)) + D(v(x))*u(x, v(x))^2
@test isequal(expand_derivatives(eq.lhs), Symbolics.value(eq_test))