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
Create operators.py file #12696
Merged
Merged
Create operators.py file #12696
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f9527ab
Create operators.py file.
szymag 7e1c4c6
Apply sugestion from comments.
szymag 269622a
Remove coord_sys parameter from constructor.
szymag f40c5a1
Extract _get_coord_sys_from_expr as global function.
szymag 6334e3d
Small clean up in operators.py. Add warning.
szymag 467a87b
doit( ) with **kwargs and sympify in constructors
Upabjojr 77bc74e
Remove unnecessary things.
szymag 07f2af1
Add tests to core/tests/tests_args.py.
szymag e5b359a
Add exception in operators file.
szymag 3a75e7e
Repair test name in test_args.py
szymag 6f2333d
Clean up in operators.py.
szymag 9f666a0
inherit Expr instead of Basic
Upabjojr File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,184 @@ | ||
from sympy.core import Expr | ||
from sympy.vector.coordsysrect import CoordSysCartesian | ||
from sympy.vector.vector import Vector | ||
from sympy.vector.deloperator import Del | ||
|
||
|
||
class Gradient(Expr): | ||
""" | ||
Represents unevaluated Gradient. | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, Gradient | ||
>>> R = CoordSysCartesian('R') | ||
>>> s = R.x*R.y*R.z | ||
>>> Gradient(s, R) | ||
Gradient(R.x*R.y*R.z, R) | ||
|
||
""" | ||
|
||
def __new__(cls, scalar, coord_sys): | ||
obj = Expr.__new__(cls, scalar, coord_sys) | ||
obj._scalar = scalar | ||
obj._coord_sys = coord_sys | ||
return obj | ||
|
||
def doit(self): | ||
return Del(self._coord_sys).gradient(self._scalar).doit() | ||
|
||
|
||
class Divergence(Expr): | ||
""" | ||
Represents unevaluated Divergence. | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, Gradient | ||
>>> R = CoordSysCartesian('R') | ||
>>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k | ||
>>> Divergence(v, R) | ||
Divergence(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k, R) | ||
|
||
""" | ||
|
||
def __new__(cls, vect, coord_sys): | ||
obj = Expr.__new__(cls, vect, coord_sys) | ||
obj._vect = vect | ||
obj._coord_sys = coord_sys | ||
return obj | ||
|
||
def doit(self): | ||
return Del(self._coord_sys).dot(self._vect).doit() | ||
|
||
|
||
class Curl(Expr): | ||
""" | ||
Represents unevaluated Curl. | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, Curl | ||
>>> R = CoordSysCartesian('R') | ||
>>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k | ||
>>> Curl(v, R) | ||
Curl(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k, R) | ||
|
||
""" | ||
|
||
def __new__(cls, vect, coord_sys): | ||
obj = Expr.__new__(cls, vect, coord_sys) | ||
obj._vect = vect | ||
obj._coord_sys = coord_sys | ||
return obj | ||
|
||
def doit(self): | ||
return Del(self._coord_sys).cross(self._vect).doit() | ||
|
||
|
||
def gradient(scalar, coord_sys=None): | ||
""" | ||
Returns the vector gradient of a scalar field computed wrt the | ||
base scalars of the given coordinate system. | ||
|
||
Parameters | ||
========== | ||
|
||
scalar : SymPy Expr | ||
The scalar field to compute the gradient of | ||
|
||
coord_sys : CoordSysCartesian | ||
The coordinate system to calculate the gradient in | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, gradient | ||
>>> R = CoordSysCartesian('R') | ||
>>> s1 = R.x*R.y*R.z | ||
>>> gradient(s1, R) | ||
R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k | ||
>>> s2 = 5*R.x**2*R.z | ||
>>> gradient(s2, R) | ||
10*R.x*R.z*R.i + 5*R.x**2*R.k | ||
|
||
""" | ||
|
||
if coord_sys is None: | ||
coord_sys = list(scalar.atoms(CoordSysCartesian))[0] | ||
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. Put this in a private function, like |
||
return Gradient(scalar, coord_sys).doit() | ||
else: | ||
return Gradient(scalar, coord_sys).doit() | ||
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. Repetition of code. |
||
|
||
|
||
def divergence(vect, coord_sys=None): | ||
""" | ||
Returns the divergence of a vector field computed wrt the base | ||
scalars of the given coordinate system. | ||
|
||
Parameters | ||
========== | ||
|
||
vect : Vector | ||
The vector operand | ||
|
||
coord_sys : CoordSysCartesian | ||
The coordinate system to calculate the gradient in | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, divergence | ||
>>> R = CoordSysCartesian('R') | ||
>>> v1 = R.x*R.y*R.z * (R.i+R.j+R.k) | ||
>>> divergence(v1) | ||
R.x*R.y + R.x*R.z + R.y*R.z | ||
>>> v2 = 2*R.y*R.z*R.j | ||
>>> divergence(v2, R) | ||
2*R.z | ||
|
||
""" | ||
|
||
if coord_sys is None: | ||
coord_sys = vect._sys | ||
return Curl(vect, coord_sys).doit() | ||
else: | ||
return Curl(vect, coord_sys).doit() | ||
|
||
|
||
def curl(vect, coord_sys=None): | ||
""" | ||
Returns the curl of a vector field computed wrt the base scalars | ||
of the given coordinate system. | ||
|
||
Parameters | ||
========== | ||
|
||
vect : Vector | ||
The vector operand | ||
|
||
coord_sys : CoordSysCartesian | ||
The coordinate system to calculate the gradient in | ||
|
||
Examples | ||
======== | ||
|
||
>>> from sympy.vector import CoordSysCartesian, curl | ||
>>> R = CoordSysCartesian('R') | ||
>>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k | ||
>>> curl(v1) | ||
0 | ||
>>> v2 = R.x*R.y*R.z*R.i | ||
>>> curl(v2, R) | ||
R.x*R.y*R.j + (-R.x*R.z)*R.k | ||
|
||
""" | ||
|
||
if coord_sys is None: | ||
coord_sys = vect._sys | ||
return Curl(vect, coord_sys).doit() | ||
else: | ||
return Curl(vect, coord_sys).doit() |
This file contains 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,29 @@ | ||
from sympy.vector import CoordSysCartesian, Gradient, Divergence, Curl, VectorZero | ||
|
||
|
||
R = CoordSysCartesian('R') | ||
s1 = R.x*R.y*R.z | ||
s2 = R.x + 3*R.y**2 | ||
v1 = R.x*R.i + R.z*R.z*R.j | ||
v2 = R.x*R.i + R.y*R.j + R.z*R.k | ||
|
||
|
||
def test_Gradient(): | ||
assert Gradient(s1, R) == Gradient(R.x*R.y*R.z, R) | ||
assert Gradient(s2, R) == Gradient(R.x + 3*R.y**2, R) | ||
assert Gradient(s1, R).doit() == R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k | ||
assert Gradient(s2, R).doit() == R.i + 6*R.y*R.j | ||
|
||
|
||
def test_Divergence(): | ||
assert Divergence(v1, R) == Divergence(R.x*R.i + R.z*R.z*R.j, R) | ||
assert Divergence(v2, R) == Divergence(R.x*R.i + R.y*R.j + R.z*R.k, R) | ||
assert Divergence(v1, R).doit() == 1 | ||
assert Divergence(v2, R).doit() == 3 | ||
|
||
|
||
def test_Curl(): | ||
assert Curl(v1, R) == Curl(R.x*R.i + R.z*R.z*R.j, R) | ||
assert Curl(v2, R) == Curl(R.x*R.i + R.y*R.j + R.z*R.k, R) | ||
assert Curl(v1, R).doit() == (-2*R.z)*R.i | ||
assert Curl(v2, R).doit() == VectorZero() |
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.
Let's remove the
coord_sys
dependency, let's define the operators as close as possible to how they are defined in the math books.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.
OK. I will remove this parameter and obtain it in similar manner as in funtions