scalar.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from sympy.core import AtomicExpr, Symbol, S
  2. from sympy.core.sympify import _sympify
  3. from sympy.printing.pretty.stringpict import prettyForm
  4. from sympy.printing.precedence import PRECEDENCE
  5. from sympy.core.kind import NumberKind
  6. class BaseScalar(AtomicExpr):
  7. """
  8. A coordinate symbol/base scalar.
  9. Ideally, users should not instantiate this class.
  10. """
  11. kind = NumberKind
  12. def __new__(cls, index, system, pretty_str=None, latex_str=None):
  13. from sympy.vector.coordsysrect import CoordSys3D
  14. if pretty_str is None:
  15. pretty_str = "x{}".format(index)
  16. elif isinstance(pretty_str, Symbol):
  17. pretty_str = pretty_str.name
  18. if latex_str is None:
  19. latex_str = "x_{}".format(index)
  20. elif isinstance(latex_str, Symbol):
  21. latex_str = latex_str.name
  22. index = _sympify(index)
  23. system = _sympify(system)
  24. obj = super().__new__(cls, index, system)
  25. if not isinstance(system, CoordSys3D):
  26. raise TypeError("system should be a CoordSys3D")
  27. if index not in range(0, 3):
  28. raise ValueError("Invalid index specified.")
  29. # The _id is used for equating purposes, and for hashing
  30. obj._id = (index, system)
  31. obj._name = obj.name = system._name + '.' + system._variable_names[index]
  32. obj._pretty_form = '' + pretty_str
  33. obj._latex_form = latex_str
  34. obj._system = system
  35. return obj
  36. is_commutative = True
  37. is_symbol = True
  38. @property
  39. def free_symbols(self):
  40. return {self}
  41. _diff_wrt = True
  42. def _eval_derivative(self, s):
  43. if self == s:
  44. return S.One
  45. return S.Zero
  46. def _latex(self, printer=None):
  47. return self._latex_form
  48. def _pretty(self, printer=None):
  49. return prettyForm(self._pretty_form)
  50. precedence = PRECEDENCE['Atom']
  51. @property
  52. def system(self):
  53. return self._system
  54. def _sympystr(self, printer):
  55. return self._name