-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Addition of Directional derivatives #12417
Conversation
sympy/vector/functions.py
Outdated
@@ -214,6 +214,38 @@ def gradient(scalar, coord_sys): | |||
|
|||
return coord_sys.delop(scalar).doit() | |||
|
|||
def directional_derivative(scalar, vect, coord_sys): | |||
""" | |||
Returns the directional derivative of a scalar field computed along a given vector |
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.
Here you have double white space, after "along a".
If you want to correct this, you shouldn't push new commit. You can use git commit --amend
. It overrides before one.
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.
@szymag , thanks for reviewing it. I will correct the mistake.
I have some doubts about your solution. I think that vector, which you take as parameter, should be normalized. Here, you can find some information about it (here or in the next film): Directional derivative is a function, but you return vector. More explicitly What do you think? |
Also quoting from Wikipedia's Definition
Therefore I have not kept the condition of |
You always should allowed user to put any vector, but then normalized them. I'm not going to persist on my idea, it is just convention. It will be better, if someone from maintainers takes a look. |
The directional derivative is unfortunately named, but you should allow any vector (including the zero vector) as input. That way, the directional derivative is a linear function in its direction for differentiable functions. Some textbooks work around the issue by defining the directional derivative only for unit vectors and leaving it the term "directional derivative" unspecified for non-unit vectors. However, your code will only produce the directional derivative for a differentiable function. The true definition of the directional derivative of |
As you said this functions allows any vector as input.
I have used the existing Gradient function in Sympy for DIrectional Derivative so it must be checking the conditions of differentiability as Gradient function further calls the existing Derivative function of Sympy. |
sympy/vector/functions.py
Outdated
5*R.x**2 + 30*R.x*R.z | ||
|
||
""" | ||
return gradient(scalar,coord_sys).dot(vect).doit() |
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.
This is unrelated to this PR, but I believe gradient
should be called as gradient(scala)
without the coord_sys
variable.
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.
Not really, look at documentation:
http://docs.sympy.org/dev/modules/vector/fields.html
We need manually put information about coordinate system. For me it is also strange
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.
I know that, that's why we need to modify the gradient function.
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.
Sorry, I misunderstand your comment a bit.
I know that, that's why we need to modify the gradient function.
I agree with you, but now it isn't so easy. If I correctly understand code, vector
has no information about his coordinate system, we need forward this information explicitly to function (like gradient).
I am writing proposal, where I also include this information, and I would like to fix this issue.
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.
@Upabjojr , It won't be that easy now as this would mean changing the whole sytem of deloperator.py and functions.py. So it's better to let it be in this way.
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.
Yes, I wrote that this issue is not related to this PR.
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.
If I correctly understand code, vector has no information about his coordinate system,
It has:
In [6]: c = CoordSysCartesian('rect')
In [7]: c
Out[7]: rect
In [8]: c.x
Out[8]: rect_x
In [9]: c.x.args
Out[9]: (rect.x, 0, rect, rectₓ, \mathbf{{x}_{rect}})
In [10]: c.x.args[2]
Out[10]: rect
In [11]: c.x.args[2] == c
Out[11]: True
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.
@Upabjojr , in reply to your last comment. I would like to point out that in whenever we call the gradient
function from vector/functions.py then it calls the class CoordSysCartesian
in vector/coordsysrect.py int his class delop
function is called. which further calls Del
function in deloperator.py . Now, the class CoordSysCartesian
requires the name of Coordinate system for assigning purposes so that's we are passing coordinate system in gradient function.
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.
Yeah, it needs some cleanup.
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.
@Upabjojr , please merge this PR. Thanks
sympy/vector/functions.py
Outdated
>>> R = CoordSysCartesian('R') | ||
>>> f1 = R.x*R.y*R.z | ||
>>> v1 = 3*R.i + 4*R.j + R.k | ||
>>> directional_derivative(f1, v1, R) |
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.
It's the same problem as in the gradient function. I don't like the R
parameter in the API.
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.
@Upabjojr , I understand what you mean, but as I told you earlier it would be a very large amount of work to clean up the whole coordinate system with each function. So for the existing system this is the best possible way
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.
Maybe the work for redefining the whole system can be done in other PRs
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.
No, it's simple. Use .atoms( )
.
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.
What does .atoms() does ? and how it will help here ?
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.
Maybe you can also extract the coordinate system object from vector.
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.
@Upabjojr , maybe we can extract that information but then it would create a problem that it would be out of convention. What I mean is that in the existing system the user has to to give cordinate system as input in every function , now if I create a function where user doesn't have to give the coordinate system as input then it would create confusion and disturb the conventions.
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.
Every time user wishes to use this function then he/she has to use a syntax which would be different from other functions.
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.
Let's start writing the new functions with an intuitive syntax. This can be fixed by adding one line to extract the coordinate system and then calling the gradient.
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.
Okay , let's do it
vector.atoms(CoordSysCartesian) |
@Upabjojr , it has passed all Tests except one which has error due to exceeded time limit. |
@Upabjojr , please review this PR thanks |
@Upabjojr , Thanks |
Fixes #12416