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

Segfault in Derivative #82

Closed
jpfairbanks opened this issue Jan 11, 2019 · 2 comments · Fixed by #88
Closed

Segfault in Derivative #82

jpfairbanks opened this issue Jan 11, 2019 · 2 comments · Fixed by #88
Assignees
Labels
bug Something isn't working

Comments

@jpfairbanks
Copy link
Contributor

This package looks so cool. I was trying to use it based on the readme and came across this error.

It is probably user error, but why does this segfault?

# This causes a seg fault.
using ModelingToolkit

# Define some variables
@IVar t
@DVar x(t) y(t) z(t)
@Param σ ρ β

f(x,y,z) = z*x+y
Derivative(f, x, 1)
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.2 (2018-11-08)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> include("test/derivatives_fail.jl")
Segmentation fault: 11
@jpfairbanks jpfairbanks changed the title Stack overflow in Derivative Segfault in Derivative Jan 11, 2019
@HarrisonGrodin HarrisonGrodin self-assigned this Jan 11, 2019
@HarrisonGrodin HarrisonGrodin added the bug Something isn't working label Jan 11, 2019
@HarrisonGrodin
Copy link
Contributor

HarrisonGrodin commented Jan 11, 2019

This seems like a combination of user and internal error. Hopefully the internal error is solved in #84.

To get the desired behavior (which I'm assuming is differentiating f(x,y,z) with respect to x?), you should be able to use the following:

@Deriv D'~x

expand_derivatives(D(f(x,y,z)))

Derivative doesn't taken into account the chain rule. (In fact, it acts more as a database of partial derivative rules.) Thus, we instead construct a Differential object with respect to x using @Deriv D'~x, apply it to the desired expression, and (symbolically) expand the derivatives.

@ChrisRackauckas
Copy link
Member

Yes, the Differential should be used, and Derivative is a behind the scenes way that it's actually implemented in the IR. We need to better separate the internals vs externals of the documentation here.

jpfairbanks added a commit to jpfairbanks/ModelingToolkit.jl that referenced this issue Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants