test_operators.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from sympy.vector import CoordSys3D, Gradient, Divergence, Curl, VectorZero, Laplacian
  2. from sympy.printing.repr import srepr
  3. R = CoordSys3D('R')
  4. s1 = R.x*R.y*R.z # type: ignore
  5. s2 = R.x + 3*R.y**2 # type: ignore
  6. s3 = R.x**2 + R.y**2 + R.z**2 # type: ignore
  7. v1 = R.x*R.i + R.z*R.z*R.j # type: ignore
  8. v2 = R.x*R.i + R.y*R.j + R.z*R.k # type: ignore
  9. v3 = R.x**2*R.i + R.y**2*R.j + R.z**2*R.k # type: ignore
  10. def test_Gradient():
  11. assert Gradient(s1) == Gradient(R.x*R.y*R.z)
  12. assert Gradient(s2) == Gradient(R.x + 3*R.y**2)
  13. assert Gradient(s1).doit() == R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
  14. assert Gradient(s2).doit() == R.i + 6*R.y*R.j
  15. def test_Divergence():
  16. assert Divergence(v1) == Divergence(R.x*R.i + R.z*R.z*R.j)
  17. assert Divergence(v2) == Divergence(R.x*R.i + R.y*R.j + R.z*R.k)
  18. assert Divergence(v1).doit() == 1
  19. assert Divergence(v2).doit() == 3
  20. # issue 22384
  21. Rc = CoordSys3D('R', transformation='cylindrical')
  22. assert Divergence(Rc.i).doit() == 1/Rc.r
  23. def test_Curl():
  24. assert Curl(v1) == Curl(R.x*R.i + R.z*R.z*R.j)
  25. assert Curl(v2) == Curl(R.x*R.i + R.y*R.j + R.z*R.k)
  26. assert Curl(v1).doit() == (-2*R.z)*R.i
  27. assert Curl(v2).doit() == VectorZero()
  28. def test_Laplacian():
  29. assert Laplacian(s3) == Laplacian(R.x**2 + R.y**2 + R.z**2)
  30. assert Laplacian(v3) == Laplacian(R.x**2*R.i + R.y**2*R.j + R.z**2*R.k)
  31. assert Laplacian(s3).doit() == 6
  32. assert Laplacian(v3).doit() == 2*R.i + 2*R.j + 2*R.k
  33. assert srepr(Laplacian(s3)) == \
  34. 'Laplacian(Add(Pow(R.x, Integer(2)), Pow(R.y, Integer(2)), Pow(R.z, Integer(2))))'