-
-
Notifications
You must be signed in to change notification settings - Fork 232
Redesign Differential #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f7f45e2
Turn Differential into a higher-order function
HarrisonGrodin b7a88c2
Clean up Derivative generation from DiffRules
HarrisonGrodin efe1d97
Remove "operator" terminology
HarrisonGrodin 07b15e8
Fix subtyping
HarrisonGrodin 97c5097
Fix dependence checking in differentiation
HarrisonGrodin 4043598
Make Differential directly callable
HarrisonGrodin d023a90
Change Expr conversion of Differential
HarrisonGrodin c7250b1
Test Expr conversions
HarrisonGrodin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
struct Differential <: Function | ||
x::Expression | ||
order::Int | ||
end | ||
Differential(x) = Differential(x,1) | ||
|
||
Base.show(io::IO, D::Differential) = print(io,"($(D.x),$(D.order))") | ||
Base.Expr(D::Differential) = 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. this might be problematic. This could be a good place to use a unicode symbol. |
||
|
||
function Derivative end | ||
(D::Differential)(x::Operation) = Operation(D, Expression[x]) | ||
function (D::Differential)(x::Variable) | ||
D.x === x && return Constant(1) | ||
has_dependent(x, D.x) || return Constant(0) | ||
return Variable(x,D) | ||
end | ||
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x | ||
|
||
Variable(x::Variable, D::Differential) = Variable(x.name,x.value,x.value_type, | ||
x.subtype,D,x.dependents,x.description,x.flow,x.domain, | ||
x.size,x.context) | ||
|
||
function expand_derivatives(O::Operation) | ||
@. O.args = expand_derivatives(O.args) | ||
|
||
if O.op isa Differential | ||
D = O.op | ||
o = O.args[1] | ||
return simplify_constants(sum(i->Derivative(o,i)*expand_derivatives(D(o.args[i])),1:length(o.args))) | ||
end | ||
|
||
return O | ||
end | ||
expand_derivatives(x::Variable) = x | ||
|
||
# Don't specialize on the function here | ||
function Derivative(O::Operation,idx) | ||
# This calls the Derivative dispatch from the user or pre-defined code | ||
Derivative(O.op, O.args, Val(idx)) | ||
end | ||
Derivative(op, args, idx) = Derivative(op, (args...,), idx) | ||
|
||
# Pre-defined derivatives | ||
import DiffRules, SpecialFunctions, NaNMath | ||
for (modu, fun, arity) ∈ DiffRules.diffrules() | ||
for i ∈ 1:arity | ||
@eval function Derivative(::typeof($modu.$fun), args::NTuple{$arity,Any}, ::Val{$i}) | ||
M, f = $(modu, fun) | ||
partials = DiffRules.diffrule(M, f, args...) | ||
dx = @static $arity == 1 ? partials : partials[$i] | ||
parse(Operation,dx) | ||
end | ||
end | ||
end | ||
|
||
function count_order(x) | ||
@assert !(x isa Symbol) "The variable $x must have an order of differentiation that is greater or equal to 1!" | ||
n = 1 | ||
while !(x.args[1] isa Symbol) | ||
n = n+1 | ||
x = x.args[1] | ||
end | ||
n, x.args[1] | ||
end | ||
|
||
function _differential_macro(x) | ||
ex = Expr(:block) | ||
lhss = Symbol[] | ||
x = flatten_expr!(x) | ||
for di in x | ||
@assert di isa Expr && di.args[1] == :~ "@Deriv expects a form that looks like `@Deriv D''~t E'~t`" | ||
lhs = di.args[2] | ||
rhs = di.args[3] | ||
order, lhs = count_order(lhs) | ||
push!(lhss, lhs) | ||
expr = :($lhs = Differential($rhs, $order)) | ||
push!(ex.args, expr) | ||
end | ||
push!(ex.args, Expr(:tuple, lhss...)) | ||
ex | ||
end | ||
|
||
macro Deriv(x...) | ||
esc(_differential_macro(x)) | ||
end | ||
|
||
function calculate_jacobian(eqs,vars) | ||
Expression[Differential(vars[j])(eqs[i]) for i in 1:length(eqs), j in 1:length(vars)] | ||
end | ||
|
||
export Differential, Derivative, expand_derivatives, @Deriv, calculate_jacobian |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hmm, this won't have the small union optimizations, but might be more correct?
Uh oh!
There was an error while loading. Please reload this page.
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.
Right. I'm currently attempting to refactor the IR such that the types seem a bit more correct/similarly shaped to the data they store (which in turn should make a lot of the code cleaner). Once everything seems correct and fully featured, I can focus more on optimizations.