-
Notifications
You must be signed in to change notification settings - Fork 140
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
Changes from 10 commits
b3401bf
9ada0ec
0ed131b
4ac71b3
0f2fe53
0dae216
76871b3
59c7fae
53b3227
4c7bd4e
98efa55
73b3d54
3b2cb3c
530f3d5
061043c
a508dbf
29ff905
7f4d43f
60c1323
153463a
d96f248
320a71d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,25 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace. |
||
c = 0 | ||
inner_function = expand_derivatives_(arguments(arg)[1]) | ||
if isa(value(a), Term) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
t1 = replaceSym(operation(arg).x ,value(a) , inner_function ) | ||
t2 = D(a) | ||
c -= t1*t2 | ||
elseif isa(value(b) , Term) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I resolved this |
||
t1 = replaceSym(operation(arg).x ,value(b) , inner_function ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting needs to be cleaned up, we also never use camelCase function names. Good to stick to the package's style when you're contributing to it. Where is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I infact removed |
||
t2 = D(b) | ||
c += t1*t2 | ||
end | ||
inner = expand_derivatives_(D(arguments(arg)[1])) | ||
c += operation(arg)(inner) | ||
return c | ||
end | ||
end | ||
|
||
l = length(arguments(arg)) | ||
|
@@ -151,6 +170,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 isa(operation(O) , Integral ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you be more careful about whitespace? |
||
return operation(O)(expand_derivatives_(arguments(O)[1])) | ||
elseif !hasderiv(O) | ||
return O | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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) = (isequal(I1.x, I2.x) && isequal(I1.domain, I2.domain)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When comparing numbers, we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I guess domains could be symbolic. |
||
(D::Differential)(I::Integral{X,T}) where{X,T} = I∘D | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation. |
||
|
||
function replaceSym(a::Sym, b, O) | ||
if isa(O , Sym) | ||
if isequal(O , a) | ||
return b | ||
else | ||
return O | ||
end | ||
else | ||
args_replace = Vector{Symbolic{Real}}() | ||
args_ = arguments(O) | ||
for i in 1:length(args_) | ||
push!(args_replace, replaceSym(a , b , args_[i])) | ||
end | ||
return operation(O)(args_replace...) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Symbolics, Test | ||
using DomainSets | ||
@variables x y | ||
|
||
I1 = Integral( x , ClosedInterval(1, 5)) | ||
I2 = Integral( x , ClosedInterval(1, 5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here. |
||
@test I1 == I2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whitespace.