deloperator.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from sympy.core import Basic
  2. from sympy.vector.operators import gradient, divergence, curl
  3. class Del(Basic):
  4. """
  5. Represents the vector differential operator, usually represented in
  6. mathematical expressions as the 'nabla' symbol.
  7. """
  8. def __new__(cls):
  9. obj = super().__new__(cls)
  10. obj._name = "delop"
  11. return obj
  12. def gradient(self, scalar_field, doit=False):
  13. """
  14. Returns the gradient of the given scalar field, as a
  15. Vector instance.
  16. Parameters
  17. ==========
  18. scalar_field : SymPy expression
  19. The scalar field to calculate the gradient of.
  20. doit : bool
  21. If True, the result is returned after calling .doit() on
  22. each component. Else, the returned expression contains
  23. Derivative instances
  24. Examples
  25. ========
  26. >>> from sympy.vector import CoordSys3D, Del
  27. >>> C = CoordSys3D('C')
  28. >>> delop = Del()
  29. >>> delop.gradient(9)
  30. 0
  31. >>> delop(C.x*C.y*C.z).doit()
  32. C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k
  33. """
  34. return gradient(scalar_field, doit=doit)
  35. __call__ = gradient
  36. __call__.__doc__ = gradient.__doc__
  37. def dot(self, vect, doit=False):
  38. """
  39. Represents the dot product between this operator and a given
  40. vector - equal to the divergence of the vector field.
  41. Parameters
  42. ==========
  43. vect : Vector
  44. The vector whose divergence is to be calculated.
  45. doit : bool
  46. If True, the result is returned after calling .doit() on
  47. each component. Else, the returned expression contains
  48. Derivative instances
  49. Examples
  50. ========
  51. >>> from sympy.vector import CoordSys3D, Del
  52. >>> delop = Del()
  53. >>> C = CoordSys3D('C')
  54. >>> delop.dot(C.x*C.i)
  55. Derivative(C.x, C.x)
  56. >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
  57. >>> (delop & v).doit()
  58. C.x*C.y + C.x*C.z + C.y*C.z
  59. """
  60. return divergence(vect, doit=doit)
  61. __and__ = dot
  62. __and__.__doc__ = dot.__doc__
  63. def cross(self, vect, doit=False):
  64. """
  65. Represents the cross product between this operator and a given
  66. vector - equal to the curl of the vector field.
  67. Parameters
  68. ==========
  69. vect : Vector
  70. The vector whose curl is to be calculated.
  71. doit : bool
  72. If True, the result is returned after calling .doit() on
  73. each component. Else, the returned expression contains
  74. Derivative instances
  75. Examples
  76. ========
  77. >>> from sympy.vector import CoordSys3D, Del
  78. >>> C = CoordSys3D('C')
  79. >>> delop = Del()
  80. >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
  81. >>> delop.cross(v, doit = True)
  82. (-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j +
  83. (-C.x*C.z + C.y*C.z)*C.k
  84. >>> (delop ^ C.i).doit()
  85. 0
  86. """
  87. return curl(vect, doit=doit)
  88. __xor__ = cross
  89. __xor__.__doc__ = cross.__doc__
  90. def _sympystr(self, printer):
  91. return self._name