FaustDerLegende commited on
Commit
c2dfb99
Β·
verified Β·
1 Parent(s): 8013315

Upload folder using huggingface_hub (part 19)

Browse files
This view is limited to 50 files because it contains too many changes. Β  See raw diff
Files changed (50) hide show
  1. .gitattributes +38 -0
  2. .venv/lib/python3.12/site-packages/sympy/physics/units/unitsystem.py +204 -0
  3. .venv/lib/python3.12/site-packages/sympy/physics/units/util.py +265 -0
  4. .venv/lib/python3.12/site-packages/sympy/physics/vector/__init__.py +36 -0
  5. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/__init__.cpython-312.pyc +0 -0
  6. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/dyadic.cpython-312.pyc +0 -0
  7. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/fieldfunctions.cpython-312.pyc +0 -0
  8. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/frame.cpython-312.pyc +0 -0
  9. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/functions.cpython-312.pyc +0 -0
  10. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/point.cpython-312.pyc +0 -0
  11. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/printing.cpython-312.pyc +0 -0
  12. .venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/vector.cpython-312.pyc +0 -0
  13. .venv/lib/python3.12/site-packages/sympy/physics/vector/dyadic.py +545 -0
  14. .venv/lib/python3.12/site-packages/sympy/physics/vector/fieldfunctions.py +313 -0
  15. .venv/lib/python3.12/site-packages/sympy/physics/vector/frame.py +1575 -0
  16. .venv/lib/python3.12/site-packages/sympy/physics/vector/functions.py +650 -0
  17. .venv/lib/python3.12/site-packages/sympy/physics/vector/point.py +635 -0
  18. .venv/lib/python3.12/site-packages/sympy/physics/vector/printing.py +371 -0
  19. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__init__.py +0 -0
  20. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/__init__.cpython-312.pyc +0 -0
  21. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_dyadic.cpython-312.pyc +0 -0
  22. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_fieldfunctions.cpython-312.pyc +0 -0
  23. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_frame.cpython-312.pyc +0 -0
  24. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_functions.cpython-312.pyc +0 -0
  25. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_output.cpython-312.pyc +0 -0
  26. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_point.cpython-312.pyc +0 -0
  27. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_printing.cpython-312.pyc +0 -0
  28. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_vector.cpython-312.pyc +0 -0
  29. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_dyadic.py +123 -0
  30. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_fieldfunctions.py +133 -0
  31. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_frame.py +761 -0
  32. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_functions.py +509 -0
  33. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_output.py +75 -0
  34. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_point.py +382 -0
  35. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_printing.py +353 -0
  36. .venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_vector.py +274 -0
  37. .venv/lib/python3.12/site-packages/sympy/physics/vector/vector.py +806 -0
  38. .venv/lib/python3.12/site-packages/sympy/physics/wigner.py +1213 -0
  39. .venv/lib/python3.12/site-packages/sympy/plotting/__init__.py +22 -0
  40. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/__init__.cpython-312.pyc +0 -0
  41. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/experimental_lambdify.cpython-312.pyc +0 -0
  42. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plot.cpython-312.pyc +0 -0
  43. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plot_implicit.cpython-312.pyc +0 -0
  44. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plotgrid.cpython-312.pyc +0 -0
  45. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/series.cpython-312.pyc +3 -0
  46. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/textplot.cpython-312.pyc +0 -0
  47. .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/utils.cpython-312.pyc +0 -0
  48. .venv/lib/python3.12/site-packages/sympy/plotting/backends/__init__.py +0 -0
  49. .venv/lib/python3.12/site-packages/sympy/plotting/backends/__pycache__/__init__.cpython-312.pyc +0 -0
  50. .venv/lib/python3.12/site-packages/sympy/plotting/backends/__pycache__/base_backend.cpython-312.pyc +0 -0
.gitattributes CHANGED
@@ -455,3 +455,41 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
455
  .venv/lib/python3.12/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
456
  .venv/lib/python3.12/site-packages/sympy/physics/quantum/__pycache__/spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
457
  .venv/lib/python3.12/site-packages/sympy/physics/quantum/tests/__pycache__/test_spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  .venv/lib/python3.12/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
456
  .venv/lib/python3.12/site-packages/sympy/physics/quantum/__pycache__/spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
457
  .venv/lib/python3.12/site-packages/sympy/physics/quantum/tests/__pycache__/test_spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
458
+ .venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
459
+ .venv/lib/python3.12/site-packages/sympy/plotting/tests/__pycache__/test_series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
460
+ .venv/lib/python3.12/site-packages/sympy/polys/__pycache__/compatibility.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
461
+ .venv/lib/python3.12/site-packages/sympy/polys/__pycache__/polyclasses.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
462
+ .venv/lib/python3.12/site-packages/sympy/polys/__pycache__/polyquinticconst.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
463
+ .venv/lib/python3.12/site-packages/sympy/polys/__pycache__/polytools.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
464
+ .venv/lib/python3.12/site-packages/sympy/polys/__pycache__/rings.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
465
+ .venv/lib/python3.12/site-packages/sympy/polys/benchmarks/__pycache__/bench_solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
466
+ .venv/lib/python3.12/site-packages/sympy/polys/domains/tests/__pycache__/test_domains.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
467
+ .venv/lib/python3.12/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
468
+ .venv/lib/python3.12/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainmatrix.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
469
+ .venv/lib/python3.12/site-packages/sympy/polys/numberfields/__pycache__/resolvent_lookup.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
470
+ .venv/lib/python3.12/site-packages/sympy/polys/tests/__pycache__/test_polytools.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
471
+ .venv/lib/python3.12/site-packages/sympy/polys/tests/__pycache__/test_rings.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
472
+ .venv/lib/python3.12/site-packages/sympy/printing/__pycache__/latex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
473
+ .venv/lib/python3.12/site-packages/sympy/printing/__pycache__/mathml.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
474
+ .venv/lib/python3.12/site-packages/sympy/printing/pretty/__pycache__/pretty.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
475
+ .venv/lib/python3.12/site-packages/sympy/printing/pretty/tests/__pycache__/test_pretty.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
476
+ .venv/lib/python3.12/site-packages/sympy/printing/tests/__pycache__/test_latex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
477
+ .venv/lib/python3.12/site-packages/sympy/printing/tests/__pycache__/test_mathml.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
478
+ .venv/lib/python3.12/site-packages/sympy/series/tests/__pycache__/test_limits.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
479
+ .venv/lib/python3.12/site-packages/sympy/sets/__pycache__/sets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
480
+ .venv/lib/python3.12/site-packages/sympy/sets/tests/__pycache__/test_fancysets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
481
+ .venv/lib/python3.12/site-packages/sympy/sets/tests/__pycache__/test_sets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
482
+ .venv/lib/python3.12/site-packages/sympy/simplify/__pycache__/hyperexpand.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
483
+ .venv/lib/python3.12/site-packages/sympy/solvers/__pycache__/solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
484
+ .venv/lib/python3.12/site-packages/sympy/solvers/__pycache__/solveset.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
485
+ .venv/lib/python3.12/site-packages/sympy/solvers/diophantine/__pycache__/diophantine.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
486
+ .venv/lib/python3.12/site-packages/sympy/solvers/ode/__pycache__/ode.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
487
+ .venv/lib/python3.12/site-packages/sympy/solvers/ode/__pycache__/single.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
488
+ .venv/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_ode.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
489
+ .venv/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_single.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
490
+ .venv/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_systems.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
491
+ .venv/lib/python3.12/site-packages/sympy/solvers/tests/__pycache__/test_solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
492
+ .venv/lib/python3.12/site-packages/sympy/solvers/tests/__pycache__/test_solveset.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
493
+ .venv/lib/python3.12/site-packages/sympy/stats/__pycache__/crv_types.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
494
+ .venv/lib/python3.12/site-packages/sympy/stats/__pycache__/stochastic_process_types.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
495
+ .venv/lib/python3.12/site-packages/sympy/stats/tests/__pycache__/test_continuous_rv.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
.venv/lib/python3.12/site-packages/sympy/physics/units/unitsystem.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit system for physical quantities; include definition of constants.
3
+ """
4
+ from __future__ import annotations
5
+
6
+ from sympy.core.add import Add
7
+ from sympy.core.function import (Derivative, Function)
8
+ from sympy.core.mul import Mul
9
+ from sympy.core.power import Pow
10
+ from sympy.core.singleton import S
11
+ from sympy.physics.units.dimensions import _QuantityMapper
12
+ from sympy.physics.units.quantities import Quantity
13
+
14
+ from .dimensions import Dimension
15
+
16
+
17
+ class UnitSystem(_QuantityMapper):
18
+ """
19
+ UnitSystem represents a coherent set of units.
20
+
21
+ A unit system is basically a dimension system with notions of scales. Many
22
+ of the methods are defined in the same way.
23
+
24
+ It is much better if all base units have a symbol.
25
+ """
26
+
27
+ _unit_systems: dict[str, UnitSystem] = {}
28
+
29
+ def __init__(self, base_units, units=(), name="", descr="", dimension_system=None, derived_units: dict[Dimension, Quantity]={}):
30
+
31
+ UnitSystem._unit_systems[name] = self
32
+
33
+ self.name = name
34
+ self.descr = descr
35
+
36
+ self._base_units = base_units
37
+ self._dimension_system = dimension_system
38
+ self._units = tuple(set(base_units) | set(units))
39
+ self._base_units = tuple(base_units)
40
+ self._derived_units = derived_units
41
+
42
+ super().__init__()
43
+
44
+ def __str__(self):
45
+ """
46
+ Return the name of the system.
47
+
48
+ If it does not exist, then it makes a list of symbols (or names) of
49
+ the base dimensions.
50
+ """
51
+
52
+ if self.name != "":
53
+ return self.name
54
+ else:
55
+ return "UnitSystem((%s))" % ", ".join(
56
+ str(d) for d in self._base_units)
57
+
58
+ def __repr__(self):
59
+ return '<UnitSystem: %s>' % repr(self._base_units)
60
+
61
+ def extend(self, base, units=(), name="", description="", dimension_system=None, derived_units: dict[Dimension, Quantity]={}):
62
+ """Extend the current system into a new one.
63
+
64
+ Take the base and normal units of the current system to merge
65
+ them to the base and normal units given in argument.
66
+ If not provided, name and description are overridden by empty strings.
67
+ """
68
+
69
+ base = self._base_units + tuple(base)
70
+ units = self._units + tuple(units)
71
+
72
+ return UnitSystem(base, units, name, description, dimension_system, {**self._derived_units, **derived_units})
73
+
74
+ def get_dimension_system(self):
75
+ return self._dimension_system
76
+
77
+ def get_quantity_dimension(self, unit):
78
+ qdm = self.get_dimension_system()._quantity_dimension_map
79
+ if unit in qdm:
80
+ return qdm[unit]
81
+ return super().get_quantity_dimension(unit)
82
+
83
+ def get_quantity_scale_factor(self, unit):
84
+ qsfm = self.get_dimension_system()._quantity_scale_factors
85
+ if unit in qsfm:
86
+ return qsfm[unit]
87
+ return super().get_quantity_scale_factor(unit)
88
+
89
+ @staticmethod
90
+ def get_unit_system(unit_system):
91
+ if isinstance(unit_system, UnitSystem):
92
+ return unit_system
93
+
94
+ if unit_system not in UnitSystem._unit_systems:
95
+ raise ValueError(
96
+ "Unit system is not supported. Currently"
97
+ "supported unit systems are {}".format(
98
+ ", ".join(sorted(UnitSystem._unit_systems))
99
+ )
100
+ )
101
+
102
+ return UnitSystem._unit_systems[unit_system]
103
+
104
+ @staticmethod
105
+ def get_default_unit_system():
106
+ return UnitSystem._unit_systems["SI"]
107
+
108
+ @property
109
+ def dim(self):
110
+ """
111
+ Give the dimension of the system.
112
+
113
+ That is return the number of units forming the basis.
114
+ """
115
+ return len(self._base_units)
116
+
117
+ @property
118
+ def is_consistent(self):
119
+ """
120
+ Check if the underlying dimension system is consistent.
121
+ """
122
+ # test is performed in DimensionSystem
123
+ return self.get_dimension_system().is_consistent
124
+
125
+ @property
126
+ def derived_units(self) -> dict[Dimension, Quantity]:
127
+ return self._derived_units
128
+
129
+ def get_dimensional_expr(self, expr):
130
+ from sympy.physics.units import Quantity
131
+ if isinstance(expr, Mul):
132
+ return Mul(*[self.get_dimensional_expr(i) for i in expr.args])
133
+ elif isinstance(expr, Pow):
134
+ return self.get_dimensional_expr(expr.base) ** expr.exp
135
+ elif isinstance(expr, Add):
136
+ return self.get_dimensional_expr(expr.args[0])
137
+ elif isinstance(expr, Derivative):
138
+ dim = self.get_dimensional_expr(expr.expr)
139
+ for independent, count in expr.variable_count:
140
+ dim /= self.get_dimensional_expr(independent)**count
141
+ return dim
142
+ elif isinstance(expr, Function):
143
+ args = [self.get_dimensional_expr(arg) for arg in expr.args]
144
+ if all(i == 1 for i in args):
145
+ return S.One
146
+ return expr.func(*args)
147
+ elif isinstance(expr, Quantity):
148
+ return self.get_quantity_dimension(expr).name
149
+ return S.One
150
+
151
+ def _collect_factor_and_dimension(self, expr):
152
+ """
153
+ Return tuple with scale factor expression and dimension expression.
154
+ """
155
+ from sympy.physics.units import Quantity
156
+ if isinstance(expr, Quantity):
157
+ return expr.scale_factor, expr.dimension
158
+ elif isinstance(expr, Mul):
159
+ factor = 1
160
+ dimension = Dimension(1)
161
+ for arg in expr.args:
162
+ arg_factor, arg_dim = self._collect_factor_and_dimension(arg)
163
+ factor *= arg_factor
164
+ dimension *= arg_dim
165
+ return factor, dimension
166
+ elif isinstance(expr, Pow):
167
+ factor, dim = self._collect_factor_and_dimension(expr.base)
168
+ exp_factor, exp_dim = self._collect_factor_and_dimension(expr.exp)
169
+ if self.get_dimension_system().is_dimensionless(exp_dim):
170
+ exp_dim = 1
171
+ return factor ** exp_factor, dim ** (exp_factor * exp_dim)
172
+ elif isinstance(expr, Add):
173
+ factor, dim = self._collect_factor_and_dimension(expr.args[0])
174
+ for addend in expr.args[1:]:
175
+ addend_factor, addend_dim = \
176
+ self._collect_factor_and_dimension(addend)
177
+ if not self.get_dimension_system().equivalent_dims(dim, addend_dim):
178
+ raise ValueError(
179
+ 'Dimension of "{}" is {}, '
180
+ 'but it should be {}'.format(
181
+ addend, addend_dim, dim))
182
+ factor += addend_factor
183
+ return factor, dim
184
+ elif isinstance(expr, Derivative):
185
+ factor, dim = self._collect_factor_and_dimension(expr.args[0])
186
+ for independent, count in expr.variable_count:
187
+ ifactor, idim = self._collect_factor_and_dimension(independent)
188
+ factor /= ifactor**count
189
+ dim /= idim**count
190
+ return factor, dim
191
+ elif isinstance(expr, Function):
192
+ fds = [self._collect_factor_and_dimension(arg) for arg in expr.args]
193
+ dims = [Dimension(1) if self.get_dimension_system().is_dimensionless(d[1]) else d[1] for d in fds]
194
+ return (expr.func(*(f[0] for f in fds)), *dims)
195
+ elif isinstance(expr, Dimension):
196
+ return S.One, expr
197
+ else:
198
+ return expr, Dimension(1)
199
+
200
+ def get_units_non_prefixed(self) -> set[Quantity]:
201
+ """
202
+ Return the units of the system that do not have a prefix.
203
+ """
204
+ return set(filter(lambda u: not u.is_prefixed and not u.is_physical_constant, self._units))
.venv/lib/python3.12/site-packages/sympy/physics/units/util.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Several methods to simplify expressions involving unit objects.
3
+ """
4
+ from functools import reduce
5
+ from collections.abc import Iterable
6
+ from typing import Optional
7
+
8
+ from sympy import default_sort_key
9
+ from sympy.core.add import Add
10
+ from sympy.core.containers import Tuple
11
+ from sympy.core.mul import Mul
12
+ from sympy.core.power import Pow
13
+ from sympy.core.sorting import ordered
14
+ from sympy.core.sympify import sympify
15
+ from sympy.core.function import Function
16
+ from sympy.matrices.exceptions import NonInvertibleMatrixError
17
+ from sympy.physics.units.dimensions import Dimension, DimensionSystem
18
+ from sympy.physics.units.prefixes import Prefix
19
+ from sympy.physics.units.quantities import Quantity
20
+ from sympy.physics.units.unitsystem import UnitSystem
21
+ from sympy.utilities.iterables import sift
22
+
23
+
24
+ def _get_conversion_matrix_for_expr(expr, target_units, unit_system):
25
+ from sympy.matrices.dense import Matrix
26
+
27
+ dimension_system = unit_system.get_dimension_system()
28
+
29
+ expr_dim = Dimension(unit_system.get_dimensional_expr(expr))
30
+ dim_dependencies = dimension_system.get_dimensional_dependencies(expr_dim, mark_dimensionless=True)
31
+ target_dims = [Dimension(unit_system.get_dimensional_expr(x)) for x in target_units]
32
+ canon_dim_units = [i for x in target_dims for i in dimension_system.get_dimensional_dependencies(x, mark_dimensionless=True)]
33
+ canon_expr_units = set(dim_dependencies)
34
+
35
+ if not canon_expr_units.issubset(set(canon_dim_units)):
36
+ return None
37
+
38
+ seen = set()
39
+ canon_dim_units = [i for i in canon_dim_units if not (i in seen or seen.add(i))]
40
+
41
+ camat = Matrix([[dimension_system.get_dimensional_dependencies(i, mark_dimensionless=True).get(j, 0) for i in target_dims] for j in canon_dim_units])
42
+ exprmat = Matrix([dim_dependencies.get(k, 0) for k in canon_dim_units])
43
+
44
+ try:
45
+ res_exponents = camat.solve(exprmat)
46
+ except NonInvertibleMatrixError:
47
+ return None
48
+
49
+ return res_exponents
50
+
51
+
52
+ def convert_to(expr, target_units, unit_system="SI"):
53
+ """
54
+ Convert ``expr`` to the same expression with all of its units and quantities
55
+ represented as factors of ``target_units``, whenever the dimension is compatible.
56
+
57
+ ``target_units`` may be a single unit/quantity, or a collection of
58
+ units/quantities.
59
+
60
+ Examples
61
+ ========
62
+
63
+ >>> from sympy.physics.units import speed_of_light, meter, gram, second, day
64
+ >>> from sympy.physics.units import mile, newton, kilogram, atomic_mass_constant
65
+ >>> from sympy.physics.units import kilometer, centimeter
66
+ >>> from sympy.physics.units import gravitational_constant, hbar
67
+ >>> from sympy.physics.units import convert_to
68
+ >>> convert_to(mile, kilometer)
69
+ 25146*kilometer/15625
70
+ >>> convert_to(mile, kilometer).n()
71
+ 1.609344*kilometer
72
+ >>> convert_to(speed_of_light, meter/second)
73
+ 299792458*meter/second
74
+ >>> convert_to(day, second)
75
+ 86400*second
76
+ >>> 3*newton
77
+ 3*newton
78
+ >>> convert_to(3*newton, kilogram*meter/second**2)
79
+ 3*kilogram*meter/second**2
80
+ >>> convert_to(atomic_mass_constant, gram)
81
+ 1.660539060e-24*gram
82
+
83
+ Conversion to multiple units:
84
+
85
+ >>> convert_to(speed_of_light, [meter, second])
86
+ 299792458*meter/second
87
+ >>> convert_to(3*newton, [centimeter, gram, second])
88
+ 300000*centimeter*gram/second**2
89
+
90
+ Conversion to Planck units:
91
+
92
+ >>> convert_to(atomic_mass_constant, [gravitational_constant, speed_of_light, hbar]).n()
93
+ 7.62963087839509e-20*hbar**0.5*speed_of_light**0.5/gravitational_constant**0.5
94
+
95
+ """
96
+ from sympy.physics.units import UnitSystem
97
+ unit_system = UnitSystem.get_unit_system(unit_system)
98
+
99
+ if not isinstance(target_units, (Iterable, Tuple)):
100
+ target_units = [target_units]
101
+
102
+ def handle_Adds(expr):
103
+ return Add.fromiter(convert_to(i, target_units, unit_system)
104
+ for i in expr.args)
105
+
106
+ if isinstance(expr, Add):
107
+ return handle_Adds(expr)
108
+ elif isinstance(expr, Pow) and isinstance(expr.base, Add):
109
+ return handle_Adds(expr.base) ** expr.exp
110
+
111
+ expr = sympify(expr)
112
+ target_units = sympify(target_units)
113
+
114
+ if isinstance(expr, Function):
115
+ expr = expr.together()
116
+
117
+ if not isinstance(expr, Quantity) and expr.has(Quantity):
118
+ expr = expr.replace(lambda x: isinstance(x, Quantity),
119
+ lambda x: x.convert_to(target_units, unit_system))
120
+
121
+ def get_total_scale_factor(expr):
122
+ if isinstance(expr, Mul):
123
+ return reduce(lambda x, y: x * y,
124
+ [get_total_scale_factor(i) for i in expr.args])
125
+ elif isinstance(expr, Pow):
126
+ return get_total_scale_factor(expr.base) ** expr.exp
127
+ elif isinstance(expr, Quantity):
128
+ return unit_system.get_quantity_scale_factor(expr)
129
+ return expr
130
+
131
+ depmat = _get_conversion_matrix_for_expr(expr, target_units, unit_system)
132
+ if depmat is None:
133
+ return expr
134
+
135
+ expr_scale_factor = get_total_scale_factor(expr)
136
+ return expr_scale_factor * Mul.fromiter(
137
+ (1/get_total_scale_factor(u)*u)**p for u, p in
138
+ zip(target_units, depmat))
139
+
140
+
141
+ def quantity_simplify(expr, across_dimensions: bool=False, unit_system=None):
142
+ """Return an equivalent expression in which prefixes are replaced
143
+ with numerical values and all units of a given dimension are the
144
+ unified in a canonical manner by default. `across_dimensions` allows
145
+ for units of different dimensions to be simplified together.
146
+
147
+ `unit_system` must be specified if `across_dimensions` is True.
148
+
149
+ Examples
150
+ ========
151
+
152
+ >>> from sympy.physics.units.util import quantity_simplify
153
+ >>> from sympy.physics.units.prefixes import kilo
154
+ >>> from sympy.physics.units import foot, inch, joule, coulomb
155
+ >>> quantity_simplify(kilo*foot*inch)
156
+ 250*foot**2/3
157
+ >>> quantity_simplify(foot - 6*inch)
158
+ foot/2
159
+ >>> quantity_simplify(5*joule/coulomb, across_dimensions=True, unit_system="SI")
160
+ 5*volt
161
+ """
162
+
163
+ if expr.is_Atom or not expr.has(Prefix, Quantity):
164
+ return expr
165
+
166
+ # replace all prefixes with numerical values
167
+ p = expr.atoms(Prefix)
168
+ expr = expr.xreplace({p: p.scale_factor for p in p})
169
+
170
+ # replace all quantities of given dimension with a canonical
171
+ # quantity, chosen from those in the expression
172
+ d = sift(expr.atoms(Quantity), lambda i: i.dimension)
173
+ for k in d:
174
+ if len(d[k]) == 1:
175
+ continue
176
+ v = list(ordered(d[k]))
177
+ ref = v[0]/v[0].scale_factor
178
+ expr = expr.xreplace({vi: ref*vi.scale_factor for vi in v[1:]})
179
+
180
+ if across_dimensions:
181
+ # combine quantities of different dimensions into a single
182
+ # quantity that is equivalent to the original expression
183
+
184
+ if unit_system is None:
185
+ raise ValueError("unit_system must be specified if across_dimensions is True")
186
+
187
+ unit_system = UnitSystem.get_unit_system(unit_system)
188
+ dimension_system: DimensionSystem = unit_system.get_dimension_system()
189
+ dim_expr = unit_system.get_dimensional_expr(expr)
190
+ dim_deps = dimension_system.get_dimensional_dependencies(dim_expr, mark_dimensionless=True)
191
+
192
+ target_dimension: Optional[Dimension] = None
193
+ for ds_dim, ds_dim_deps in dimension_system.dimensional_dependencies.items():
194
+ if ds_dim_deps == dim_deps:
195
+ target_dimension = ds_dim
196
+ break
197
+
198
+ if target_dimension is None:
199
+ # if we can't find a target dimension, we can't do anything. unsure how to handle this case.
200
+ return expr
201
+
202
+ target_unit = unit_system.derived_units.get(target_dimension)
203
+ if target_unit:
204
+ expr = convert_to(expr, target_unit, unit_system)
205
+
206
+ return expr
207
+
208
+
209
+ def check_dimensions(expr, unit_system="SI"):
210
+ """Return expr if units in addends have the same
211
+ base dimensions, else raise a ValueError."""
212
+ # the case of adding a number to a dimensional quantity
213
+ # is ignored for the sake of SymPy core routines, so this
214
+ # function will raise an error now if such an addend is
215
+ # found.
216
+ # Also, when doing substitutions, multiplicative constants
217
+ # might be introduced, so remove those now
218
+
219
+ from sympy.physics.units import UnitSystem
220
+ unit_system = UnitSystem.get_unit_system(unit_system)
221
+
222
+ def addDict(dict1, dict2):
223
+ """Merge dictionaries by adding values of common keys and
224
+ removing keys with value of 0."""
225
+ dict3 = {**dict1, **dict2}
226
+ for key, value in dict3.items():
227
+ if key in dict1 and key in dict2:
228
+ dict3[key] = value + dict1[key]
229
+ return {key:val for key, val in dict3.items() if val != 0}
230
+
231
+ adds = expr.atoms(Add)
232
+ DIM_OF = unit_system.get_dimension_system().get_dimensional_dependencies
233
+ for a in adds:
234
+ deset = set()
235
+ for ai in a.args:
236
+ if ai.is_number:
237
+ deset.add(())
238
+ continue
239
+ dims = []
240
+ skip = False
241
+ dimdict = {}
242
+ for i in Mul.make_args(ai):
243
+ if i.has(Quantity):
244
+ i = Dimension(unit_system.get_dimensional_expr(i))
245
+ if i.has(Dimension):
246
+ dimdict = addDict(dimdict, DIM_OF(i))
247
+ elif i.free_symbols:
248
+ skip = True
249
+ break
250
+ dims.extend(dimdict.items())
251
+ if not skip:
252
+ deset.add(tuple(sorted(dims, key=default_sort_key)))
253
+ if len(deset) > 1:
254
+ raise ValueError(
255
+ "addends have incompatible dimensions: {}".format(deset))
256
+
257
+ # clear multiplicative constants on Dimensions which may be
258
+ # left after substitution
259
+ reps = {}
260
+ for m in expr.atoms(Mul):
261
+ if any(isinstance(i, Dimension) for i in m.args):
262
+ reps[m] = m.func(*[
263
+ i for i in m.args if not i.is_number])
264
+
265
+ return expr.xreplace(reps)
.venv/lib/python3.12/site-packages/sympy/physics/vector/__init__.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = [
2
+ 'CoordinateSym', 'ReferenceFrame',
3
+
4
+ 'Dyadic',
5
+
6
+ 'Vector',
7
+
8
+ 'Point',
9
+
10
+ 'cross', 'dot', 'express', 'time_derivative', 'outer',
11
+ 'kinematic_equations', 'get_motion_params', 'partial_velocity',
12
+ 'dynamicsymbols',
13
+
14
+ 'vprint', 'vsstrrepr', 'vsprint', 'vpprint', 'vlatex', 'init_vprinting',
15
+
16
+ 'curl', 'divergence', 'gradient', 'is_conservative', 'is_solenoidal',
17
+ 'scalar_potential', 'scalar_potential_difference',
18
+
19
+ ]
20
+ from .frame import CoordinateSym, ReferenceFrame
21
+
22
+ from .dyadic import Dyadic
23
+
24
+ from .vector import Vector
25
+
26
+ from .point import Point
27
+
28
+ from .functions import (cross, dot, express, time_derivative, outer,
29
+ kinematic_equations, get_motion_params, partial_velocity,
30
+ dynamicsymbols)
31
+
32
+ from .printing import (vprint, vsstrrepr, vsprint, vpprint, vlatex,
33
+ init_vprinting)
34
+
35
+ from .fieldfunctions import (curl, divergence, gradient, is_conservative,
36
+ is_solenoidal, scalar_potential, scalar_potential_difference)
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (1.14 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/dyadic.cpython-312.pyc ADDED
Binary file (22.1 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/fieldfunctions.cpython-312.pyc ADDED
Binary file (10.2 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/frame.cpython-312.pyc ADDED
Binary file (63.5 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/functions.cpython-312.pyc ADDED
Binary file (27.7 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/point.cpython-312.pyc ADDED
Binary file (23.5 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/printing.cpython-312.pyc ADDED
Binary file (14.6 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/vector.cpython-312.pyc ADDED
Binary file (32.2 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/dyadic.py ADDED
@@ -0,0 +1,545 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy import sympify, Add, ImmutableMatrix as Matrix
2
+ from sympy.core.evalf import EvalfMixin
3
+ from sympy.printing.defaults import Printable
4
+
5
+ from mpmath.libmp.libmpf import prec_to_dps
6
+
7
+
8
+ __all__ = ['Dyadic']
9
+
10
+
11
+ class Dyadic(Printable, EvalfMixin):
12
+ """A Dyadic object.
13
+
14
+ See:
15
+ https://en.wikipedia.org/wiki/Dyadic_tensor
16
+ Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill
17
+
18
+ A more powerful way to represent a rigid body's inertia. While it is more
19
+ complex, by choosing Dyadic components to be in body fixed basis vectors,
20
+ the resulting matrix is equivalent to the inertia tensor.
21
+
22
+ """
23
+
24
+ is_number = False
25
+
26
+ def __init__(self, inlist):
27
+ """
28
+ Just like Vector's init, you should not call this unless creating a
29
+ zero dyadic.
30
+
31
+ zd = Dyadic(0)
32
+
33
+ Stores a Dyadic as a list of lists; the inner list has the measure
34
+ number and the two unit vectors; the outerlist holds each unique
35
+ unit vector pair.
36
+
37
+ """
38
+
39
+ self.args = []
40
+ if inlist == 0:
41
+ inlist = []
42
+ while len(inlist) != 0:
43
+ added = 0
44
+ for i, v in enumerate(self.args):
45
+ if ((str(inlist[0][1]) == str(self.args[i][1])) and
46
+ (str(inlist[0][2]) == str(self.args[i][2]))):
47
+ self.args[i] = (self.args[i][0] + inlist[0][0],
48
+ inlist[0][1], inlist[0][2])
49
+ inlist.remove(inlist[0])
50
+ added = 1
51
+ break
52
+ if added != 1:
53
+ self.args.append(inlist[0])
54
+ inlist.remove(inlist[0])
55
+ i = 0
56
+ # This code is to remove empty parts from the list
57
+ while i < len(self.args):
58
+ if ((self.args[i][0] == 0) | (self.args[i][1] == 0) |
59
+ (self.args[i][2] == 0)):
60
+ self.args.remove(self.args[i])
61
+ i -= 1
62
+ i += 1
63
+
64
+ @property
65
+ def func(self):
66
+ """Returns the class Dyadic. """
67
+ return Dyadic
68
+
69
+ def __add__(self, other):
70
+ """The add operator for Dyadic. """
71
+ other = _check_dyadic(other)
72
+ return Dyadic(self.args + other.args)
73
+
74
+ __radd__ = __add__
75
+
76
+ def __mul__(self, other):
77
+ """Multiplies the Dyadic by a sympifyable expression.
78
+
79
+ Parameters
80
+ ==========
81
+
82
+ other : Sympafiable
83
+ The scalar to multiply this Dyadic with
84
+
85
+ Examples
86
+ ========
87
+
88
+ >>> from sympy.physics.vector import ReferenceFrame, outer
89
+ >>> N = ReferenceFrame('N')
90
+ >>> d = outer(N.x, N.x)
91
+ >>> 5 * d
92
+ 5*(N.x|N.x)
93
+
94
+ """
95
+ newlist = list(self.args)
96
+ other = sympify(other)
97
+ for i in range(len(newlist)):
98
+ newlist[i] = (other * newlist[i][0], newlist[i][1],
99
+ newlist[i][2])
100
+ return Dyadic(newlist)
101
+
102
+ __rmul__ = __mul__
103
+
104
+ def dot(self, other):
105
+ """The inner product operator for a Dyadic and a Dyadic or Vector.
106
+
107
+ Parameters
108
+ ==========
109
+
110
+ other : Dyadic or Vector
111
+ The other Dyadic or Vector to take the inner product with
112
+
113
+ Examples
114
+ ========
115
+
116
+ >>> from sympy.physics.vector import ReferenceFrame, outer
117
+ >>> N = ReferenceFrame('N')
118
+ >>> D1 = outer(N.x, N.y)
119
+ >>> D2 = outer(N.y, N.y)
120
+ >>> D1.dot(D2)
121
+ (N.x|N.y)
122
+ >>> D1.dot(N.y)
123
+ N.x
124
+
125
+ """
126
+ from sympy.physics.vector.vector import Vector, _check_vector
127
+ if isinstance(other, Dyadic):
128
+ other = _check_dyadic(other)
129
+ ol = Dyadic(0)
130
+ for v in self.args:
131
+ for v2 in other.args:
132
+ ol += v[0] * v2[0] * (v[2].dot(v2[1])) * (v[1].outer(v2[2]))
133
+ else:
134
+ other = _check_vector(other)
135
+ ol = Vector(0)
136
+ for v in self.args:
137
+ ol += v[0] * v[1] * (v[2].dot(other))
138
+ return ol
139
+
140
+ # NOTE : supports non-advertised Dyadic & Dyadic, Dyadic & Vector notation
141
+ __and__ = dot
142
+
143
+ def __truediv__(self, other):
144
+ """Divides the Dyadic by a sympifyable expression. """
145
+ return self.__mul__(1 / other)
146
+
147
+ def __eq__(self, other):
148
+ """Tests for equality.
149
+
150
+ Is currently weak; needs stronger comparison testing
151
+
152
+ """
153
+
154
+ if other == 0:
155
+ other = Dyadic(0)
156
+ other = _check_dyadic(other)
157
+ if (self.args == []) and (other.args == []):
158
+ return True
159
+ elif (self.args == []) or (other.args == []):
160
+ return False
161
+ return set(self.args) == set(other.args)
162
+
163
+ def __ne__(self, other):
164
+ return not self == other
165
+
166
+ def __neg__(self):
167
+ return self * -1
168
+
169
+ def _latex(self, printer):
170
+ ar = self.args # just to shorten things
171
+ if len(ar) == 0:
172
+ return str(0)
173
+ ol = [] # output list, to be concatenated to a string
174
+ for v in ar:
175
+ # if the coef of the dyadic is 1, we skip the 1
176
+ if v[0] == 1:
177
+ ol.append(' + ' + printer._print(v[1]) + r"\otimes " +
178
+ printer._print(v[2]))
179
+ # if the coef of the dyadic is -1, we skip the 1
180
+ elif v[0] == -1:
181
+ ol.append(' - ' +
182
+ printer._print(v[1]) +
183
+ r"\otimes " +
184
+ printer._print(v[2]))
185
+ # If the coefficient of the dyadic is not 1 or -1,
186
+ # we might wrap it in parentheses, for readability.
187
+ elif v[0] != 0:
188
+ arg_str = printer._print(v[0])
189
+ if isinstance(v[0], Add):
190
+ arg_str = '(%s)' % arg_str
191
+ if arg_str.startswith('-'):
192
+ arg_str = arg_str[1:]
193
+ str_start = ' - '
194
+ else:
195
+ str_start = ' + '
196
+ ol.append(str_start + arg_str + printer._print(v[1]) +
197
+ r"\otimes " + printer._print(v[2]))
198
+ outstr = ''.join(ol)
199
+ if outstr.startswith(' + '):
200
+ outstr = outstr[3:]
201
+ elif outstr.startswith(' '):
202
+ outstr = outstr[1:]
203
+ return outstr
204
+
205
+ def _pretty(self, printer):
206
+ e = self
207
+
208
+ class Fake:
209
+ baseline = 0
210
+
211
+ def render(self, *args, **kwargs):
212
+ ar = e.args # just to shorten things
213
+ mpp = printer
214
+ if len(ar) == 0:
215
+ return str(0)
216
+ bar = "\N{CIRCLED TIMES}" if printer._use_unicode else "|"
217
+ ol = [] # output list, to be concatenated to a string
218
+ for v in ar:
219
+ # if the coef of the dyadic is 1, we skip the 1
220
+ if v[0] == 1:
221
+ ol.extend([" + ",
222
+ mpp.doprint(v[1]),
223
+ bar,
224
+ mpp.doprint(v[2])])
225
+
226
+ # if the coef of the dyadic is -1, we skip the 1
227
+ elif v[0] == -1:
228
+ ol.extend([" - ",
229
+ mpp.doprint(v[1]),
230
+ bar,
231
+ mpp.doprint(v[2])])
232
+
233
+ # If the coefficient of the dyadic is not 1 or -1,
234
+ # we might wrap it in parentheses, for readability.
235
+ elif v[0] != 0:
236
+ if isinstance(v[0], Add):
237
+ arg_str = mpp._print(
238
+ v[0]).parens()[0]
239
+ else:
240
+ arg_str = mpp.doprint(v[0])
241
+ if arg_str.startswith("-"):
242
+ arg_str = arg_str[1:]
243
+ str_start = " - "
244
+ else:
245
+ str_start = " + "
246
+ ol.extend([str_start, arg_str, " ",
247
+ mpp.doprint(v[1]),
248
+ bar,
249
+ mpp.doprint(v[2])])
250
+
251
+ outstr = "".join(ol)
252
+ if outstr.startswith(" + "):
253
+ outstr = outstr[3:]
254
+ elif outstr.startswith(" "):
255
+ outstr = outstr[1:]
256
+ return outstr
257
+ return Fake()
258
+
259
+ def __rsub__(self, other):
260
+ return (-1 * self) + other
261
+
262
+ def _sympystr(self, printer):
263
+ """Printing method. """
264
+ ar = self.args # just to shorten things
265
+ if len(ar) == 0:
266
+ return printer._print(0)
267
+ ol = [] # output list, to be concatenated to a string
268
+ for v in ar:
269
+ # if the coef of the dyadic is 1, we skip the 1
270
+ if v[0] == 1:
271
+ ol.append(' + (' + printer._print(v[1]) + '|' +
272
+ printer._print(v[2]) + ')')
273
+ # if the coef of the dyadic is -1, we skip the 1
274
+ elif v[0] == -1:
275
+ ol.append(' - (' + printer._print(v[1]) + '|' +
276
+ printer._print(v[2]) + ')')
277
+ # If the coefficient of the dyadic is not 1 or -1,
278
+ # we might wrap it in parentheses, for readability.
279
+ elif v[0] != 0:
280
+ arg_str = printer._print(v[0])
281
+ if isinstance(v[0], Add):
282
+ arg_str = "(%s)" % arg_str
283
+ if arg_str[0] == '-':
284
+ arg_str = arg_str[1:]
285
+ str_start = ' - '
286
+ else:
287
+ str_start = ' + '
288
+ ol.append(str_start + arg_str + '*(' +
289
+ printer._print(v[1]) +
290
+ '|' + printer._print(v[2]) + ')')
291
+ outstr = ''.join(ol)
292
+ if outstr.startswith(' + '):
293
+ outstr = outstr[3:]
294
+ elif outstr.startswith(' '):
295
+ outstr = outstr[1:]
296
+ return outstr
297
+
298
+ def __sub__(self, other):
299
+ """The subtraction operator. """
300
+ return self.__add__(other * -1)
301
+
302
+ def cross(self, other):
303
+ """Returns the dyadic resulting from the dyadic vector cross product:
304
+ Dyadic x Vector.
305
+
306
+ Parameters
307
+ ==========
308
+ other : Vector
309
+ Vector to cross with.
310
+
311
+ Examples
312
+ ========
313
+ >>> from sympy.physics.vector import ReferenceFrame, outer, cross
314
+ >>> N = ReferenceFrame('N')
315
+ >>> d = outer(N.x, N.x)
316
+ >>> cross(d, N.y)
317
+ (N.x|N.z)
318
+
319
+ """
320
+ from sympy.physics.vector.vector import _check_vector
321
+ other = _check_vector(other)
322
+ ol = Dyadic(0)
323
+ for v in self.args:
324
+ ol += v[0] * (v[1].outer((v[2].cross(other))))
325
+ return ol
326
+
327
+ # NOTE : supports non-advertised Dyadic ^ Vector notation
328
+ __xor__ = cross
329
+
330
+ def express(self, frame1, frame2=None):
331
+ """Expresses this Dyadic in alternate frame(s)
332
+
333
+ The first frame is the list side expression, the second frame is the
334
+ right side; if Dyadic is in form A.x|B.y, you can express it in two
335
+ different frames. If no second frame is given, the Dyadic is
336
+ expressed in only one frame.
337
+
338
+ Calls the global express function
339
+
340
+ Parameters
341
+ ==========
342
+
343
+ frame1 : ReferenceFrame
344
+ The frame to express the left side of the Dyadic in
345
+ frame2 : ReferenceFrame
346
+ If provided, the frame to express the right side of the Dyadic in
347
+
348
+ Examples
349
+ ========
350
+
351
+ >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
352
+ >>> from sympy.physics.vector import init_vprinting
353
+ >>> init_vprinting(pretty_print=False)
354
+ >>> N = ReferenceFrame('N')
355
+ >>> q = dynamicsymbols('q')
356
+ >>> B = N.orientnew('B', 'Axis', [q, N.z])
357
+ >>> d = outer(N.x, N.x)
358
+ >>> d.express(B, N)
359
+ cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)
360
+
361
+ """
362
+ from sympy.physics.vector.functions import express
363
+ return express(self, frame1, frame2)
364
+
365
+ def to_matrix(self, reference_frame, second_reference_frame=None):
366
+ """Returns the matrix form of the dyadic with respect to one or two
367
+ reference frames.
368
+
369
+ Parameters
370
+ ----------
371
+ reference_frame : ReferenceFrame
372
+ The reference frame that the rows and columns of the matrix
373
+ correspond to. If a second reference frame is provided, this
374
+ only corresponds to the rows of the matrix.
375
+ second_reference_frame : ReferenceFrame, optional, default=None
376
+ The reference frame that the columns of the matrix correspond
377
+ to.
378
+
379
+ Returns
380
+ -------
381
+ matrix : ImmutableMatrix, shape(3,3)
382
+ The matrix that gives the 2D tensor form.
383
+
384
+ Examples
385
+ ========
386
+
387
+ >>> from sympy import symbols, trigsimp
388
+ >>> from sympy.physics.vector import ReferenceFrame
389
+ >>> from sympy.physics.mechanics import inertia
390
+ >>> Ixx, Iyy, Izz, Ixy, Iyz, Ixz = symbols('Ixx, Iyy, Izz, Ixy, Iyz, Ixz')
391
+ >>> N = ReferenceFrame('N')
392
+ >>> inertia_dyadic = inertia(N, Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
393
+ >>> inertia_dyadic.to_matrix(N)
394
+ Matrix([
395
+ [Ixx, Ixy, Ixz],
396
+ [Ixy, Iyy, Iyz],
397
+ [Ixz, Iyz, Izz]])
398
+ >>> beta = symbols('beta')
399
+ >>> A = N.orientnew('A', 'Axis', (beta, N.x))
400
+ >>> trigsimp(inertia_dyadic.to_matrix(A))
401
+ Matrix([
402
+ [ Ixx, Ixy*cos(beta) + Ixz*sin(beta), -Ixy*sin(beta) + Ixz*cos(beta)],
403
+ [ Ixy*cos(beta) + Ixz*sin(beta), Iyy*cos(2*beta)/2 + Iyy/2 + Iyz*sin(2*beta) - Izz*cos(2*beta)/2 + Izz/2, -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2],
404
+ [-Ixy*sin(beta) + Ixz*cos(beta), -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2, -Iyy*cos(2*beta)/2 + Iyy/2 - Iyz*sin(2*beta) + Izz*cos(2*beta)/2 + Izz/2]])
405
+
406
+ """
407
+
408
+ if second_reference_frame is None:
409
+ second_reference_frame = reference_frame
410
+
411
+ return Matrix([i.dot(self).dot(j) for i in reference_frame for j in
412
+ second_reference_frame]).reshape(3, 3)
413
+
414
+ def doit(self, **hints):
415
+ """Calls .doit() on each term in the Dyadic"""
416
+ return sum([Dyadic([(v[0].doit(**hints), v[1], v[2])])
417
+ for v in self.args], Dyadic(0))
418
+
419
+ def dt(self, frame):
420
+ """Take the time derivative of this Dyadic in a frame.
421
+
422
+ This function calls the global time_derivative method
423
+
424
+ Parameters
425
+ ==========
426
+
427
+ frame : ReferenceFrame
428
+ The frame to take the time derivative in
429
+
430
+ Examples
431
+ ========
432
+
433
+ >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
434
+ >>> from sympy.physics.vector import init_vprinting
435
+ >>> init_vprinting(pretty_print=False)
436
+ >>> N = ReferenceFrame('N')
437
+ >>> q = dynamicsymbols('q')
438
+ >>> B = N.orientnew('B', 'Axis', [q, N.z])
439
+ >>> d = outer(N.x, N.x)
440
+ >>> d.dt(B)
441
+ - q'*(N.y|N.x) - q'*(N.x|N.y)
442
+
443
+ """
444
+ from sympy.physics.vector.functions import time_derivative
445
+ return time_derivative(self, frame)
446
+
447
+ def simplify(self):
448
+ """Returns a simplified Dyadic."""
449
+ out = Dyadic(0)
450
+ for v in self.args:
451
+ out += Dyadic([(v[0].simplify(), v[1], v[2])])
452
+ return out
453
+
454
+ def subs(self, *args, **kwargs):
455
+ """Substitution on the Dyadic.
456
+
457
+ Examples
458
+ ========
459
+
460
+ >>> from sympy.physics.vector import ReferenceFrame
461
+ >>> from sympy import Symbol
462
+ >>> N = ReferenceFrame('N')
463
+ >>> s = Symbol('s')
464
+ >>> a = s*(N.x|N.x)
465
+ >>> a.subs({s: 2})
466
+ 2*(N.x|N.x)
467
+
468
+ """
469
+
470
+ return sum([Dyadic([(v[0].subs(*args, **kwargs), v[1], v[2])])
471
+ for v in self.args], Dyadic(0))
472
+
473
+ def applyfunc(self, f):
474
+ """Apply a function to each component of a Dyadic."""
475
+ if not callable(f):
476
+ raise TypeError("`f` must be callable.")
477
+
478
+ out = Dyadic(0)
479
+ for a, b, c in self.args:
480
+ out += f(a) * (b.outer(c))
481
+ return out
482
+
483
+ def _eval_evalf(self, prec):
484
+ if not self.args:
485
+ return self
486
+ new_args = []
487
+ dps = prec_to_dps(prec)
488
+ for inlist in self.args:
489
+ new_inlist = list(inlist)
490
+ new_inlist[0] = inlist[0].evalf(n=dps)
491
+ new_args.append(tuple(new_inlist))
492
+ return Dyadic(new_args)
493
+
494
+ def xreplace(self, rule):
495
+ """
496
+ Replace occurrences of objects within the measure numbers of the
497
+ Dyadic.
498
+
499
+ Parameters
500
+ ==========
501
+
502
+ rule : dict-like
503
+ Expresses a replacement rule.
504
+
505
+ Returns
506
+ =======
507
+
508
+ Dyadic
509
+ Result of the replacement.
510
+
511
+ Examples
512
+ ========
513
+
514
+ >>> from sympy import symbols, pi
515
+ >>> from sympy.physics.vector import ReferenceFrame, outer
516
+ >>> N = ReferenceFrame('N')
517
+ >>> D = outer(N.x, N.x)
518
+ >>> x, y, z = symbols('x y z')
519
+ >>> ((1 + x*y) * D).xreplace({x: pi})
520
+ (pi*y + 1)*(N.x|N.x)
521
+ >>> ((1 + x*y) * D).xreplace({x: pi, y: 2})
522
+ (1 + 2*pi)*(N.x|N.x)
523
+
524
+ Replacements occur only if an entire node in the expression tree is
525
+ matched:
526
+
527
+ >>> ((x*y + z) * D).xreplace({x*y: pi})
528
+ (z + pi)*(N.x|N.x)
529
+ >>> ((x*y*z) * D).xreplace({x*y: pi})
530
+ x*y*z*(N.x|N.x)
531
+
532
+ """
533
+
534
+ new_args = []
535
+ for inlist in self.args:
536
+ new_inlist = list(inlist)
537
+ new_inlist[0] = new_inlist[0].xreplace(rule)
538
+ new_args.append(tuple(new_inlist))
539
+ return Dyadic(new_args)
540
+
541
+
542
+ def _check_dyadic(other):
543
+ if not isinstance(other, Dyadic):
544
+ raise TypeError('A Dyadic must be supplied')
545
+ return other
.venv/lib/python3.12/site-packages/sympy/physics/vector/fieldfunctions.py ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.function import diff
2
+ from sympy.core.singleton import S
3
+ from sympy.integrals.integrals import integrate
4
+ from sympy.physics.vector import Vector, express
5
+ from sympy.physics.vector.frame import _check_frame
6
+ from sympy.physics.vector.vector import _check_vector
7
+
8
+
9
+ __all__ = ['curl', 'divergence', 'gradient', 'is_conservative',
10
+ 'is_solenoidal', 'scalar_potential',
11
+ 'scalar_potential_difference']
12
+
13
+
14
+ def curl(vect, frame):
15
+ """
16
+ Returns the curl of a vector field computed wrt the coordinate
17
+ symbols of the given frame.
18
+
19
+ Parameters
20
+ ==========
21
+
22
+ vect : Vector
23
+ The vector operand
24
+
25
+ frame : ReferenceFrame
26
+ The reference frame to calculate the curl in
27
+
28
+ Examples
29
+ ========
30
+
31
+ >>> from sympy.physics.vector import ReferenceFrame
32
+ >>> from sympy.physics.vector import curl
33
+ >>> R = ReferenceFrame('R')
34
+ >>> v1 = R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
35
+ >>> curl(v1, R)
36
+ 0
37
+ >>> v2 = R[0]*R[1]*R[2]*R.x
38
+ >>> curl(v2, R)
39
+ R_x*R_y*R.y - R_x*R_z*R.z
40
+
41
+ """
42
+
43
+ _check_vector(vect)
44
+ if vect == 0:
45
+ return Vector(0)
46
+ vect = express(vect, frame, variables=True)
47
+ # A mechanical approach to avoid looping overheads
48
+ vectx = vect.dot(frame.x)
49
+ vecty = vect.dot(frame.y)
50
+ vectz = vect.dot(frame.z)
51
+ outvec = Vector(0)
52
+ outvec += (diff(vectz, frame[1]) - diff(vecty, frame[2])) * frame.x
53
+ outvec += (diff(vectx, frame[2]) - diff(vectz, frame[0])) * frame.y
54
+ outvec += (diff(vecty, frame[0]) - diff(vectx, frame[1])) * frame.z
55
+ return outvec
56
+
57
+
58
+ def divergence(vect, frame):
59
+ """
60
+ Returns the divergence of a vector field computed wrt the coordinate
61
+ symbols of the given frame.
62
+
63
+ Parameters
64
+ ==========
65
+
66
+ vect : Vector
67
+ The vector operand
68
+
69
+ frame : ReferenceFrame
70
+ The reference frame to calculate the divergence in
71
+
72
+ Examples
73
+ ========
74
+
75
+ >>> from sympy.physics.vector import ReferenceFrame
76
+ >>> from sympy.physics.vector import divergence
77
+ >>> R = ReferenceFrame('R')
78
+ >>> v1 = R[0]*R[1]*R[2] * (R.x+R.y+R.z)
79
+ >>> divergence(v1, R)
80
+ R_x*R_y + R_x*R_z + R_y*R_z
81
+ >>> v2 = 2*R[1]*R[2]*R.y
82
+ >>> divergence(v2, R)
83
+ 2*R_z
84
+
85
+ """
86
+
87
+ _check_vector(vect)
88
+ if vect == 0:
89
+ return S.Zero
90
+ vect = express(vect, frame, variables=True)
91
+ vectx = vect.dot(frame.x)
92
+ vecty = vect.dot(frame.y)
93
+ vectz = vect.dot(frame.z)
94
+ out = S.Zero
95
+ out += diff(vectx, frame[0])
96
+ out += diff(vecty, frame[1])
97
+ out += diff(vectz, frame[2])
98
+ return out
99
+
100
+
101
+ def gradient(scalar, frame):
102
+ """
103
+ Returns the vector gradient of a scalar field computed wrt the
104
+ coordinate symbols of the given frame.
105
+
106
+ Parameters
107
+ ==========
108
+
109
+ scalar : sympifiable
110
+ The scalar field to take the gradient of
111
+
112
+ frame : ReferenceFrame
113
+ The frame to calculate the gradient in
114
+
115
+ Examples
116
+ ========
117
+
118
+ >>> from sympy.physics.vector import ReferenceFrame
119
+ >>> from sympy.physics.vector import gradient
120
+ >>> R = ReferenceFrame('R')
121
+ >>> s1 = R[0]*R[1]*R[2]
122
+ >>> gradient(s1, R)
123
+ R_y*R_z*R.x + R_x*R_z*R.y + R_x*R_y*R.z
124
+ >>> s2 = 5*R[0]**2*R[2]
125
+ >>> gradient(s2, R)
126
+ 10*R_x*R_z*R.x + 5*R_x**2*R.z
127
+
128
+ """
129
+
130
+ _check_frame(frame)
131
+ outvec = Vector(0)
132
+ scalar = express(scalar, frame, variables=True)
133
+ for i, x in enumerate(frame):
134
+ outvec += diff(scalar, frame[i]) * x # noqa: PLR1736
135
+ return outvec
136
+
137
+
138
+ def is_conservative(field):
139
+ """
140
+ Checks if a field is conservative.
141
+
142
+ Parameters
143
+ ==========
144
+
145
+ field : Vector
146
+ The field to check for conservative property
147
+
148
+ Examples
149
+ ========
150
+
151
+ >>> from sympy.physics.vector import ReferenceFrame
152
+ >>> from sympy.physics.vector import is_conservative
153
+ >>> R = ReferenceFrame('R')
154
+ >>> is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z)
155
+ True
156
+ >>> is_conservative(R[2] * R.y)
157
+ False
158
+
159
+ """
160
+
161
+ # Field is conservative irrespective of frame
162
+ # Take the first frame in the result of the separate method of Vector
163
+ if field == Vector(0):
164
+ return True
165
+ frame = list(field.separate())[0]
166
+ return curl(field, frame).simplify() == Vector(0)
167
+
168
+
169
+ def is_solenoidal(field):
170
+ """
171
+ Checks if a field is solenoidal.
172
+
173
+ Parameters
174
+ ==========
175
+
176
+ field : Vector
177
+ The field to check for solenoidal property
178
+
179
+ Examples
180
+ ========
181
+
182
+ >>> from sympy.physics.vector import ReferenceFrame
183
+ >>> from sympy.physics.vector import is_solenoidal
184
+ >>> R = ReferenceFrame('R')
185
+ >>> is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z)
186
+ True
187
+ >>> is_solenoidal(R[1] * R.y)
188
+ False
189
+
190
+ """
191
+
192
+ # Field is solenoidal irrespective of frame
193
+ # Take the first frame in the result of the separate method in Vector
194
+ if field == Vector(0):
195
+ return True
196
+ frame = list(field.separate())[0]
197
+ return divergence(field, frame).simplify() is S.Zero
198
+
199
+
200
+ def scalar_potential(field, frame):
201
+ """
202
+ Returns the scalar potential function of a field in a given frame
203
+ (without the added integration constant).
204
+
205
+ Parameters
206
+ ==========
207
+
208
+ field : Vector
209
+ The vector field whose scalar potential function is to be
210
+ calculated
211
+
212
+ frame : ReferenceFrame
213
+ The frame to do the calculation in
214
+
215
+ Examples
216
+ ========
217
+
218
+ >>> from sympy.physics.vector import ReferenceFrame
219
+ >>> from sympy.physics.vector import scalar_potential, gradient
220
+ >>> R = ReferenceFrame('R')
221
+ >>> scalar_potential(R.z, R) == R[2]
222
+ True
223
+ >>> scalar_field = 2*R[0]**2*R[1]*R[2]
224
+ >>> grad_field = gradient(scalar_field, R)
225
+ >>> scalar_potential(grad_field, R)
226
+ 2*R_x**2*R_y*R_z
227
+
228
+ """
229
+
230
+ # Check whether field is conservative
231
+ if not is_conservative(field):
232
+ raise ValueError("Field is not conservative")
233
+ if field == Vector(0):
234
+ return S.Zero
235
+ # Express the field exntirely in frame
236
+ # Substitute coordinate variables also
237
+ _check_frame(frame)
238
+ field = express(field, frame, variables=True)
239
+ # Make a list of dimensions of the frame
240
+ dimensions = list(frame)
241
+ # Calculate scalar potential function
242
+ temp_function = integrate(field.dot(dimensions[0]), frame[0])
243
+ for i, dim in enumerate(dimensions[1:]):
244
+ partial_diff = diff(temp_function, frame[i + 1])
245
+ partial_diff = field.dot(dim) - partial_diff
246
+ temp_function += integrate(partial_diff, frame[i + 1])
247
+ return temp_function
248
+
249
+
250
+ def scalar_potential_difference(field, frame, point1, point2, origin):
251
+ """
252
+ Returns the scalar potential difference between two points in a
253
+ certain frame, wrt a given field.
254
+
255
+ If a scalar field is provided, its values at the two points are
256
+ considered. If a conservative vector field is provided, the values
257
+ of its scalar potential function at the two points are used.
258
+
259
+ Returns (potential at position 2) - (potential at position 1)
260
+
261
+ Parameters
262
+ ==========
263
+
264
+ field : Vector/sympyfiable
265
+ The field to calculate wrt
266
+
267
+ frame : ReferenceFrame
268
+ The frame to do the calculations in
269
+
270
+ point1 : Point
271
+ The initial Point in given frame
272
+
273
+ position2 : Point
274
+ The second Point in the given frame
275
+
276
+ origin : Point
277
+ The Point to use as reference point for position vector
278
+ calculation
279
+
280
+ Examples
281
+ ========
282
+
283
+ >>> from sympy.physics.vector import ReferenceFrame, Point
284
+ >>> from sympy.physics.vector import scalar_potential_difference
285
+ >>> R = ReferenceFrame('R')
286
+ >>> O = Point('O')
287
+ >>> P = O.locatenew('P', R[0]*R.x + R[1]*R.y + R[2]*R.z)
288
+ >>> vectfield = 4*R[0]*R[1]*R.x + 2*R[0]**2*R.y
289
+ >>> scalar_potential_difference(vectfield, R, O, P, O)
290
+ 2*R_x**2*R_y
291
+ >>> Q = O.locatenew('O', 3*R.x + R.y + 2*R.z)
292
+ >>> scalar_potential_difference(vectfield, R, P, Q, O)
293
+ -2*R_x**2*R_y + 18
294
+
295
+ """
296
+
297
+ _check_frame(frame)
298
+ if isinstance(field, Vector):
299
+ # Get the scalar potential function
300
+ scalar_fn = scalar_potential(field, frame)
301
+ else:
302
+ # Field is a scalar
303
+ scalar_fn = field
304
+ # Express positions in required frame
305
+ position1 = express(point1.pos_from(origin), frame, variables=True)
306
+ position2 = express(point2.pos_from(origin), frame, variables=True)
307
+ # Get the two positions as substitution dicts for coordinate variables
308
+ subs_dict1 = {}
309
+ subs_dict2 = {}
310
+ for i, x in enumerate(frame):
311
+ subs_dict1[frame[i]] = x.dot(position1)
312
+ subs_dict2[frame[i]] = x.dot(position2)
313
+ return scalar_fn.subs(subs_dict2) - scalar_fn.subs(subs_dict1)
.venv/lib/python3.12/site-packages/sympy/physics/vector/frame.py ADDED
@@ -0,0 +1,1575 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy import (diff, expand, sin, cos, sympify, eye, zeros,
2
+ ImmutableMatrix as Matrix, MatrixBase)
3
+ from sympy.core.symbol import Symbol
4
+ from sympy.simplify.trigsimp import trigsimp
5
+ from sympy.physics.vector.vector import Vector, _check_vector
6
+ from sympy.utilities.misc import translate
7
+
8
+ from warnings import warn
9
+
10
+ __all__ = ['CoordinateSym', 'ReferenceFrame']
11
+
12
+
13
+ class CoordinateSym(Symbol):
14
+ """
15
+ A coordinate symbol/base scalar associated wrt a Reference Frame.
16
+
17
+ Ideally, users should not instantiate this class. Instances of
18
+ this class must only be accessed through the corresponding frame
19
+ as 'frame[index]'.
20
+
21
+ CoordinateSyms having the same frame and index parameters are equal
22
+ (even though they may be instantiated separately).
23
+
24
+ Parameters
25
+ ==========
26
+
27
+ name : string
28
+ The display name of the CoordinateSym
29
+
30
+ frame : ReferenceFrame
31
+ The reference frame this base scalar belongs to
32
+
33
+ index : 0, 1 or 2
34
+ The index of the dimension denoted by this coordinate variable
35
+
36
+ Examples
37
+ ========
38
+
39
+ >>> from sympy.physics.vector import ReferenceFrame, CoordinateSym
40
+ >>> A = ReferenceFrame('A')
41
+ >>> A[1]
42
+ A_y
43
+ >>> type(A[0])
44
+ <class 'sympy.physics.vector.frame.CoordinateSym'>
45
+ >>> a_y = CoordinateSym('a_y', A, 1)
46
+ >>> a_y == A[1]
47
+ True
48
+
49
+ """
50
+
51
+ def __new__(cls, name, frame, index):
52
+ # We can't use the cached Symbol.__new__ because this class depends on
53
+ # frame and index, which are not passed to Symbol.__xnew__.
54
+ assumptions = {}
55
+ super()._sanitize(assumptions, cls)
56
+ obj = super().__xnew__(cls, name, **assumptions)
57
+ _check_frame(frame)
58
+ if index not in range(0, 3):
59
+ raise ValueError("Invalid index specified")
60
+ obj._id = (frame, index)
61
+ return obj
62
+
63
+ def __getnewargs_ex__(self):
64
+ return (self.name, *self._id), {}
65
+
66
+ @property
67
+ def frame(self):
68
+ return self._id[0]
69
+
70
+ def __eq__(self, other):
71
+ # Check if the other object is a CoordinateSym of the same frame and
72
+ # same index
73
+ if isinstance(other, CoordinateSym):
74
+ if other._id == self._id:
75
+ return True
76
+ return False
77
+
78
+ def __ne__(self, other):
79
+ return not self == other
80
+
81
+ def __hash__(self):
82
+ return (self._id[0].__hash__(), self._id[1]).__hash__()
83
+
84
+
85
+ class ReferenceFrame:
86
+ """A reference frame in classical mechanics.
87
+
88
+ ReferenceFrame is a class used to represent a reference frame in classical
89
+ mechanics. It has a standard basis of three unit vectors in the frame's
90
+ x, y, and z directions.
91
+
92
+ It also can have a rotation relative to a parent frame; this rotation is
93
+ defined by a direction cosine matrix relating this frame's basis vectors to
94
+ the parent frame's basis vectors. It can also have an angular velocity
95
+ vector, defined in another frame.
96
+
97
+ """
98
+ _count = 0
99
+
100
+ def __init__(self, name, indices=None, latexs=None, variables=None):
101
+ """ReferenceFrame initialization method.
102
+
103
+ A ReferenceFrame has a set of orthonormal basis vectors, along with
104
+ orientations relative to other ReferenceFrames and angular velocities
105
+ relative to other ReferenceFrames.
106
+
107
+ Parameters
108
+ ==========
109
+
110
+ indices : tuple of str
111
+ Enables the reference frame's basis unit vectors to be accessed by
112
+ Python's square bracket indexing notation using the provided three
113
+ indice strings and alters the printing of the unit vectors to
114
+ reflect this choice.
115
+ latexs : tuple of str
116
+ Alters the LaTeX printing of the reference frame's basis unit
117
+ vectors to the provided three valid LaTeX strings.
118
+
119
+ Examples
120
+ ========
121
+
122
+ >>> from sympy.physics.vector import ReferenceFrame, vlatex
123
+ >>> N = ReferenceFrame('N')
124
+ >>> N.x
125
+ N.x
126
+ >>> O = ReferenceFrame('O', indices=('1', '2', '3'))
127
+ >>> O.x
128
+ O['1']
129
+ >>> O['1']
130
+ O['1']
131
+ >>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3'))
132
+ >>> vlatex(P.x)
133
+ 'A1'
134
+
135
+ ``symbols()`` can be used to create multiple Reference Frames in one
136
+ step, for example:
137
+
138
+ >>> from sympy.physics.vector import ReferenceFrame
139
+ >>> from sympy import symbols
140
+ >>> A, B, C = symbols('A B C', cls=ReferenceFrame)
141
+ >>> D, E = symbols('D E', cls=ReferenceFrame, indices=('1', '2', '3'))
142
+ >>> A[0]
143
+ A_x
144
+ >>> D.x
145
+ D['1']
146
+ >>> E.y
147
+ E['2']
148
+ >>> type(A) == type(D)
149
+ True
150
+
151
+ Unit dyads for the ReferenceFrame can be accessed through the attributes ``xx``, ``xy``, etc. For example:
152
+
153
+ >>> from sympy.physics.vector import ReferenceFrame
154
+ >>> N = ReferenceFrame('N')
155
+ >>> N.yz
156
+ (N.y|N.z)
157
+ >>> N.zx
158
+ (N.z|N.x)
159
+ >>> P = ReferenceFrame('P', indices=['1', '2', '3'])
160
+ >>> P.xx
161
+ (P['1']|P['1'])
162
+ >>> P.zy
163
+ (P['3']|P['2'])
164
+
165
+ Unit dyadic is also accessible via the ``u`` attribute:
166
+
167
+ >>> from sympy.physics.vector import ReferenceFrame
168
+ >>> N = ReferenceFrame('N')
169
+ >>> N.u
170
+ (N.x|N.x) + (N.y|N.y) + (N.z|N.z)
171
+ >>> P = ReferenceFrame('P', indices=['1', '2', '3'])
172
+ >>> P.u
173
+ (P['1']|P['1']) + (P['2']|P['2']) + (P['3']|P['3'])
174
+
175
+ """
176
+
177
+ if not isinstance(name, str):
178
+ raise TypeError('Need to supply a valid name')
179
+ # The if statements below are for custom printing of basis-vectors for
180
+ # each frame.
181
+ # First case, when custom indices are supplied
182
+ if indices is not None:
183
+ if not isinstance(indices, (tuple, list)):
184
+ raise TypeError('Supply the indices as a list')
185
+ if len(indices) != 3:
186
+ raise ValueError('Supply 3 indices')
187
+ for i in indices:
188
+ if not isinstance(i, str):
189
+ raise TypeError('Indices must be strings')
190
+ self.str_vecs = [(name + '[\'' + indices[0] + '\']'),
191
+ (name + '[\'' + indices[1] + '\']'),
192
+ (name + '[\'' + indices[2] + '\']')]
193
+ self.pretty_vecs = [(name.lower() + "_" + indices[0]),
194
+ (name.lower() + "_" + indices[1]),
195
+ (name.lower() + "_" + indices[2])]
196
+ self.latex_vecs = [(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
197
+ indices[0])),
198
+ (r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
199
+ indices[1])),
200
+ (r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
201
+ indices[2]))]
202
+ self.indices = indices
203
+ # Second case, when no custom indices are supplied
204
+ else:
205
+ self.str_vecs = [(name + '.x'), (name + '.y'), (name + '.z')]
206
+ self.pretty_vecs = [name.lower() + "_x",
207
+ name.lower() + "_y",
208
+ name.lower() + "_z"]
209
+ self.latex_vecs = [(r"\mathbf{\hat{%s}_x}" % name.lower()),
210
+ (r"\mathbf{\hat{%s}_y}" % name.lower()),
211
+ (r"\mathbf{\hat{%s}_z}" % name.lower())]
212
+ self.indices = ['x', 'y', 'z']
213
+ # Different step, for custom latex basis vectors
214
+ if latexs is not None:
215
+ if not isinstance(latexs, (tuple, list)):
216
+ raise TypeError('Supply the indices as a list')
217
+ if len(latexs) != 3:
218
+ raise ValueError('Supply 3 indices')
219
+ for i in latexs:
220
+ if not isinstance(i, str):
221
+ raise TypeError('Latex entries must be strings')
222
+ self.latex_vecs = latexs
223
+ self.name = name
224
+ self._var_dict = {}
225
+ # The _dcm_dict dictionary will only store the dcms of adjacent
226
+ # parent-child relationships. The _dcm_cache dictionary will store
227
+ # calculated dcm along with all content of _dcm_dict for faster
228
+ # retrieval of dcms.
229
+ self._dcm_dict = {}
230
+ self._dcm_cache = {}
231
+ self._ang_vel_dict = {}
232
+ self._ang_acc_dict = {}
233
+ self._dlist = [self._dcm_dict, self._ang_vel_dict, self._ang_acc_dict]
234
+ self._cur = 0
235
+ self._x = Vector([(Matrix([1, 0, 0]), self)])
236
+ self._y = Vector([(Matrix([0, 1, 0]), self)])
237
+ self._z = Vector([(Matrix([0, 0, 1]), self)])
238
+ # Associate coordinate symbols wrt this frame
239
+ if variables is not None:
240
+ if not isinstance(variables, (tuple, list)):
241
+ raise TypeError('Supply the variable names as a list/tuple')
242
+ if len(variables) != 3:
243
+ raise ValueError('Supply 3 variable names')
244
+ for i in variables:
245
+ if not isinstance(i, str):
246
+ raise TypeError('Variable names must be strings')
247
+ else:
248
+ variables = [name + '_x', name + '_y', name + '_z']
249
+ self.varlist = (CoordinateSym(variables[0], self, 0),
250
+ CoordinateSym(variables[1], self, 1),
251
+ CoordinateSym(variables[2], self, 2))
252
+ ReferenceFrame._count += 1
253
+ self.index = ReferenceFrame._count
254
+
255
+ def __getitem__(self, ind):
256
+ """
257
+ Returns basis vector for the provided index, if the index is a string.
258
+
259
+ If the index is a number, returns the coordinate variable correspon-
260
+ -ding to that index.
261
+ """
262
+ if not isinstance(ind, str):
263
+ if ind < 3:
264
+ return self.varlist[ind]
265
+ else:
266
+ raise ValueError("Invalid index provided")
267
+ if self.indices[0] == ind:
268
+ return self.x
269
+ if self.indices[1] == ind:
270
+ return self.y
271
+ if self.indices[2] == ind:
272
+ return self.z
273
+ else:
274
+ raise ValueError('Not a defined index')
275
+
276
+ def __iter__(self):
277
+ return iter([self.x, self.y, self.z])
278
+
279
+ def __str__(self):
280
+ """Returns the name of the frame. """
281
+ return self.name
282
+
283
+ __repr__ = __str__
284
+
285
+ def _dict_list(self, other, num):
286
+ """Returns an inclusive list of reference frames that connect this
287
+ reference frame to the provided reference frame.
288
+
289
+ Parameters
290
+ ==========
291
+ other : ReferenceFrame
292
+ The other reference frame to look for a connecting relationship to.
293
+ num : integer
294
+ ``0``, ``1``, and ``2`` will look for orientation, angular
295
+ velocity, and angular acceleration relationships between the two
296
+ frames, respectively.
297
+
298
+ Returns
299
+ =======
300
+ list
301
+ Inclusive list of reference frames that connect this reference
302
+ frame to the other reference frame.
303
+
304
+ Examples
305
+ ========
306
+
307
+ >>> from sympy.physics.vector import ReferenceFrame
308
+ >>> A = ReferenceFrame('A')
309
+ >>> B = ReferenceFrame('B')
310
+ >>> C = ReferenceFrame('C')
311
+ >>> D = ReferenceFrame('D')
312
+ >>> B.orient_axis(A, A.x, 1.0)
313
+ >>> C.orient_axis(B, B.x, 1.0)
314
+ >>> D.orient_axis(C, C.x, 1.0)
315
+ >>> D._dict_list(A, 0)
316
+ [D, C, B, A]
317
+
318
+ Raises
319
+ ======
320
+
321
+ ValueError
322
+ When no path is found between the two reference frames or ``num``
323
+ is an incorrect value.
324
+
325
+ """
326
+
327
+ connect_type = {0: 'orientation',
328
+ 1: 'angular velocity',
329
+ 2: 'angular acceleration'}
330
+
331
+ if num not in connect_type.keys():
332
+ raise ValueError('Valid values for num are 0, 1, or 2.')
333
+
334
+ possible_connecting_paths = [[self]]
335
+ oldlist = [[]]
336
+ while possible_connecting_paths != oldlist:
337
+ oldlist = possible_connecting_paths.copy()
338
+ for frame_list in possible_connecting_paths:
339
+ frames_adjacent_to_last = frame_list[-1]._dlist[num].keys()
340
+ for adjacent_frame in frames_adjacent_to_last:
341
+ if adjacent_frame not in frame_list:
342
+ connecting_path = frame_list + [adjacent_frame]
343
+ if connecting_path not in possible_connecting_paths:
344
+ possible_connecting_paths.append(connecting_path)
345
+
346
+ for connecting_path in oldlist:
347
+ if connecting_path[-1] != other:
348
+ possible_connecting_paths.remove(connecting_path)
349
+ possible_connecting_paths.sort(key=len)
350
+
351
+ if len(possible_connecting_paths) != 0:
352
+ return possible_connecting_paths[0] # selects the shortest path
353
+
354
+ msg = 'No connecting {} path found between {} and {}.'
355
+ raise ValueError(msg.format(connect_type[num], self.name, other.name))
356
+
357
+ def _w_diff_dcm(self, otherframe):
358
+ """Angular velocity from time differentiating the DCM. """
359
+ from sympy.physics.vector.functions import dynamicsymbols
360
+ dcm2diff = otherframe.dcm(self)
361
+ diffed = dcm2diff.diff(dynamicsymbols._t)
362
+ angvelmat = diffed * dcm2diff.T
363
+ w1 = trigsimp(expand(angvelmat[7]), recursive=True)
364
+ w2 = trigsimp(expand(angvelmat[2]), recursive=True)
365
+ w3 = trigsimp(expand(angvelmat[3]), recursive=True)
366
+ return Vector([(Matrix([w1, w2, w3]), otherframe)])
367
+
368
+ def variable_map(self, otherframe):
369
+ """
370
+ Returns a dictionary which expresses the coordinate variables
371
+ of this frame in terms of the variables of otherframe.
372
+
373
+ If Vector.simp is True, returns a simplified version of the mapped
374
+ values. Else, returns them without simplification.
375
+
376
+ Simplification of the expressions may take time.
377
+
378
+ Parameters
379
+ ==========
380
+
381
+ otherframe : ReferenceFrame
382
+ The other frame to map the variables to
383
+
384
+ Examples
385
+ ========
386
+
387
+ >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
388
+ >>> A = ReferenceFrame('A')
389
+ >>> q = dynamicsymbols('q')
390
+ >>> B = A.orientnew('B', 'Axis', [q, A.z])
391
+ >>> A.variable_map(B)
392
+ {A_x: B_x*cos(q(t)) - B_y*sin(q(t)), A_y: B_x*sin(q(t)) + B_y*cos(q(t)), A_z: B_z}
393
+
394
+ """
395
+
396
+ _check_frame(otherframe)
397
+ if (otherframe, Vector.simp) in self._var_dict:
398
+ return self._var_dict[(otherframe, Vector.simp)]
399
+ else:
400
+ vars_matrix = self.dcm(otherframe) * Matrix(otherframe.varlist)
401
+ mapping = {}
402
+ for i, x in enumerate(self):
403
+ if Vector.simp:
404
+ mapping[self.varlist[i]] = trigsimp(vars_matrix[i],
405
+ method='fu')
406
+ else:
407
+ mapping[self.varlist[i]] = vars_matrix[i]
408
+ self._var_dict[(otherframe, Vector.simp)] = mapping
409
+ return mapping
410
+
411
+ def ang_acc_in(self, otherframe):
412
+ """Returns the angular acceleration Vector of the ReferenceFrame.
413
+
414
+ Effectively returns the Vector:
415
+
416
+ ``N_alpha_B``
417
+
418
+ which represent the angular acceleration of B in N, where B is self,
419
+ and N is otherframe.
420
+
421
+ Parameters
422
+ ==========
423
+
424
+ otherframe : ReferenceFrame
425
+ The ReferenceFrame which the angular acceleration is returned in.
426
+
427
+ Examples
428
+ ========
429
+
430
+ >>> from sympy.physics.vector import ReferenceFrame
431
+ >>> N = ReferenceFrame('N')
432
+ >>> A = ReferenceFrame('A')
433
+ >>> V = 10 * N.x
434
+ >>> A.set_ang_acc(N, V)
435
+ >>> A.ang_acc_in(N)
436
+ 10*N.x
437
+
438
+ """
439
+
440
+ _check_frame(otherframe)
441
+ if otherframe in self._ang_acc_dict:
442
+ return self._ang_acc_dict[otherframe]
443
+ else:
444
+ return self.ang_vel_in(otherframe).dt(otherframe)
445
+
446
+ def ang_vel_in(self, otherframe):
447
+ """Returns the angular velocity Vector of the ReferenceFrame.
448
+
449
+ Effectively returns the Vector:
450
+
451
+ ^N omega ^B
452
+
453
+ which represent the angular velocity of B in N, where B is self, and
454
+ N is otherframe.
455
+
456
+ Parameters
457
+ ==========
458
+
459
+ otherframe : ReferenceFrame
460
+ The ReferenceFrame which the angular velocity is returned in.
461
+
462
+ Examples
463
+ ========
464
+
465
+ >>> from sympy.physics.vector import ReferenceFrame
466
+ >>> N = ReferenceFrame('N')
467
+ >>> A = ReferenceFrame('A')
468
+ >>> V = 10 * N.x
469
+ >>> A.set_ang_vel(N, V)
470
+ >>> A.ang_vel_in(N)
471
+ 10*N.x
472
+
473
+ """
474
+
475
+ _check_frame(otherframe)
476
+ flist = self._dict_list(otherframe, 1)
477
+ outvec = Vector(0)
478
+ for i in range(len(flist) - 1):
479
+ outvec += flist[i]._ang_vel_dict[flist[i + 1]]
480
+ return outvec
481
+
482
+ def dcm(self, otherframe):
483
+ r"""Returns the direction cosine matrix of this reference frame
484
+ relative to the provided reference frame.
485
+
486
+ The returned matrix can be used to express the orthogonal unit vectors
487
+ of this frame in terms of the orthogonal unit vectors of
488
+ ``otherframe``.
489
+
490
+ Parameters
491
+ ==========
492
+
493
+ otherframe : ReferenceFrame
494
+ The reference frame which the direction cosine matrix of this frame
495
+ is formed relative to.
496
+
497
+ Examples
498
+ ========
499
+
500
+ The following example rotates the reference frame A relative to N by a
501
+ simple rotation and then calculates the direction cosine matrix of N
502
+ relative to A.
503
+
504
+ >>> from sympy import symbols, sin, cos
505
+ >>> from sympy.physics.vector import ReferenceFrame
506
+ >>> q1 = symbols('q1')
507
+ >>> N = ReferenceFrame('N')
508
+ >>> A = ReferenceFrame('A')
509
+ >>> A.orient_axis(N, q1, N.x)
510
+ >>> N.dcm(A)
511
+ Matrix([
512
+ [1, 0, 0],
513
+ [0, cos(q1), -sin(q1)],
514
+ [0, sin(q1), cos(q1)]])
515
+
516
+ The second row of the above direction cosine matrix represents the
517
+ ``N.y`` unit vector in N expressed in A. Like so:
518
+
519
+ >>> Ny = 0*A.x + cos(q1)*A.y - sin(q1)*A.z
520
+
521
+ Thus, expressing ``N.y`` in A should return the same result:
522
+
523
+ >>> N.y.express(A)
524
+ cos(q1)*A.y - sin(q1)*A.z
525
+
526
+ Notes
527
+ =====
528
+
529
+ It is important to know what form of the direction cosine matrix is
530
+ returned. If ``B.dcm(A)`` is called, it means the "direction cosine
531
+ matrix of B rotated relative to A". This is the matrix
532
+ :math:`{}^B\mathbf{C}^A` shown in the following relationship:
533
+
534
+ .. math::
535
+
536
+ \begin{bmatrix}
537
+ \hat{\mathbf{b}}_1 \\
538
+ \hat{\mathbf{b}}_2 \\
539
+ \hat{\mathbf{b}}_3
540
+ \end{bmatrix}
541
+ =
542
+ {}^B\mathbf{C}^A
543
+ \begin{bmatrix}
544
+ \hat{\mathbf{a}}_1 \\
545
+ \hat{\mathbf{a}}_2 \\
546
+ \hat{\mathbf{a}}_3
547
+ \end{bmatrix}.
548
+
549
+ :math:`{}^B\mathbf{C}^A` is the matrix that expresses the B unit
550
+ vectors in terms of the A unit vectors.
551
+
552
+ """
553
+
554
+ _check_frame(otherframe)
555
+ # Check if the dcm wrt that frame has already been calculated
556
+ if otherframe in self._dcm_cache:
557
+ return self._dcm_cache[otherframe]
558
+ flist = self._dict_list(otherframe, 0)
559
+ outdcm = eye(3)
560
+ for i in range(len(flist) - 1):
561
+ outdcm = outdcm * flist[i]._dcm_dict[flist[i + 1]]
562
+ # After calculation, store the dcm in dcm cache for faster future
563
+ # retrieval
564
+ self._dcm_cache[otherframe] = outdcm
565
+ otherframe._dcm_cache[self] = outdcm.T
566
+ return outdcm
567
+
568
+ def _dcm(self, parent, parent_orient):
569
+ # If parent.oreint(self) is already defined,then
570
+ # update the _dcm_dict of parent while over write
571
+ # all content of self._dcm_dict and self._dcm_cache
572
+ # with new dcm relation.
573
+ # Else update _dcm_cache and _dcm_dict of both
574
+ # self and parent.
575
+ frames = self._dcm_cache.keys()
576
+ dcm_dict_del = []
577
+ dcm_cache_del = []
578
+ if parent in frames:
579
+ for frame in frames:
580
+ if frame in self._dcm_dict:
581
+ dcm_dict_del += [frame]
582
+ dcm_cache_del += [frame]
583
+ # Reset the _dcm_cache of this frame, and remove it from the
584
+ # _dcm_caches of the frames it is linked to. Also remove it from
585
+ # the _dcm_dict of its parent
586
+ for frame in dcm_dict_del:
587
+ del frame._dcm_dict[self]
588
+ for frame in dcm_cache_del:
589
+ del frame._dcm_cache[self]
590
+ # Reset the _dcm_dict
591
+ self._dcm_dict = self._dlist[0] = {}
592
+ # Reset the _dcm_cache
593
+ self._dcm_cache = {}
594
+
595
+ else:
596
+ # Check for loops and raise warning accordingly.
597
+ visited = []
598
+ queue = list(frames)
599
+ cont = True # Flag to control queue loop.
600
+ while queue and cont:
601
+ node = queue.pop(0)
602
+ if node not in visited:
603
+ visited.append(node)
604
+ neighbors = node._dcm_dict.keys()
605
+ for neighbor in neighbors:
606
+ if neighbor == parent:
607
+ warn('Loops are defined among the orientation of '
608
+ 'frames. This is likely not desired and may '
609
+ 'cause errors in your calculations.')
610
+ cont = False
611
+ break
612
+ queue.append(neighbor)
613
+
614
+ # Add the dcm relationship to _dcm_dict
615
+ self._dcm_dict.update({parent: parent_orient.T})
616
+ parent._dcm_dict.update({self: parent_orient})
617
+ # Update the dcm cache
618
+ self._dcm_cache.update({parent: parent_orient.T})
619
+ parent._dcm_cache.update({self: parent_orient})
620
+
621
+ def orient_axis(self, parent, axis, angle):
622
+ """Sets the orientation of this reference frame with respect to a
623
+ parent reference frame by rotating through an angle about an axis fixed
624
+ in the parent reference frame.
625
+
626
+ Parameters
627
+ ==========
628
+
629
+ parent : ReferenceFrame
630
+ Reference frame that this reference frame will be rotated relative
631
+ to.
632
+ axis : Vector
633
+ Vector fixed in the parent frame about about which this frame is
634
+ rotated. It need not be a unit vector and the rotation follows the
635
+ right hand rule.
636
+ angle : sympifiable
637
+ Angle in radians by which it the frame is to be rotated.
638
+
639
+ Warns
640
+ ======
641
+
642
+ UserWarning
643
+ If the orientation creates a kinematic loop.
644
+
645
+ Examples
646
+ ========
647
+
648
+ Setup variables for the examples:
649
+
650
+ >>> from sympy import symbols
651
+ >>> from sympy.physics.vector import ReferenceFrame
652
+ >>> q1 = symbols('q1')
653
+ >>> N = ReferenceFrame('N')
654
+ >>> B = ReferenceFrame('B')
655
+ >>> B.orient_axis(N, N.x, q1)
656
+
657
+ The ``orient_axis()`` method generates a direction cosine matrix and
658
+ its transpose which defines the orientation of B relative to N and vice
659
+ versa. Once orient is called, ``dcm()`` outputs the appropriate
660
+ direction cosine matrix:
661
+
662
+ >>> B.dcm(N)
663
+ Matrix([
664
+ [1, 0, 0],
665
+ [0, cos(q1), sin(q1)],
666
+ [0, -sin(q1), cos(q1)]])
667
+ >>> N.dcm(B)
668
+ Matrix([
669
+ [1, 0, 0],
670
+ [0, cos(q1), -sin(q1)],
671
+ [0, sin(q1), cos(q1)]])
672
+
673
+ The following two lines show that the sense of the rotation can be
674
+ defined by negating the vector direction or the angle. Both lines
675
+ produce the same result.
676
+
677
+ >>> B.orient_axis(N, -N.x, q1)
678
+ >>> B.orient_axis(N, N.x, -q1)
679
+
680
+ """
681
+
682
+ from sympy.physics.vector.functions import dynamicsymbols
683
+ _check_frame(parent)
684
+
685
+ if not isinstance(axis, Vector) and isinstance(angle, Vector):
686
+ axis, angle = angle, axis
687
+
688
+ axis = _check_vector(axis)
689
+ theta = sympify(angle)
690
+
691
+ if not axis.dt(parent) == 0:
692
+ raise ValueError('Axis cannot be time-varying.')
693
+ unit_axis = axis.express(parent).normalize()
694
+ unit_col = unit_axis.args[0][0]
695
+ parent_orient_axis = (
696
+ (eye(3) - unit_col * unit_col.T) * cos(theta) +
697
+ Matrix([[0, -unit_col[2], unit_col[1]],
698
+ [unit_col[2], 0, -unit_col[0]],
699
+ [-unit_col[1], unit_col[0], 0]]) *
700
+ sin(theta) + unit_col * unit_col.T)
701
+
702
+ self._dcm(parent, parent_orient_axis)
703
+
704
+ thetad = (theta).diff(dynamicsymbols._t)
705
+ wvec = thetad*axis.express(parent).normalize()
706
+ self._ang_vel_dict.update({parent: wvec})
707
+ parent._ang_vel_dict.update({self: -wvec})
708
+ self._var_dict = {}
709
+
710
+ def orient_explicit(self, parent, dcm):
711
+ """Sets the orientation of this reference frame relative to another (parent) reference frame
712
+ using a direction cosine matrix that describes the rotation from the parent to the child.
713
+
714
+ Parameters
715
+ ==========
716
+
717
+ parent : ReferenceFrame
718
+ Reference frame that this reference frame will be rotated relative
719
+ to.
720
+ dcm : Matrix, shape(3, 3)
721
+ Direction cosine matrix that specifies the relative rotation
722
+ between the two reference frames.
723
+
724
+ Warns
725
+ ======
726
+
727
+ UserWarning
728
+ If the orientation creates a kinematic loop.
729
+
730
+ Examples
731
+ ========
732
+
733
+ Setup variables for the examples:
734
+
735
+ >>> from sympy import symbols, Matrix, sin, cos
736
+ >>> from sympy.physics.vector import ReferenceFrame
737
+ >>> q1 = symbols('q1')
738
+ >>> A = ReferenceFrame('A')
739
+ >>> B = ReferenceFrame('B')
740
+ >>> N = ReferenceFrame('N')
741
+
742
+ A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
743
+ by the following direction cosine matrix:
744
+
745
+ >>> dcm = Matrix([[1, 0, 0],
746
+ ... [0, cos(q1), -sin(q1)],
747
+ ... [0, sin(q1), cos(q1)]])
748
+ >>> A.orient_explicit(N, dcm)
749
+ >>> A.dcm(N)
750
+ Matrix([
751
+ [1, 0, 0],
752
+ [0, cos(q1), sin(q1)],
753
+ [0, -sin(q1), cos(q1)]])
754
+
755
+ This is equivalent to using ``orient_axis()``:
756
+
757
+ >>> B.orient_axis(N, N.x, q1)
758
+ >>> B.dcm(N)
759
+ Matrix([
760
+ [1, 0, 0],
761
+ [0, cos(q1), sin(q1)],
762
+ [0, -sin(q1), cos(q1)]])
763
+
764
+ **Note carefully that** ``N.dcm(B)`` **(the transpose) would be passed
765
+ into** ``orient_explicit()`` **for** ``A.dcm(N)`` **to match**
766
+ ``B.dcm(N)``:
767
+
768
+ >>> A.orient_explicit(N, N.dcm(B))
769
+ >>> A.dcm(N)
770
+ Matrix([
771
+ [1, 0, 0],
772
+ [0, cos(q1), sin(q1)],
773
+ [0, -sin(q1), cos(q1)]])
774
+
775
+ """
776
+ _check_frame(parent)
777
+ # amounts must be a Matrix type object
778
+ # (e.g. sympy.matrices.dense.MutableDenseMatrix).
779
+ if not isinstance(dcm, MatrixBase):
780
+ raise TypeError("Amounts must be a SymPy Matrix type object.")
781
+
782
+ self.orient_dcm(parent, dcm.T)
783
+
784
+ def orient_dcm(self, parent, dcm):
785
+ """Sets the orientation of this reference frame relative to another (parent) reference frame
786
+ using a direction cosine matrix that describes the rotation from the child to the parent.
787
+
788
+ Parameters
789
+ ==========
790
+
791
+ parent : ReferenceFrame
792
+ Reference frame that this reference frame will be rotated relative
793
+ to.
794
+ dcm : Matrix, shape(3, 3)
795
+ Direction cosine matrix that specifies the relative rotation
796
+ between the two reference frames.
797
+
798
+ Warns
799
+ ======
800
+
801
+ UserWarning
802
+ If the orientation creates a kinematic loop.
803
+
804
+ Examples
805
+ ========
806
+
807
+ Setup variables for the examples:
808
+
809
+ >>> from sympy import symbols, Matrix, sin, cos
810
+ >>> from sympy.physics.vector import ReferenceFrame
811
+ >>> q1 = symbols('q1')
812
+ >>> A = ReferenceFrame('A')
813
+ >>> B = ReferenceFrame('B')
814
+ >>> N = ReferenceFrame('N')
815
+
816
+ A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
817
+ by the following direction cosine matrix:
818
+
819
+ >>> dcm = Matrix([[1, 0, 0],
820
+ ... [0, cos(q1), sin(q1)],
821
+ ... [0, -sin(q1), cos(q1)]])
822
+ >>> A.orient_dcm(N, dcm)
823
+ >>> A.dcm(N)
824
+ Matrix([
825
+ [1, 0, 0],
826
+ [0, cos(q1), sin(q1)],
827
+ [0, -sin(q1), cos(q1)]])
828
+
829
+ This is equivalent to using ``orient_axis()``:
830
+
831
+ >>> B.orient_axis(N, N.x, q1)
832
+ >>> B.dcm(N)
833
+ Matrix([
834
+ [1, 0, 0],
835
+ [0, cos(q1), sin(q1)],
836
+ [0, -sin(q1), cos(q1)]])
837
+
838
+ """
839
+
840
+ _check_frame(parent)
841
+ # amounts must be a Matrix type object
842
+ # (e.g. sympy.matrices.dense.MutableDenseMatrix).
843
+ if not isinstance(dcm, MatrixBase):
844
+ raise TypeError("Amounts must be a SymPy Matrix type object.")
845
+
846
+ self._dcm(parent, dcm.T)
847
+
848
+ wvec = self._w_diff_dcm(parent)
849
+ self._ang_vel_dict.update({parent: wvec})
850
+ parent._ang_vel_dict.update({self: -wvec})
851
+ self._var_dict = {}
852
+
853
+ def _rot(self, axis, angle):
854
+ """DCM for simple axis 1,2,or 3 rotations."""
855
+ if axis == 1:
856
+ return Matrix([[1, 0, 0],
857
+ [0, cos(angle), -sin(angle)],
858
+ [0, sin(angle), cos(angle)]])
859
+ elif axis == 2:
860
+ return Matrix([[cos(angle), 0, sin(angle)],
861
+ [0, 1, 0],
862
+ [-sin(angle), 0, cos(angle)]])
863
+ elif axis == 3:
864
+ return Matrix([[cos(angle), -sin(angle), 0],
865
+ [sin(angle), cos(angle), 0],
866
+ [0, 0, 1]])
867
+
868
+ def _parse_consecutive_rotations(self, angles, rotation_order):
869
+ """Helper for orient_body_fixed and orient_space_fixed.
870
+
871
+ Parameters
872
+ ==========
873
+ angles : 3-tuple of sympifiable
874
+ Three angles in radians used for the successive rotations.
875
+ rotation_order : 3 character string or 3 digit integer
876
+ Order of the rotations. The order can be specified by the strings
877
+ ``'XZX'``, ``'131'``, or the integer ``131``. There are 12 unique
878
+ valid rotation orders.
879
+
880
+ Returns
881
+ =======
882
+
883
+ amounts : list
884
+ List of sympifiables corresponding to the rotation angles.
885
+ rot_order : list
886
+ List of integers corresponding to the axis of rotation.
887
+ rot_matrices : list
888
+ List of DCM around the given axis with corresponding magnitude.
889
+
890
+ """
891
+ amounts = list(angles)
892
+ for i, v in enumerate(amounts):
893
+ if not isinstance(v, Vector):
894
+ amounts[i] = sympify(v)
895
+
896
+ approved_orders = ('123', '231', '312', '132', '213', '321', '121',
897
+ '131', '212', '232', '313', '323', '')
898
+ # make sure XYZ => 123
899
+ rot_order = translate(str(rotation_order), 'XYZxyz', '123123')
900
+ if rot_order not in approved_orders:
901
+ raise TypeError('The rotation order is not a valid order.')
902
+
903
+ rot_order = [int(r) for r in rot_order]
904
+ if not (len(amounts) == 3 & len(rot_order) == 3):
905
+ raise TypeError('Body orientation takes 3 values & 3 orders')
906
+ rot_matrices = [self._rot(order, amount)
907
+ for (order, amount) in zip(rot_order, amounts)]
908
+ return amounts, rot_order, rot_matrices
909
+
910
+ def orient_body_fixed(self, parent, angles, rotation_order):
911
+ """Rotates this reference frame relative to the parent reference frame
912
+ by right hand rotating through three successive body fixed simple axis
913
+ rotations. Each subsequent axis of rotation is about the "body fixed"
914
+ unit vectors of a new intermediate reference frame. This type of
915
+ rotation is also referred to rotating through the `Euler and Tait-Bryan
916
+ Angles`_.
917
+
918
+ .. _Euler and Tait-Bryan Angles: https://en.wikipedia.org/wiki/Euler_angles
919
+
920
+ The computed angular velocity in this method is by default expressed in
921
+ the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
922
+ child.y + u3 * child.z`` as generalized speeds.
923
+
924
+ Parameters
925
+ ==========
926
+
927
+ parent : ReferenceFrame
928
+ Reference frame that this reference frame will be rotated relative
929
+ to.
930
+ angles : 3-tuple of sympifiable
931
+ Three angles in radians used for the successive rotations.
932
+ rotation_order : 3 character string or 3 digit integer
933
+ Order of the rotations about each intermediate reference frames'
934
+ unit vectors. The Euler rotation about the X, Z', X'' axes can be
935
+ specified by the strings ``'XZX'``, ``'131'``, or the integer
936
+ ``131``. There are 12 unique valid rotation orders (6 Euler and 6
937
+ Tait-Bryan): zxz, xyx, yzy, zyz, xzx, yxy, xyz, yzx, zxy, xzy, zyx,
938
+ and yxz.
939
+
940
+ Warns
941
+ ======
942
+
943
+ UserWarning
944
+ If the orientation creates a kinematic loop.
945
+
946
+ Examples
947
+ ========
948
+
949
+ Setup variables for the examples:
950
+
951
+ >>> from sympy import symbols
952
+ >>> from sympy.physics.vector import ReferenceFrame
953
+ >>> q1, q2, q3 = symbols('q1, q2, q3')
954
+ >>> N = ReferenceFrame('N')
955
+ >>> B = ReferenceFrame('B')
956
+ >>> B1 = ReferenceFrame('B1')
957
+ >>> B2 = ReferenceFrame('B2')
958
+ >>> B3 = ReferenceFrame('B3')
959
+
960
+ For example, a classic Euler Angle rotation can be done by:
961
+
962
+ >>> B.orient_body_fixed(N, (q1, q2, q3), 'XYX')
963
+ >>> B.dcm(N)
964
+ Matrix([
965
+ [ cos(q2), sin(q1)*sin(q2), -sin(q2)*cos(q1)],
966
+ [sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3), sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
967
+ [sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])
968
+
969
+ This rotates reference frame B relative to reference frame N through
970
+ ``q1`` about ``N.x``, then rotates B again through ``q2`` about
971
+ ``B.y``, and finally through ``q3`` about ``B.x``. It is equivalent to
972
+ three successive ``orient_axis()`` calls:
973
+
974
+ >>> B1.orient_axis(N, N.x, q1)
975
+ >>> B2.orient_axis(B1, B1.y, q2)
976
+ >>> B3.orient_axis(B2, B2.x, q3)
977
+ >>> B3.dcm(N)
978
+ Matrix([
979
+ [ cos(q2), sin(q1)*sin(q2), -sin(q2)*cos(q1)],
980
+ [sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3), sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
981
+ [sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])
982
+
983
+ Acceptable rotation orders are of length 3, expressed in as a string
984
+ ``'XYZ'`` or ``'123'`` or integer ``123``. Rotations about an axis
985
+ twice in a row are prohibited.
986
+
987
+ >>> B.orient_body_fixed(N, (q1, q2, 0), 'ZXZ')
988
+ >>> B.orient_body_fixed(N, (q1, q2, 0), '121')
989
+ >>> B.orient_body_fixed(N, (q1, q2, q3), 123)
990
+
991
+ """
992
+ from sympy.physics.vector.functions import dynamicsymbols
993
+
994
+ _check_frame(parent)
995
+
996
+ amounts, rot_order, rot_matrices = self._parse_consecutive_rotations(
997
+ angles, rotation_order)
998
+ self._dcm(parent, rot_matrices[0] * rot_matrices[1] * rot_matrices[2])
999
+
1000
+ rot_vecs = [zeros(3, 1) for _ in range(3)]
1001
+ for i, order in enumerate(rot_order):
1002
+ rot_vecs[i][order - 1] = amounts[i].diff(dynamicsymbols._t)
1003
+ u1, u2, u3 = rot_vecs[2] + rot_matrices[2].T * (
1004
+ rot_vecs[1] + rot_matrices[1].T * rot_vecs[0])
1005
+ wvec = u1 * self.x + u2 * self.y + u3 * self.z # There is a double -
1006
+ self._ang_vel_dict.update({parent: wvec})
1007
+ parent._ang_vel_dict.update({self: -wvec})
1008
+ self._var_dict = {}
1009
+
1010
+ def orient_space_fixed(self, parent, angles, rotation_order):
1011
+ """Rotates this reference frame relative to the parent reference frame
1012
+ by right hand rotating through three successive space fixed simple axis
1013
+ rotations. Each subsequent axis of rotation is about the "space fixed"
1014
+ unit vectors of the parent reference frame.
1015
+
1016
+ The computed angular velocity in this method is by default expressed in
1017
+ the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
1018
+ child.y + u3 * child.z`` as generalized speeds.
1019
+
1020
+ Parameters
1021
+ ==========
1022
+ parent : ReferenceFrame
1023
+ Reference frame that this reference frame will be rotated relative
1024
+ to.
1025
+ angles : 3-tuple of sympifiable
1026
+ Three angles in radians used for the successive rotations.
1027
+ rotation_order : 3 character string or 3 digit integer
1028
+ Order of the rotations about the parent reference frame's unit
1029
+ vectors. The order can be specified by the strings ``'XZX'``,
1030
+ ``'131'``, or the integer ``131``. There are 12 unique valid
1031
+ rotation orders.
1032
+
1033
+ Warns
1034
+ ======
1035
+
1036
+ UserWarning
1037
+ If the orientation creates a kinematic loop.
1038
+
1039
+ Examples
1040
+ ========
1041
+
1042
+ Setup variables for the examples:
1043
+
1044
+ >>> from sympy import symbols
1045
+ >>> from sympy.physics.vector import ReferenceFrame
1046
+ >>> q1, q2, q3 = symbols('q1, q2, q3')
1047
+ >>> N = ReferenceFrame('N')
1048
+ >>> B = ReferenceFrame('B')
1049
+ >>> B1 = ReferenceFrame('B1')
1050
+ >>> B2 = ReferenceFrame('B2')
1051
+ >>> B3 = ReferenceFrame('B3')
1052
+
1053
+ >>> B.orient_space_fixed(N, (q1, q2, q3), '312')
1054
+ >>> B.dcm(N)
1055
+ Matrix([
1056
+ [ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
1057
+ [-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
1058
+ [ sin(q3)*cos(q2), -sin(q2), cos(q2)*cos(q3)]])
1059
+
1060
+ is equivalent to:
1061
+
1062
+ >>> B1.orient_axis(N, N.z, q1)
1063
+ >>> B2.orient_axis(B1, N.x, q2)
1064
+ >>> B3.orient_axis(B2, N.y, q3)
1065
+ >>> B3.dcm(N).simplify()
1066
+ Matrix([
1067
+ [ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
1068
+ [-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
1069
+ [ sin(q3)*cos(q2), -sin(q2), cos(q2)*cos(q3)]])
1070
+
1071
+ It is worth noting that space-fixed and body-fixed rotations are
1072
+ related by the order of the rotations, i.e. the reverse order of body
1073
+ fixed will give space fixed and vice versa.
1074
+
1075
+ >>> B.orient_space_fixed(N, (q1, q2, q3), '231')
1076
+ >>> B.dcm(N)
1077
+ Matrix([
1078
+ [cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
1079
+ [ -sin(q2), cos(q2)*cos(q3), sin(q3)*cos(q2)],
1080
+ [sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])
1081
+
1082
+ >>> B.orient_body_fixed(N, (q3, q2, q1), '132')
1083
+ >>> B.dcm(N)
1084
+ Matrix([
1085
+ [cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
1086
+ [ -sin(q2), cos(q2)*cos(q3), sin(q3)*cos(q2)],
1087
+ [sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])
1088
+
1089
+ """
1090
+ from sympy.physics.vector.functions import dynamicsymbols
1091
+
1092
+ _check_frame(parent)
1093
+
1094
+ amounts, rot_order, rot_matrices = self._parse_consecutive_rotations(
1095
+ angles, rotation_order)
1096
+ self._dcm(parent, rot_matrices[2] * rot_matrices[1] * rot_matrices[0])
1097
+
1098
+ rot_vecs = [zeros(3, 1) for _ in range(3)]
1099
+ for i, order in enumerate(rot_order):
1100
+ rot_vecs[i][order - 1] = amounts[i].diff(dynamicsymbols._t)
1101
+ u1, u2, u3 = rot_vecs[0] + rot_matrices[0].T * (
1102
+ rot_vecs[1] + rot_matrices[1].T * rot_vecs[2])
1103
+ wvec = u1 * self.x + u2 * self.y + u3 * self.z # There is a double -
1104
+ self._ang_vel_dict.update({parent: wvec})
1105
+ parent._ang_vel_dict.update({self: -wvec})
1106
+ self._var_dict = {}
1107
+
1108
+ def orient_quaternion(self, parent, numbers):
1109
+ """Sets the orientation of this reference frame relative to a parent
1110
+ reference frame via an orientation quaternion. An orientation
1111
+ quaternion is defined as a finite rotation a unit vector, ``(lambda_x,
1112
+ lambda_y, lambda_z)``, by an angle ``theta``. The orientation
1113
+ quaternion is described by four parameters:
1114
+
1115
+ - ``q0 = cos(theta/2)``
1116
+ - ``q1 = lambda_x*sin(theta/2)``
1117
+ - ``q2 = lambda_y*sin(theta/2)``
1118
+ - ``q3 = lambda_z*sin(theta/2)``
1119
+
1120
+ See `Quaternions and Spatial Rotation
1121
+ <https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation>`_ on
1122
+ Wikipedia for more information.
1123
+
1124
+ Parameters
1125
+ ==========
1126
+ parent : ReferenceFrame
1127
+ Reference frame that this reference frame will be rotated relative
1128
+ to.
1129
+ numbers : 4-tuple of sympifiable
1130
+ The four quaternion scalar numbers as defined above: ``q0``,
1131
+ ``q1``, ``q2``, ``q3``.
1132
+
1133
+ Warns
1134
+ ======
1135
+
1136
+ UserWarning
1137
+ If the orientation creates a kinematic loop.
1138
+
1139
+ Examples
1140
+ ========
1141
+
1142
+ Setup variables for the examples:
1143
+
1144
+ >>> from sympy import symbols
1145
+ >>> from sympy.physics.vector import ReferenceFrame
1146
+ >>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
1147
+ >>> N = ReferenceFrame('N')
1148
+ >>> B = ReferenceFrame('B')
1149
+
1150
+ Set the orientation:
1151
+
1152
+ >>> B.orient_quaternion(N, (q0, q1, q2, q3))
1153
+ >>> B.dcm(N)
1154
+ Matrix([
1155
+ [q0**2 + q1**2 - q2**2 - q3**2, 2*q0*q3 + 2*q1*q2, -2*q0*q2 + 2*q1*q3],
1156
+ [ -2*q0*q3 + 2*q1*q2, q0**2 - q1**2 + q2**2 - q3**2, 2*q0*q1 + 2*q2*q3],
1157
+ [ 2*q0*q2 + 2*q1*q3, -2*q0*q1 + 2*q2*q3, q0**2 - q1**2 - q2**2 + q3**2]])
1158
+
1159
+ """
1160
+
1161
+ from sympy.physics.vector.functions import dynamicsymbols
1162
+ _check_frame(parent)
1163
+
1164
+ numbers = list(numbers)
1165
+ for i, v in enumerate(numbers):
1166
+ if not isinstance(v, Vector):
1167
+ numbers[i] = sympify(v)
1168
+
1169
+ if not (isinstance(numbers, (list, tuple)) & (len(numbers) == 4)):
1170
+ raise TypeError('Amounts are a list or tuple of length 4')
1171
+ q0, q1, q2, q3 = numbers
1172
+ parent_orient_quaternion = (
1173
+ Matrix([[q0**2 + q1**2 - q2**2 - q3**2,
1174
+ 2 * (q1 * q2 - q0 * q3),
1175
+ 2 * (q0 * q2 + q1 * q3)],
1176
+ [2 * (q1 * q2 + q0 * q3),
1177
+ q0**2 - q1**2 + q2**2 - q3**2,
1178
+ 2 * (q2 * q3 - q0 * q1)],
1179
+ [2 * (q1 * q3 - q0 * q2),
1180
+ 2 * (q0 * q1 + q2 * q3),
1181
+ q0**2 - q1**2 - q2**2 + q3**2]]))
1182
+
1183
+ self._dcm(parent, parent_orient_quaternion)
1184
+
1185
+ t = dynamicsymbols._t
1186
+ q0, q1, q2, q3 = numbers
1187
+ q0d = diff(q0, t)
1188
+ q1d = diff(q1, t)
1189
+ q2d = diff(q2, t)
1190
+ q3d = diff(q3, t)
1191
+ w1 = 2 * (q1d * q0 + q2d * q3 - q3d * q2 - q0d * q1)
1192
+ w2 = 2 * (q2d * q0 + q3d * q1 - q1d * q3 - q0d * q2)
1193
+ w3 = 2 * (q3d * q0 + q1d * q2 - q2d * q1 - q0d * q3)
1194
+ wvec = Vector([(Matrix([w1, w2, w3]), self)])
1195
+
1196
+ self._ang_vel_dict.update({parent: wvec})
1197
+ parent._ang_vel_dict.update({self: -wvec})
1198
+ self._var_dict = {}
1199
+
1200
+ def orient(self, parent, rot_type, amounts, rot_order=''):
1201
+ """Sets the orientation of this reference frame relative to another
1202
+ (parent) reference frame.
1203
+
1204
+ .. note:: It is now recommended to use the ``.orient_axis,
1205
+ .orient_body_fixed, .orient_space_fixed, .orient_quaternion``
1206
+ methods for the different rotation types.
1207
+
1208
+ Parameters
1209
+ ==========
1210
+
1211
+ parent : ReferenceFrame
1212
+ Reference frame that this reference frame will be rotated relative
1213
+ to.
1214
+ rot_type : str
1215
+ The method used to generate the direction cosine matrix. Supported
1216
+ methods are:
1217
+
1218
+ - ``'Axis'``: simple rotations about a single common axis
1219
+ - ``'DCM'``: for setting the direction cosine matrix directly
1220
+ - ``'Body'``: three successive rotations about new intermediate
1221
+ axes, also called "Euler and Tait-Bryan angles"
1222
+ - ``'Space'``: three successive rotations about the parent
1223
+ frames' unit vectors
1224
+ - ``'Quaternion'``: rotations defined by four parameters which
1225
+ result in a singularity free direction cosine matrix
1226
+
1227
+ amounts :
1228
+ Expressions defining the rotation angles or direction cosine
1229
+ matrix. These must match the ``rot_type``. See examples below for
1230
+ details. The input types are:
1231
+
1232
+ - ``'Axis'``: 2-tuple (expr/sym/func, Vector)
1233
+ - ``'DCM'``: Matrix, shape(3,3)
1234
+ - ``'Body'``: 3-tuple of expressions, symbols, or functions
1235
+ - ``'Space'``: 3-tuple of expressions, symbols, or functions
1236
+ - ``'Quaternion'``: 4-tuple of expressions, symbols, or
1237
+ functions
1238
+
1239
+ rot_order : str or int, optional
1240
+ If applicable, the order of the successive of rotations. The string
1241
+ ``'123'`` and integer ``123`` are equivalent, for example. Required
1242
+ for ``'Body'`` and ``'Space'``.
1243
+
1244
+ Warns
1245
+ ======
1246
+
1247
+ UserWarning
1248
+ If the orientation creates a kinematic loop.
1249
+
1250
+ """
1251
+
1252
+ _check_frame(parent)
1253
+
1254
+ approved_orders = ('123', '231', '312', '132', '213', '321', '121',
1255
+ '131', '212', '232', '313', '323', '')
1256
+ rot_order = translate(str(rot_order), 'XYZxyz', '123123')
1257
+ rot_type = rot_type.upper()
1258
+
1259
+ if rot_order not in approved_orders:
1260
+ raise TypeError('The supplied order is not an approved type')
1261
+
1262
+ if rot_type == 'AXIS':
1263
+ self.orient_axis(parent, amounts[1], amounts[0])
1264
+
1265
+ elif rot_type == 'DCM':
1266
+ self.orient_explicit(parent, amounts)
1267
+
1268
+ elif rot_type == 'BODY':
1269
+ self.orient_body_fixed(parent, amounts, rot_order)
1270
+
1271
+ elif rot_type == 'SPACE':
1272
+ self.orient_space_fixed(parent, amounts, rot_order)
1273
+
1274
+ elif rot_type == 'QUATERNION':
1275
+ self.orient_quaternion(parent, amounts)
1276
+
1277
+ else:
1278
+ raise NotImplementedError('That is not an implemented rotation')
1279
+
1280
+ def orientnew(self, newname, rot_type, amounts, rot_order='',
1281
+ variables=None, indices=None, latexs=None):
1282
+ r"""Returns a new reference frame oriented with respect to this
1283
+ reference frame.
1284
+
1285
+ See ``ReferenceFrame.orient()`` for detailed examples of how to orient
1286
+ reference frames.
1287
+
1288
+ Parameters
1289
+ ==========
1290
+
1291
+ newname : str
1292
+ Name for the new reference frame.
1293
+ rot_type : str
1294
+ The method used to generate the direction cosine matrix. Supported
1295
+ methods are:
1296
+
1297
+ - ``'Axis'``: simple rotations about a single common axis
1298
+ - ``'DCM'``: for setting the direction cosine matrix directly
1299
+ - ``'Body'``: three successive rotations about new intermediate
1300
+ axes, also called "Euler and Tait-Bryan angles"
1301
+ - ``'Space'``: three successive rotations about the parent
1302
+ frames' unit vectors
1303
+ - ``'Quaternion'``: rotations defined by four parameters which
1304
+ result in a singularity free direction cosine matrix
1305
+
1306
+ amounts :
1307
+ Expressions defining the rotation angles or direction cosine
1308
+ matrix. These must match the ``rot_type``. See examples below for
1309
+ details. The input types are:
1310
+
1311
+ - ``'Axis'``: 2-tuple (expr/sym/func, Vector)
1312
+ - ``'DCM'``: Matrix, shape(3,3)
1313
+ - ``'Body'``: 3-tuple of expressions, symbols, or functions
1314
+ - ``'Space'``: 3-tuple of expressions, symbols, or functions
1315
+ - ``'Quaternion'``: 4-tuple of expressions, symbols, or
1316
+ functions
1317
+
1318
+ rot_order : str or int, optional
1319
+ If applicable, the order of the successive of rotations. The string
1320
+ ``'123'`` and integer ``123`` are equivalent, for example. Required
1321
+ for ``'Body'`` and ``'Space'``.
1322
+ indices : tuple of str
1323
+ Enables the reference frame's basis unit vectors to be accessed by
1324
+ Python's square bracket indexing notation using the provided three
1325
+ indice strings and alters the printing of the unit vectors to
1326
+ reflect this choice.
1327
+ latexs : tuple of str
1328
+ Alters the LaTeX printing of the reference frame's basis unit
1329
+ vectors to the provided three valid LaTeX strings.
1330
+
1331
+ Examples
1332
+ ========
1333
+
1334
+ >>> from sympy import symbols
1335
+ >>> from sympy.physics.vector import ReferenceFrame, vlatex
1336
+ >>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
1337
+ >>> N = ReferenceFrame('N')
1338
+
1339
+ Create a new reference frame A rotated relative to N through a simple
1340
+ rotation.
1341
+
1342
+ >>> A = N.orientnew('A', 'Axis', (q0, N.x))
1343
+
1344
+ Create a new reference frame B rotated relative to N through body-fixed
1345
+ rotations.
1346
+
1347
+ >>> B = N.orientnew('B', 'Body', (q1, q2, q3), '123')
1348
+
1349
+ Create a new reference frame C rotated relative to N through a simple
1350
+ rotation with unique indices and LaTeX printing.
1351
+
1352
+ >>> C = N.orientnew('C', 'Axis', (q0, N.x), indices=('1', '2', '3'),
1353
+ ... latexs=(r'\hat{\mathbf{c}}_1',r'\hat{\mathbf{c}}_2',
1354
+ ... r'\hat{\mathbf{c}}_3'))
1355
+ >>> C['1']
1356
+ C['1']
1357
+ >>> print(vlatex(C['1']))
1358
+ \hat{\mathbf{c}}_1
1359
+
1360
+ """
1361
+
1362
+ newframe = self.__class__(newname, variables=variables,
1363
+ indices=indices, latexs=latexs)
1364
+
1365
+ approved_orders = ('123', '231', '312', '132', '213', '321', '121',
1366
+ '131', '212', '232', '313', '323', '')
1367
+ rot_order = translate(str(rot_order), 'XYZxyz', '123123')
1368
+ rot_type = rot_type.upper()
1369
+
1370
+ if rot_order not in approved_orders:
1371
+ raise TypeError('The supplied order is not an approved type')
1372
+
1373
+ if rot_type == 'AXIS':
1374
+ newframe.orient_axis(self, amounts[1], amounts[0])
1375
+
1376
+ elif rot_type == 'DCM':
1377
+ newframe.orient_explicit(self, amounts)
1378
+
1379
+ elif rot_type == 'BODY':
1380
+ newframe.orient_body_fixed(self, amounts, rot_order)
1381
+
1382
+ elif rot_type == 'SPACE':
1383
+ newframe.orient_space_fixed(self, amounts, rot_order)
1384
+
1385
+ elif rot_type == 'QUATERNION':
1386
+ newframe.orient_quaternion(self, amounts)
1387
+
1388
+ else:
1389
+ raise NotImplementedError('That is not an implemented rotation')
1390
+ return newframe
1391
+
1392
+ def set_ang_acc(self, otherframe, value):
1393
+ """Define the angular acceleration Vector in a ReferenceFrame.
1394
+
1395
+ Defines the angular acceleration of this ReferenceFrame, in another.
1396
+ Angular acceleration can be defined with respect to multiple different
1397
+ ReferenceFrames. Care must be taken to not create loops which are
1398
+ inconsistent.
1399
+
1400
+ Parameters
1401
+ ==========
1402
+
1403
+ otherframe : ReferenceFrame
1404
+ A ReferenceFrame to define the angular acceleration in
1405
+ value : Vector
1406
+ The Vector representing angular acceleration
1407
+
1408
+ Examples
1409
+ ========
1410
+
1411
+ >>> from sympy.physics.vector import ReferenceFrame
1412
+ >>> N = ReferenceFrame('N')
1413
+ >>> A = ReferenceFrame('A')
1414
+ >>> V = 10 * N.x
1415
+ >>> A.set_ang_acc(N, V)
1416
+ >>> A.ang_acc_in(N)
1417
+ 10*N.x
1418
+
1419
+ """
1420
+
1421
+ if value == 0:
1422
+ value = Vector(0)
1423
+ value = _check_vector(value)
1424
+ _check_frame(otherframe)
1425
+ self._ang_acc_dict.update({otherframe: value})
1426
+ otherframe._ang_acc_dict.update({self: -value})
1427
+
1428
+ def set_ang_vel(self, otherframe, value):
1429
+ """Define the angular velocity vector in a ReferenceFrame.
1430
+
1431
+ Defines the angular velocity of this ReferenceFrame, in another.
1432
+ Angular velocity can be defined with respect to multiple different
1433
+ ReferenceFrames. Care must be taken to not create loops which are
1434
+ inconsistent.
1435
+
1436
+ Parameters
1437
+ ==========
1438
+
1439
+ otherframe : ReferenceFrame
1440
+ A ReferenceFrame to define the angular velocity in
1441
+ value : Vector
1442
+ The Vector representing angular velocity
1443
+
1444
+ Examples
1445
+ ========
1446
+
1447
+ >>> from sympy.physics.vector import ReferenceFrame
1448
+ >>> N = ReferenceFrame('N')
1449
+ >>> A = ReferenceFrame('A')
1450
+ >>> V = 10 * N.x
1451
+ >>> A.set_ang_vel(N, V)
1452
+ >>> A.ang_vel_in(N)
1453
+ 10*N.x
1454
+
1455
+ """
1456
+
1457
+ if value == 0:
1458
+ value = Vector(0)
1459
+ value = _check_vector(value)
1460
+ _check_frame(otherframe)
1461
+ self._ang_vel_dict.update({otherframe: value})
1462
+ otherframe._ang_vel_dict.update({self: -value})
1463
+
1464
+ @property
1465
+ def x(self):
1466
+ """The basis Vector for the ReferenceFrame, in the x direction. """
1467
+ return self._x
1468
+
1469
+ @property
1470
+ def y(self):
1471
+ """The basis Vector for the ReferenceFrame, in the y direction. """
1472
+ return self._y
1473
+
1474
+ @property
1475
+ def z(self):
1476
+ """The basis Vector for the ReferenceFrame, in the z direction. """
1477
+ return self._z
1478
+
1479
+ @property
1480
+ def xx(self):
1481
+ """Unit dyad of basis Vectors x and x for the ReferenceFrame."""
1482
+ return Vector.outer(self.x, self.x)
1483
+
1484
+ @property
1485
+ def xy(self):
1486
+ """Unit dyad of basis Vectors x and y for the ReferenceFrame."""
1487
+ return Vector.outer(self.x, self.y)
1488
+
1489
+ @property
1490
+ def xz(self):
1491
+ """Unit dyad of basis Vectors x and z for the ReferenceFrame."""
1492
+ return Vector.outer(self.x, self.z)
1493
+
1494
+ @property
1495
+ def yx(self):
1496
+ """Unit dyad of basis Vectors y and x for the ReferenceFrame."""
1497
+ return Vector.outer(self.y, self.x)
1498
+
1499
+ @property
1500
+ def yy(self):
1501
+ """Unit dyad of basis Vectors y and y for the ReferenceFrame."""
1502
+ return Vector.outer(self.y, self.y)
1503
+
1504
+ @property
1505
+ def yz(self):
1506
+ """Unit dyad of basis Vectors y and z for the ReferenceFrame."""
1507
+ return Vector.outer(self.y, self.z)
1508
+
1509
+ @property
1510
+ def zx(self):
1511
+ """Unit dyad of basis Vectors z and x for the ReferenceFrame."""
1512
+ return Vector.outer(self.z, self.x)
1513
+
1514
+ @property
1515
+ def zy(self):
1516
+ """Unit dyad of basis Vectors z and y for the ReferenceFrame."""
1517
+ return Vector.outer(self.z, self.y)
1518
+
1519
+ @property
1520
+ def zz(self):
1521
+ """Unit dyad of basis Vectors z and z for the ReferenceFrame."""
1522
+ return Vector.outer(self.z, self.z)
1523
+
1524
+ @property
1525
+ def u(self):
1526
+ """Unit dyadic for the ReferenceFrame."""
1527
+ return self.xx + self.yy + self.zz
1528
+
1529
+ def partial_velocity(self, frame, *gen_speeds):
1530
+ """Returns the partial angular velocities of this frame in the given
1531
+ frame with respect to one or more provided generalized speeds.
1532
+
1533
+ Parameters
1534
+ ==========
1535
+ frame : ReferenceFrame
1536
+ The frame with which the angular velocity is defined in.
1537
+ gen_speeds : functions of time
1538
+ The generalized speeds.
1539
+
1540
+ Returns
1541
+ =======
1542
+ partial_velocities : tuple of Vector
1543
+ The partial angular velocity vectors corresponding to the provided
1544
+ generalized speeds.
1545
+
1546
+ Examples
1547
+ ========
1548
+
1549
+ >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
1550
+ >>> N = ReferenceFrame('N')
1551
+ >>> A = ReferenceFrame('A')
1552
+ >>> u1, u2 = dynamicsymbols('u1, u2')
1553
+ >>> A.set_ang_vel(N, u1 * A.x + u2 * N.y)
1554
+ >>> A.partial_velocity(N, u1)
1555
+ A.x
1556
+ >>> A.partial_velocity(N, u1, u2)
1557
+ (A.x, N.y)
1558
+
1559
+ """
1560
+
1561
+ from sympy.physics.vector.functions import partial_velocity
1562
+
1563
+ vel = self.ang_vel_in(frame)
1564
+ partials = partial_velocity([vel], gen_speeds, frame)[0]
1565
+
1566
+ if len(partials) == 1:
1567
+ return partials[0]
1568
+ else:
1569
+ return tuple(partials)
1570
+
1571
+
1572
+ def _check_frame(other):
1573
+ from .vector import VectorTypeError
1574
+ if not isinstance(other, ReferenceFrame):
1575
+ raise VectorTypeError(other, ReferenceFrame('A'))
.venv/lib/python3.12/site-packages/sympy/physics/vector/functions.py ADDED
@@ -0,0 +1,650 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import reduce
2
+
3
+ from sympy import (sympify, diff, sin, cos, Matrix, symbols,
4
+ Function, S, Symbol, linear_eq_to_matrix)
5
+ from sympy.integrals.integrals import integrate
6
+ from sympy.simplify.trigsimp import trigsimp
7
+ from .vector import Vector, _check_vector
8
+ from .frame import CoordinateSym, _check_frame
9
+ from .dyadic import Dyadic
10
+ from .printing import vprint, vsprint, vpprint, vlatex, init_vprinting
11
+ from sympy.utilities.iterables import iterable
12
+ from sympy.utilities.misc import translate
13
+
14
+ __all__ = ['cross', 'dot', 'express', 'time_derivative', 'outer',
15
+ 'kinematic_equations', 'get_motion_params', 'partial_velocity',
16
+ 'dynamicsymbols', 'vprint', 'vsprint', 'vpprint', 'vlatex',
17
+ 'init_vprinting']
18
+
19
+
20
+ def cross(vec1, vec2):
21
+ """Cross product convenience wrapper for Vector.cross(): \n"""
22
+ if not isinstance(vec1, (Vector, Dyadic)):
23
+ raise TypeError('Cross product is between two vectors')
24
+ return vec1 ^ vec2
25
+
26
+
27
+ cross.__doc__ += Vector.cross.__doc__ # type: ignore
28
+
29
+
30
+ def dot(vec1, vec2):
31
+ """Dot product convenience wrapper for Vector.dot(): \n"""
32
+ if not isinstance(vec1, (Vector, Dyadic)):
33
+ raise TypeError('Dot product is between two vectors')
34
+ return vec1 & vec2
35
+
36
+
37
+ dot.__doc__ += Vector.dot.__doc__ # type: ignore
38
+
39
+
40
+ def express(expr, frame, frame2=None, variables=False):
41
+ """
42
+ Global function for 'express' functionality.
43
+
44
+ Re-expresses a Vector, scalar(sympyfiable) or Dyadic in given frame.
45
+
46
+ Refer to the local methods of Vector and Dyadic for details.
47
+ If 'variables' is True, then the coordinate variables (CoordinateSym
48
+ instances) of other frames present in the vector/scalar field or
49
+ dyadic expression are also substituted in terms of the base scalars of
50
+ this frame.
51
+
52
+ Parameters
53
+ ==========
54
+
55
+ expr : Vector/Dyadic/scalar(sympyfiable)
56
+ The expression to re-express in ReferenceFrame 'frame'
57
+
58
+ frame: ReferenceFrame
59
+ The reference frame to express expr in
60
+
61
+ frame2 : ReferenceFrame
62
+ The other frame required for re-expression(only for Dyadic expr)
63
+
64
+ variables : boolean
65
+ Specifies whether to substitute the coordinate variables present
66
+ in expr, in terms of those of frame
67
+
68
+ Examples
69
+ ========
70
+
71
+ >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
72
+ >>> from sympy.physics.vector import init_vprinting
73
+ >>> init_vprinting(pretty_print=False)
74
+ >>> N = ReferenceFrame('N')
75
+ >>> q = dynamicsymbols('q')
76
+ >>> B = N.orientnew('B', 'Axis', [q, N.z])
77
+ >>> d = outer(N.x, N.x)
78
+ >>> from sympy.physics.vector import express
79
+ >>> express(d, B, N)
80
+ cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)
81
+ >>> express(B.x, N)
82
+ cos(q)*N.x + sin(q)*N.y
83
+ >>> express(N[0], B, variables=True)
84
+ B_x*cos(q) - B_y*sin(q)
85
+
86
+ """
87
+
88
+ _check_frame(frame)
89
+
90
+ if expr == 0:
91
+ return expr
92
+
93
+ if isinstance(expr, Vector):
94
+ # Given expr is a Vector
95
+ if variables:
96
+ # If variables attribute is True, substitute the coordinate
97
+ # variables in the Vector
98
+ frame_list = [x[-1] for x in expr.args]
99
+ subs_dict = {}
100
+ for f in frame_list:
101
+ subs_dict.update(f.variable_map(frame))
102
+ expr = expr.subs(subs_dict)
103
+ # Re-express in this frame
104
+ outvec = Vector([])
105
+ for v in expr.args:
106
+ if v[1] != frame:
107
+ temp = frame.dcm(v[1]) * v[0]
108
+ if Vector.simp:
109
+ temp = temp.applyfunc(lambda x:
110
+ trigsimp(x, method='fu'))
111
+ outvec += Vector([(temp, frame)])
112
+ else:
113
+ outvec += Vector([v])
114
+ return outvec
115
+
116
+ if isinstance(expr, Dyadic):
117
+ if frame2 is None:
118
+ frame2 = frame
119
+ _check_frame(frame2)
120
+ ol = Dyadic(0)
121
+ for v in expr.args:
122
+ ol += express(v[0], frame, variables=variables) * \
123
+ (express(v[1], frame, variables=variables) |
124
+ express(v[2], frame2, variables=variables))
125
+ return ol
126
+
127
+ else:
128
+ if variables:
129
+ # Given expr is a scalar field
130
+ frame_set = set()
131
+ expr = sympify(expr)
132
+ # Substitute all the coordinate variables
133
+ for x in expr.free_symbols:
134
+ if isinstance(x, CoordinateSym) and x.frame != frame:
135
+ frame_set.add(x.frame)
136
+ subs_dict = {}
137
+ for f in frame_set:
138
+ subs_dict.update(f.variable_map(frame))
139
+ return expr.subs(subs_dict)
140
+ return expr
141
+
142
+
143
+ def time_derivative(expr, frame, order=1):
144
+ """
145
+ Calculate the time derivative of a vector/scalar field function
146
+ or dyadic expression in given frame.
147
+
148
+ References
149
+ ==========
150
+
151
+ https://en.wikipedia.org/wiki/Rotating_reference_frame#Time_derivatives_in_the_two_frames
152
+
153
+ Parameters
154
+ ==========
155
+
156
+ expr : Vector/Dyadic/sympifyable
157
+ The expression whose time derivative is to be calculated
158
+
159
+ frame : ReferenceFrame
160
+ The reference frame to calculate the time derivative in
161
+
162
+ order : integer
163
+ The order of the derivative to be calculated
164
+
165
+ Examples
166
+ ========
167
+
168
+ >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
169
+ >>> from sympy.physics.vector import init_vprinting
170
+ >>> init_vprinting(pretty_print=False)
171
+ >>> from sympy import Symbol
172
+ >>> q1 = Symbol('q1')
173
+ >>> u1 = dynamicsymbols('u1')
174
+ >>> N = ReferenceFrame('N')
175
+ >>> A = N.orientnew('A', 'Axis', [q1, N.x])
176
+ >>> v = u1 * N.x
177
+ >>> A.set_ang_vel(N, 10*A.x)
178
+ >>> from sympy.physics.vector import time_derivative
179
+ >>> time_derivative(v, N)
180
+ u1'*N.x
181
+ >>> time_derivative(u1*A[0], N)
182
+ N_x*u1'
183
+ >>> B = N.orientnew('B', 'Axis', [u1, N.z])
184
+ >>> from sympy.physics.vector import outer
185
+ >>> d = outer(N.x, N.x)
186
+ >>> time_derivative(d, B)
187
+ - u1'*(N.y|N.x) - u1'*(N.x|N.y)
188
+
189
+ """
190
+
191
+ t = dynamicsymbols._t
192
+ _check_frame(frame)
193
+
194
+ if order == 0:
195
+ return expr
196
+ if order % 1 != 0 or order < 0:
197
+ raise ValueError("Unsupported value of order entered")
198
+
199
+ if isinstance(expr, Vector):
200
+ outlist = []
201
+ for v in expr.args:
202
+ if v[1] == frame:
203
+ outlist += [(express(v[0], frame, variables=True).diff(t),
204
+ frame)]
205
+ else:
206
+ outlist += (time_derivative(Vector([v]), v[1]) +
207
+ (v[1].ang_vel_in(frame) ^ Vector([v]))).args
208
+ outvec = Vector(outlist)
209
+ return time_derivative(outvec, frame, order - 1)
210
+
211
+ if isinstance(expr, Dyadic):
212
+ ol = Dyadic(0)
213
+ for v in expr.args:
214
+ ol += (v[0].diff(t) * (v[1] | v[2]))
215
+ ol += (v[0] * (time_derivative(v[1], frame) | v[2]))
216
+ ol += (v[0] * (v[1] | time_derivative(v[2], frame)))
217
+ return time_derivative(ol, frame, order - 1)
218
+
219
+ else:
220
+ return diff(express(expr, frame, variables=True), t, order)
221
+
222
+
223
+ def outer(vec1, vec2):
224
+ """Outer product convenience wrapper for Vector.outer():\n"""
225
+ if not isinstance(vec1, Vector):
226
+ raise TypeError('Outer product is between two Vectors')
227
+ return vec1.outer(vec2)
228
+
229
+
230
+ outer.__doc__ += Vector.outer.__doc__ # type: ignore
231
+
232
+
233
+ def kinematic_equations(speeds, coords, rot_type, rot_order=''):
234
+ """Gives equations relating the qdot's to u's for a rotation type.
235
+
236
+ Supply rotation type and order as in orient. Speeds are assumed to be
237
+ body-fixed; if we are defining the orientation of B in A using by rot_type,
238
+ the angular velocity of B in A is assumed to be in the form: speed[0]*B.x +
239
+ speed[1]*B.y + speed[2]*B.z
240
+
241
+ Parameters
242
+ ==========
243
+
244
+ speeds : list of length 3
245
+ The body fixed angular velocity measure numbers.
246
+ coords : list of length 3 or 4
247
+ The coordinates used to define the orientation of the two frames.
248
+ rot_type : str
249
+ The type of rotation used to create the equations. Body, Space, or
250
+ Quaternion only
251
+ rot_order : str or int
252
+ If applicable, the order of a series of rotations.
253
+
254
+ Examples
255
+ ========
256
+
257
+ >>> from sympy.physics.vector import dynamicsymbols
258
+ >>> from sympy.physics.vector import kinematic_equations, vprint
259
+ >>> u1, u2, u3 = dynamicsymbols('u1 u2 u3')
260
+ >>> q1, q2, q3 = dynamicsymbols('q1 q2 q3')
261
+ >>> vprint(kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313'),
262
+ ... order=None)
263
+ [-(u1*sin(q3) + u2*cos(q3))/sin(q2) + q1', -u1*cos(q3) + u2*sin(q3) + q2', (u1*sin(q3) + u2*cos(q3))*cos(q2)/sin(q2) - u3 + q3']
264
+
265
+ """
266
+
267
+ # Code below is checking and sanitizing input
268
+ approved_orders = ('123', '231', '312', '132', '213', '321', '121', '131',
269
+ '212', '232', '313', '323', '1', '2', '3', '')
270
+ # make sure XYZ => 123 and rot_type is in lower case
271
+ rot_order = translate(str(rot_order), 'XYZxyz', '123123')
272
+ rot_type = rot_type.lower()
273
+
274
+ if not isinstance(speeds, (list, tuple)):
275
+ raise TypeError('Need to supply speeds in a list')
276
+ if len(speeds) != 3:
277
+ raise TypeError('Need to supply 3 body-fixed speeds')
278
+ if not isinstance(coords, (list, tuple)):
279
+ raise TypeError('Need to supply coordinates in a list')
280
+ if rot_type in ['body', 'space']:
281
+ if rot_order not in approved_orders:
282
+ raise ValueError('Not an acceptable rotation order')
283
+ if len(coords) != 3:
284
+ raise ValueError('Need 3 coordinates for body or space')
285
+ # Actual hard-coded kinematic differential equations
286
+ w1, w2, w3 = speeds
287
+ if w1 == w2 == w3 == 0:
288
+ return [S.Zero]*3
289
+ q1, q2, q3 = coords
290
+ q1d, q2d, q3d = [diff(i, dynamicsymbols._t) for i in coords]
291
+ s1, s2, s3 = [sin(q1), sin(q2), sin(q3)]
292
+ c1, c2, c3 = [cos(q1), cos(q2), cos(q3)]
293
+ if rot_type == 'body':
294
+ if rot_order == '123':
295
+ return [q1d - (w1 * c3 - w2 * s3) / c2, q2d - w1 * s3 - w2 *
296
+ c3, q3d - (-w1 * c3 + w2 * s3) * s2 / c2 - w3]
297
+ if rot_order == '231':
298
+ return [q1d - (w2 * c3 - w3 * s3) / c2, q2d - w2 * s3 - w3 *
299
+ c3, q3d - w1 - (- w2 * c3 + w3 * s3) * s2 / c2]
300
+ if rot_order == '312':
301
+ return [q1d - (-w1 * s3 + w3 * c3) / c2, q2d - w1 * c3 - w3 *
302
+ s3, q3d - (w1 * s3 - w3 * c3) * s2 / c2 - w2]
303
+ if rot_order == '132':
304
+ return [q1d - (w1 * c3 + w3 * s3) / c2, q2d + w1 * s3 - w3 *
305
+ c3, q3d - (w1 * c3 + w3 * s3) * s2 / c2 - w2]
306
+ if rot_order == '213':
307
+ return [q1d - (w1 * s3 + w2 * c3) / c2, q2d - w1 * c3 + w2 *
308
+ s3, q3d - (w1 * s3 + w2 * c3) * s2 / c2 - w3]
309
+ if rot_order == '321':
310
+ return [q1d - (w2 * s3 + w3 * c3) / c2, q2d - w2 * c3 + w3 *
311
+ s3, q3d - w1 - (w2 * s3 + w3 * c3) * s2 / c2]
312
+ if rot_order == '121':
313
+ return [q1d - (w2 * s3 + w3 * c3) / s2, q2d - w2 * c3 + w3 *
314
+ s3, q3d - w1 + (w2 * s3 + w3 * c3) * c2 / s2]
315
+ if rot_order == '131':
316
+ return [q1d - (-w2 * c3 + w3 * s3) / s2, q2d - w2 * s3 - w3 *
317
+ c3, q3d - w1 - (w2 * c3 - w3 * s3) * c2 / s2]
318
+ if rot_order == '212':
319
+ return [q1d - (w1 * s3 - w3 * c3) / s2, q2d - w1 * c3 - w3 *
320
+ s3, q3d - (-w1 * s3 + w3 * c3) * c2 / s2 - w2]
321
+ if rot_order == '232':
322
+ return [q1d - (w1 * c3 + w3 * s3) / s2, q2d + w1 * s3 - w3 *
323
+ c3, q3d + (w1 * c3 + w3 * s3) * c2 / s2 - w2]
324
+ if rot_order == '313':
325
+ return [q1d - (w1 * s3 + w2 * c3) / s2, q2d - w1 * c3 + w2 *
326
+ s3, q3d + (w1 * s3 + w2 * c3) * c2 / s2 - w3]
327
+ if rot_order == '323':
328
+ return [q1d - (-w1 * c3 + w2 * s3) / s2, q2d - w1 * s3 - w2 *
329
+ c3, q3d - (w1 * c3 - w2 * s3) * c2 / s2 - w3]
330
+ if rot_type == 'space':
331
+ if rot_order == '123':
332
+ return [q1d - w1 - (w2 * s1 + w3 * c1) * s2 / c2, q2d - w2 *
333
+ c1 + w3 * s1, q3d - (w2 * s1 + w3 * c1) / c2]
334
+ if rot_order == '231':
335
+ return [q1d - (w1 * c1 + w3 * s1) * s2 / c2 - w2, q2d + w1 *
336
+ s1 - w3 * c1, q3d - (w1 * c1 + w3 * s1) / c2]
337
+ if rot_order == '312':
338
+ return [q1d - (w1 * s1 + w2 * c1) * s2 / c2 - w3, q2d - w1 *
339
+ c1 + w2 * s1, q3d - (w1 * s1 + w2 * c1) / c2]
340
+ if rot_order == '132':
341
+ return [q1d - w1 - (-w2 * c1 + w3 * s1) * s2 / c2, q2d - w2 *
342
+ s1 - w3 * c1, q3d - (w2 * c1 - w3 * s1) / c2]
343
+ if rot_order == '213':
344
+ return [q1d - (w1 * s1 - w3 * c1) * s2 / c2 - w2, q2d - w1 *
345
+ c1 - w3 * s1, q3d - (-w1 * s1 + w3 * c1) / c2]
346
+ if rot_order == '321':
347
+ return [q1d - (-w1 * c1 + w2 * s1) * s2 / c2 - w3, q2d - w1 *
348
+ s1 - w2 * c1, q3d - (w1 * c1 - w2 * s1) / c2]
349
+ if rot_order == '121':
350
+ return [q1d - w1 + (w2 * s1 + w3 * c1) * c2 / s2, q2d - w2 *
351
+ c1 + w3 * s1, q3d - (w2 * s1 + w3 * c1) / s2]
352
+ if rot_order == '131':
353
+ return [q1d - w1 - (w2 * c1 - w3 * s1) * c2 / s2, q2d - w2 *
354
+ s1 - w3 * c1, q3d - (-w2 * c1 + w3 * s1) / s2]
355
+ if rot_order == '212':
356
+ return [q1d - (-w1 * s1 + w3 * c1) * c2 / s2 - w2, q2d - w1 *
357
+ c1 - w3 * s1, q3d - (w1 * s1 - w3 * c1) / s2]
358
+ if rot_order == '232':
359
+ return [q1d + (w1 * c1 + w3 * s1) * c2 / s2 - w2, q2d + w1 *
360
+ s1 - w3 * c1, q3d - (w1 * c1 + w3 * s1) / s2]
361
+ if rot_order == '313':
362
+ return [q1d + (w1 * s1 + w2 * c1) * c2 / s2 - w3, q2d - w1 *
363
+ c1 + w2 * s1, q3d - (w1 * s1 + w2 * c1) / s2]
364
+ if rot_order == '323':
365
+ return [q1d - (w1 * c1 - w2 * s1) * c2 / s2 - w3, q2d - w1 *
366
+ s1 - w2 * c1, q3d - (-w1 * c1 + w2 * s1) / s2]
367
+ elif rot_type == 'quaternion':
368
+ if rot_order != '':
369
+ raise ValueError('Cannot have rotation order for quaternion')
370
+ if len(coords) != 4:
371
+ raise ValueError('Need 4 coordinates for quaternion')
372
+ # Actual hard-coded kinematic differential equations
373
+ e0, e1, e2, e3 = coords
374
+ w = Matrix(speeds + [0])
375
+ E = Matrix([[e0, -e3, e2, e1],
376
+ [e3, e0, -e1, e2],
377
+ [-e2, e1, e0, e3],
378
+ [-e1, -e2, -e3, e0]])
379
+ edots = Matrix([diff(i, dynamicsymbols._t) for i in [e1, e2, e3, e0]])
380
+ return list(edots.T - 0.5 * w.T * E.T)
381
+ else:
382
+ raise ValueError('Not an approved rotation type for this function')
383
+
384
+
385
+ def get_motion_params(frame, **kwargs):
386
+ """
387
+ Returns the three motion parameters - (acceleration, velocity, and
388
+ position) as vectorial functions of time in the given frame.
389
+
390
+ If a higher order differential function is provided, the lower order
391
+ functions are used as boundary conditions. For example, given the
392
+ acceleration, the velocity and position parameters are taken as
393
+ boundary conditions.
394
+
395
+ The values of time at which the boundary conditions are specified
396
+ are taken from timevalue1(for position boundary condition) and
397
+ timevalue2(for velocity boundary condition).
398
+
399
+ If any of the boundary conditions are not provided, they are taken
400
+ to be zero by default (zero vectors, in case of vectorial inputs). If
401
+ the boundary conditions are also functions of time, they are converted
402
+ to constants by substituting the time values in the dynamicsymbols._t
403
+ time Symbol.
404
+
405
+ This function can also be used for calculating rotational motion
406
+ parameters. Have a look at the Parameters and Examples for more clarity.
407
+
408
+ Parameters
409
+ ==========
410
+
411
+ frame : ReferenceFrame
412
+ The frame to express the motion parameters in
413
+
414
+ acceleration : Vector
415
+ Acceleration of the object/frame as a function of time
416
+
417
+ velocity : Vector
418
+ Velocity as function of time or as boundary condition
419
+ of velocity at time = timevalue1
420
+
421
+ position : Vector
422
+ Velocity as function of time or as boundary condition
423
+ of velocity at time = timevalue1
424
+
425
+ timevalue1 : sympyfiable
426
+ Value of time for position boundary condition
427
+
428
+ timevalue2 : sympyfiable
429
+ Value of time for velocity boundary condition
430
+
431
+ Examples
432
+ ========
433
+
434
+ >>> from sympy.physics.vector import ReferenceFrame, get_motion_params, dynamicsymbols
435
+ >>> from sympy.physics.vector import init_vprinting
436
+ >>> init_vprinting(pretty_print=False)
437
+ >>> from sympy import symbols
438
+ >>> R = ReferenceFrame('R')
439
+ >>> v1, v2, v3 = dynamicsymbols('v1 v2 v3')
440
+ >>> v = v1*R.x + v2*R.y + v3*R.z
441
+ >>> get_motion_params(R, position = v)
442
+ (v1''*R.x + v2''*R.y + v3''*R.z, v1'*R.x + v2'*R.y + v3'*R.z, v1*R.x + v2*R.y + v3*R.z)
443
+ >>> a, b, c = symbols('a b c')
444
+ >>> v = a*R.x + b*R.y + c*R.z
445
+ >>> get_motion_params(R, velocity = v)
446
+ (0, a*R.x + b*R.y + c*R.z, a*t*R.x + b*t*R.y + c*t*R.z)
447
+ >>> parameters = get_motion_params(R, acceleration = v)
448
+ >>> parameters[1]
449
+ a*t*R.x + b*t*R.y + c*t*R.z
450
+ >>> parameters[2]
451
+ a*t**2/2*R.x + b*t**2/2*R.y + c*t**2/2*R.z
452
+
453
+ """
454
+
455
+ def _process_vector_differential(vectdiff, condition, variable, ordinate,
456
+ frame):
457
+ """
458
+ Helper function for get_motion methods. Finds derivative of vectdiff
459
+ wrt variable, and its integral using the specified boundary condition
460
+ at value of variable = ordinate.
461
+ Returns a tuple of - (derivative, function and integral) wrt vectdiff
462
+
463
+ """
464
+
465
+ # Make sure boundary condition is independent of 'variable'
466
+ if condition != 0:
467
+ condition = express(condition, frame, variables=True)
468
+ # Special case of vectdiff == 0
469
+ if vectdiff == Vector(0):
470
+ return (0, 0, condition)
471
+ # Express vectdiff completely in condition's frame to give vectdiff1
472
+ vectdiff1 = express(vectdiff, frame)
473
+ # Find derivative of vectdiff
474
+ vectdiff2 = time_derivative(vectdiff, frame)
475
+ # Integrate and use boundary condition
476
+ vectdiff0 = Vector(0)
477
+ lims = (variable, ordinate, variable)
478
+ for dim in frame:
479
+ function1 = vectdiff1.dot(dim)
480
+ abscissa = dim.dot(condition).subs({variable: ordinate})
481
+ # Indefinite integral of 'function1' wrt 'variable', using
482
+ # the given initial condition (ordinate, abscissa).
483
+ vectdiff0 += (integrate(function1, lims) + abscissa) * dim
484
+ # Return tuple
485
+ return (vectdiff2, vectdiff, vectdiff0)
486
+
487
+ _check_frame(frame)
488
+ # Decide mode of operation based on user's input
489
+ if 'acceleration' in kwargs:
490
+ mode = 2
491
+ elif 'velocity' in kwargs:
492
+ mode = 1
493
+ else:
494
+ mode = 0
495
+ # All the possible parameters in kwargs
496
+ # Not all are required for every case
497
+ # If not specified, set to default values(may or may not be used in
498
+ # calculations)
499
+ conditions = ['acceleration', 'velocity', 'position',
500
+ 'timevalue', 'timevalue1', 'timevalue2']
501
+ for i, x in enumerate(conditions):
502
+ if x not in kwargs:
503
+ if i < 3:
504
+ kwargs[x] = Vector(0)
505
+ else:
506
+ kwargs[x] = S.Zero
507
+ elif i < 3:
508
+ _check_vector(kwargs[x])
509
+ else:
510
+ kwargs[x] = sympify(kwargs[x])
511
+ if mode == 2:
512
+ vel = _process_vector_differential(kwargs['acceleration'],
513
+ kwargs['velocity'],
514
+ dynamicsymbols._t,
515
+ kwargs['timevalue2'], frame)[2]
516
+ pos = _process_vector_differential(vel, kwargs['position'],
517
+ dynamicsymbols._t,
518
+ kwargs['timevalue1'], frame)[2]
519
+ return (kwargs['acceleration'], vel, pos)
520
+ elif mode == 1:
521
+ return _process_vector_differential(kwargs['velocity'],
522
+ kwargs['position'],
523
+ dynamicsymbols._t,
524
+ kwargs['timevalue1'], frame)
525
+ else:
526
+ vel = time_derivative(kwargs['position'], frame)
527
+ acc = time_derivative(vel, frame)
528
+ return (acc, vel, kwargs['position'])
529
+
530
+
531
+ def partial_velocity(vel_vecs, gen_speeds, frame):
532
+ """Returns a list of partial velocities with respect to the provided
533
+ generalized speeds in the given reference frame for each of the supplied
534
+ velocity vectors.
535
+
536
+ The output is a list of lists. The outer list has a number of elements
537
+ equal to the number of supplied velocity vectors. The inner lists are, for
538
+ each velocity vector, the partial derivatives of that velocity vector with
539
+ respect to the generalized speeds supplied.
540
+
541
+ Parameters
542
+ ==========
543
+
544
+ vel_vecs : iterable
545
+ An iterable of velocity vectors (angular or linear).
546
+ gen_speeds : iterable
547
+ An iterable of generalized speeds.
548
+ frame : ReferenceFrame
549
+ The reference frame that the partial derivatives are going to be taken
550
+ in.
551
+
552
+ Examples
553
+ ========
554
+
555
+ >>> from sympy.physics.vector import Point, ReferenceFrame
556
+ >>> from sympy.physics.vector import dynamicsymbols
557
+ >>> from sympy.physics.vector import partial_velocity
558
+ >>> u = dynamicsymbols('u')
559
+ >>> N = ReferenceFrame('N')
560
+ >>> P = Point('P')
561
+ >>> P.set_vel(N, u * N.x)
562
+ >>> vel_vecs = [P.vel(N)]
563
+ >>> gen_speeds = [u]
564
+ >>> partial_velocity(vel_vecs, gen_speeds, N)
565
+ [[N.x]]
566
+
567
+ """
568
+
569
+ if not iterable(vel_vecs):
570
+ raise TypeError('Velocity vectors must be contained in an iterable.')
571
+
572
+ if not iterable(gen_speeds):
573
+ raise TypeError('Generalized speeds must be contained in an iterable')
574
+
575
+ vec_partials = []
576
+ gen_speeds = list(gen_speeds)
577
+ for vel in vel_vecs:
578
+ partials = [Vector(0) for _ in gen_speeds]
579
+ for components, ref in vel.args:
580
+ mat, _ = linear_eq_to_matrix(components, gen_speeds)
581
+ for i in range(len(gen_speeds)):
582
+ for dim, direction in enumerate(ref):
583
+ if mat[dim, i] != 0:
584
+ partials[i] += direction * mat[dim, i]
585
+
586
+ vec_partials.append(partials)
587
+
588
+ return vec_partials
589
+
590
+
591
+ def dynamicsymbols(names, level=0, **assumptions):
592
+ """Uses symbols and Function for functions of time.
593
+
594
+ Creates a SymPy UndefinedFunction, which is then initialized as a function
595
+ of a variable, the default being Symbol('t').
596
+
597
+ Parameters
598
+ ==========
599
+
600
+ names : str
601
+ Names of the dynamic symbols you want to create; works the same way as
602
+ inputs to symbols
603
+ level : int
604
+ Level of differentiation of the returned function; d/dt once of t,
605
+ twice of t, etc.
606
+ assumptions :
607
+ - real(bool) : This is used to set the dynamicsymbol as real,
608
+ by default is False.
609
+ - positive(bool) : This is used to set the dynamicsymbol as positive,
610
+ by default is False.
611
+ - commutative(bool) : This is used to set the commutative property of
612
+ a dynamicsymbol, by default is True.
613
+ - integer(bool) : This is used to set the dynamicsymbol as integer,
614
+ by default is False.
615
+
616
+ Examples
617
+ ========
618
+
619
+ >>> from sympy.physics.vector import dynamicsymbols
620
+ >>> from sympy import diff, Symbol
621
+ >>> q1 = dynamicsymbols('q1')
622
+ >>> q1
623
+ q1(t)
624
+ >>> q2 = dynamicsymbols('q2', real=True)
625
+ >>> q2.is_real
626
+ True
627
+ >>> q3 = dynamicsymbols('q3', positive=True)
628
+ >>> q3.is_positive
629
+ True
630
+ >>> q4, q5 = dynamicsymbols('q4,q5', commutative=False)
631
+ >>> bool(q4*q5 != q5*q4)
632
+ True
633
+ >>> q6 = dynamicsymbols('q6', integer=True)
634
+ >>> q6.is_integer
635
+ True
636
+ >>> diff(q1, Symbol('t'))
637
+ Derivative(q1(t), t)
638
+
639
+ """
640
+ esses = symbols(names, cls=Function, **assumptions)
641
+ t = dynamicsymbols._t
642
+ if iterable(esses):
643
+ esses = [reduce(diff, [t] * level, e(t)) for e in esses]
644
+ return esses
645
+ else:
646
+ return reduce(diff, [t] * level, esses(t))
647
+
648
+
649
+ dynamicsymbols._t = Symbol('t') # type: ignore
650
+ dynamicsymbols._str = '\'' # type: ignore
.venv/lib/python3.12/site-packages/sympy/physics/vector/point.py ADDED
@@ -0,0 +1,635 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .vector import Vector, _check_vector
2
+ from .frame import _check_frame
3
+ from warnings import warn
4
+ from sympy.utilities.misc import filldedent
5
+
6
+ __all__ = ['Point']
7
+
8
+
9
+ class Point:
10
+ """This object represents a point in a dynamic system.
11
+
12
+ It stores the: position, velocity, and acceleration of a point.
13
+ The position is a vector defined as the vector distance from a parent
14
+ point to this point.
15
+
16
+ Parameters
17
+ ==========
18
+
19
+ name : string
20
+ The display name of the Point
21
+
22
+ Examples
23
+ ========
24
+
25
+ >>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
26
+ >>> from sympy.physics.vector import init_vprinting
27
+ >>> init_vprinting(pretty_print=False)
28
+ >>> N = ReferenceFrame('N')
29
+ >>> O = Point('O')
30
+ >>> P = Point('P')
31
+ >>> u1, u2, u3 = dynamicsymbols('u1 u2 u3')
32
+ >>> O.set_vel(N, u1 * N.x + u2 * N.y + u3 * N.z)
33
+ >>> O.acc(N)
34
+ u1'*N.x + u2'*N.y + u3'*N.z
35
+
36
+ ``symbols()`` can be used to create multiple Points in a single step, for
37
+ example:
38
+
39
+ >>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
40
+ >>> from sympy.physics.vector import init_vprinting
41
+ >>> init_vprinting(pretty_print=False)
42
+ >>> from sympy import symbols
43
+ >>> N = ReferenceFrame('N')
44
+ >>> u1, u2 = dynamicsymbols('u1 u2')
45
+ >>> A, B = symbols('A B', cls=Point)
46
+ >>> type(A)
47
+ <class 'sympy.physics.vector.point.Point'>
48
+ >>> A.set_vel(N, u1 * N.x + u2 * N.y)
49
+ >>> B.set_vel(N, u2 * N.x + u1 * N.y)
50
+ >>> A.acc(N) - B.acc(N)
51
+ (u1' - u2')*N.x + (-u1' + u2')*N.y
52
+
53
+ """
54
+
55
+ def __init__(self, name):
56
+ """Initialization of a Point object. """
57
+ self.name = name
58
+ self._pos_dict = {}
59
+ self._vel_dict = {}
60
+ self._acc_dict = {}
61
+ self._pdlist = [self._pos_dict, self._vel_dict, self._acc_dict]
62
+
63
+ def __str__(self):
64
+ return self.name
65
+
66
+ __repr__ = __str__
67
+
68
+ def _check_point(self, other):
69
+ if not isinstance(other, Point):
70
+ raise TypeError('A Point must be supplied')
71
+
72
+ def _pdict_list(self, other, num):
73
+ """Returns a list of points that gives the shortest path with respect
74
+ to position, velocity, or acceleration from this point to the provided
75
+ point.
76
+
77
+ Parameters
78
+ ==========
79
+ other : Point
80
+ A point that may be related to this point by position, velocity, or
81
+ acceleration.
82
+ num : integer
83
+ 0 for searching the position tree, 1 for searching the velocity
84
+ tree, and 2 for searching the acceleration tree.
85
+
86
+ Returns
87
+ =======
88
+ list of Points
89
+ A sequence of points from self to other.
90
+
91
+ Notes
92
+ =====
93
+
94
+ It is not clear if num = 1 or num = 2 actually works because the keys
95
+ to ``_vel_dict`` and ``_acc_dict`` are :class:`ReferenceFrame` objects
96
+ which do not have the ``_pdlist`` attribute.
97
+
98
+ """
99
+ outlist = [[self]]
100
+ oldlist = [[]]
101
+ while outlist != oldlist:
102
+ oldlist = outlist.copy()
103
+ for v in outlist:
104
+ templist = v[-1]._pdlist[num].keys()
105
+ for v2 in templist:
106
+ if not v.__contains__(v2):
107
+ littletemplist = v + [v2]
108
+ if not outlist.__contains__(littletemplist):
109
+ outlist.append(littletemplist)
110
+ for v in oldlist:
111
+ if v[-1] != other:
112
+ outlist.remove(v)
113
+ outlist.sort(key=len)
114
+ if len(outlist) != 0:
115
+ return outlist[0]
116
+ raise ValueError('No Connecting Path found between ' + other.name +
117
+ ' and ' + self.name)
118
+
119
+ def a1pt_theory(self, otherpoint, outframe, interframe):
120
+ """Sets the acceleration of this point with the 1-point theory.
121
+
122
+ The 1-point theory for point acceleration looks like this:
123
+
124
+ ^N a^P = ^B a^P + ^N a^O + ^N alpha^B x r^OP + ^N omega^B x (^N omega^B
125
+ x r^OP) + 2 ^N omega^B x ^B v^P
126
+
127
+ where O is a point fixed in B, P is a point moving in B, and B is
128
+ rotating in frame N.
129
+
130
+ Parameters
131
+ ==========
132
+
133
+ otherpoint : Point
134
+ The first point of the 1-point theory (O)
135
+ outframe : ReferenceFrame
136
+ The frame we want this point's acceleration defined in (N)
137
+ fixedframe : ReferenceFrame
138
+ The intermediate frame in this calculation (B)
139
+
140
+ Examples
141
+ ========
142
+
143
+ >>> from sympy.physics.vector import Point, ReferenceFrame
144
+ >>> from sympy.physics.vector import dynamicsymbols
145
+ >>> from sympy.physics.vector import init_vprinting
146
+ >>> init_vprinting(pretty_print=False)
147
+ >>> q = dynamicsymbols('q')
148
+ >>> q2 = dynamicsymbols('q2')
149
+ >>> qd = dynamicsymbols('q', 1)
150
+ >>> q2d = dynamicsymbols('q2', 1)
151
+ >>> N = ReferenceFrame('N')
152
+ >>> B = ReferenceFrame('B')
153
+ >>> B.set_ang_vel(N, 5 * B.y)
154
+ >>> O = Point('O')
155
+ >>> P = O.locatenew('P', q * B.x + q2 * B.y)
156
+ >>> P.set_vel(B, qd * B.x + q2d * B.y)
157
+ >>> O.set_vel(N, 0)
158
+ >>> P.a1pt_theory(O, N, B)
159
+ (-25*q + q'')*B.x + q2''*B.y - 10*q'*B.z
160
+
161
+ """
162
+
163
+ _check_frame(outframe)
164
+ _check_frame(interframe)
165
+ self._check_point(otherpoint)
166
+ dist = self.pos_from(otherpoint)
167
+ v = self.vel(interframe)
168
+ a1 = otherpoint.acc(outframe)
169
+ a2 = self.acc(interframe)
170
+ omega = interframe.ang_vel_in(outframe)
171
+ alpha = interframe.ang_acc_in(outframe)
172
+ self.set_acc(outframe, a2 + 2 * (omega.cross(v)) + a1 +
173
+ (alpha.cross(dist)) + (omega.cross(omega.cross(dist))))
174
+ return self.acc(outframe)
175
+
176
+ def a2pt_theory(self, otherpoint, outframe, fixedframe):
177
+ """Sets the acceleration of this point with the 2-point theory.
178
+
179
+ The 2-point theory for point acceleration looks like this:
180
+
181
+ ^N a^P = ^N a^O + ^N alpha^B x r^OP + ^N omega^B x (^N omega^B x r^OP)
182
+
183
+ where O and P are both points fixed in frame B, which is rotating in
184
+ frame N.
185
+
186
+ Parameters
187
+ ==========
188
+
189
+ otherpoint : Point
190
+ The first point of the 2-point theory (O)
191
+ outframe : ReferenceFrame
192
+ The frame we want this point's acceleration defined in (N)
193
+ fixedframe : ReferenceFrame
194
+ The frame in which both points are fixed (B)
195
+
196
+ Examples
197
+ ========
198
+
199
+ >>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
200
+ >>> from sympy.physics.vector import init_vprinting
201
+ >>> init_vprinting(pretty_print=False)
202
+ >>> q = dynamicsymbols('q')
203
+ >>> qd = dynamicsymbols('q', 1)
204
+ >>> N = ReferenceFrame('N')
205
+ >>> B = N.orientnew('B', 'Axis', [q, N.z])
206
+ >>> O = Point('O')
207
+ >>> P = O.locatenew('P', 10 * B.x)
208
+ >>> O.set_vel(N, 5 * N.x)
209
+ >>> P.a2pt_theory(O, N, B)
210
+ - 10*q'**2*B.x + 10*q''*B.y
211
+
212
+ """
213
+
214
+ _check_frame(outframe)
215
+ _check_frame(fixedframe)
216
+ self._check_point(otherpoint)
217
+ dist = self.pos_from(otherpoint)
218
+ a = otherpoint.acc(outframe)
219
+ omega = fixedframe.ang_vel_in(outframe)
220
+ alpha = fixedframe.ang_acc_in(outframe)
221
+ self.set_acc(outframe, a + (alpha.cross(dist)) +
222
+ (omega.cross(omega.cross(dist))))
223
+ return self.acc(outframe)
224
+
225
+ def acc(self, frame):
226
+ """The acceleration Vector of this Point in a ReferenceFrame.
227
+
228
+ Parameters
229
+ ==========
230
+
231
+ frame : ReferenceFrame
232
+ The frame in which the returned acceleration vector will be defined
233
+ in.
234
+
235
+ Examples
236
+ ========
237
+
238
+ >>> from sympy.physics.vector import Point, ReferenceFrame
239
+ >>> N = ReferenceFrame('N')
240
+ >>> p1 = Point('p1')
241
+ >>> p1.set_acc(N, 10 * N.x)
242
+ >>> p1.acc(N)
243
+ 10*N.x
244
+
245
+ """
246
+
247
+ _check_frame(frame)
248
+ if not (frame in self._acc_dict):
249
+ if self.vel(frame) != 0:
250
+ return (self._vel_dict[frame]).dt(frame)
251
+ else:
252
+ return Vector(0)
253
+ return self._acc_dict[frame]
254
+
255
+ def locatenew(self, name, value):
256
+ """Creates a new point with a position defined from this point.
257
+
258
+ Parameters
259
+ ==========
260
+
261
+ name : str
262
+ The name for the new point
263
+ value : Vector
264
+ The position of the new point relative to this point
265
+
266
+ Examples
267
+ ========
268
+
269
+ >>> from sympy.physics.vector import ReferenceFrame, Point
270
+ >>> N = ReferenceFrame('N')
271
+ >>> P1 = Point('P1')
272
+ >>> P2 = P1.locatenew('P2', 10 * N.x)
273
+
274
+ """
275
+
276
+ if not isinstance(name, str):
277
+ raise TypeError('Must supply a valid name')
278
+ if value == 0:
279
+ value = Vector(0)
280
+ value = _check_vector(value)
281
+ p = Point(name)
282
+ p.set_pos(self, value)
283
+ self.set_pos(p, -value)
284
+ return p
285
+
286
+ def pos_from(self, otherpoint):
287
+ """Returns a Vector distance between this Point and the other Point.
288
+
289
+ Parameters
290
+ ==========
291
+
292
+ otherpoint : Point
293
+ The otherpoint we are locating this one relative to
294
+
295
+ Examples
296
+ ========
297
+
298
+ >>> from sympy.physics.vector import Point, ReferenceFrame
299
+ >>> N = ReferenceFrame('N')
300
+ >>> p1 = Point('p1')
301
+ >>> p2 = Point('p2')
302
+ >>> p1.set_pos(p2, 10 * N.x)
303
+ >>> p1.pos_from(p2)
304
+ 10*N.x
305
+
306
+ """
307
+
308
+ outvec = Vector(0)
309
+ plist = self._pdict_list(otherpoint, 0)
310
+ for i in range(len(plist) - 1):
311
+ outvec += plist[i]._pos_dict[plist[i + 1]]
312
+ return outvec
313
+
314
+ def set_acc(self, frame, value):
315
+ """Used to set the acceleration of this Point in a ReferenceFrame.
316
+
317
+ Parameters
318
+ ==========
319
+
320
+ frame : ReferenceFrame
321
+ The frame in which this point's acceleration is defined
322
+ value : Vector
323
+ The vector value of this point's acceleration in the frame
324
+
325
+ Examples
326
+ ========
327
+
328
+ >>> from sympy.physics.vector import Point, ReferenceFrame
329
+ >>> N = ReferenceFrame('N')
330
+ >>> p1 = Point('p1')
331
+ >>> p1.set_acc(N, 10 * N.x)
332
+ >>> p1.acc(N)
333
+ 10*N.x
334
+
335
+ """
336
+
337
+ if value == 0:
338
+ value = Vector(0)
339
+ value = _check_vector(value)
340
+ _check_frame(frame)
341
+ self._acc_dict.update({frame: value})
342
+
343
+ def set_pos(self, otherpoint, value):
344
+ """Used to set the position of this point w.r.t. another point.
345
+
346
+ Parameters
347
+ ==========
348
+
349
+ otherpoint : Point
350
+ The other point which this point's location is defined relative to
351
+ value : Vector
352
+ The vector which defines the location of this point
353
+
354
+ Examples
355
+ ========
356
+
357
+ >>> from sympy.physics.vector import Point, ReferenceFrame
358
+ >>> N = ReferenceFrame('N')
359
+ >>> p1 = Point('p1')
360
+ >>> p2 = Point('p2')
361
+ >>> p1.set_pos(p2, 10 * N.x)
362
+ >>> p1.pos_from(p2)
363
+ 10*N.x
364
+
365
+ """
366
+
367
+ if value == 0:
368
+ value = Vector(0)
369
+ value = _check_vector(value)
370
+ self._check_point(otherpoint)
371
+ self._pos_dict.update({otherpoint: value})
372
+ otherpoint._pos_dict.update({self: -value})
373
+
374
+ def set_vel(self, frame, value):
375
+ """Sets the velocity Vector of this Point in a ReferenceFrame.
376
+
377
+ Parameters
378
+ ==========
379
+
380
+ frame : ReferenceFrame
381
+ The frame in which this point's velocity is defined
382
+ value : Vector
383
+ The vector value of this point's velocity in the frame
384
+
385
+ Examples
386
+ ========
387
+
388
+ >>> from sympy.physics.vector import Point, ReferenceFrame
389
+ >>> N = ReferenceFrame('N')
390
+ >>> p1 = Point('p1')
391
+ >>> p1.set_vel(N, 10 * N.x)
392
+ >>> p1.vel(N)
393
+ 10*N.x
394
+
395
+ """
396
+
397
+ if value == 0:
398
+ value = Vector(0)
399
+ value = _check_vector(value)
400
+ _check_frame(frame)
401
+ self._vel_dict.update({frame: value})
402
+
403
+ def v1pt_theory(self, otherpoint, outframe, interframe):
404
+ """Sets the velocity of this point with the 1-point theory.
405
+
406
+ The 1-point theory for point velocity looks like this:
407
+
408
+ ^N v^P = ^B v^P + ^N v^O + ^N omega^B x r^OP
409
+
410
+ where O is a point fixed in B, P is a point moving in B, and B is
411
+ rotating in frame N.
412
+
413
+ Parameters
414
+ ==========
415
+
416
+ otherpoint : Point
417
+ The first point of the 1-point theory (O)
418
+ outframe : ReferenceFrame
419
+ The frame we want this point's velocity defined in (N)
420
+ interframe : ReferenceFrame
421
+ The intermediate frame in this calculation (B)
422
+
423
+ Examples
424
+ ========
425
+
426
+ >>> from sympy.physics.vector import Point, ReferenceFrame
427
+ >>> from sympy.physics.vector import dynamicsymbols
428
+ >>> from sympy.physics.vector import init_vprinting
429
+ >>> init_vprinting(pretty_print=False)
430
+ >>> q = dynamicsymbols('q')
431
+ >>> q2 = dynamicsymbols('q2')
432
+ >>> qd = dynamicsymbols('q', 1)
433
+ >>> q2d = dynamicsymbols('q2', 1)
434
+ >>> N = ReferenceFrame('N')
435
+ >>> B = ReferenceFrame('B')
436
+ >>> B.set_ang_vel(N, 5 * B.y)
437
+ >>> O = Point('O')
438
+ >>> P = O.locatenew('P', q * B.x + q2 * B.y)
439
+ >>> P.set_vel(B, qd * B.x + q2d * B.y)
440
+ >>> O.set_vel(N, 0)
441
+ >>> P.v1pt_theory(O, N, B)
442
+ q'*B.x + q2'*B.y - 5*q*B.z
443
+
444
+ """
445
+
446
+ _check_frame(outframe)
447
+ _check_frame(interframe)
448
+ self._check_point(otherpoint)
449
+ dist = self.pos_from(otherpoint)
450
+ v1 = self.vel(interframe)
451
+ v2 = otherpoint.vel(outframe)
452
+ omega = interframe.ang_vel_in(outframe)
453
+ self.set_vel(outframe, v1 + v2 + (omega.cross(dist)))
454
+ return self.vel(outframe)
455
+
456
+ def v2pt_theory(self, otherpoint, outframe, fixedframe):
457
+ """Sets the velocity of this point with the 2-point theory.
458
+
459
+ The 2-point theory for point velocity looks like this:
460
+
461
+ ^N v^P = ^N v^O + ^N omega^B x r^OP
462
+
463
+ where O and P are both points fixed in frame B, which is rotating in
464
+ frame N.
465
+
466
+ Parameters
467
+ ==========
468
+
469
+ otherpoint : Point
470
+ The first point of the 2-point theory (O)
471
+ outframe : ReferenceFrame
472
+ The frame we want this point's velocity defined in (N)
473
+ fixedframe : ReferenceFrame
474
+ The frame in which both points are fixed (B)
475
+
476
+ Examples
477
+ ========
478
+
479
+ >>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
480
+ >>> from sympy.physics.vector import init_vprinting
481
+ >>> init_vprinting(pretty_print=False)
482
+ >>> q = dynamicsymbols('q')
483
+ >>> qd = dynamicsymbols('q', 1)
484
+ >>> N = ReferenceFrame('N')
485
+ >>> B = N.orientnew('B', 'Axis', [q, N.z])
486
+ >>> O = Point('O')
487
+ >>> P = O.locatenew('P', 10 * B.x)
488
+ >>> O.set_vel(N, 5 * N.x)
489
+ >>> P.v2pt_theory(O, N, B)
490
+ 5*N.x + 10*q'*B.y
491
+
492
+ """
493
+
494
+ _check_frame(outframe)
495
+ _check_frame(fixedframe)
496
+ self._check_point(otherpoint)
497
+ dist = self.pos_from(otherpoint)
498
+ v = otherpoint.vel(outframe)
499
+ omega = fixedframe.ang_vel_in(outframe)
500
+ self.set_vel(outframe, v + (omega.cross(dist)))
501
+ return self.vel(outframe)
502
+
503
+ def vel(self, frame):
504
+ """The velocity Vector of this Point in the ReferenceFrame.
505
+
506
+ Parameters
507
+ ==========
508
+
509
+ frame : ReferenceFrame
510
+ The frame in which the returned velocity vector will be defined in
511
+
512
+ Examples
513
+ ========
514
+
515
+ >>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
516
+ >>> N = ReferenceFrame('N')
517
+ >>> p1 = Point('p1')
518
+ >>> p1.set_vel(N, 10 * N.x)
519
+ >>> p1.vel(N)
520
+ 10*N.x
521
+
522
+ Velocities will be automatically calculated if possible, otherwise a
523
+ ``ValueError`` will be returned. If it is possible to calculate
524
+ multiple different velocities from the relative points, the points
525
+ defined most directly relative to this point will be used. In the case
526
+ of inconsistent relative positions of points, incorrect velocities may
527
+ be returned. It is up to the user to define prior relative positions
528
+ and velocities of points in a self-consistent way.
529
+
530
+ >>> p = Point('p')
531
+ >>> q = dynamicsymbols('q')
532
+ >>> p.set_vel(N, 10 * N.x)
533
+ >>> p2 = Point('p2')
534
+ >>> p2.set_pos(p, q*N.x)
535
+ >>> p2.vel(N)
536
+ (Derivative(q(t), t) + 10)*N.x
537
+
538
+ """
539
+
540
+ _check_frame(frame)
541
+ if not (frame in self._vel_dict):
542
+ valid_neighbor_found = False
543
+ is_cyclic = False
544
+ visited = []
545
+ queue = [self]
546
+ candidate_neighbor = []
547
+ while queue: # BFS to find nearest point
548
+ node = queue.pop(0)
549
+ if node not in visited:
550
+ visited.append(node)
551
+ for neighbor, neighbor_pos in node._pos_dict.items():
552
+ if neighbor in visited:
553
+ continue
554
+ try:
555
+ # Checks if pos vector is valid
556
+ neighbor_pos.express(frame)
557
+ except ValueError:
558
+ continue
559
+ if neighbor in queue:
560
+ is_cyclic = True
561
+ try:
562
+ # Checks if point has its vel defined in req frame
563
+ neighbor_velocity = neighbor._vel_dict[frame]
564
+ except KeyError:
565
+ queue.append(neighbor)
566
+ continue
567
+ candidate_neighbor.append(neighbor)
568
+ if not valid_neighbor_found:
569
+ self.set_vel(frame, self.pos_from(neighbor).dt(frame) + neighbor_velocity)
570
+ valid_neighbor_found = True
571
+ if is_cyclic:
572
+ warn(filldedent("""
573
+ Kinematic loops are defined among the positions of points. This
574
+ is likely not desired and may cause errors in your calculations.
575
+ """))
576
+ if len(candidate_neighbor) > 1:
577
+ warn(filldedent(f"""
578
+ Velocity of {self.name} automatically calculated based on point
579
+ {candidate_neighbor[0].name} but it is also possible from
580
+ points(s): {str(candidate_neighbor[1:])}. Velocities from these
581
+ points are not necessarily the same. This may cause errors in
582
+ your calculations."""))
583
+ if valid_neighbor_found:
584
+ return self._vel_dict[frame]
585
+ else:
586
+ raise ValueError(filldedent(f"""
587
+ Velocity of point {self.name} has not been defined in
588
+ ReferenceFrame {frame.name}."""))
589
+
590
+ return self._vel_dict[frame]
591
+
592
+ def partial_velocity(self, frame, *gen_speeds):
593
+ """Returns the partial velocities of the linear velocity vector of this
594
+ point in the given frame with respect to one or more provided
595
+ generalized speeds.
596
+
597
+ Parameters
598
+ ==========
599
+ frame : ReferenceFrame
600
+ The frame with which the velocity is defined in.
601
+ gen_speeds : functions of time
602
+ The generalized speeds.
603
+
604
+ Returns
605
+ =======
606
+ partial_velocities : tuple of Vector
607
+ The partial velocity vectors corresponding to the provided
608
+ generalized speeds.
609
+
610
+ Examples
611
+ ========
612
+
613
+ >>> from sympy.physics.vector import ReferenceFrame, Point
614
+ >>> from sympy.physics.vector import dynamicsymbols
615
+ >>> N = ReferenceFrame('N')
616
+ >>> A = ReferenceFrame('A')
617
+ >>> p = Point('p')
618
+ >>> u1, u2 = dynamicsymbols('u1, u2')
619
+ >>> p.set_vel(N, u1 * N.x + u2 * A.y)
620
+ >>> p.partial_velocity(N, u1)
621
+ N.x
622
+ >>> p.partial_velocity(N, u1, u2)
623
+ (N.x, A.y)
624
+
625
+ """
626
+
627
+ from sympy.physics.vector.functions import partial_velocity
628
+
629
+ vel = self.vel(frame)
630
+ partials = partial_velocity([vel], gen_speeds, frame)[0]
631
+
632
+ if len(partials) == 1:
633
+ return partials[0]
634
+ else:
635
+ return tuple(partials)
.venv/lib/python3.12/site-packages/sympy/physics/vector/printing.py ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.function import Derivative
2
+ from sympy.core.function import UndefinedFunction, AppliedUndef
3
+ from sympy.core.symbol import Symbol
4
+ from sympy.interactive.printing import init_printing
5
+ from sympy.printing.latex import LatexPrinter
6
+ from sympy.printing.pretty.pretty import PrettyPrinter
7
+ from sympy.printing.pretty.pretty_symbology import center_accent
8
+ from sympy.printing.str import StrPrinter
9
+ from sympy.printing.precedence import PRECEDENCE
10
+
11
+ __all__ = ['vprint', 'vsstrrepr', 'vsprint', 'vpprint', 'vlatex',
12
+ 'init_vprinting']
13
+
14
+
15
+ class VectorStrPrinter(StrPrinter):
16
+ """String Printer for vector expressions. """
17
+
18
+ def _print_Derivative(self, e):
19
+ from sympy.physics.vector.functions import dynamicsymbols
20
+ t = dynamicsymbols._t
21
+ if (bool(sum(i == t for i in e.variables)) &
22
+ isinstance(type(e.args[0]), UndefinedFunction)):
23
+ ol = str(e.args[0].func)
24
+ for i, v in enumerate(e.variables):
25
+ ol += dynamicsymbols._str
26
+ return ol
27
+ else:
28
+ return StrPrinter().doprint(e)
29
+
30
+ def _print_Function(self, e):
31
+ from sympy.physics.vector.functions import dynamicsymbols
32
+ t = dynamicsymbols._t
33
+ if isinstance(type(e), UndefinedFunction):
34
+ return StrPrinter().doprint(e).replace("(%s)" % t, '')
35
+ return e.func.__name__ + "(%s)" % self.stringify(e.args, ", ")
36
+
37
+
38
+ class VectorStrReprPrinter(VectorStrPrinter):
39
+ """String repr printer for vector expressions."""
40
+ def _print_str(self, s):
41
+ return repr(s)
42
+
43
+
44
+ class VectorLatexPrinter(LatexPrinter):
45
+ """Latex Printer for vector expressions. """
46
+
47
+ def _print_Function(self, expr, exp=None):
48
+ from sympy.physics.vector.functions import dynamicsymbols
49
+ func = expr.func.__name__
50
+ t = dynamicsymbols._t
51
+
52
+ if (hasattr(self, '_print_' + func) and not
53
+ isinstance(type(expr), UndefinedFunction)):
54
+ return getattr(self, '_print_' + func)(expr, exp)
55
+ elif isinstance(type(expr), UndefinedFunction) and (expr.args == (t,)):
56
+ # treat this function like a symbol
57
+ expr = Symbol(func)
58
+ if exp is not None:
59
+ # copied from LatexPrinter._helper_print_standard_power, which
60
+ # we can't call because we only have exp as a string.
61
+ base = self.parenthesize(expr, PRECEDENCE['Pow'])
62
+ base = self.parenthesize_super(base)
63
+ return r"%s^{%s}" % (base, exp)
64
+ else:
65
+ return super()._print(expr)
66
+ else:
67
+ return super()._print_Function(expr, exp)
68
+
69
+ def _print_Derivative(self, der_expr):
70
+ from sympy.physics.vector.functions import dynamicsymbols
71
+ # make sure it is in the right form
72
+ der_expr = der_expr.doit()
73
+ if not isinstance(der_expr, Derivative):
74
+ return r"\left(%s\right)" % self.doprint(der_expr)
75
+
76
+ # check if expr is a dynamicsymbol
77
+ t = dynamicsymbols._t
78
+ expr = der_expr.expr
79
+ red = expr.atoms(AppliedUndef)
80
+ syms = der_expr.variables
81
+ test1 = not all(True for i in red if i.free_symbols == {t})
82
+ test2 = not all(t == i for i in syms)
83
+ if test1 or test2:
84
+ return super()._print_Derivative(der_expr)
85
+
86
+ # done checking
87
+ dots = len(syms)
88
+ base = self._print_Function(expr)
89
+ base_split = base.split('_', 1)
90
+ base = base_split[0]
91
+ if dots == 1:
92
+ base = r"\dot{%s}" % base
93
+ elif dots == 2:
94
+ base = r"\ddot{%s}" % base
95
+ elif dots == 3:
96
+ base = r"\dddot{%s}" % base
97
+ elif dots == 4:
98
+ base = r"\ddddot{%s}" % base
99
+ else: # Fallback to standard printing
100
+ return super()._print_Derivative(der_expr)
101
+ if len(base_split) != 1:
102
+ base += '_' + base_split[1]
103
+ return base
104
+
105
+
106
+ class VectorPrettyPrinter(PrettyPrinter):
107
+ """Pretty Printer for vectorialexpressions. """
108
+
109
+ def _print_Derivative(self, deriv):
110
+ from sympy.physics.vector.functions import dynamicsymbols
111
+ # XXX use U('PARTIAL DIFFERENTIAL') here ?
112
+ t = dynamicsymbols._t
113
+ dot_i = 0
114
+ syms = list(reversed(deriv.variables))
115
+
116
+ while len(syms) > 0:
117
+ if syms[-1] == t:
118
+ syms.pop()
119
+ dot_i += 1
120
+ else:
121
+ return super()._print_Derivative(deriv)
122
+
123
+ if not (isinstance(type(deriv.expr), UndefinedFunction) and
124
+ (deriv.expr.args == (t,))):
125
+ return super()._print_Derivative(deriv)
126
+ else:
127
+ pform = self._print_Function(deriv.expr)
128
+
129
+ # the following condition would happen with some sort of non-standard
130
+ # dynamic symbol I guess, so we'll just print the SymPy way
131
+ if len(pform.picture) > 1:
132
+ return super()._print_Derivative(deriv)
133
+
134
+ # There are only special symbols up to fourth-order derivatives
135
+ if dot_i >= 5:
136
+ return super()._print_Derivative(deriv)
137
+
138
+ # Deal with special symbols
139
+ dots = {0: "",
140
+ 1: "\N{COMBINING DOT ABOVE}",
141
+ 2: "\N{COMBINING DIAERESIS}",
142
+ 3: "\N{COMBINING THREE DOTS ABOVE}",
143
+ 4: "\N{COMBINING FOUR DOTS ABOVE}"}
144
+
145
+ d = pform.__dict__
146
+ # if unicode is false then calculate number of apostrophes needed and
147
+ # add to output
148
+ if not self._use_unicode:
149
+ apostrophes = ""
150
+ for i in range(0, dot_i):
151
+ apostrophes += "'"
152
+ d['picture'][0] += apostrophes + "(t)"
153
+ else:
154
+ d['picture'] = [center_accent(d['picture'][0], dots[dot_i])]
155
+ return pform
156
+
157
+ def _print_Function(self, e):
158
+ from sympy.physics.vector.functions import dynamicsymbols
159
+ t = dynamicsymbols._t
160
+ # XXX works only for applied functions
161
+ func = e.func
162
+ args = e.args
163
+ func_name = func.__name__
164
+ pform = self._print_Symbol(Symbol(func_name))
165
+ # If this function is an Undefined function of t, it is probably a
166
+ # dynamic symbol, so we'll skip the (t). The rest of the code is
167
+ # identical to the normal PrettyPrinter code
168
+ if not (isinstance(func, UndefinedFunction) and (args == (t,))):
169
+ return super()._print_Function(e)
170
+ return pform
171
+
172
+
173
+ def vprint(expr, **settings):
174
+ r"""Function for printing of expressions generated in the
175
+ sympy.physics vector package.
176
+
177
+ Extends SymPy's StrPrinter, takes the same setting accepted by SymPy's
178
+ :func:`~.sstr`, and is equivalent to ``print(sstr(foo))``.
179
+
180
+ Parameters
181
+ ==========
182
+
183
+ expr : valid SymPy object
184
+ SymPy expression to print.
185
+ settings : args
186
+ Same as the settings accepted by SymPy's sstr().
187
+
188
+ Examples
189
+ ========
190
+
191
+ >>> from sympy.physics.vector import vprint, dynamicsymbols
192
+ >>> u1 = dynamicsymbols('u1')
193
+ >>> print(u1)
194
+ u1(t)
195
+ >>> vprint(u1)
196
+ u1
197
+
198
+ """
199
+
200
+ outstr = vsprint(expr, **settings)
201
+
202
+ import builtins
203
+ if (outstr != 'None'):
204
+ builtins._ = outstr
205
+ print(outstr)
206
+
207
+
208
+ def vsstrrepr(expr, **settings):
209
+ """Function for displaying expression representation's with vector
210
+ printing enabled.
211
+
212
+ Parameters
213
+ ==========
214
+
215
+ expr : valid SymPy object
216
+ SymPy expression to print.
217
+ settings : args
218
+ Same as the settings accepted by SymPy's sstrrepr().
219
+
220
+ """
221
+ p = VectorStrReprPrinter(settings)
222
+ return p.doprint(expr)
223
+
224
+
225
+ def vsprint(expr, **settings):
226
+ r"""Function for displaying expressions generated in the
227
+ sympy.physics vector package.
228
+
229
+ Returns the output of vprint() as a string.
230
+
231
+ Parameters
232
+ ==========
233
+
234
+ expr : valid SymPy object
235
+ SymPy expression to print
236
+ settings : args
237
+ Same as the settings accepted by SymPy's sstr().
238
+
239
+ Examples
240
+ ========
241
+
242
+ >>> from sympy.physics.vector import vsprint, dynamicsymbols
243
+ >>> u1, u2 = dynamicsymbols('u1 u2')
244
+ >>> u2d = dynamicsymbols('u2', level=1)
245
+ >>> print("%s = %s" % (u1, u2 + u2d))
246
+ u1(t) = u2(t) + Derivative(u2(t), t)
247
+ >>> print("%s = %s" % (vsprint(u1), vsprint(u2 + u2d)))
248
+ u1 = u2 + u2'
249
+
250
+ """
251
+
252
+ string_printer = VectorStrPrinter(settings)
253
+ return string_printer.doprint(expr)
254
+
255
+
256
+ def vpprint(expr, **settings):
257
+ r"""Function for pretty printing of expressions generated in the
258
+ sympy.physics vector package.
259
+
260
+ Mainly used for expressions not inside a vector; the output of running
261
+ scripts and generating equations of motion. Takes the same options as
262
+ SymPy's :func:`~.pretty_print`; see that function for more information.
263
+
264
+ Parameters
265
+ ==========
266
+
267
+ expr : valid SymPy object
268
+ SymPy expression to pretty print
269
+ settings : args
270
+ Same as those accepted by SymPy's pretty_print.
271
+
272
+
273
+ """
274
+
275
+ pp = VectorPrettyPrinter(settings)
276
+
277
+ # Note that this is copied from sympy.printing.pretty.pretty_print:
278
+
279
+ # XXX: this is an ugly hack, but at least it works
280
+ use_unicode = pp._settings['use_unicode']
281
+ from sympy.printing.pretty.pretty_symbology import pretty_use_unicode
282
+ uflag = pretty_use_unicode(use_unicode)
283
+
284
+ try:
285
+ return pp.doprint(expr)
286
+ finally:
287
+ pretty_use_unicode(uflag)
288
+
289
+
290
+ def vlatex(expr, **settings):
291
+ r"""Function for printing latex representation of sympy.physics.vector
292
+ objects.
293
+
294
+ For latex representation of Vectors, Dyadics, and dynamicsymbols. Takes the
295
+ same options as SymPy's :func:`~.latex`; see that function for more
296
+ information;
297
+
298
+ Parameters
299
+ ==========
300
+
301
+ expr : valid SymPy object
302
+ SymPy expression to represent in LaTeX form
303
+ settings : args
304
+ Same as latex()
305
+
306
+ Examples
307
+ ========
308
+
309
+ >>> from sympy.physics.vector import vlatex, ReferenceFrame, dynamicsymbols
310
+ >>> N = ReferenceFrame('N')
311
+ >>> q1, q2 = dynamicsymbols('q1 q2')
312
+ >>> q1d, q2d = dynamicsymbols('q1 q2', 1)
313
+ >>> q1dd, q2dd = dynamicsymbols('q1 q2', 2)
314
+ >>> vlatex(N.x + N.y)
315
+ '\\mathbf{\\hat{n}_x} + \\mathbf{\\hat{n}_y}'
316
+ >>> vlatex(q1 + q2)
317
+ 'q_{1} + q_{2}'
318
+ >>> vlatex(q1d)
319
+ '\\dot{q}_{1}'
320
+ >>> vlatex(q1 * q2d)
321
+ 'q_{1} \\dot{q}_{2}'
322
+ >>> vlatex(q1dd * q1 / q1d)
323
+ '\\frac{q_{1} \\ddot{q}_{1}}{\\dot{q}_{1}}'
324
+
325
+ """
326
+ latex_printer = VectorLatexPrinter(settings)
327
+
328
+ return latex_printer.doprint(expr)
329
+
330
+
331
+ def init_vprinting(**kwargs):
332
+ """Initializes time derivative printing for all SymPy objects, i.e. any
333
+ functions of time will be displayed in a more compact notation. The main
334
+ benefit of this is for printing of time derivatives; instead of
335
+ displaying as ``Derivative(f(t),t)``, it will display ``f'``. This is
336
+ only actually needed for when derivatives are present and are not in a
337
+ physics.vector.Vector or physics.vector.Dyadic object. This function is a
338
+ light wrapper to :func:`~.init_printing`. Any keyword
339
+ arguments for it are valid here.
340
+
341
+ {0}
342
+
343
+ Examples
344
+ ========
345
+
346
+ >>> from sympy import Function, symbols
347
+ >>> t, x = symbols('t, x')
348
+ >>> omega = Function('omega')
349
+ >>> omega(x).diff()
350
+ Derivative(omega(x), x)
351
+ >>> omega(t).diff()
352
+ Derivative(omega(t), t)
353
+
354
+ Now use the string printer:
355
+
356
+ >>> from sympy.physics.vector import init_vprinting
357
+ >>> init_vprinting(pretty_print=False)
358
+ >>> omega(x).diff()
359
+ Derivative(omega(x), x)
360
+ >>> omega(t).diff()
361
+ omega'
362
+
363
+ """
364
+ kwargs['str_printer'] = vsstrrepr
365
+ kwargs['pretty_printer'] = vpprint
366
+ kwargs['latex_printer'] = vlatex
367
+ init_printing(**kwargs)
368
+
369
+
370
+ params = init_printing.__doc__.split('Examples\n ========')[0] # type: ignore
371
+ init_vprinting.__doc__ = init_vprinting.__doc__.format(params) # type: ignore
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__init__.py ADDED
File without changes
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (201 Bytes). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_dyadic.cpython-312.pyc ADDED
Binary file (11.6 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_fieldfunctions.cpython-312.pyc ADDED
Binary file (16.5 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_frame.cpython-312.pyc ADDED
Binary file (68.1 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_functions.cpython-312.pyc ADDED
Binary file (62.9 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_output.cpython-312.pyc ADDED
Binary file (8.58 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_point.cpython-312.pyc ADDED
Binary file (30.3 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_printing.cpython-312.pyc ADDED
Binary file (17.3 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_vector.cpython-312.pyc ADDED
Binary file (26.7 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_dyadic.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import (Float, pi)
2
+ from sympy.core.symbol import symbols
3
+ from sympy.functions.elementary.trigonometric import (cos, sin)
4
+ from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
5
+ from sympy.physics.vector import ReferenceFrame, dynamicsymbols, outer
6
+ from sympy.physics.vector.dyadic import _check_dyadic
7
+ from sympy.testing.pytest import raises
8
+
9
+ A = ReferenceFrame('A')
10
+
11
+
12
+ def test_dyadic():
13
+ d1 = A.x | A.x
14
+ d2 = A.y | A.y
15
+ d3 = A.x | A.y
16
+ assert d1 * 0 == 0
17
+ assert d1 != 0
18
+ assert d1 * 2 == 2 * A.x | A.x
19
+ assert d1 / 2. == 0.5 * d1
20
+ assert d1 & (0 * d1) == 0
21
+ assert d1 & d2 == 0
22
+ assert d1 & A.x == A.x
23
+ assert d1 ^ A.x == 0
24
+ assert d1 ^ A.y == A.x | A.z
25
+ assert d1 ^ A.z == - A.x | A.y
26
+ assert d2 ^ A.x == - A.y | A.z
27
+ assert A.x ^ d1 == 0
28
+ assert A.y ^ d1 == - A.z | A.x
29
+ assert A.z ^ d1 == A.y | A.x
30
+ assert A.x & d1 == A.x
31
+ assert A.y & d1 == 0
32
+ assert A.y & d2 == A.y
33
+ assert d1 & d3 == A.x | A.y
34
+ assert d3 & d1 == 0
35
+ assert d1.dt(A) == 0
36
+ q = dynamicsymbols('q')
37
+ qd = dynamicsymbols('q', 1)
38
+ B = A.orientnew('B', 'Axis', [q, A.z])
39
+ assert d1.express(B) == d1.express(B, B)
40
+ assert d1.express(B) == ((cos(q)**2) * (B.x | B.x) + (-sin(q) * cos(q)) *
41
+ (B.x | B.y) + (-sin(q) * cos(q)) * (B.y | B.x) + (sin(q)**2) *
42
+ (B.y | B.y))
43
+ assert d1.express(B, A) == (cos(q)) * (B.x | A.x) + (-sin(q)) * (B.y | A.x)
44
+ assert d1.express(A, B) == (cos(q)) * (A.x | B.x) + (-sin(q)) * (A.x | B.y)
45
+ assert d1.dt(B) == (-qd) * (A.y | A.x) + (-qd) * (A.x | A.y)
46
+
47
+ assert d1.to_matrix(A) == Matrix([[1, 0, 0], [0, 0, 0], [0, 0, 0]])
48
+ assert d1.to_matrix(A, B) == Matrix([[cos(q), -sin(q), 0],
49
+ [0, 0, 0],
50
+ [0, 0, 0]])
51
+ assert d3.to_matrix(A) == Matrix([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
52
+ a, b, c, d, e, f = symbols('a, b, c, d, e, f')
53
+ v1 = a * A.x + b * A.y + c * A.z
54
+ v2 = d * A.x + e * A.y + f * A.z
55
+ d4 = v1.outer(v2)
56
+ assert d4.to_matrix(A) == Matrix([[a * d, a * e, a * f],
57
+ [b * d, b * e, b * f],
58
+ [c * d, c * e, c * f]])
59
+ d5 = v1.outer(v1)
60
+ C = A.orientnew('C', 'Axis', [q, A.x])
61
+ for expected, actual in zip(C.dcm(A) * d5.to_matrix(A) * C.dcm(A).T,
62
+ d5.to_matrix(C)):
63
+ assert (expected - actual).simplify() == 0
64
+
65
+ raises(TypeError, lambda: d1.applyfunc(0))
66
+
67
+
68
+ def test_dyadic_simplify():
69
+ x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A')
70
+ N = ReferenceFrame('N')
71
+
72
+ dy = N.x | N.x
73
+ test1 = (1 / x + 1 / y) * dy
74
+ assert (N.x & test1 & N.x) != (x + y) / (x * y)
75
+ test1 = test1.simplify()
76
+ assert (N.x & test1 & N.x) == (x + y) / (x * y)
77
+
78
+ test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * dy
79
+ test2 = test2.simplify()
80
+ assert (N.x & test2 & N.x) == (A**2 * s**4 / (4 * pi * k * m**3))
81
+
82
+ test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * dy
83
+ test3 = test3.simplify()
84
+ assert (N.x & test3 & N.x) == 0
85
+
86
+ test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * dy
87
+ test4 = test4.simplify()
88
+ assert (N.x & test4 & N.x) == -2 * y
89
+
90
+
91
+ def test_dyadic_subs():
92
+ N = ReferenceFrame('N')
93
+ s = symbols('s')
94
+ a = s*(N.x | N.x)
95
+ assert a.subs({s: 2}) == 2*(N.x | N.x)
96
+
97
+
98
+ def test_check_dyadic():
99
+ raises(TypeError, lambda: _check_dyadic(0))
100
+
101
+
102
+ def test_dyadic_evalf():
103
+ N = ReferenceFrame('N')
104
+ a = pi * (N.x | N.x)
105
+ assert a.evalf(3) == Float('3.1416', 3) * (N.x | N.x)
106
+ s = symbols('s')
107
+ a = 5 * s * pi* (N.x | N.x)
108
+ assert a.evalf(2) == Float('5', 2) * Float('3.1416', 2) * s * (N.x | N.x)
109
+ assert a.evalf(9, subs={s: 5.124}) == Float('80.48760378', 9) * (N.x | N.x)
110
+
111
+
112
+ def test_dyadic_xreplace():
113
+ x, y, z = symbols('x y z')
114
+ N = ReferenceFrame('N')
115
+ D = outer(N.x, N.x)
116
+ v = x*y * D
117
+ assert v.xreplace({x : cos(x)}) == cos(x)*y * D
118
+ assert v.xreplace({x*y : pi}) == pi * D
119
+ v = (x*y)**z * D
120
+ assert v.xreplace({(x*y)**z : 1}) == D
121
+ assert v.xreplace({x:1, z:0}) == D
122
+ raises(TypeError, lambda: v.xreplace())
123
+ raises(TypeError, lambda: v.xreplace([x, y]))
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_fieldfunctions.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.singleton import S
2
+ from sympy.core.symbol import Symbol
3
+ from sympy.functions.elementary.trigonometric import (cos, sin)
4
+ from sympy.physics.vector import ReferenceFrame, Vector, Point, \
5
+ dynamicsymbols
6
+ from sympy.physics.vector.fieldfunctions import divergence, \
7
+ gradient, curl, is_conservative, is_solenoidal, \
8
+ scalar_potential, scalar_potential_difference
9
+ from sympy.testing.pytest import raises
10
+
11
+ R = ReferenceFrame('R')
12
+ q = dynamicsymbols('q')
13
+ P = R.orientnew('P', 'Axis', [q, R.z])
14
+
15
+
16
+ def test_curl():
17
+ assert curl(Vector(0), R) == Vector(0)
18
+ assert curl(R.x, R) == Vector(0)
19
+ assert curl(2*R[1]**2*R.y, R) == Vector(0)
20
+ assert curl(R[0]*R[1]*R.z, R) == R[0]*R.x - R[1]*R.y
21
+ assert curl(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
22
+ (-R[0]*R[1] + R[0]*R[2])*R.x + (R[0]*R[1] - R[1]*R[2])*R.y + \
23
+ (-R[0]*R[2] + R[1]*R[2])*R.z
24
+ assert curl(2*R[0]**2*R.y, R) == 4*R[0]*R.z
25
+ assert curl(P[0]**2*R.x + P.y, R) == \
26
+ - 2*(R[0]*cos(q) + R[1]*sin(q))*sin(q)*R.z
27
+ assert curl(P[0]*R.y, P) == cos(q)*P.z
28
+
29
+
30
+ def test_divergence():
31
+ assert divergence(Vector(0), R) is S.Zero
32
+ assert divergence(R.x, R) is S.Zero
33
+ assert divergence(R[0]**2*R.x, R) == 2*R[0]
34
+ assert divergence(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
35
+ R[0]*R[1] + R[0]*R[2] + R[1]*R[2]
36
+ assert divergence((1/(R[0]*R[1]*R[2])) * (R.x+R.y+R.z), R) == \
37
+ -1/(R[0]*R[1]*R[2]**2) - 1/(R[0]*R[1]**2*R[2]) - \
38
+ 1/(R[0]**2*R[1]*R[2])
39
+ v = P[0]*P.x + P[1]*P.y + P[2]*P.z
40
+ assert divergence(v, P) == 3
41
+ assert divergence(v, R).simplify() == 3
42
+ assert divergence(P[0]*R.x + R[0]*P.x, R) == 2*cos(q)
43
+
44
+
45
+ def test_gradient():
46
+ a = Symbol('a')
47
+ assert gradient(0, R) == Vector(0)
48
+ assert gradient(R[0], R) == R.x
49
+ assert gradient(R[0]*R[1]*R[2], R) == \
50
+ R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
51
+ assert gradient(2*R[0]**2, R) == 4*R[0]*R.x
52
+ assert gradient(a*sin(R[1])/R[0], R) == \
53
+ - a*sin(R[1])/R[0]**2*R.x + a*cos(R[1])/R[0]*R.y
54
+ assert gradient(P[0]*P[1], R) == \
55
+ ((-R[0]*sin(q) + R[1]*cos(q))*cos(q) - (R[0]*cos(q) + R[1]*sin(q))*sin(q))*R.x + \
56
+ ((-R[0]*sin(q) + R[1]*cos(q))*sin(q) + (R[0]*cos(q) + R[1]*sin(q))*cos(q))*R.y
57
+ assert gradient(P[0]*R[2], P) == P[2]*P.x + P[0]*P.z
58
+
59
+
60
+ scalar_field = 2*R[0]**2*R[1]*R[2]
61
+ grad_field = gradient(scalar_field, R)
62
+ vector_field = R[1]**2*R.x + 3*R[0]*R.y + 5*R[1]*R[2]*R.z
63
+ curl_field = curl(vector_field, R)
64
+
65
+
66
+ def test_conservative():
67
+ assert is_conservative(0) is True
68
+ assert is_conservative(R.x) is True
69
+ assert is_conservative(2 * R.x + 3 * R.y + 4 * R.z) is True
70
+ assert is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
71
+ True
72
+ assert is_conservative(R[0] * R.y) is False
73
+ assert is_conservative(grad_field) is True
74
+ assert is_conservative(curl_field) is False
75
+ assert is_conservative(4*R[0]*R[1]*R[2]*R.x + 2*R[0]**2*R[2]*R.y) is \
76
+ False
77
+ assert is_conservative(R[2]*P.x + P[0]*R.z) is True
78
+
79
+
80
+ def test_solenoidal():
81
+ assert is_solenoidal(0) is True
82
+ assert is_solenoidal(R.x) is True
83
+ assert is_solenoidal(2 * R.x + 3 * R.y + 4 * R.z) is True
84
+ assert is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
85
+ True
86
+ assert is_solenoidal(R[1] * R.y) is False
87
+ assert is_solenoidal(grad_field) is False
88
+ assert is_solenoidal(curl_field) is True
89
+ assert is_solenoidal((-2*R[1] + 3)*R.z) is True
90
+ assert is_solenoidal(cos(q)*R.x + sin(q)*R.y + cos(q)*P.z) is True
91
+ assert is_solenoidal(R[2]*P.x + P[0]*R.z) is True
92
+
93
+
94
+ def test_scalar_potential():
95
+ assert scalar_potential(0, R) == 0
96
+ assert scalar_potential(R.x, R) == R[0]
97
+ assert scalar_potential(R.y, R) == R[1]
98
+ assert scalar_potential(R.z, R) == R[2]
99
+ assert scalar_potential(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
100
+ R[0]*R[1]*R.z, R) == R[0]*R[1]*R[2]
101
+ assert scalar_potential(grad_field, R) == scalar_field
102
+ assert scalar_potential(R[2]*P.x + P[0]*R.z, R) == \
103
+ R[0]*R[2]*cos(q) + R[1]*R[2]*sin(q)
104
+ assert scalar_potential(R[2]*P.x + P[0]*R.z, P) == P[0]*P[2]
105
+ raises(ValueError, lambda: scalar_potential(R[0] * R.y, R))
106
+
107
+
108
+ def test_scalar_potential_difference():
109
+ origin = Point('O')
110
+ point1 = origin.locatenew('P1', 1*R.x + 2*R.y + 3*R.z)
111
+ point2 = origin.locatenew('P2', 4*R.x + 5*R.y + 6*R.z)
112
+ genericpointR = origin.locatenew('RP', R[0]*R.x + R[1]*R.y + R[2]*R.z)
113
+ genericpointP = origin.locatenew('PP', P[0]*P.x + P[1]*P.y + P[2]*P.z)
114
+ assert scalar_potential_difference(S.Zero, R, point1, point2, \
115
+ origin) == 0
116
+ assert scalar_potential_difference(scalar_field, R, origin, \
117
+ genericpointR, origin) == \
118
+ scalar_field
119
+ assert scalar_potential_difference(grad_field, R, origin, \
120
+ genericpointR, origin) == \
121
+ scalar_field
122
+ assert scalar_potential_difference(grad_field, R, point1, point2,
123
+ origin) == 948
124
+ assert scalar_potential_difference(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
125
+ R[0]*R[1]*R.z, R, point1,
126
+ genericpointR, origin) == \
127
+ R[0]*R[1]*R[2] - 6
128
+ potential_diff_P = 2*P[2]*(P[0]*sin(q) + P[1]*cos(q))*\
129
+ (P[0]*cos(q) - P[1]*sin(q))**2
130
+ assert scalar_potential_difference(grad_field, P, origin, \
131
+ genericpointP, \
132
+ origin).simplify() == \
133
+ potential_diff_P
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_frame.py ADDED
@@ -0,0 +1,761 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import pi
2
+ from sympy.core.symbol import symbols
3
+ from sympy.simplify import trigsimp
4
+ from sympy.functions.elementary.trigonometric import (cos, sin)
5
+ from sympy.matrices.dense import (eye, zeros)
6
+ from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
7
+ from sympy.simplify.simplify import simplify
8
+ from sympy.physics.vector import (ReferenceFrame, Vector, CoordinateSym,
9
+ dynamicsymbols, time_derivative, express,
10
+ dot)
11
+ from sympy.physics.vector.frame import _check_frame
12
+ from sympy.physics.vector.vector import VectorTypeError
13
+ from sympy.testing.pytest import raises
14
+ import warnings
15
+ import pickle
16
+
17
+
18
+ def test_dict_list():
19
+
20
+ A = ReferenceFrame('A')
21
+ B = ReferenceFrame('B')
22
+ C = ReferenceFrame('C')
23
+ D = ReferenceFrame('D')
24
+ E = ReferenceFrame('E')
25
+ F = ReferenceFrame('F')
26
+
27
+ B.orient_axis(A, A.x, 1.0)
28
+ C.orient_axis(B, B.x, 1.0)
29
+ D.orient_axis(C, C.x, 1.0)
30
+
31
+ assert D._dict_list(A, 0) == [D, C, B, A]
32
+
33
+ E.orient_axis(D, D.x, 1.0)
34
+
35
+ assert C._dict_list(A, 0) == [C, B, A]
36
+ assert C._dict_list(E, 0) == [C, D, E]
37
+
38
+ # only 0, 1, 2 permitted for second argument
39
+ raises(ValueError, lambda: C._dict_list(E, 5))
40
+ # no connecting path
41
+ raises(ValueError, lambda: F._dict_list(A, 0))
42
+
43
+
44
+ def test_coordinate_vars():
45
+ """Tests the coordinate variables functionality"""
46
+ A = ReferenceFrame('A')
47
+ assert CoordinateSym('Ax', A, 0) == A[0]
48
+ assert CoordinateSym('Ax', A, 1) == A[1]
49
+ assert CoordinateSym('Ax', A, 2) == A[2]
50
+ raises(ValueError, lambda: CoordinateSym('Ax', A, 3))
51
+ q = dynamicsymbols('q')
52
+ qd = dynamicsymbols('q', 1)
53
+ assert isinstance(A[0], CoordinateSym) and \
54
+ isinstance(A[0], CoordinateSym) and \
55
+ isinstance(A[0], CoordinateSym)
56
+ assert A.variable_map(A) == {A[0]:A[0], A[1]:A[1], A[2]:A[2]}
57
+ assert A[0].frame == A
58
+ B = A.orientnew('B', 'Axis', [q, A.z])
59
+ assert B.variable_map(A) == {B[2]: A[2], B[1]: -A[0]*sin(q) + A[1]*cos(q),
60
+ B[0]: A[0]*cos(q) + A[1]*sin(q)}
61
+ assert A.variable_map(B) == {A[0]: B[0]*cos(q) - B[1]*sin(q),
62
+ A[1]: B[0]*sin(q) + B[1]*cos(q), A[2]: B[2]}
63
+ assert time_derivative(B[0], A) == -A[0]*sin(q)*qd + A[1]*cos(q)*qd
64
+ assert time_derivative(B[1], A) == -A[0]*cos(q)*qd - A[1]*sin(q)*qd
65
+ assert time_derivative(B[2], A) == 0
66
+ assert express(B[0], A, variables=True) == A[0]*cos(q) + A[1]*sin(q)
67
+ assert express(B[1], A, variables=True) == -A[0]*sin(q) + A[1]*cos(q)
68
+ assert express(B[2], A, variables=True) == A[2]
69
+ assert time_derivative(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == A[1]*qd*A.x - A[0]*qd*A.y
70
+ assert time_derivative(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == - B[1]*qd*B.x + B[0]*qd*B.y
71
+ assert express(B[0]*B[1]*B[2], A, variables=True) == \
72
+ A[2]*(-A[0]*sin(q) + A[1]*cos(q))*(A[0]*cos(q) + A[1]*sin(q))
73
+ assert (time_derivative(B[0]*B[1]*B[2], A) -
74
+ (A[2]*(-A[0]**2*cos(2*q) -
75
+ 2*A[0]*A[1]*sin(2*q) +
76
+ A[1]**2*cos(2*q))*qd)).trigsimp() == 0
77
+ assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == \
78
+ (B[0]*cos(q) - B[1]*sin(q))*A.x + (B[0]*sin(q) + \
79
+ B[1]*cos(q))*A.y + B[2]*A.z
80
+ assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A,
81
+ variables=True).simplify() == A[0]*A.x + A[1]*A.y + A[2]*A.z
82
+ assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == \
83
+ (A[0]*cos(q) + A[1]*sin(q))*B.x + \
84
+ (-A[0]*sin(q) + A[1]*cos(q))*B.y + A[2]*B.z
85
+ assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B,
86
+ variables=True).simplify() == B[0]*B.x + B[1]*B.y + B[2]*B.z
87
+ N = B.orientnew('N', 'Axis', [-q, B.z])
88
+ assert ({k: v.simplify() for k, v in N.variable_map(A).items()} ==
89
+ {N[0]: A[0], N[2]: A[2], N[1]: A[1]})
90
+ C = A.orientnew('C', 'Axis', [q, A.x + A.y + A.z])
91
+ mapping = A.variable_map(C)
92
+ assert trigsimp(mapping[A[0]]) == (2*C[0]*cos(q)/3 + C[0]/3 -
93
+ 2*C[1]*sin(q + pi/6)/3 +
94
+ C[1]/3 - 2*C[2]*cos(q + pi/3)/3 +
95
+ C[2]/3)
96
+ assert trigsimp(mapping[A[1]]) == -2*C[0]*cos(q + pi/3)/3 + \
97
+ C[0]/3 + 2*C[1]*cos(q)/3 + C[1]/3 - 2*C[2]*sin(q + pi/6)/3 + C[2]/3
98
+ assert trigsimp(mapping[A[2]]) == -2*C[0]*sin(q + pi/6)/3 + C[0]/3 - \
99
+ 2*C[1]*cos(q + pi/3)/3 + C[1]/3 + 2*C[2]*cos(q)/3 + C[2]/3
100
+
101
+
102
+ def test_ang_vel():
103
+ q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
104
+ q1d, q2d, q3d, q4d = dynamicsymbols('q1 q2 q3 q4', 1)
105
+ N = ReferenceFrame('N')
106
+ A = N.orientnew('A', 'Axis', [q1, N.z])
107
+ B = A.orientnew('B', 'Axis', [q2, A.x])
108
+ C = B.orientnew('C', 'Axis', [q3, B.y])
109
+ D = N.orientnew('D', 'Axis', [q4, N.y])
110
+ u1, u2, u3 = dynamicsymbols('u1 u2 u3')
111
+ assert A.ang_vel_in(N) == (q1d)*A.z
112
+ assert B.ang_vel_in(N) == (q2d)*B.x + (q1d)*A.z
113
+ assert C.ang_vel_in(N) == (q3d)*C.y + (q2d)*B.x + (q1d)*A.z
114
+
115
+ A2 = N.orientnew('A2', 'Axis', [q4, N.y])
116
+ assert N.ang_vel_in(N) == 0
117
+ assert N.ang_vel_in(A) == -q1d*N.z
118
+ assert N.ang_vel_in(B) == -q1d*A.z - q2d*B.x
119
+ assert N.ang_vel_in(C) == -q1d*A.z - q2d*B.x - q3d*B.y
120
+ assert N.ang_vel_in(A2) == -q4d*N.y
121
+
122
+ assert A.ang_vel_in(N) == q1d*N.z
123
+ assert A.ang_vel_in(A) == 0
124
+ assert A.ang_vel_in(B) == - q2d*B.x
125
+ assert A.ang_vel_in(C) == - q2d*B.x - q3d*B.y
126
+ assert A.ang_vel_in(A2) == q1d*N.z - q4d*N.y
127
+
128
+ assert B.ang_vel_in(N) == q1d*A.z + q2d*A.x
129
+ assert B.ang_vel_in(A) == q2d*A.x
130
+ assert B.ang_vel_in(B) == 0
131
+ assert B.ang_vel_in(C) == -q3d*B.y
132
+ assert B.ang_vel_in(A2) == q1d*A.z + q2d*A.x - q4d*N.y
133
+
134
+ assert C.ang_vel_in(N) == q1d*A.z + q2d*A.x + q3d*B.y
135
+ assert C.ang_vel_in(A) == q2d*A.x + q3d*C.y
136
+ assert C.ang_vel_in(B) == q3d*B.y
137
+ assert C.ang_vel_in(C) == 0
138
+ assert C.ang_vel_in(A2) == q1d*A.z + q2d*A.x + q3d*B.y - q4d*N.y
139
+
140
+ assert A2.ang_vel_in(N) == q4d*A2.y
141
+ assert A2.ang_vel_in(A) == q4d*A2.y - q1d*N.z
142
+ assert A2.ang_vel_in(B) == q4d*N.y - q1d*A.z - q2d*A.x
143
+ assert A2.ang_vel_in(C) == q4d*N.y - q1d*A.z - q2d*A.x - q3d*B.y
144
+ assert A2.ang_vel_in(A2) == 0
145
+
146
+ C.set_ang_vel(N, u1*C.x + u2*C.y + u3*C.z)
147
+ assert C.ang_vel_in(N) == (u1)*C.x + (u2)*C.y + (u3)*C.z
148
+ assert N.ang_vel_in(C) == (-u1)*C.x + (-u2)*C.y + (-u3)*C.z
149
+ assert C.ang_vel_in(D) == (u1)*C.x + (u2)*C.y + (u3)*C.z + (-q4d)*D.y
150
+ assert D.ang_vel_in(C) == (-u1)*C.x + (-u2)*C.y + (-u3)*C.z + (q4d)*D.y
151
+
152
+ q0 = dynamicsymbols('q0')
153
+ q0d = dynamicsymbols('q0', 1)
154
+ E = N.orientnew('E', 'Quaternion', (q0, q1, q2, q3))
155
+ assert E.ang_vel_in(N) == (
156
+ 2 * (q1d * q0 + q2d * q3 - q3d * q2 - q0d * q1) * E.x +
157
+ 2 * (q2d * q0 + q3d * q1 - q1d * q3 - q0d * q2) * E.y +
158
+ 2 * (q3d * q0 + q1d * q2 - q2d * q1 - q0d * q3) * E.z)
159
+
160
+ F = N.orientnew('F', 'Body', (q1, q2, q3), 313)
161
+ assert F.ang_vel_in(N) == ((sin(q2)*sin(q3)*q1d + cos(q3)*q2d)*F.x +
162
+ (sin(q2)*cos(q3)*q1d - sin(q3)*q2d)*F.y + (cos(q2)*q1d + q3d)*F.z)
163
+ G = N.orientnew('G', 'Axis', (q1, N.x + N.y))
164
+ assert G.ang_vel_in(N) == q1d * (N.x + N.y).normalize()
165
+ assert N.ang_vel_in(G) == -q1d * (N.x + N.y).normalize()
166
+
167
+
168
+ def test_dcm():
169
+ q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
170
+ N = ReferenceFrame('N')
171
+ A = N.orientnew('A', 'Axis', [q1, N.z])
172
+ B = A.orientnew('B', 'Axis', [q2, A.x])
173
+ C = B.orientnew('C', 'Axis', [q3, B.y])
174
+ D = N.orientnew('D', 'Axis', [q4, N.y])
175
+ E = N.orientnew('E', 'Space', [q1, q2, q3], '123')
176
+ assert N.dcm(C) == Matrix([
177
+ [- sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), - sin(q1) *
178
+ cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)], [sin(q1) *
179
+ cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) *
180
+ sin(q3) - sin(q2) * cos(q1) * cos(q3)], [- sin(q3) * cos(q2), sin(q2),
181
+ cos(q2) * cos(q3)]])
182
+ # This is a little touchy. Is it ok to use simplify in assert?
183
+ test_mat = D.dcm(C) - Matrix(
184
+ [[cos(q1) * cos(q3) * cos(q4) - sin(q3) * (- sin(q4) * cos(q2) +
185
+ sin(q1) * sin(q2) * cos(q4)), - sin(q2) * sin(q4) - sin(q1) *
186
+ cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) * (- sin(q4) *
187
+ cos(q2) + sin(q1) * sin(q2) * cos(q4))], [sin(q1) * cos(q3) +
188
+ sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) * sin(q3) -
189
+ sin(q2) * cos(q1) * cos(q3)], [sin(q4) * cos(q1) * cos(q3) -
190
+ sin(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4)), sin(q2) *
191
+ cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) * sin(q4) * cos(q1) +
192
+ cos(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4))]])
193
+ assert test_mat.expand() == zeros(3, 3)
194
+ assert E.dcm(N) == Matrix(
195
+ [[cos(q2)*cos(q3), sin(q3)*cos(q2), -sin(q2)],
196
+ [sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) +
197
+ cos(q1)*cos(q3), sin(q1)*cos(q2)], [sin(q1)*sin(q3) +
198
+ sin(q2)*cos(q1)*cos(q3), - sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1),
199
+ cos(q1)*cos(q2)]])
200
+
201
+ def test_w_diff_dcm1():
202
+ # Ref:
203
+ # Dynamics Theory and Applications, Kane 1985
204
+ # Sec. 2.1 ANGULAR VELOCITY
205
+ A = ReferenceFrame('A')
206
+ B = ReferenceFrame('B')
207
+
208
+ c11, c12, c13 = dynamicsymbols('C11 C12 C13')
209
+ c21, c22, c23 = dynamicsymbols('C21 C22 C23')
210
+ c31, c32, c33 = dynamicsymbols('C31 C32 C33')
211
+
212
+ c11d, c12d, c13d = dynamicsymbols('C11 C12 C13', level=1)
213
+ c21d, c22d, c23d = dynamicsymbols('C21 C22 C23', level=1)
214
+ c31d, c32d, c33d = dynamicsymbols('C31 C32 C33', level=1)
215
+
216
+ DCM = Matrix([
217
+ [c11, c12, c13],
218
+ [c21, c22, c23],
219
+ [c31, c32, c33]
220
+ ])
221
+
222
+ B.orient(A, 'DCM', DCM)
223
+ b1a = (B.x).express(A)
224
+ b2a = (B.y).express(A)
225
+ b3a = (B.z).express(A)
226
+
227
+ # Equation (2.1.1)
228
+ B.set_ang_vel(A, B.x*(dot((b3a).dt(A), B.y))
229
+ + B.y*(dot((b1a).dt(A), B.z))
230
+ + B.z*(dot((b2a).dt(A), B.x)))
231
+
232
+ # Equation (2.1.21)
233
+ expr = ( (c12*c13d + c22*c23d + c32*c33d)*B.x
234
+ + (c13*c11d + c23*c21d + c33*c31d)*B.y
235
+ + (c11*c12d + c21*c22d + c31*c32d)*B.z)
236
+ assert B.ang_vel_in(A) - expr == 0
237
+
238
+ def test_w_diff_dcm2():
239
+ q1, q2, q3 = dynamicsymbols('q1:4')
240
+ N = ReferenceFrame('N')
241
+ A = N.orientnew('A', 'axis', [q1, N.x])
242
+ B = A.orientnew('B', 'axis', [q2, A.y])
243
+ C = B.orientnew('C', 'axis', [q3, B.z])
244
+
245
+ DCM = C.dcm(N).T
246
+ D = N.orientnew('D', 'DCM', DCM)
247
+
248
+ # Frames D and C are the same ReferenceFrame,
249
+ # since they have equal DCM respect to frame N.
250
+ # Therefore, D and C should have same angle velocity in N.
251
+ assert D.dcm(N) == C.dcm(N) == Matrix([
252
+ [cos(q2)*cos(q3), sin(q1)*sin(q2)*cos(q3) +
253
+ sin(q3)*cos(q1), sin(q1)*sin(q3) -
254
+ sin(q2)*cos(q1)*cos(q3)], [-sin(q3)*cos(q2),
255
+ -sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3),
256
+ sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
257
+ [sin(q2), -sin(q1)*cos(q2), cos(q1)*cos(q2)]])
258
+ assert (D.ang_vel_in(N) - C.ang_vel_in(N)).express(N).simplify() == 0
259
+
260
+ def test_orientnew_respects_parent_class():
261
+ class MyReferenceFrame(ReferenceFrame):
262
+ pass
263
+ B = MyReferenceFrame('B')
264
+ C = B.orientnew('C', 'Axis', [0, B.x])
265
+ assert isinstance(C, MyReferenceFrame)
266
+
267
+
268
+ def test_orientnew_respects_input_indices():
269
+ N = ReferenceFrame('N')
270
+ q1 = dynamicsymbols('q1')
271
+ A = N.orientnew('a', 'Axis', [q1, N.z])
272
+ #modify default indices:
273
+ minds = [x+'1' for x in N.indices]
274
+ B = N.orientnew('b', 'Axis', [q1, N.z], indices=minds)
275
+
276
+ assert N.indices == A.indices
277
+ assert B.indices == minds
278
+
279
+ def test_orientnew_respects_input_latexs():
280
+ N = ReferenceFrame('N')
281
+ q1 = dynamicsymbols('q1')
282
+ A = N.orientnew('a', 'Axis', [q1, N.z])
283
+
284
+ #build default and alternate latex_vecs:
285
+ def_latex_vecs = [(r"\mathbf{\hat{%s}_%s}" % (A.name.lower(),
286
+ A.indices[0])), (r"\mathbf{\hat{%s}_%s}" %
287
+ (A.name.lower(), A.indices[1])),
288
+ (r"\mathbf{\hat{%s}_%s}" % (A.name.lower(),
289
+ A.indices[2]))]
290
+
291
+ name = 'b'
292
+ indices = [x+'1' for x in N.indices]
293
+ new_latex_vecs = [(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
294
+ indices[0])), (r"\mathbf{\hat{%s}_{%s}}" %
295
+ (name.lower(), indices[1])),
296
+ (r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
297
+ indices[2]))]
298
+
299
+ B = N.orientnew(name, 'Axis', [q1, N.z], latexs=new_latex_vecs)
300
+
301
+ assert A.latex_vecs == def_latex_vecs
302
+ assert B.latex_vecs == new_latex_vecs
303
+ assert B.indices != indices
304
+
305
+ def test_orientnew_respects_input_variables():
306
+ N = ReferenceFrame('N')
307
+ q1 = dynamicsymbols('q1')
308
+ A = N.orientnew('a', 'Axis', [q1, N.z])
309
+
310
+ #build non-standard variable names
311
+ name = 'b'
312
+ new_variables = ['notb_'+x+'1' for x in N.indices]
313
+ B = N.orientnew(name, 'Axis', [q1, N.z], variables=new_variables)
314
+
315
+ for j,var in enumerate(A.varlist):
316
+ assert var.name == A.name + '_' + A.indices[j]
317
+
318
+ for j,var in enumerate(B.varlist):
319
+ assert var.name == new_variables[j]
320
+
321
+ def test_issue_10348():
322
+ u = dynamicsymbols('u:3')
323
+ I = ReferenceFrame('I')
324
+ I.orientnew('A', 'space', u, 'XYZ')
325
+
326
+
327
+ def test_issue_11503():
328
+ A = ReferenceFrame("A")
329
+ A.orientnew("B", "Axis", [35, A.y])
330
+ C = ReferenceFrame("C")
331
+ A.orient(C, "Axis", [70, C.z])
332
+
333
+
334
+ def test_partial_velocity():
335
+
336
+ N = ReferenceFrame('N')
337
+ A = ReferenceFrame('A')
338
+
339
+ u1, u2 = dynamicsymbols('u1, u2')
340
+
341
+ A.set_ang_vel(N, u1 * A.x + u2 * N.y)
342
+
343
+ assert N.partial_velocity(A, u1) == -A.x
344
+ assert N.partial_velocity(A, u1, u2) == (-A.x, -N.y)
345
+
346
+ assert A.partial_velocity(N, u1) == A.x
347
+ assert A.partial_velocity(N, u1, u2) == (A.x, N.y)
348
+
349
+ assert N.partial_velocity(N, u1) == 0
350
+ assert A.partial_velocity(A, u1) == 0
351
+
352
+
353
+ def test_issue_11498():
354
+ A = ReferenceFrame('A')
355
+ B = ReferenceFrame('B')
356
+
357
+ # Identity transformation
358
+ A.orient(B, 'DCM', eye(3))
359
+ assert A.dcm(B) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
360
+ assert B.dcm(A) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
361
+
362
+ # x -> y
363
+ # y -> -z
364
+ # z -> -x
365
+ A.orient(B, 'DCM', Matrix([[0, 1, 0], [0, 0, -1], [-1, 0, 0]]))
366
+ assert B.dcm(A) == Matrix([[0, 1, 0], [0, 0, -1], [-1, 0, 0]])
367
+ assert A.dcm(B) == Matrix([[0, 0, -1], [1, 0, 0], [0, -1, 0]])
368
+ assert B.dcm(A).T == A.dcm(B)
369
+
370
+
371
+ def test_reference_frame():
372
+ raises(TypeError, lambda: ReferenceFrame(0))
373
+ raises(TypeError, lambda: ReferenceFrame('N', 0))
374
+ raises(ValueError, lambda: ReferenceFrame('N', [0, 1]))
375
+ raises(TypeError, lambda: ReferenceFrame('N', [0, 1, 2]))
376
+ raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], 0))
377
+ raises(ValueError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], [0, 1]))
378
+ raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], [0, 1, 2]))
379
+ raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
380
+ ['a', 'b', 'c'], 0))
381
+ raises(ValueError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
382
+ ['a', 'b', 'c'], [0, 1]))
383
+ raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
384
+ ['a', 'b', 'c'], [0, 1, 2]))
385
+ N = ReferenceFrame('N')
386
+ assert N[0] == CoordinateSym('N_x', N, 0)
387
+ assert N[1] == CoordinateSym('N_y', N, 1)
388
+ assert N[2] == CoordinateSym('N_z', N, 2)
389
+ raises(ValueError, lambda: N[3])
390
+ N = ReferenceFrame('N', ['a', 'b', 'c'])
391
+ assert N['a'] == N.x
392
+ assert N['b'] == N.y
393
+ assert N['c'] == N.z
394
+ raises(ValueError, lambda: N['d'])
395
+ assert str(N) == 'N'
396
+
397
+ A = ReferenceFrame('A')
398
+ B = ReferenceFrame('B')
399
+ q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
400
+ raises(TypeError, lambda: A.orient(B, 'DCM', 0))
401
+ raises(TypeError, lambda: B.orient(N, 'Space', [q1, q2, q3], '222'))
402
+ raises(TypeError, lambda: B.orient(N, 'Axis', [q1, N.x + 2 * N.y], '222'))
403
+ raises(TypeError, lambda: B.orient(N, 'Axis', q1))
404
+ raises(IndexError, lambda: B.orient(N, 'Axis', [q1]))
405
+ raises(TypeError, lambda: B.orient(N, 'Quaternion', [q0, q1, q2, q3], '222'))
406
+ raises(TypeError, lambda: B.orient(N, 'Quaternion', q0))
407
+ raises(TypeError, lambda: B.orient(N, 'Quaternion', [q0, q1, q2]))
408
+ raises(NotImplementedError, lambda: B.orient(N, 'Foo', [q0, q1, q2]))
409
+ raises(TypeError, lambda: B.orient(N, 'Body', [q1, q2], '232'))
410
+ raises(TypeError, lambda: B.orient(N, 'Space', [q1, q2], '232'))
411
+
412
+ N.set_ang_acc(B, 0)
413
+ assert N.ang_acc_in(B) == Vector(0)
414
+ N.set_ang_vel(B, 0)
415
+ assert N.ang_vel_in(B) == Vector(0)
416
+
417
+
418
+ def test_check_frame():
419
+ raises(VectorTypeError, lambda: _check_frame(0))
420
+
421
+
422
+ def test_dcm_diff_16824():
423
+ # NOTE : This is a regression test for the bug introduced in PR 14758,
424
+ # identified in 16824, and solved by PR 16828.
425
+
426
+ # This is the solution to Problem 2.2 on page 264 in Kane & Lenvinson's
427
+ # 1985 book.
428
+
429
+ q1, q2, q3 = dynamicsymbols('q1:4')
430
+
431
+ s1 = sin(q1)
432
+ c1 = cos(q1)
433
+ s2 = sin(q2)
434
+ c2 = cos(q2)
435
+ s3 = sin(q3)
436
+ c3 = cos(q3)
437
+
438
+ dcm = Matrix([[c2*c3, s1*s2*c3 - s3*c1, c1*s2*c3 + s3*s1],
439
+ [c2*s3, s1*s2*s3 + c3*c1, c1*s2*s3 - c3*s1],
440
+ [-s2, s1*c2, c1*c2]])
441
+
442
+ A = ReferenceFrame('A')
443
+ B = ReferenceFrame('B')
444
+ B.orient(A, 'DCM', dcm)
445
+
446
+ AwB = B.ang_vel_in(A)
447
+
448
+ alpha2 = s3*c2*q1.diff() + c3*q2.diff()
449
+ beta2 = s1*c2*q3.diff() + c1*q2.diff()
450
+
451
+ assert simplify(AwB.dot(A.y) - alpha2) == 0
452
+ assert simplify(AwB.dot(B.y) - beta2) == 0
453
+
454
+ def test_orient_explicit():
455
+ cxx, cyy, czz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}')
456
+ cxy, cxz, cyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}')
457
+ cyz, czx, czy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}')
458
+ dcxx, dcyy, dczz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}', 1)
459
+ dcxy, dcxz, dcyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}', 1)
460
+ dcyz, dczx, dczy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}', 1)
461
+ A = ReferenceFrame('A')
462
+ B = ReferenceFrame('B')
463
+ B_C_A = Matrix([[cxx, cxy, cxz],
464
+ [cyx, cyy, cyz],
465
+ [czx, czy, czz]])
466
+ B_w_A = ((cyx*dczx + cyy*dczy + cyz*dczz)*B.x +
467
+ (czx*dcxx + czy*dcxy + czz*dcxz)*B.y +
468
+ (cxx*dcyx + cxy*dcyy + cxz*dcyz)*B.z)
469
+ A.orient_explicit(B, B_C_A)
470
+ assert B.dcm(A) == B_C_A
471
+ assert A.ang_vel_in(B) == B_w_A
472
+ assert B.ang_vel_in(A) == -B_w_A
473
+
474
+ def test_orient_dcm():
475
+ cxx, cyy, czz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}')
476
+ cxy, cxz, cyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}')
477
+ cyz, czx, czy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}')
478
+ B_C_A = Matrix([[cxx, cxy, cxz],
479
+ [cyx, cyy, cyz],
480
+ [czx, czy, czz]])
481
+ A = ReferenceFrame('A')
482
+ B = ReferenceFrame('B')
483
+ B.orient_dcm(A, B_C_A)
484
+ assert B.dcm(A) == Matrix([[cxx, cxy, cxz],
485
+ [cyx, cyy, cyz],
486
+ [czx, czy, czz]])
487
+
488
+ def test_orient_axis():
489
+ A = ReferenceFrame('A')
490
+ B = ReferenceFrame('B')
491
+ A.orient_axis(B,-B.x, 1)
492
+ A1 = A.dcm(B)
493
+ A.orient_axis(B, B.x, -1)
494
+ A2 = A.dcm(B)
495
+ A.orient_axis(B, 1, -B.x)
496
+ A3 = A.dcm(B)
497
+ assert A1 == A2
498
+ assert A2 == A3
499
+ raises(TypeError, lambda: A.orient_axis(B, 1, 1))
500
+
501
+ def test_orient_body():
502
+ A = ReferenceFrame('A')
503
+ B = ReferenceFrame('B')
504
+ B.orient_body_fixed(A, (1,1,0), 'XYX')
505
+ assert B.dcm(A) == Matrix([[cos(1), sin(1)**2, -sin(1)*cos(1)], [0, cos(1), sin(1)], [sin(1), -sin(1)*cos(1), cos(1)**2]])
506
+
507
+
508
+ def test_orient_body_advanced():
509
+ q1, q2, q3 = dynamicsymbols('q1:4')
510
+ c1, c2, c3 = symbols('c1:4')
511
+ u1, u2, u3 = dynamicsymbols('q1:4', 1)
512
+
513
+ # Test with everything as dynamicsymbols
514
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
515
+ B.orient_body_fixed(A, (q1, q2, q3), 'zxy')
516
+ assert A.dcm(B) == Matrix([
517
+ [-sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), -sin(q1) * cos(q2),
518
+ sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)],
519
+ [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2),
520
+ sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)],
521
+ [-sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
522
+ assert B.ang_vel_in(A).to_matrix(B) == Matrix([
523
+ [-sin(q3) * cos(q2) * u1 + cos(q3) * u2],
524
+ [sin(q2) * u1 + u3],
525
+ [sin(q3) * u2 + cos(q2) * cos(q3) * u1]])
526
+
527
+ # Test with constant symbol
528
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
529
+ B.orient_body_fixed(A, (q1, c2, q3), 131)
530
+ assert A.dcm(B) == Matrix([
531
+ [cos(c2), -sin(c2) * cos(q3), sin(c2) * sin(q3)],
532
+ [sin(c2) * cos(q1), -sin(q1) * sin(q3) + cos(c2) * cos(q1) * cos(q3),
533
+ -sin(q1) * cos(q3) - sin(q3) * cos(c2) * cos(q1)],
534
+ [sin(c2) * sin(q1), sin(q1) * cos(c2) * cos(q3) + sin(q3) * cos(q1),
535
+ -sin(q1) * sin(q3) * cos(c2) + cos(q1) * cos(q3)]])
536
+ assert B.ang_vel_in(A).to_matrix(B) == Matrix([
537
+ [cos(c2) * u1 + u3],
538
+ [-sin(c2) * cos(q3) * u1],
539
+ [sin(c2) * sin(q3) * u1]])
540
+
541
+ # Test all symbols not time dependent
542
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
543
+ B.orient_body_fixed(A, (c1, c2, c3), 123)
544
+ assert B.ang_vel_in(A) == Vector(0)
545
+
546
+
547
+ def test_orient_space_advanced():
548
+ # space fixed is in the end like body fixed only in opposite order
549
+ q1, q2, q3 = dynamicsymbols('q1:4')
550
+ c1, c2, c3 = symbols('c1:4')
551
+ u1, u2, u3 = dynamicsymbols('q1:4', 1)
552
+
553
+ # Test with everything as dynamicsymbols
554
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
555
+ B.orient_space_fixed(A, (q3, q2, q1), 'yxz')
556
+ assert A.dcm(B) == Matrix([
557
+ [-sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), -sin(q1) * cos(q2),
558
+ sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)],
559
+ [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2),
560
+ sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)],
561
+ [-sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
562
+ assert B.ang_vel_in(A).to_matrix(B) == Matrix([
563
+ [-sin(q3) * cos(q2) * u1 + cos(q3) * u2],
564
+ [sin(q2) * u1 + u3],
565
+ [sin(q3) * u2 + cos(q2) * cos(q3) * u1]])
566
+
567
+ # Test with constant symbol
568
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
569
+ B.orient_space_fixed(A, (q3, c2, q1), 131)
570
+ assert A.dcm(B) == Matrix([
571
+ [cos(c2), -sin(c2) * cos(q3), sin(c2) * sin(q3)],
572
+ [sin(c2) * cos(q1), -sin(q1) * sin(q3) + cos(c2) * cos(q1) * cos(q3),
573
+ -sin(q1) * cos(q3) - sin(q3) * cos(c2) * cos(q1)],
574
+ [sin(c2) * sin(q1), sin(q1) * cos(c2) * cos(q3) + sin(q3) * cos(q1),
575
+ -sin(q1) * sin(q3) * cos(c2) + cos(q1) * cos(q3)]])
576
+ assert B.ang_vel_in(A).to_matrix(B) == Matrix([
577
+ [cos(c2) * u1 + u3],
578
+ [-sin(c2) * cos(q3) * u1],
579
+ [sin(c2) * sin(q3) * u1]])
580
+
581
+ # Test all symbols not time dependent
582
+ A, B = ReferenceFrame('A'), ReferenceFrame('B')
583
+ B.orient_space_fixed(A, (c1, c2, c3), 123)
584
+ assert B.ang_vel_in(A) == Vector(0)
585
+
586
+
587
+ def test_orient_body_simple_ang_vel():
588
+ """This test ensures that the simplest form of that linear system solution
589
+ is returned, thus the == for the expression comparison."""
590
+
591
+ psi, theta, phi = dynamicsymbols('psi, theta, varphi')
592
+ t = dynamicsymbols._t
593
+ A = ReferenceFrame('A')
594
+ B = ReferenceFrame('B')
595
+ B.orient_body_fixed(A, (psi, theta, phi), 'ZXZ')
596
+ A_w_B = B.ang_vel_in(A)
597
+ assert A_w_B.args[0][1] == B
598
+ assert A_w_B.args[0][0][0] == (sin(theta)*sin(phi)*psi.diff(t) +
599
+ cos(phi)*theta.diff(t))
600
+ assert A_w_B.args[0][0][1] == (sin(theta)*cos(phi)*psi.diff(t) -
601
+ sin(phi)*theta.diff(t))
602
+ assert A_w_B.args[0][0][2] == cos(theta)*psi.diff(t) + phi.diff(t)
603
+
604
+
605
+ def test_orient_space():
606
+ A = ReferenceFrame('A')
607
+ B = ReferenceFrame('B')
608
+ B.orient_space_fixed(A, (0,0,0), '123')
609
+ assert B.dcm(A) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
610
+
611
+ def test_orient_quaternion():
612
+ A = ReferenceFrame('A')
613
+ B = ReferenceFrame('B')
614
+ B.orient_quaternion(A, (0,0,0,0))
615
+ assert B.dcm(A) == Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
616
+
617
+ def test_looped_frame_warning():
618
+ A = ReferenceFrame('A')
619
+ B = ReferenceFrame('B')
620
+ C = ReferenceFrame('C')
621
+
622
+ a, b, c = symbols('a b c')
623
+ B.orient_axis(A, A.x, a)
624
+ C.orient_axis(B, B.x, b)
625
+
626
+ with warnings.catch_warnings(record = True) as w:
627
+ warnings.simplefilter("always")
628
+ A.orient_axis(C, C.x, c)
629
+ assert issubclass(w[-1].category, UserWarning)
630
+ assert 'Loops are defined among the orientation of frames. ' + \
631
+ 'This is likely not desired and may cause errors in your calculations.' in str(w[-1].message)
632
+
633
+ def test_frame_dict():
634
+ A = ReferenceFrame('A')
635
+ B = ReferenceFrame('B')
636
+ C = ReferenceFrame('C')
637
+
638
+ a, b, c = symbols('a b c')
639
+
640
+ B.orient_axis(A, A.x, a)
641
+ assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
642
+ assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]])}
643
+ assert C._dcm_dict == {}
644
+
645
+ B.orient_axis(C, C.x, b)
646
+ # Previous relation is not wiped
647
+ assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
648
+ assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]]), \
649
+ C: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]])}
650
+ assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
651
+
652
+ A.orient_axis(B, B.x, c)
653
+ # Previous relation is updated
654
+ assert B._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]]),\
655
+ A: Matrix([[1, 0, 0],[0, cos(c), -sin(c)],[0, sin(c), cos(c)]])}
656
+ assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(c), sin(c)],[0, -sin(c), cos(c)]])}
657
+ assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
658
+
659
+ def test_dcm_cache_dict():
660
+ A = ReferenceFrame('A')
661
+ B = ReferenceFrame('B')
662
+ C = ReferenceFrame('C')
663
+ D = ReferenceFrame('D')
664
+
665
+ a, b, c = symbols('a b c')
666
+
667
+ B.orient_axis(A, A.x, a)
668
+ C.orient_axis(B, B.x, b)
669
+ D.orient_axis(C, C.x, c)
670
+
671
+ assert D._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(c), sin(c)],[0, -sin(c), cos(c)]])}
672
+ assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]]), \
673
+ D: Matrix([[1, 0, 0],[0, cos(c), -sin(c)],[0, sin(c), cos(c)]])}
674
+ assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]]), \
675
+ C: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
676
+ assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
677
+
678
+ assert D._dcm_dict == D._dcm_cache
679
+
680
+ D.dcm(A) # Check calculated dcm relation is stored in _dcm_cache and not in _dcm_dict
681
+ assert list(A._dcm_cache.keys()) == [A, B, D]
682
+ assert list(D._dcm_cache.keys()) == [C, A]
683
+ assert list(A._dcm_dict.keys()) == [B]
684
+ assert list(D._dcm_dict.keys()) == [C]
685
+ assert A._dcm_dict != A._dcm_cache
686
+
687
+ A.orient_axis(B, B.x, b) # _dcm_cache of A is wiped out and new relation is stored.
688
+ assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]])}
689
+ assert A._dcm_dict == A._dcm_cache
690
+ assert B._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]]), \
691
+ A: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
692
+
693
+ def test_xx_dyad():
694
+ N = ReferenceFrame('N')
695
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
696
+ assert N.xx == Vector.outer(N.x, N.x)
697
+ assert F.xx == Vector.outer(F.x, F.x)
698
+
699
+ def test_xy_dyad():
700
+ N = ReferenceFrame('N')
701
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
702
+ assert N.xy == Vector.outer(N.x, N.y)
703
+ assert F.xy == Vector.outer(F.x, F.y)
704
+
705
+ def test_xz_dyad():
706
+ N = ReferenceFrame('N')
707
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
708
+ assert N.xz == Vector.outer(N.x, N.z)
709
+ assert F.xz == Vector.outer(F.x, F.z)
710
+
711
+ def test_yx_dyad():
712
+ N = ReferenceFrame('N')
713
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
714
+ assert N.yx == Vector.outer(N.y, N.x)
715
+ assert F.yx == Vector.outer(F.y, F.x)
716
+
717
+ def test_yy_dyad():
718
+ N = ReferenceFrame('N')
719
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
720
+ assert N.yy == Vector.outer(N.y, N.y)
721
+ assert F.yy == Vector.outer(F.y, F.y)
722
+
723
+ def test_yz_dyad():
724
+ N = ReferenceFrame('N')
725
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
726
+ assert N.yz == Vector.outer(N.y, N.z)
727
+ assert F.yz == Vector.outer(F.y, F.z)
728
+
729
+ def test_zx_dyad():
730
+ N = ReferenceFrame('N')
731
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
732
+ assert N.zx == Vector.outer(N.z, N.x)
733
+ assert F.zx == Vector.outer(F.z, F.x)
734
+
735
+ def test_zy_dyad():
736
+ N = ReferenceFrame('N')
737
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
738
+ assert N.zy == Vector.outer(N.z, N.y)
739
+ assert F.zy == Vector.outer(F.z, F.y)
740
+
741
+ def test_zz_dyad():
742
+ N = ReferenceFrame('N')
743
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
744
+ assert N.zz == Vector.outer(N.z, N.z)
745
+ assert F.zz == Vector.outer(F.z, F.z)
746
+
747
+ def test_unit_dyadic():
748
+ N = ReferenceFrame('N')
749
+ F = ReferenceFrame('F', indices=['1', '2', '3'])
750
+ assert N.u == N.xx + N.yy + N.zz
751
+ assert F.u == F.xx + F.yy + F.zz
752
+
753
+
754
+ def test_pickle_frame():
755
+ N = ReferenceFrame('N')
756
+ A = ReferenceFrame('A')
757
+ A.orient_axis(N, N.x, 1)
758
+ A_C_N = A.dcm(N)
759
+ N1 = pickle.loads(pickle.dumps(N))
760
+ A1 = tuple(N1._dcm_dict.keys())[0]
761
+ assert A1.dcm(N1) == A_C_N
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_functions.py ADDED
@@ -0,0 +1,509 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import pi
2
+ from sympy.core.singleton import S
3
+ from sympy.core.symbol import symbols
4
+ from sympy.functions.elementary.miscellaneous import sqrt
5
+ from sympy.functions.elementary.trigonometric import (cos, sin)
6
+ from sympy.integrals.integrals import Integral
7
+ from sympy.physics.vector import Dyadic, Point, ReferenceFrame, Vector
8
+ from sympy.physics.vector.functions import (cross, dot, express,
9
+ time_derivative,
10
+ kinematic_equations, outer,
11
+ partial_velocity,
12
+ get_motion_params, dynamicsymbols)
13
+ from sympy.simplify import trigsimp
14
+ from sympy.testing.pytest import raises
15
+
16
+ q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5')
17
+ N = ReferenceFrame('N')
18
+ A = N.orientnew('A', 'Axis', [q1, N.z])
19
+ B = A.orientnew('B', 'Axis', [q2, A.x])
20
+ C = B.orientnew('C', 'Axis', [q3, B.y])
21
+
22
+
23
+ def test_dot():
24
+ assert dot(A.x, A.x) == 1
25
+ assert dot(A.x, A.y) == 0
26
+ assert dot(A.x, A.z) == 0
27
+
28
+ assert dot(A.y, A.x) == 0
29
+ assert dot(A.y, A.y) == 1
30
+ assert dot(A.y, A.z) == 0
31
+
32
+ assert dot(A.z, A.x) == 0
33
+ assert dot(A.z, A.y) == 0
34
+ assert dot(A.z, A.z) == 1
35
+
36
+
37
+ def test_dot_different_frames():
38
+ assert dot(N.x, A.x) == cos(q1)
39
+ assert dot(N.x, A.y) == -sin(q1)
40
+ assert dot(N.x, A.z) == 0
41
+ assert dot(N.y, A.x) == sin(q1)
42
+ assert dot(N.y, A.y) == cos(q1)
43
+ assert dot(N.y, A.z) == 0
44
+ assert dot(N.z, A.x) == 0
45
+ assert dot(N.z, A.y) == 0
46
+ assert dot(N.z, A.z) == 1
47
+
48
+ assert trigsimp(dot(N.x, A.x + A.y)) == sqrt(2)*cos(q1 + pi/4)
49
+ assert trigsimp(dot(N.x, A.x + A.y)) == trigsimp(dot(A.x + A.y, N.x))
50
+
51
+ assert dot(A.x, C.x) == cos(q3)
52
+ assert dot(A.x, C.y) == 0
53
+ assert dot(A.x, C.z) == sin(q3)
54
+ assert dot(A.y, C.x) == sin(q2)*sin(q3)
55
+ assert dot(A.y, C.y) == cos(q2)
56
+ assert dot(A.y, C.z) == -sin(q2)*cos(q3)
57
+ assert dot(A.z, C.x) == -cos(q2)*sin(q3)
58
+ assert dot(A.z, C.y) == sin(q2)
59
+ assert dot(A.z, C.z) == cos(q2)*cos(q3)
60
+
61
+
62
+ def test_cross():
63
+ assert cross(A.x, A.x) == 0
64
+ assert cross(A.x, A.y) == A.z
65
+ assert cross(A.x, A.z) == -A.y
66
+
67
+ assert cross(A.y, A.x) == -A.z
68
+ assert cross(A.y, A.y) == 0
69
+ assert cross(A.y, A.z) == A.x
70
+
71
+ assert cross(A.z, A.x) == A.y
72
+ assert cross(A.z, A.y) == -A.x
73
+ assert cross(A.z, A.z) == 0
74
+
75
+
76
+ def test_cross_different_frames():
77
+ assert cross(N.x, A.x) == sin(q1)*A.z
78
+ assert cross(N.x, A.y) == cos(q1)*A.z
79
+ assert cross(N.x, A.z) == -sin(q1)*A.x - cos(q1)*A.y
80
+ assert cross(N.y, A.x) == -cos(q1)*A.z
81
+ assert cross(N.y, A.y) == sin(q1)*A.z
82
+ assert cross(N.y, A.z) == cos(q1)*A.x - sin(q1)*A.y
83
+ assert cross(N.z, A.x) == A.y
84
+ assert cross(N.z, A.y) == -A.x
85
+ assert cross(N.z, A.z) == 0
86
+
87
+ assert cross(N.x, A.x) == sin(q1)*A.z
88
+ assert cross(N.x, A.y) == cos(q1)*A.z
89
+ assert cross(N.x, A.x + A.y) == sin(q1)*A.z + cos(q1)*A.z
90
+ assert cross(A.x + A.y, N.x) == -sin(q1)*A.z - cos(q1)*A.z
91
+
92
+ assert cross(A.x, C.x) == sin(q3)*C.y
93
+ assert cross(A.x, C.y) == -sin(q3)*C.x + cos(q3)*C.z
94
+ assert cross(A.x, C.z) == -cos(q3)*C.y
95
+ assert cross(C.x, A.x) == -sin(q3)*C.y
96
+ assert cross(C.y, A.x).express(C).simplify() == sin(q3)*C.x - cos(q3)*C.z
97
+ assert cross(C.z, A.x) == cos(q3)*C.y
98
+
99
+ def test_operator_match():
100
+ """Test that the output of dot, cross, outer functions match
101
+ operator behavior.
102
+ """
103
+ A = ReferenceFrame('A')
104
+ v = A.x + A.y
105
+ d = v | v
106
+ zerov = Vector(0)
107
+ zerod = Dyadic(0)
108
+
109
+ # dot products
110
+ assert d & d == dot(d, d)
111
+ assert d & zerod == dot(d, zerod)
112
+ assert zerod & d == dot(zerod, d)
113
+ assert d & v == dot(d, v)
114
+ assert v & d == dot(v, d)
115
+ assert d & zerov == dot(d, zerov)
116
+ assert zerov & d == dot(zerov, d)
117
+ raises(TypeError, lambda: dot(d, S.Zero))
118
+ raises(TypeError, lambda: dot(S.Zero, d))
119
+ raises(TypeError, lambda: dot(d, 0))
120
+ raises(TypeError, lambda: dot(0, d))
121
+ assert v & v == dot(v, v)
122
+ assert v & zerov == dot(v, zerov)
123
+ assert zerov & v == dot(zerov, v)
124
+ raises(TypeError, lambda: dot(v, S.Zero))
125
+ raises(TypeError, lambda: dot(S.Zero, v))
126
+ raises(TypeError, lambda: dot(v, 0))
127
+ raises(TypeError, lambda: dot(0, v))
128
+
129
+ # cross products
130
+ raises(TypeError, lambda: cross(d, d))
131
+ raises(TypeError, lambda: cross(d, zerod))
132
+ raises(TypeError, lambda: cross(zerod, d))
133
+ assert d ^ v == cross(d, v)
134
+ assert v ^ d == cross(v, d)
135
+ assert d ^ zerov == cross(d, zerov)
136
+ assert zerov ^ d == cross(zerov, d)
137
+ assert zerov ^ d == cross(zerov, d)
138
+ raises(TypeError, lambda: cross(d, S.Zero))
139
+ raises(TypeError, lambda: cross(S.Zero, d))
140
+ raises(TypeError, lambda: cross(d, 0))
141
+ raises(TypeError, lambda: cross(0, d))
142
+ assert v ^ v == cross(v, v)
143
+ assert v ^ zerov == cross(v, zerov)
144
+ assert zerov ^ v == cross(zerov, v)
145
+ raises(TypeError, lambda: cross(v, S.Zero))
146
+ raises(TypeError, lambda: cross(S.Zero, v))
147
+ raises(TypeError, lambda: cross(v, 0))
148
+ raises(TypeError, lambda: cross(0, v))
149
+
150
+ # outer products
151
+ raises(TypeError, lambda: outer(d, d))
152
+ raises(TypeError, lambda: outer(d, zerod))
153
+ raises(TypeError, lambda: outer(zerod, d))
154
+ raises(TypeError, lambda: outer(d, v))
155
+ raises(TypeError, lambda: outer(v, d))
156
+ raises(TypeError, lambda: outer(d, zerov))
157
+ raises(TypeError, lambda: outer(zerov, d))
158
+ raises(TypeError, lambda: outer(zerov, d))
159
+ raises(TypeError, lambda: outer(d, S.Zero))
160
+ raises(TypeError, lambda: outer(S.Zero, d))
161
+ raises(TypeError, lambda: outer(d, 0))
162
+ raises(TypeError, lambda: outer(0, d))
163
+ assert v | v == outer(v, v)
164
+ assert v | zerov == outer(v, zerov)
165
+ assert zerov | v == outer(zerov, v)
166
+ raises(TypeError, lambda: outer(v, S.Zero))
167
+ raises(TypeError, lambda: outer(S.Zero, v))
168
+ raises(TypeError, lambda: outer(v, 0))
169
+ raises(TypeError, lambda: outer(0, v))
170
+
171
+
172
+ def test_express():
173
+ assert express(Vector(0), N) == Vector(0)
174
+ assert express(S.Zero, N) is S.Zero
175
+ assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z
176
+ assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \
177
+ sin(q2)*cos(q3)*C.z
178
+ assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \
179
+ cos(q2)*cos(q3)*C.z
180
+ assert express(A.x, N) == cos(q1)*N.x + sin(q1)*N.y
181
+ assert express(A.y, N) == -sin(q1)*N.x + cos(q1)*N.y
182
+ assert express(A.z, N) == N.z
183
+ assert express(A.x, A) == A.x
184
+ assert express(A.y, A) == A.y
185
+ assert express(A.z, A) == A.z
186
+ assert express(A.x, B) == B.x
187
+ assert express(A.y, B) == cos(q2)*B.y - sin(q2)*B.z
188
+ assert express(A.z, B) == sin(q2)*B.y + cos(q2)*B.z
189
+ assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z
190
+ assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \
191
+ sin(q2)*cos(q3)*C.z
192
+ assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \
193
+ cos(q2)*cos(q3)*C.z
194
+ # Check to make sure UnitVectors get converted properly
195
+ assert express(N.x, N) == N.x
196
+ assert express(N.y, N) == N.y
197
+ assert express(N.z, N) == N.z
198
+ assert express(N.x, A) == (cos(q1)*A.x - sin(q1)*A.y)
199
+ assert express(N.y, A) == (sin(q1)*A.x + cos(q1)*A.y)
200
+ assert express(N.z, A) == A.z
201
+ assert express(N.x, B) == (cos(q1)*B.x - sin(q1)*cos(q2)*B.y +
202
+ sin(q1)*sin(q2)*B.z)
203
+ assert express(N.y, B) == (sin(q1)*B.x + cos(q1)*cos(q2)*B.y -
204
+ sin(q2)*cos(q1)*B.z)
205
+ assert express(N.z, B) == (sin(q2)*B.y + cos(q2)*B.z)
206
+ assert express(N.x, C) == (
207
+ (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*C.x -
208
+ sin(q1)*cos(q2)*C.y +
209
+ (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*C.z)
210
+ assert express(N.y, C) == (
211
+ (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x +
212
+ cos(q1)*cos(q2)*C.y +
213
+ (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z)
214
+ assert express(N.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
215
+ cos(q2)*cos(q3)*C.z)
216
+
217
+ assert express(A.x, N) == (cos(q1)*N.x + sin(q1)*N.y)
218
+ assert express(A.y, N) == (-sin(q1)*N.x + cos(q1)*N.y)
219
+ assert express(A.z, N) == N.z
220
+ assert express(A.x, A) == A.x
221
+ assert express(A.y, A) == A.y
222
+ assert express(A.z, A) == A.z
223
+ assert express(A.x, B) == B.x
224
+ assert express(A.y, B) == (cos(q2)*B.y - sin(q2)*B.z)
225
+ assert express(A.z, B) == (sin(q2)*B.y + cos(q2)*B.z)
226
+ assert express(A.x, C) == (cos(q3)*C.x + sin(q3)*C.z)
227
+ assert express(A.y, C) == (sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
228
+ sin(q2)*cos(q3)*C.z)
229
+ assert express(A.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
230
+ cos(q2)*cos(q3)*C.z)
231
+
232
+ assert express(B.x, N) == (cos(q1)*N.x + sin(q1)*N.y)
233
+ assert express(B.y, N) == (-sin(q1)*cos(q2)*N.x +
234
+ cos(q1)*cos(q2)*N.y + sin(q2)*N.z)
235
+ assert express(B.z, N) == (sin(q1)*sin(q2)*N.x -
236
+ sin(q2)*cos(q1)*N.y + cos(q2)*N.z)
237
+ assert express(B.x, A) == A.x
238
+ assert express(B.y, A) == (cos(q2)*A.y + sin(q2)*A.z)
239
+ assert express(B.z, A) == (-sin(q2)*A.y + cos(q2)*A.z)
240
+ assert express(B.x, B) == B.x
241
+ assert express(B.y, B) == B.y
242
+ assert express(B.z, B) == B.z
243
+ assert express(B.x, C) == (cos(q3)*C.x + sin(q3)*C.z)
244
+ assert express(B.y, C) == C.y
245
+ assert express(B.z, C) == (-sin(q3)*C.x + cos(q3)*C.z)
246
+
247
+ assert express(C.x, N) == (
248
+ (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*N.x +
249
+ (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*N.y -
250
+ sin(q3)*cos(q2)*N.z)
251
+ assert express(C.y, N) == (
252
+ -sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z)
253
+ assert express(C.z, N) == (
254
+ (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*N.x +
255
+ (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*N.y +
256
+ cos(q2)*cos(q3)*N.z)
257
+ assert express(C.x, A) == (cos(q3)*A.x + sin(q2)*sin(q3)*A.y -
258
+ sin(q3)*cos(q2)*A.z)
259
+ assert express(C.y, A) == (cos(q2)*A.y + sin(q2)*A.z)
260
+ assert express(C.z, A) == (sin(q3)*A.x - sin(q2)*cos(q3)*A.y +
261
+ cos(q2)*cos(q3)*A.z)
262
+ assert express(C.x, B) == (cos(q3)*B.x - sin(q3)*B.z)
263
+ assert express(C.y, B) == B.y
264
+ assert express(C.z, B) == (sin(q3)*B.x + cos(q3)*B.z)
265
+ assert express(C.x, C) == C.x
266
+ assert express(C.y, C) == C.y
267
+ assert express(C.z, C) == C.z == (C.z)
268
+
269
+ # Check to make sure Vectors get converted back to UnitVectors
270
+ assert N.x == express((cos(q1)*A.x - sin(q1)*A.y), N).simplify()
271
+ assert N.y == express((sin(q1)*A.x + cos(q1)*A.y), N).simplify()
272
+ assert N.x == express((cos(q1)*B.x - sin(q1)*cos(q2)*B.y +
273
+ sin(q1)*sin(q2)*B.z), N).simplify()
274
+ assert N.y == express((sin(q1)*B.x + cos(q1)*cos(q2)*B.y -
275
+ sin(q2)*cos(q1)*B.z), N).simplify()
276
+ assert N.z == express((sin(q2)*B.y + cos(q2)*B.z), N).simplify()
277
+
278
+ """
279
+ These don't really test our code, they instead test the auto simplification
280
+ (or lack thereof) of SymPy.
281
+ assert N.x == express((
282
+ (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*C.x -
283
+ sin(q1)*cos(q2)*C.y +
284
+ (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*C.z), N)
285
+ assert N.y == express((
286
+ (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x +
287
+ cos(q1)*cos(q2)*C.y +
288
+ (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z), N)
289
+ assert N.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
290
+ cos(q2)*cos(q3)*C.z), N)
291
+ """
292
+
293
+ assert A.x == express((cos(q1)*N.x + sin(q1)*N.y), A).simplify()
294
+ assert A.y == express((-sin(q1)*N.x + cos(q1)*N.y), A).simplify()
295
+
296
+ assert A.y == express((cos(q2)*B.y - sin(q2)*B.z), A).simplify()
297
+ assert A.z == express((sin(q2)*B.y + cos(q2)*B.z), A).simplify()
298
+
299
+ assert A.x == express((cos(q3)*C.x + sin(q3)*C.z), A).simplify()
300
+
301
+ # Tripsimp messes up here too.
302
+ #print express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
303
+ # sin(q2)*cos(q3)*C.z), A)
304
+ assert A.y == express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
305
+ sin(q2)*cos(q3)*C.z), A).simplify()
306
+
307
+ assert A.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
308
+ cos(q2)*cos(q3)*C.z), A).simplify()
309
+ assert B.x == express((cos(q1)*N.x + sin(q1)*N.y), B).simplify()
310
+ assert B.y == express((-sin(q1)*cos(q2)*N.x +
311
+ cos(q1)*cos(q2)*N.y + sin(q2)*N.z), B).simplify()
312
+
313
+ assert B.z == express((sin(q1)*sin(q2)*N.x -
314
+ sin(q2)*cos(q1)*N.y + cos(q2)*N.z), B).simplify()
315
+
316
+ assert B.y == express((cos(q2)*A.y + sin(q2)*A.z), B).simplify()
317
+ assert B.z == express((-sin(q2)*A.y + cos(q2)*A.z), B).simplify()
318
+ assert B.x == express((cos(q3)*C.x + sin(q3)*C.z), B).simplify()
319
+ assert B.z == express((-sin(q3)*C.x + cos(q3)*C.z), B).simplify()
320
+
321
+ """
322
+ assert C.x == express((
323
+ (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*N.x +
324
+ (sin(q1)*cos(q3)+sin(q2)*sin(q3)*cos(q1))*N.y -
325
+ sin(q3)*cos(q2)*N.z), C)
326
+ assert C.y == express((
327
+ -sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z), C)
328
+ assert C.z == express((
329
+ (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*N.x +
330
+ (sin(q1)*sin(q3)-sin(q2)*cos(q1)*cos(q3))*N.y +
331
+ cos(q2)*cos(q3)*N.z), C)
332
+ """
333
+ assert C.x == express((cos(q3)*A.x + sin(q2)*sin(q3)*A.y -
334
+ sin(q3)*cos(q2)*A.z), C).simplify()
335
+ assert C.y == express((cos(q2)*A.y + sin(q2)*A.z), C).simplify()
336
+ assert C.z == express((sin(q3)*A.x - sin(q2)*cos(q3)*A.y +
337
+ cos(q2)*cos(q3)*A.z), C).simplify()
338
+ assert C.x == express((cos(q3)*B.x - sin(q3)*B.z), C).simplify()
339
+ assert C.z == express((sin(q3)*B.x + cos(q3)*B.z), C).simplify()
340
+
341
+
342
+ def test_time_derivative():
343
+ #The use of time_derivative for calculations pertaining to scalar
344
+ #fields has been tested in test_coordinate_vars in test_essential.py
345
+ A = ReferenceFrame('A')
346
+ q = dynamicsymbols('q')
347
+ qd = dynamicsymbols('q', 1)
348
+ B = A.orientnew('B', 'Axis', [q, A.z])
349
+ d = A.x | A.x
350
+ assert time_derivative(d, B) == (-qd) * (A.y | A.x) + \
351
+ (-qd) * (A.x | A.y)
352
+ d1 = A.x | B.y
353
+ assert time_derivative(d1, A) == - qd*(A.x|B.x)
354
+ assert time_derivative(d1, B) == - qd*(A.y|B.y)
355
+ d2 = A.x | B.x
356
+ assert time_derivative(d2, A) == qd*(A.x|B.y)
357
+ assert time_derivative(d2, B) == - qd*(A.y|B.x)
358
+ d3 = A.x | B.z
359
+ assert time_derivative(d3, A) == 0
360
+ assert time_derivative(d3, B) == - qd*(A.y|B.z)
361
+ q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
362
+ q1d, q2d, q3d, q4d = dynamicsymbols('q1 q2 q3 q4', 1)
363
+ q1dd, q2dd, q3dd, q4dd = dynamicsymbols('q1 q2 q3 q4', 2)
364
+ C = B.orientnew('C', 'Axis', [q4, B.x])
365
+ v1 = q1 * A.z
366
+ v2 = q2*A.x + q3*B.y
367
+ v3 = q1*A.x + q2*A.y + q3*A.z
368
+ assert time_derivative(B.x, C) == 0
369
+ assert time_derivative(B.y, C) == - q4d*B.z
370
+ assert time_derivative(B.z, C) == q4d*B.y
371
+ assert time_derivative(v1, B) == q1d*A.z
372
+ assert time_derivative(v1, C) == - q1*sin(q)*q4d*A.x + \
373
+ q1*cos(q)*q4d*A.y + q1d*A.z
374
+ assert time_derivative(v2, A) == q2d*A.x - q3*qd*B.x + q3d*B.y
375
+ assert time_derivative(v2, C) == q2d*A.x - q2*qd*A.y + \
376
+ q2*sin(q)*q4d*A.z + q3d*B.y - q3*q4d*B.z
377
+ assert time_derivative(v3, B) == (q2*qd + q1d)*A.x + \
378
+ (-q1*qd + q2d)*A.y + q3d*A.z
379
+ assert time_derivative(d, C) == - qd*(A.y|A.x) + \
380
+ sin(q)*q4d*(A.z|A.x) - qd*(A.x|A.y) + sin(q)*q4d*(A.x|A.z)
381
+ raises(ValueError, lambda: time_derivative(B.x, C, order=0.5))
382
+ raises(ValueError, lambda: time_derivative(B.x, C, order=-1))
383
+
384
+
385
+ def test_get_motion_methods():
386
+ #Initialization
387
+ t = dynamicsymbols._t
388
+ s1, s2, s3 = symbols('s1 s2 s3')
389
+ S1, S2, S3 = symbols('S1 S2 S3')
390
+ S4, S5, S6 = symbols('S4 S5 S6')
391
+ t1, t2 = symbols('t1 t2')
392
+ a, b, c = dynamicsymbols('a b c')
393
+ ad, bd, cd = dynamicsymbols('a b c', 1)
394
+ a2d, b2d, c2d = dynamicsymbols('a b c', 2)
395
+ v0 = S1*N.x + S2*N.y + S3*N.z
396
+ v01 = S4*N.x + S5*N.y + S6*N.z
397
+ v1 = s1*N.x + s2*N.y + s3*N.z
398
+ v2 = a*N.x + b*N.y + c*N.z
399
+ v2d = ad*N.x + bd*N.y + cd*N.z
400
+ v2dd = a2d*N.x + b2d*N.y + c2d*N.z
401
+ #Test position parameter
402
+ assert get_motion_params(frame = N) == (0, 0, 0)
403
+ assert get_motion_params(N, position=v1) == (0, 0, v1)
404
+ assert get_motion_params(N, position=v2) == (v2dd, v2d, v2)
405
+ #Test velocity parameter
406
+ assert get_motion_params(N, velocity=v1) == (0, v1, v1 * t)
407
+ assert get_motion_params(N, velocity=v1, position=v0, timevalue1=t1) == \
408
+ (0, v1, v0 + v1*(t - t1))
409
+ answer = get_motion_params(N, velocity=v1, position=v2, timevalue1=t1)
410
+ answer_expected = (0, v1, v1*t - v1*t1 + v2.subs(t, t1))
411
+ assert answer == answer_expected
412
+
413
+ answer = get_motion_params(N, velocity=v2, position=v0, timevalue1=t1)
414
+ integral_vector = Integral(a, (t, t1, t))*N.x + Integral(b, (t, t1, t))*N.y \
415
+ + Integral(c, (t, t1, t))*N.z
416
+ answer_expected = (v2d, v2, v0 + integral_vector)
417
+ assert answer == answer_expected
418
+
419
+ #Test acceleration parameter
420
+ assert get_motion_params(N, acceleration=v1) == \
421
+ (v1, v1 * t, v1 * t**2/2)
422
+ assert get_motion_params(N, acceleration=v1, velocity=v0,
423
+ position=v2, timevalue1=t1, timevalue2=t2) == \
424
+ (v1, (v0 + v1*t - v1*t2),
425
+ -v0*t1 + v1*t**2/2 + v1*t2*t1 - \
426
+ v1*t1**2/2 + t*(v0 - v1*t2) + \
427
+ v2.subs(t, t1))
428
+ assert get_motion_params(N, acceleration=v1, velocity=v0,
429
+ position=v01, timevalue1=t1, timevalue2=t2) == \
430
+ (v1, v0 + v1*t - v1*t2,
431
+ -v0*t1 + v01 + v1*t**2/2 + \
432
+ v1*t2*t1 - v1*t1**2/2 + \
433
+ t*(v0 - v1*t2))
434
+ answer = get_motion_params(N, acceleration=a*N.x, velocity=S1*N.x,
435
+ position=S2*N.x, timevalue1=t1, timevalue2=t2)
436
+ i1 = Integral(a, (t, t2, t))
437
+ answer_expected = (a*N.x, (S1 + i1)*N.x, \
438
+ (S2 + Integral(S1 + i1, (t, t1, t)))*N.x)
439
+ assert answer == answer_expected
440
+
441
+
442
+ def test_kin_eqs():
443
+ q0, q1, q2, q3 = dynamicsymbols('q0 q1 q2 q3')
444
+ q0d, q1d, q2d, q3d = dynamicsymbols('q0 q1 q2 q3', 1)
445
+ u1, u2, u3 = dynamicsymbols('u1 u2 u3')
446
+ ke = kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', 313)
447
+ assert ke == kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313')
448
+ kds = kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'quaternion')
449
+ assert kds == [-0.5 * q0 * u1 - 0.5 * q2 * u3 + 0.5 * q3 * u2 + q1d,
450
+ -0.5 * q0 * u2 + 0.5 * q1 * u3 - 0.5 * q3 * u1 + q2d,
451
+ -0.5 * q0 * u3 - 0.5 * q1 * u2 + 0.5 * q2 * u1 + q3d,
452
+ 0.5 * q1 * u1 + 0.5 * q2 * u2 + 0.5 * q3 * u3 + q0d]
453
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2], 'quaternion'))
454
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'quaternion', '123'))
455
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'foo'))
456
+ raises(TypeError, lambda: kinematic_equations(u1, [q0, q1, q2, q3], 'quaternion'))
457
+ raises(TypeError, lambda: kinematic_equations([u1], [q0, q1, q2, q3], 'quaternion'))
458
+ raises(TypeError, lambda: kinematic_equations([u1, u2, u3], q0, 'quaternion'))
459
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'body'))
460
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'space'))
461
+ raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2], 'body', '222'))
462
+ assert kinematic_equations([0, 0, 0], [q0, q1, q2], 'space') == [S.Zero, S.Zero, S.Zero]
463
+
464
+
465
+ def test_partial_velocity():
466
+ q1, q2, q3, u1, u2, u3 = dynamicsymbols('q1 q2 q3 u1 u2 u3')
467
+ u4, u5 = dynamicsymbols('u4, u5')
468
+ r = symbols('r')
469
+
470
+ N = ReferenceFrame('N')
471
+ Y = N.orientnew('Y', 'Axis', [q1, N.z])
472
+ L = Y.orientnew('L', 'Axis', [q2, Y.x])
473
+ R = L.orientnew('R', 'Axis', [q3, L.y])
474
+ R.set_ang_vel(N, u1 * L.x + u2 * L.y + u3 * L.z)
475
+
476
+ C = Point('C')
477
+ C.set_vel(N, u4 * L.x + u5 * (Y.z ^ L.x))
478
+ Dmc = C.locatenew('Dmc', r * L.z)
479
+ Dmc.v2pt_theory(C, N, R)
480
+
481
+ vel_list = [Dmc.vel(N), C.vel(N), R.ang_vel_in(N)]
482
+ u_list = [u1, u2, u3, u4, u5]
483
+ assert (partial_velocity(vel_list, u_list, N) ==
484
+ [[- r*L.y, r*L.x, 0, L.x, cos(q2)*L.y - sin(q2)*L.z],
485
+ [0, 0, 0, L.x, cos(q2)*L.y - sin(q2)*L.z],
486
+ [L.x, L.y, L.z, 0, 0]])
487
+
488
+ # Make sure that partial velocities can be computed regardless if the
489
+ # orientation between frames is defined or not.
490
+ A = ReferenceFrame('A')
491
+ B = ReferenceFrame('B')
492
+ v = u4 * A.x + u5 * B.y
493
+ assert partial_velocity((v, ), (u4, u5), A) == [[A.x, B.y]]
494
+
495
+ raises(TypeError, lambda: partial_velocity(Dmc.vel(N), u_list, N))
496
+ raises(TypeError, lambda: partial_velocity(vel_list, u1, N))
497
+
498
+ def test_dynamicsymbols():
499
+ #Tests to check the assumptions applied to dynamicsymbols
500
+ f1 = dynamicsymbols('f1')
501
+ f2 = dynamicsymbols('f2', real=True)
502
+ f3 = dynamicsymbols('f3', positive=True)
503
+ f4, f5 = dynamicsymbols('f4,f5', commutative=False)
504
+ f6 = dynamicsymbols('f6', integer=True)
505
+ assert f1.is_real is None
506
+ assert f2.is_real
507
+ assert f3.is_positive
508
+ assert f4*f5 != f5*f4
509
+ assert f6.is_integer
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_output.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.singleton import S
2
+ from sympy.physics.vector import Vector, ReferenceFrame, Dyadic
3
+ from sympy.testing.pytest import raises
4
+
5
+ A = ReferenceFrame('A')
6
+
7
+
8
+ def test_output_type():
9
+ A = ReferenceFrame('A')
10
+ v = A.x + A.y
11
+ d = v | v
12
+ zerov = Vector(0)
13
+ zerod = Dyadic(0)
14
+
15
+ # dot products
16
+ assert isinstance(d & d, Dyadic)
17
+ assert isinstance(d & zerod, Dyadic)
18
+ assert isinstance(zerod & d, Dyadic)
19
+ assert isinstance(d & v, Vector)
20
+ assert isinstance(v & d, Vector)
21
+ assert isinstance(d & zerov, Vector)
22
+ assert isinstance(zerov & d, Vector)
23
+ raises(TypeError, lambda: d & S.Zero)
24
+ raises(TypeError, lambda: S.Zero & d)
25
+ raises(TypeError, lambda: d & 0)
26
+ raises(TypeError, lambda: 0 & d)
27
+ assert not isinstance(v & v, (Vector, Dyadic))
28
+ assert not isinstance(v & zerov, (Vector, Dyadic))
29
+ assert not isinstance(zerov & v, (Vector, Dyadic))
30
+ raises(TypeError, lambda: v & S.Zero)
31
+ raises(TypeError, lambda: S.Zero & v)
32
+ raises(TypeError, lambda: v & 0)
33
+ raises(TypeError, lambda: 0 & v)
34
+
35
+ # cross products
36
+ raises(TypeError, lambda: d ^ d)
37
+ raises(TypeError, lambda: d ^ zerod)
38
+ raises(TypeError, lambda: zerod ^ d)
39
+ assert isinstance(d ^ v, Dyadic)
40
+ assert isinstance(v ^ d, Dyadic)
41
+ assert isinstance(d ^ zerov, Dyadic)
42
+ assert isinstance(zerov ^ d, Dyadic)
43
+ assert isinstance(zerov ^ d, Dyadic)
44
+ raises(TypeError, lambda: d ^ S.Zero)
45
+ raises(TypeError, lambda: S.Zero ^ d)
46
+ raises(TypeError, lambda: d ^ 0)
47
+ raises(TypeError, lambda: 0 ^ d)
48
+ assert isinstance(v ^ v, Vector)
49
+ assert isinstance(v ^ zerov, Vector)
50
+ assert isinstance(zerov ^ v, Vector)
51
+ raises(TypeError, lambda: v ^ S.Zero)
52
+ raises(TypeError, lambda: S.Zero ^ v)
53
+ raises(TypeError, lambda: v ^ 0)
54
+ raises(TypeError, lambda: 0 ^ v)
55
+
56
+ # outer products
57
+ raises(TypeError, lambda: d | d)
58
+ raises(TypeError, lambda: d | zerod)
59
+ raises(TypeError, lambda: zerod | d)
60
+ raises(TypeError, lambda: d | v)
61
+ raises(TypeError, lambda: v | d)
62
+ raises(TypeError, lambda: d | zerov)
63
+ raises(TypeError, lambda: zerov | d)
64
+ raises(TypeError, lambda: zerov | d)
65
+ raises(TypeError, lambda: d | S.Zero)
66
+ raises(TypeError, lambda: S.Zero | d)
67
+ raises(TypeError, lambda: d | 0)
68
+ raises(TypeError, lambda: 0 | d)
69
+ assert isinstance(v | v, Dyadic)
70
+ assert isinstance(v | zerov, Dyadic)
71
+ assert isinstance(zerov | v, Dyadic)
72
+ raises(TypeError, lambda: v | S.Zero)
73
+ raises(TypeError, lambda: S.Zero | v)
74
+ raises(TypeError, lambda: v | 0)
75
+ raises(TypeError, lambda: 0 | v)
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_point.py ADDED
@@ -0,0 +1,382 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.physics.vector import dynamicsymbols, Point, ReferenceFrame
2
+ from sympy.testing.pytest import raises, ignore_warnings
3
+ import warnings
4
+
5
+ def test_point_v1pt_theorys():
6
+ q, q2 = dynamicsymbols('q q2')
7
+ qd, q2d = dynamicsymbols('q q2', 1)
8
+ qdd, q2dd = dynamicsymbols('q q2', 2)
9
+ N = ReferenceFrame('N')
10
+ B = ReferenceFrame('B')
11
+ B.set_ang_vel(N, qd * B.z)
12
+ O = Point('O')
13
+ P = O.locatenew('P', B.x)
14
+ P.set_vel(B, 0)
15
+ O.set_vel(N, 0)
16
+ assert P.v1pt_theory(O, N, B) == qd * B.y
17
+ O.set_vel(N, N.x)
18
+ assert P.v1pt_theory(O, N, B) == N.x + qd * B.y
19
+ P.set_vel(B, B.z)
20
+ assert P.v1pt_theory(O, N, B) == B.z + N.x + qd * B.y
21
+
22
+
23
+ def test_point_a1pt_theorys():
24
+ q, q2 = dynamicsymbols('q q2')
25
+ qd, q2d = dynamicsymbols('q q2', 1)
26
+ qdd, q2dd = dynamicsymbols('q q2', 2)
27
+ N = ReferenceFrame('N')
28
+ B = ReferenceFrame('B')
29
+ B.set_ang_vel(N, qd * B.z)
30
+ O = Point('O')
31
+ P = O.locatenew('P', B.x)
32
+ P.set_vel(B, 0)
33
+ O.set_vel(N, 0)
34
+ assert P.a1pt_theory(O, N, B) == -(qd**2) * B.x + qdd * B.y
35
+ P.set_vel(B, q2d * B.z)
36
+ assert P.a1pt_theory(O, N, B) == -(qd**2) * B.x + qdd * B.y + q2dd * B.z
37
+ O.set_vel(N, q2d * B.x)
38
+ assert P.a1pt_theory(O, N, B) == ((q2dd - qd**2) * B.x + (q2d * qd + qdd) * B.y +
39
+ q2dd * B.z)
40
+
41
+
42
+ def test_point_v2pt_theorys():
43
+ q = dynamicsymbols('q')
44
+ qd = dynamicsymbols('q', 1)
45
+ N = ReferenceFrame('N')
46
+ B = N.orientnew('B', 'Axis', [q, N.z])
47
+ O = Point('O')
48
+ P = O.locatenew('P', 0)
49
+ O.set_vel(N, 0)
50
+ assert P.v2pt_theory(O, N, B) == 0
51
+ P = O.locatenew('P', B.x)
52
+ assert P.v2pt_theory(O, N, B) == (qd * B.z ^ B.x)
53
+ O.set_vel(N, N.x)
54
+ assert P.v2pt_theory(O, N, B) == N.x + qd * B.y
55
+
56
+
57
+ def test_point_a2pt_theorys():
58
+ q = dynamicsymbols('q')
59
+ qd = dynamicsymbols('q', 1)
60
+ qdd = dynamicsymbols('q', 2)
61
+ N = ReferenceFrame('N')
62
+ B = N.orientnew('B', 'Axis', [q, N.z])
63
+ O = Point('O')
64
+ P = O.locatenew('P', 0)
65
+ O.set_vel(N, 0)
66
+ assert P.a2pt_theory(O, N, B) == 0
67
+ P.set_pos(O, B.x)
68
+ assert P.a2pt_theory(O, N, B) == (-qd**2) * B.x + (qdd) * B.y
69
+
70
+
71
+ def test_point_funcs():
72
+ q, q2 = dynamicsymbols('q q2')
73
+ qd, q2d = dynamicsymbols('q q2', 1)
74
+ qdd, q2dd = dynamicsymbols('q q2', 2)
75
+ N = ReferenceFrame('N')
76
+ B = ReferenceFrame('B')
77
+ B.set_ang_vel(N, 5 * B.y)
78
+ O = Point('O')
79
+ P = O.locatenew('P', q * B.x + q2 * B.y)
80
+ assert P.pos_from(O) == q * B.x + q2 * B.y
81
+ P.set_vel(B, qd * B.x + q2d * B.y)
82
+ assert P.vel(B) == qd * B.x + q2d * B.y
83
+ O.set_vel(N, 0)
84
+ assert O.vel(N) == 0
85
+ assert P.a1pt_theory(O, N, B) == ((-25 * q + qdd) * B.x + (q2dd) * B.y +
86
+ (-10 * qd) * B.z)
87
+
88
+ B = N.orientnew('B', 'Axis', [q, N.z])
89
+ O = Point('O')
90
+ P = O.locatenew('P', 10 * B.x)
91
+ O.set_vel(N, 5 * N.x)
92
+ assert O.vel(N) == 5 * N.x
93
+ assert P.a2pt_theory(O, N, B) == (-10 * qd**2) * B.x + (10 * qdd) * B.y
94
+
95
+ B.set_ang_vel(N, 5 * B.y)
96
+ O = Point('O')
97
+ P = O.locatenew('P', q * B.x + q2 * B.y)
98
+ P.set_vel(B, qd * B.x + q2d * B.y)
99
+ O.set_vel(N, 0)
100
+ assert P.v1pt_theory(O, N, B) == qd * B.x + q2d * B.y - 5 * q * B.z
101
+
102
+
103
+ def test_point_pos():
104
+ q = dynamicsymbols('q')
105
+ N = ReferenceFrame('N')
106
+ B = N.orientnew('B', 'Axis', [q, N.z])
107
+ O = Point('O')
108
+ P = O.locatenew('P', 10 * N.x + 5 * B.x)
109
+ assert P.pos_from(O) == 10 * N.x + 5 * B.x
110
+ Q = P.locatenew('Q', 10 * N.y + 5 * B.y)
111
+ assert Q.pos_from(P) == 10 * N.y + 5 * B.y
112
+ assert Q.pos_from(O) == 10 * N.x + 10 * N.y + 5 * B.x + 5 * B.y
113
+ assert O.pos_from(Q) == -10 * N.x - 10 * N.y - 5 * B.x - 5 * B.y
114
+
115
+ def test_point_partial_velocity():
116
+
117
+ N = ReferenceFrame('N')
118
+ A = ReferenceFrame('A')
119
+
120
+ p = Point('p')
121
+
122
+ u1, u2 = dynamicsymbols('u1, u2')
123
+
124
+ p.set_vel(N, u1 * A.x + u2 * N.y)
125
+
126
+ assert p.partial_velocity(N, u1) == A.x
127
+ assert p.partial_velocity(N, u1, u2) == (A.x, N.y)
128
+ raises(ValueError, lambda: p.partial_velocity(A, u1))
129
+
130
+ def test_point_vel(): #Basic functionality
131
+ q1, q2 = dynamicsymbols('q1 q2')
132
+ N = ReferenceFrame('N')
133
+ B = ReferenceFrame('B')
134
+ Q = Point('Q')
135
+ O = Point('O')
136
+ Q.set_pos(O, q1 * N.x)
137
+ raises(ValueError , lambda: Q.vel(N)) # Velocity of O in N is not defined
138
+ O.set_vel(N, q2 * N.y)
139
+ assert O.vel(N) == q2 * N.y
140
+ raises(ValueError , lambda : O.vel(B)) #Velocity of O is not defined in B
141
+
142
+ def test_auto_point_vel():
143
+ t = dynamicsymbols._t
144
+ q1, q2 = dynamicsymbols('q1 q2')
145
+ N = ReferenceFrame('N')
146
+ B = ReferenceFrame('B')
147
+ O = Point('O')
148
+ Q = Point('Q')
149
+ Q.set_pos(O, q1 * N.x)
150
+ O.set_vel(N, q2 * N.y)
151
+ assert Q.vel(N) == q1.diff(t) * N.x + q2 * N.y # Velocity of Q using O
152
+ P1 = Point('P1')
153
+ P1.set_pos(O, q1 * B.x)
154
+ P2 = Point('P2')
155
+ P2.set_pos(P1, q2 * B.z)
156
+ raises(ValueError, lambda : P2.vel(B)) # O's velocity is defined in different frame, and no
157
+ #point in between has its velocity defined
158
+ raises(ValueError, lambda: P2.vel(N)) # Velocity of O not defined in N
159
+
160
+ def test_auto_point_vel_multiple_point_path():
161
+ t = dynamicsymbols._t
162
+ q1, q2 = dynamicsymbols('q1 q2')
163
+ B = ReferenceFrame('B')
164
+ P = Point('P')
165
+ P.set_vel(B, q1 * B.x)
166
+ P1 = Point('P1')
167
+ P1.set_pos(P, q2 * B.y)
168
+ P1.set_vel(B, q1 * B.z)
169
+ P2 = Point('P2')
170
+ P2.set_pos(P1, q1 * B.z)
171
+ P3 = Point('P3')
172
+ P3.set_pos(P2, 10 * q1 * B.y)
173
+ assert P3.vel(B) == 10 * q1.diff(t) * B.y + (q1 + q1.diff(t)) * B.z
174
+
175
+ def test_auto_vel_dont_overwrite():
176
+ t = dynamicsymbols._t
177
+ q1, q2, u1 = dynamicsymbols('q1, q2, u1')
178
+ N = ReferenceFrame('N')
179
+ P = Point('P1')
180
+ P.set_vel(N, u1 * N.x)
181
+ P1 = Point('P1')
182
+ P1.set_pos(P, q2 * N.y)
183
+ assert P1.vel(N) == q2.diff(t) * N.y + u1 * N.x
184
+ assert P.vel(N) == u1 * N.x
185
+ P1.set_vel(N, u1 * N.z)
186
+ assert P1.vel(N) == u1 * N.z
187
+
188
+ def test_auto_point_vel_if_tree_has_vel_but_inappropriate_pos_vector():
189
+ q1, q2 = dynamicsymbols('q1 q2')
190
+ B = ReferenceFrame('B')
191
+ S = ReferenceFrame('S')
192
+ P = Point('P')
193
+ P.set_vel(B, q1 * B.x)
194
+ P1 = Point('P1')
195
+ P1.set_pos(P, S.y)
196
+ raises(ValueError, lambda : P1.vel(B)) # P1.pos_from(P) can't be expressed in B
197
+ raises(ValueError, lambda : P1.vel(S)) # P.vel(S) not defined
198
+
199
+ def test_auto_point_vel_shortest_path():
200
+ t = dynamicsymbols._t
201
+ q1, q2, u1, u2 = dynamicsymbols('q1 q2 u1 u2')
202
+ B = ReferenceFrame('B')
203
+ P = Point('P')
204
+ P.set_vel(B, u1 * B.x)
205
+ P1 = Point('P1')
206
+ P1.set_pos(P, q2 * B.y)
207
+ P1.set_vel(B, q1 * B.z)
208
+ P2 = Point('P2')
209
+ P2.set_pos(P1, q1 * B.z)
210
+ P3 = Point('P3')
211
+ P3.set_pos(P2, 10 * q1 * B.y)
212
+ P4 = Point('P4')
213
+ P4.set_pos(P3, q1 * B.x)
214
+ O = Point('O')
215
+ O.set_vel(B, u2 * B.y)
216
+ O1 = Point('O1')
217
+ O1.set_pos(O, q2 * B.z)
218
+ P4.set_pos(O1, q1 * B.x + q2 * B.z)
219
+ with warnings.catch_warnings(): #There are two possible paths in this point tree, thus a warning is raised
220
+ warnings.simplefilter('error')
221
+ with ignore_warnings(UserWarning):
222
+ assert P4.vel(B) == q1.diff(t) * B.x + u2 * B.y + 2 * q2.diff(t) * B.z
223
+
224
+ def test_auto_point_vel_connected_frames():
225
+ t = dynamicsymbols._t
226
+ q, q1, q2, u = dynamicsymbols('q q1 q2 u')
227
+ N = ReferenceFrame('N')
228
+ B = ReferenceFrame('B')
229
+ O = Point('O')
230
+ O.set_vel(N, u * N.x)
231
+ P = Point('P')
232
+ P.set_pos(O, q1 * N.x + q2 * B.y)
233
+ raises(ValueError, lambda: P.vel(N))
234
+ N.orient(B, 'Axis', (q, B.x))
235
+ assert P.vel(N) == (u + q1.diff(t)) * N.x + q2.diff(t) * B.y - q2 * q.diff(t) * B.z
236
+
237
+ def test_auto_point_vel_multiple_paths_warning_arises():
238
+ q, u = dynamicsymbols('q u')
239
+ N = ReferenceFrame('N')
240
+ O = Point('O')
241
+ P = Point('P')
242
+ Q = Point('Q')
243
+ R = Point('R')
244
+ P.set_vel(N, u * N.x)
245
+ Q.set_vel(N, u *N.y)
246
+ R.set_vel(N, u * N.z)
247
+ O.set_pos(P, q * N.z)
248
+ O.set_pos(Q, q * N.y)
249
+ O.set_pos(R, q * N.x)
250
+ with warnings.catch_warnings(): #There are two possible paths in this point tree, thus a warning is raised
251
+ warnings.simplefilter("error")
252
+ raises(UserWarning ,lambda: O.vel(N))
253
+
254
+ def test_auto_vel_cyclic_warning_arises():
255
+ P = Point('P')
256
+ P1 = Point('P1')
257
+ P2 = Point('P2')
258
+ P3 = Point('P3')
259
+ N = ReferenceFrame('N')
260
+ P.set_vel(N, N.x)
261
+ P1.set_pos(P, N.x)
262
+ P2.set_pos(P1, N.y)
263
+ P3.set_pos(P2, N.z)
264
+ P1.set_pos(P3, N.x + N.y)
265
+ with warnings.catch_warnings(): #The path is cyclic at P1, thus a warning is raised
266
+ warnings.simplefilter("error")
267
+ raises(UserWarning ,lambda: P2.vel(N))
268
+
269
+ def test_auto_vel_cyclic_warning_msg():
270
+ P = Point('P')
271
+ P1 = Point('P1')
272
+ P2 = Point('P2')
273
+ P3 = Point('P3')
274
+ N = ReferenceFrame('N')
275
+ P.set_vel(N, N.x)
276
+ P1.set_pos(P, N.x)
277
+ P2.set_pos(P1, N.y)
278
+ P3.set_pos(P2, N.z)
279
+ P1.set_pos(P3, N.x + N.y)
280
+ with warnings.catch_warnings(record = True) as w: #The path is cyclic at P1, thus a warning is raised
281
+ warnings.simplefilter("always")
282
+ P2.vel(N)
283
+ msg = str(w[-1].message).replace("\n", " ")
284
+ assert issubclass(w[-1].category, UserWarning)
285
+ assert 'Kinematic loops are defined among the positions of points. This is likely not desired and may cause errors in your calculations.' in msg
286
+
287
+ def test_auto_vel_multiple_path_warning_msg():
288
+ N = ReferenceFrame('N')
289
+ O = Point('O')
290
+ P = Point('P')
291
+ Q = Point('Q')
292
+ P.set_vel(N, N.x)
293
+ Q.set_vel(N, N.y)
294
+ O.set_pos(P, N.z)
295
+ O.set_pos(Q, N.y)
296
+ with warnings.catch_warnings(record = True) as w: #There are two possible paths in this point tree, thus a warning is raised
297
+ warnings.simplefilter("always")
298
+ O.vel(N)
299
+ msg = str(w[-1].message).replace("\n", " ")
300
+ assert issubclass(w[-1].category, UserWarning)
301
+ assert 'Velocity' in msg
302
+ assert 'automatically calculated based on point' in msg
303
+ assert 'Velocities from these points are not necessarily the same. This may cause errors in your calculations.' in msg
304
+
305
+ def test_auto_vel_derivative():
306
+ q1, q2 = dynamicsymbols('q1:3')
307
+ u1, u2 = dynamicsymbols('u1:3', 1)
308
+ A = ReferenceFrame('A')
309
+ B = ReferenceFrame('B')
310
+ C = ReferenceFrame('C')
311
+ B.orient_axis(A, A.z, q1)
312
+ B.set_ang_vel(A, u1 * A.z)
313
+ C.orient_axis(B, B.z, q2)
314
+ C.set_ang_vel(B, u2 * B.z)
315
+
316
+ Am = Point('Am')
317
+ Am.set_vel(A, 0)
318
+ Bm = Point('Bm')
319
+ Bm.set_pos(Am, B.x)
320
+ Bm.set_vel(B, 0)
321
+ Bm.set_vel(C, 0)
322
+ Cm = Point('Cm')
323
+ Cm.set_pos(Bm, C.x)
324
+ Cm.set_vel(C, 0)
325
+ temp = Cm._vel_dict.copy()
326
+ assert Cm.vel(A) == (u1 * B.y + (u1 + u2) * C.y)
327
+ Cm._vel_dict = temp
328
+ Cm.v2pt_theory(Bm, B, C)
329
+ assert Cm.vel(A) == (u1 * B.y + (u1 + u2) * C.y)
330
+
331
+ def test_auto_point_acc_zero_vel():
332
+ N = ReferenceFrame('N')
333
+ O = Point('O')
334
+ O.set_vel(N, 0)
335
+ assert O.acc(N) == 0 * N.x
336
+
337
+ def test_auto_point_acc_compute_vel():
338
+ t = dynamicsymbols._t
339
+ q1 = dynamicsymbols('q1')
340
+ N = ReferenceFrame('N')
341
+ A = ReferenceFrame('A')
342
+ A.orient_axis(N, N.z, q1)
343
+
344
+ O = Point('O')
345
+ O.set_vel(N, 0)
346
+ P = Point('P')
347
+ P.set_pos(O, A.x)
348
+ assert P.acc(N) == -q1.diff(t) ** 2 * A.x + q1.diff(t, 2) * A.y
349
+
350
+ def test_auto_acc_derivative():
351
+ # Tests whether the Point.acc method gives the correct acceleration of the
352
+ # end point of two linkages in series, while getting minimal information.
353
+ q1, q2 = dynamicsymbols('q1:3')
354
+ u1, u2 = dynamicsymbols('q1:3', 1)
355
+ v1, v2 = dynamicsymbols('q1:3', 2)
356
+ A = ReferenceFrame('A')
357
+ B = ReferenceFrame('B')
358
+ C = ReferenceFrame('C')
359
+ B.orient_axis(A, A.z, q1)
360
+ C.orient_axis(B, B.z, q2)
361
+
362
+ Am = Point('Am')
363
+ Am.set_vel(A, 0)
364
+ Bm = Point('Bm')
365
+ Bm.set_pos(Am, B.x)
366
+ Bm.set_vel(B, 0)
367
+ Bm.set_vel(C, 0)
368
+ Cm = Point('Cm')
369
+ Cm.set_pos(Bm, C.x)
370
+ Cm.set_vel(C, 0)
371
+
372
+ # Copy dictionaries to later check the calculation using the 2pt_theories
373
+ Bm_vel_dict, Cm_vel_dict = Bm._vel_dict.copy(), Cm._vel_dict.copy()
374
+ Bm_acc_dict, Cm_acc_dict = Bm._acc_dict.copy(), Cm._acc_dict.copy()
375
+ check = -u1 ** 2 * B.x + v1 * B.y - (u1 + u2) ** 2 * C.x + (v1 + v2) * C.y
376
+ assert Cm.acc(A) == check
377
+ Bm._vel_dict, Cm._vel_dict = Bm_vel_dict, Cm_vel_dict
378
+ Bm._acc_dict, Cm._acc_dict = Bm_acc_dict, Cm_acc_dict
379
+ Bm.v2pt_theory(Am, A, B)
380
+ Cm.v2pt_theory(Bm, A, C)
381
+ Bm.a2pt_theory(Am, A, B)
382
+ assert Cm.a2pt_theory(Bm, A, C) == check
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_printing.py ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from sympy.core.function import Function
4
+ from sympy.core.symbol import symbols
5
+ from sympy.functions.elementary.miscellaneous import sqrt
6
+ from sympy.functions.elementary.trigonometric import (asin, cos, sin)
7
+ from sympy.physics.vector import ReferenceFrame, dynamicsymbols, Dyadic
8
+ from sympy.physics.vector.printing import (VectorLatexPrinter, vpprint,
9
+ vsprint, vsstrrepr, vlatex)
10
+
11
+
12
+ a, b, c = symbols('a, b, c')
13
+ alpha, omega, beta = dynamicsymbols('alpha, omega, beta')
14
+
15
+ A = ReferenceFrame('A')
16
+ N = ReferenceFrame('N')
17
+
18
+ v = a ** 2 * N.x + b * N.y + c * sin(alpha) * N.z
19
+ w = alpha * N.x + sin(omega) * N.y + alpha * beta * N.z
20
+ ww = alpha * N.x + asin(omega) * N.y - alpha.diff() * beta * N.z
21
+ o = a/b * N.x + (c+b)/a * N.y + c**2/b * N.z
22
+
23
+ y = a ** 2 * (N.x | N.y) + b * (N.y | N.y) + c * sin(alpha) * (N.z | N.y)
24
+ x = alpha * (N.x | N.x) + sin(omega) * (N.y | N.z) + alpha * beta * (N.z | N.x)
25
+ xx = N.x | (-N.y - N.z)
26
+ xx2 = N.x | (N.y + N.z)
27
+
28
+ def ascii_vpretty(expr):
29
+ return vpprint(expr, use_unicode=False, wrap_line=False)
30
+
31
+
32
+ def unicode_vpretty(expr):
33
+ return vpprint(expr, use_unicode=True, wrap_line=False)
34
+
35
+
36
+ def test_latex_printer():
37
+ r = Function('r')('t')
38
+ assert VectorLatexPrinter().doprint(r ** 2) == "r^{2}"
39
+ r2 = Function('r^2')('t')
40
+ assert VectorLatexPrinter().doprint(r2.diff()) == r'\dot{r^{2}}'
41
+ ra = Function('r__a')('t')
42
+ assert VectorLatexPrinter().doprint(ra.diff().diff()) == r'\ddot{r^{a}}'
43
+
44
+
45
+ def test_vector_pretty_print():
46
+
47
+ # TODO : The unit vectors should print with subscripts but they just
48
+ # print as `n_x` instead of making `x` a subscript with unicode.
49
+
50
+ # TODO : The pretty print division does not print correctly here:
51
+ # w = alpha * N.x + sin(omega) * N.y + alpha / beta * N.z
52
+
53
+ expected = """\
54
+ 2 \n\
55
+ a n_x + b n_y + c*sin(alpha) n_z\
56
+ """
57
+ uexpected = """\
58
+ 2 \n\
59
+ a n_x + b n_y + cβ‹…sin(Ξ±) n_z\
60
+ """
61
+
62
+ assert ascii_vpretty(v) == expected
63
+ assert unicode_vpretty(v) == uexpected
64
+
65
+ expected = 'alpha n_x + sin(omega) n_y + alpha*beta n_z'
66
+ uexpected = 'Ξ± n_x + sin(Ο‰) n_y + Ξ±β‹…Ξ² n_z'
67
+
68
+ assert ascii_vpretty(w) == expected
69
+ assert unicode_vpretty(w) == uexpected
70
+
71
+ expected = """\
72
+ 2 \n\
73
+ a b + c c \n\
74
+ - n_x + ----- n_y + -- n_z\n\
75
+ b a b \
76
+ """
77
+ uexpected = """\
78
+ 2 \n\
79
+ a b + c c \n\
80
+ ─ n_x + ───── n_y + ── n_z\n\
81
+ b a b \
82
+ """
83
+
84
+ assert ascii_vpretty(o) == expected
85
+ assert unicode_vpretty(o) == uexpected
86
+
87
+ # https://github.com/sympy/sympy/issues/26731
88
+ assert ascii_vpretty(-A.x) == '-a_x'
89
+ assert unicode_vpretty(-A.x) == '-a_x'
90
+
91
+ # https://github.com/sympy/sympy/issues/26799
92
+ assert ascii_vpretty(0*A.x) == '0'
93
+ assert unicode_vpretty(0*A.x) == '0'
94
+
95
+
96
+ def test_vector_latex():
97
+
98
+ a, b, c, d, omega = symbols('a, b, c, d, omega')
99
+
100
+ v = (a ** 2 + b / c) * A.x + sqrt(d) * A.y + cos(omega) * A.z
101
+
102
+ assert vlatex(v) == (r'(a^{2} + \frac{b}{c})\mathbf{\hat{a}_x} + '
103
+ r'\sqrt{d}\mathbf{\hat{a}_y} + '
104
+ r'\cos{\left(\omega \right)}'
105
+ r'\mathbf{\hat{a}_z}')
106
+
107
+ theta, omega, alpha, q = dynamicsymbols('theta, omega, alpha, q')
108
+
109
+ v = theta * A.x + omega * omega * A.y + (q * alpha) * A.z
110
+
111
+ assert vlatex(v) == (r'\theta\mathbf{\hat{a}_x} + '
112
+ r'\omega^{2}\mathbf{\hat{a}_y} + '
113
+ r'\alpha q\mathbf{\hat{a}_z}')
114
+
115
+ phi1, phi2, phi3 = dynamicsymbols('phi1, phi2, phi3')
116
+ theta1, theta2, theta3 = symbols('theta1, theta2, theta3')
117
+
118
+ v = (sin(theta1) * A.x +
119
+ cos(phi1) * cos(phi2) * A.y +
120
+ cos(theta1 + phi3) * A.z)
121
+
122
+ assert vlatex(v) == (r'\sin{\left(\theta_{1} \right)}'
123
+ r'\mathbf{\hat{a}_x} + \cos{'
124
+ r'\left(\phi_{1} \right)} \cos{'
125
+ r'\left(\phi_{2} \right)}\mathbf{\hat{a}_y} + '
126
+ r'\cos{\left(\theta_{1} + '
127
+ r'\phi_{3} \right)}\mathbf{\hat{a}_z}')
128
+
129
+ N = ReferenceFrame('N')
130
+
131
+ a, b, c, d, omega = symbols('a, b, c, d, omega')
132
+
133
+ v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z
134
+
135
+ expected = (r'(a^{2} + \frac{b}{c})\mathbf{\hat{n}_x} + '
136
+ r'\sqrt{d}\mathbf{\hat{n}_y} + '
137
+ r'\cos{\left(\omega \right)}'
138
+ r'\mathbf{\hat{n}_z}')
139
+
140
+ assert vlatex(v) == expected
141
+
142
+ # Try custom unit vectors.
143
+
144
+ N = ReferenceFrame('N', latexs=(r'\hat{i}', r'\hat{j}', r'\hat{k}'))
145
+
146
+ v = (a ** 2 + b / c) * N.x + sqrt(d) * N.y + cos(omega) * N.z
147
+
148
+ expected = (r'(a^{2} + \frac{b}{c})\hat{i} + '
149
+ r'\sqrt{d}\hat{j} + '
150
+ r'\cos{\left(\omega \right)}\hat{k}')
151
+ assert vlatex(v) == expected
152
+
153
+ expected = r'\alpha\mathbf{\hat{n}_x} + \operatorname{asin}{\left(\omega ' \
154
+ r'\right)}\mathbf{\hat{n}_y} - \beta \dot{\alpha}\mathbf{\hat{n}_z}'
155
+ assert vlatex(ww) == expected
156
+
157
+ expected = r'- \mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_y} - ' \
158
+ r'\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_z}'
159
+ assert vlatex(xx) == expected
160
+
161
+ expected = r'\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_y} + ' \
162
+ r'\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_z}'
163
+ assert vlatex(xx2) == expected
164
+
165
+
166
+ def test_vector_latex_arguments():
167
+ assert vlatex(N.x * 3.0, full_prec=False) == r'3.0\mathbf{\hat{n}_x}'
168
+ assert vlatex(N.x * 3.0, full_prec=True) == r'3.00000000000000\mathbf{\hat{n}_x}'
169
+
170
+
171
+ def test_vector_latex_with_functions():
172
+
173
+ N = ReferenceFrame('N')
174
+
175
+ omega, alpha = dynamicsymbols('omega, alpha')
176
+
177
+ v = omega.diff() * N.x
178
+
179
+ assert vlatex(v) == r'\dot{\omega}\mathbf{\hat{n}_x}'
180
+
181
+ v = omega.diff() ** alpha * N.x
182
+
183
+ assert vlatex(v) == (r'\dot{\omega}^{\alpha}'
184
+ r'\mathbf{\hat{n}_x}')
185
+
186
+
187
+ def test_dyadic_pretty_print():
188
+
189
+ expected = """\
190
+ 2
191
+ a n_x|n_y + b n_y|n_y + c*sin(alpha) n_z|n_y\
192
+ """
193
+
194
+ uexpected = """\
195
+ 2
196
+ a n_xβŠ—n_y + b n_yβŠ—n_y + cβ‹…sin(Ξ±) n_zβŠ—n_y\
197
+ """
198
+ assert ascii_vpretty(y) == expected
199
+ assert unicode_vpretty(y) == uexpected
200
+
201
+ expected = 'alpha n_x|n_x + sin(omega) n_y|n_z + alpha*beta n_z|n_x'
202
+ uexpected = 'Ξ± n_xβŠ—n_x + sin(Ο‰) n_yβŠ—n_z + Ξ±β‹…Ξ² n_zβŠ—n_x'
203
+ assert ascii_vpretty(x) == expected
204
+ assert unicode_vpretty(x) == uexpected
205
+
206
+ assert ascii_vpretty(Dyadic([])) == '0'
207
+ assert unicode_vpretty(Dyadic([])) == '0'
208
+
209
+ assert ascii_vpretty(xx) == '- n_x|n_y - n_x|n_z'
210
+ assert unicode_vpretty(xx) == '- n_xβŠ—n_y - n_xβŠ—n_z'
211
+
212
+ assert ascii_vpretty(xx2) == 'n_x|n_y + n_x|n_z'
213
+ assert unicode_vpretty(xx2) == 'n_xβŠ—n_y + n_xβŠ—n_z'
214
+
215
+
216
+ def test_dyadic_latex():
217
+
218
+ expected = (r'a^{2}\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_y} + '
219
+ r'b\mathbf{\hat{n}_y}\otimes \mathbf{\hat{n}_y} + '
220
+ r'c \sin{\left(\alpha \right)}'
221
+ r'\mathbf{\hat{n}_z}\otimes \mathbf{\hat{n}_y}')
222
+
223
+ assert vlatex(y) == expected
224
+
225
+ expected = (r'\alpha\mathbf{\hat{n}_x}\otimes \mathbf{\hat{n}_x} + '
226
+ r'\sin{\left(\omega \right)}\mathbf{\hat{n}_y}'
227
+ r'\otimes \mathbf{\hat{n}_z} + '
228
+ r'\alpha \beta\mathbf{\hat{n}_z}\otimes \mathbf{\hat{n}_x}')
229
+
230
+ assert vlatex(x) == expected
231
+
232
+ assert vlatex(Dyadic([])) == '0'
233
+
234
+
235
+ def test_dyadic_str():
236
+ assert vsprint(Dyadic([])) == '0'
237
+ assert vsprint(y) == 'a**2*(N.x|N.y) + b*(N.y|N.y) + c*sin(alpha)*(N.z|N.y)'
238
+ assert vsprint(x) == 'alpha*(N.x|N.x) + sin(omega)*(N.y|N.z) + alpha*beta*(N.z|N.x)'
239
+ assert vsprint(ww) == "alpha*N.x + asin(omega)*N.y - beta*alpha'*N.z"
240
+ assert vsprint(xx) == '- (N.x|N.y) - (N.x|N.z)'
241
+ assert vsprint(xx2) == '(N.x|N.y) + (N.x|N.z)'
242
+
243
+
244
+ def test_vlatex(): # vlatex is broken #12078
245
+ from sympy.physics.vector import vlatex
246
+
247
+ x = symbols('x')
248
+ J = symbols('J')
249
+
250
+ f = Function('f')
251
+ g = Function('g')
252
+ h = Function('h')
253
+
254
+ expected = r'J \left(\frac{d}{d x} g{\left(x \right)} - \frac{d}{d x} h{\left(x \right)}\right)'
255
+
256
+ expr = J*f(x).diff(x).subs(f(x), g(x)-h(x))
257
+
258
+ assert vlatex(expr) == expected
259
+
260
+
261
+ def test_issue_13354():
262
+ """
263
+ Test for proper pretty printing of physics vectors with ADD
264
+ instances in arguments.
265
+
266
+ Test is exactly the one suggested in the original bug report by
267
+ @moorepants.
268
+ """
269
+
270
+ a, b, c = symbols('a, b, c')
271
+ A = ReferenceFrame('A')
272
+ v = a * A.x + b * A.y + c * A.z
273
+ w = b * A.x + c * A.y + a * A.z
274
+ z = w + v
275
+
276
+ expected = """(a + b) a_x + (b + c) a_y + (a + c) a_z"""
277
+
278
+ assert ascii_vpretty(z) == expected
279
+
280
+
281
+ def test_vector_derivative_printing():
282
+ # First order
283
+ v = omega.diff() * N.x
284
+ assert unicode_vpretty(v) == 'Ο‰Μ‡ n_x'
285
+ assert ascii_vpretty(v) == "omega'(t) n_x"
286
+
287
+ # Second order
288
+ v = omega.diff().diff() * N.x
289
+
290
+ assert vlatex(v) == r'\ddot{\omega}\mathbf{\hat{n}_x}'
291
+ assert unicode_vpretty(v) == 'Ο‰Μˆ n_x'
292
+ assert ascii_vpretty(v) == "omega''(t) n_x"
293
+
294
+ # Third order
295
+ v = omega.diff().diff().diff() * N.x
296
+
297
+ assert vlatex(v) == r'\dddot{\omega}\mathbf{\hat{n}_x}'
298
+ assert unicode_vpretty(v) == 'ω⃛ n_x'
299
+ assert ascii_vpretty(v) == "omega'''(t) n_x"
300
+
301
+ # Fourth order
302
+ v = omega.diff().diff().diff().diff() * N.x
303
+
304
+ assert vlatex(v) == r'\ddddot{\omega}\mathbf{\hat{n}_x}'
305
+ assert unicode_vpretty(v) == 'Ο‰βƒœ n_x'
306
+ assert ascii_vpretty(v) == "omega''''(t) n_x"
307
+
308
+ # Fifth order
309
+ v = omega.diff().diff().diff().diff().diff() * N.x
310
+
311
+ assert vlatex(v) == r'\frac{d^{5}}{d t^{5}} \omega\mathbf{\hat{n}_x}'
312
+ expected = '''\
313
+ 5 \n\
314
+ d \n\
315
+ ---(omega) n_x\n\
316
+ 5 \n\
317
+ dt \
318
+ '''
319
+ uexpected = '''\
320
+ 5 \n\
321
+ d \n\
322
+ ───(Ο‰) n_x\n\
323
+ 5 \n\
324
+ dt \
325
+ '''
326
+ assert unicode_vpretty(v) == uexpected
327
+ assert ascii_vpretty(v) == expected
328
+
329
+
330
+ def test_vector_str_printing():
331
+ assert vsprint(w) == 'alpha*N.x + sin(omega)*N.y + alpha*beta*N.z'
332
+ assert vsprint(omega.diff() * N.x) == "omega'*N.x"
333
+ assert vsstrrepr(w) == 'alpha*N.x + sin(omega)*N.y + alpha*beta*N.z'
334
+
335
+
336
+ def test_vector_str_arguments():
337
+ assert vsprint(N.x * 3.0, full_prec=False) == '3.0*N.x'
338
+ assert vsprint(N.x * 3.0, full_prec=True) == '3.00000000000000*N.x'
339
+
340
+
341
+ def test_issue_14041():
342
+ import sympy.physics.mechanics as me
343
+
344
+ A_frame = me.ReferenceFrame('A')
345
+ thetad, phid = me.dynamicsymbols('theta, phi', 1)
346
+ L = symbols('L')
347
+
348
+ assert vlatex(L*(phid + thetad)**2*A_frame.x) == \
349
+ r"L \left(\dot{\phi} + \dot{\theta}\right)^{2}\mathbf{\hat{a}_x}"
350
+ assert vlatex((phid + thetad)**2*A_frame.x) == \
351
+ r"\left(\dot{\phi} + \dot{\theta}\right)^{2}\mathbf{\hat{a}_x}"
352
+ assert vlatex((phid*thetad)**a*A_frame.x) == \
353
+ r"\left(\dot{\phi} \dot{\theta}\right)^{a}\mathbf{\hat{a}_x}"
.venv/lib/python3.12/site-packages/sympy/physics/vector/tests/test_vector.py ADDED
@@ -0,0 +1,274 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import (Float, pi)
2
+ from sympy.core.symbol import symbols
3
+ from sympy.core.sorting import ordered
4
+ from sympy.functions.elementary.trigonometric import (cos, sin)
5
+ from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
6
+ from sympy.physics.vector import ReferenceFrame, Vector, dynamicsymbols, dot
7
+ from sympy.physics.vector.vector import VectorTypeError
8
+ from sympy.abc import x, y, z
9
+ from sympy.testing.pytest import raises
10
+
11
+ A = ReferenceFrame('A')
12
+
13
+
14
+ def test_free_dynamicsymbols():
15
+ A, B, C, D = symbols('A, B, C, D', cls=ReferenceFrame)
16
+ a, b, c, d, e, f = dynamicsymbols('a, b, c, d, e, f')
17
+ B.orient_axis(A, a, A.x)
18
+ C.orient_axis(B, b, B.y)
19
+ D.orient_axis(C, c, C.x)
20
+
21
+ v = d*D.x + e*D.y + f*D.z
22
+
23
+ assert set(ordered(v.free_dynamicsymbols(A))) == {a, b, c, d, e, f}
24
+ assert set(ordered(v.free_dynamicsymbols(B))) == {b, c, d, e, f}
25
+ assert set(ordered(v.free_dynamicsymbols(C))) == {c, d, e, f}
26
+ assert set(ordered(v.free_dynamicsymbols(D))) == {d, e, f}
27
+
28
+
29
+ def test_Vector():
30
+ assert A.x != A.y
31
+ assert A.y != A.z
32
+ assert A.z != A.x
33
+
34
+ assert A.x + 0 == A.x
35
+
36
+ v1 = x*A.x + y*A.y + z*A.z
37
+ v2 = x**2*A.x + y**2*A.y + z**2*A.z
38
+ v3 = v1 + v2
39
+ v4 = v1 - v2
40
+
41
+ assert isinstance(v1, Vector)
42
+ assert dot(v1, A.x) == x
43
+ assert dot(v1, A.y) == y
44
+ assert dot(v1, A.z) == z
45
+
46
+ assert isinstance(v2, Vector)
47
+ assert dot(v2, A.x) == x**2
48
+ assert dot(v2, A.y) == y**2
49
+ assert dot(v2, A.z) == z**2
50
+
51
+ assert isinstance(v3, Vector)
52
+ # We probably shouldn't be using simplify in dot...
53
+ assert dot(v3, A.x) == x**2 + x
54
+ assert dot(v3, A.y) == y**2 + y
55
+ assert dot(v3, A.z) == z**2 + z
56
+
57
+ assert isinstance(v4, Vector)
58
+ # We probably shouldn't be using simplify in dot...
59
+ assert dot(v4, A.x) == x - x**2
60
+ assert dot(v4, A.y) == y - y**2
61
+ assert dot(v4, A.z) == z - z**2
62
+
63
+ assert v1.to_matrix(A) == Matrix([[x], [y], [z]])
64
+ q = symbols('q')
65
+ B = A.orientnew('B', 'Axis', (q, A.x))
66
+ assert v1.to_matrix(B) == Matrix([[x],
67
+ [ y * cos(q) + z * sin(q)],
68
+ [-y * sin(q) + z * cos(q)]])
69
+
70
+ #Test the separate method
71
+ B = ReferenceFrame('B')
72
+ v5 = x*A.x + y*A.y + z*B.z
73
+ assert Vector(0).separate() == {}
74
+ assert v1.separate() == {A: v1}
75
+ assert v5.separate() == {A: x*A.x + y*A.y, B: z*B.z}
76
+
77
+ #Test the free_symbols property
78
+ v6 = x*A.x + y*A.y + z*A.z
79
+ assert v6.free_symbols(A) == {x,y,z}
80
+
81
+ raises(TypeError, lambda: v3.applyfunc(v1))
82
+
83
+
84
+ def test_Vector_diffs():
85
+ q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
86
+ q1d, q2d, q3d, q4d = dynamicsymbols('q1 q2 q3 q4', 1)
87
+ q1dd, q2dd, q3dd, q4dd = dynamicsymbols('q1 q2 q3 q4', 2)
88
+ N = ReferenceFrame('N')
89
+ A = N.orientnew('A', 'Axis', [q3, N.z])
90
+ B = A.orientnew('B', 'Axis', [q2, A.x])
91
+ v1 = q2 * A.x + q3 * N.y
92
+ v2 = q3 * B.x + v1
93
+ v3 = v1.dt(B)
94
+ v4 = v2.dt(B)
95
+ v5 = q1*A.x + q2*A.y + q3*A.z
96
+
97
+ assert v1.dt(N) == q2d * A.x + q2 * q3d * A.y + q3d * N.y
98
+ assert v1.dt(A) == q2d * A.x + q3 * q3d * N.x + q3d * N.y
99
+ assert v1.dt(B) == (q2d * A.x + q3 * q3d * N.x + q3d *
100
+ N.y - q3 * cos(q3) * q2d * N.z)
101
+ assert v2.dt(N) == (q2d * A.x + (q2 + q3) * q3d * A.y + q3d * B.x + q3d *
102
+ N.y)
103
+ assert v2.dt(A) == q2d * A.x + q3d * B.x + q3 * q3d * N.x + q3d * N.y
104
+ assert v2.dt(B) == (q2d * A.x + q3d * B.x + q3 * q3d * N.x + q3d * N.y -
105
+ q3 * cos(q3) * q2d * N.z)
106
+ assert v3.dt(N) == (q2dd * A.x + q2d * q3d * A.y + (q3d**2 + q3 * q3dd) *
107
+ N.x + q3dd * N.y + (q3 * sin(q3) * q2d * q3d -
108
+ cos(q3) * q2d * q3d - q3 * cos(q3) * q2dd) * N.z)
109
+ assert v3.dt(A) == (q2dd * A.x + (2 * q3d**2 + q3 * q3dd) * N.x + (q3dd -
110
+ q3 * q3d**2) * N.y + (q3 * sin(q3) * q2d * q3d -
111
+ cos(q3) * q2d * q3d - q3 * cos(q3) * q2dd) * N.z)
112
+ assert (v3.dt(B) - (q2dd*A.x - q3*cos(q3)*q2d**2*A.y + (2*q3d**2 +
113
+ q3*q3dd)*N.x + (q3dd - q3*q3d**2)*N.y + (2*q3*sin(q3)*q2d*q3d -
114
+ 2*cos(q3)*q2d*q3d - q3*cos(q3)*q2dd)*N.z)).express(B).simplify() == 0
115
+ assert v4.dt(N) == (q2dd * A.x + q3d * (q2d + q3d) * A.y + q3dd * B.x +
116
+ (q3d**2 + q3 * q3dd) * N.x + q3dd * N.y + (q3 *
117
+ sin(q3) * q2d * q3d - cos(q3) * q2d * q3d - q3 *
118
+ cos(q3) * q2dd) * N.z)
119
+ assert v4.dt(A) == (q2dd * A.x + q3dd * B.x + (2 * q3d**2 + q3 * q3dd) *
120
+ N.x + (q3dd - q3 * q3d**2) * N.y + (q3 * sin(q3) *
121
+ q2d * q3d - cos(q3) * q2d * q3d - q3 * cos(q3) *
122
+ q2dd) * N.z)
123
+ assert (v4.dt(B) - (q2dd*A.x - q3*cos(q3)*q2d**2*A.y + q3dd*B.x +
124
+ (2*q3d**2 + q3*q3dd)*N.x + (q3dd - q3*q3d**2)*N.y +
125
+ (2*q3*sin(q3)*q2d*q3d - 2*cos(q3)*q2d*q3d -
126
+ q3*cos(q3)*q2dd)*N.z)).express(B).simplify() == 0
127
+ assert v5.dt(B) == q1d*A.x + (q3*q2d + q2d)*A.y + (-q2*q2d + q3d)*A.z
128
+ assert v5.dt(A) == q1d*A.x + q2d*A.y + q3d*A.z
129
+ assert v5.dt(N) == (-q2*q3d + q1d)*A.x + (q1*q3d + q2d)*A.y + q3d*A.z
130
+ assert v3.diff(q1d, N) == 0
131
+ assert v3.diff(q2d, N) == A.x - q3 * cos(q3) * N.z
132
+ assert v3.diff(q3d, N) == q3 * N.x + N.y
133
+ assert v3.diff(q1d, A) == 0
134
+ assert v3.diff(q2d, A) == A.x - q3 * cos(q3) * N.z
135
+ assert v3.diff(q3d, A) == q3 * N.x + N.y
136
+ assert v3.diff(q1d, B) == 0
137
+ assert v3.diff(q2d, B) == A.x - q3 * cos(q3) * N.z
138
+ assert v3.diff(q3d, B) == q3 * N.x + N.y
139
+ assert v4.diff(q1d, N) == 0
140
+ assert v4.diff(q2d, N) == A.x - q3 * cos(q3) * N.z
141
+ assert v4.diff(q3d, N) == B.x + q3 * N.x + N.y
142
+ assert v4.diff(q1d, A) == 0
143
+ assert v4.diff(q2d, A) == A.x - q3 * cos(q3) * N.z
144
+ assert v4.diff(q3d, A) == B.x + q3 * N.x + N.y
145
+ assert v4.diff(q1d, B) == 0
146
+ assert v4.diff(q2d, B) == A.x - q3 * cos(q3) * N.z
147
+ assert v4.diff(q3d, B) == B.x + q3 * N.x + N.y
148
+
149
+ # diff() should only express vector components in the derivative frame if
150
+ # the orientation of the component's frame depends on the variable
151
+ v6 = q2**2*N.y + q2**2*A.y + q2**2*B.y
152
+ # already expressed in N
153
+ n_measy = 2*q2
154
+ # A_C_N does not depend on q2, so don't express in N
155
+ a_measy = 2*q2
156
+ # B_C_N depends on q2, so express in N
157
+ b_measx = (q2**2*B.y).dot(N.x).diff(q2)
158
+ b_measy = (q2**2*B.y).dot(N.y).diff(q2)
159
+ b_measz = (q2**2*B.y).dot(N.z).diff(q2)
160
+ n_comp, a_comp = v6.diff(q2, N).args
161
+ assert len(v6.diff(q2, N).args) == 2 # only N and A parts
162
+ assert n_comp[1] == N
163
+ assert a_comp[1] == A
164
+ assert n_comp[0] == Matrix([b_measx, b_measy + n_measy, b_measz])
165
+ assert a_comp[0] == Matrix([0, a_measy, 0])
166
+
167
+
168
+ def test_vector_var_in_dcm():
169
+
170
+ N = ReferenceFrame('N')
171
+ A = ReferenceFrame('A')
172
+ B = ReferenceFrame('B')
173
+ u1, u2, u3, u4 = dynamicsymbols('u1 u2 u3 u4')
174
+
175
+ v = u1 * u2 * A.x + u3 * N.y + u4**2 * N.z
176
+
177
+ assert v.diff(u1, N, var_in_dcm=False) == u2 * A.x
178
+ assert v.diff(u1, A, var_in_dcm=False) == u2 * A.x
179
+ assert v.diff(u3, N, var_in_dcm=False) == N.y
180
+ assert v.diff(u3, A, var_in_dcm=False) == N.y
181
+ assert v.diff(u3, B, var_in_dcm=False) == N.y
182
+ assert v.diff(u4, N, var_in_dcm=False) == 2 * u4 * N.z
183
+
184
+ raises(ValueError, lambda: v.diff(u1, N))
185
+
186
+
187
+ def test_vector_simplify():
188
+ x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A')
189
+ N = ReferenceFrame('N')
190
+
191
+ test1 = (1 / x + 1 / y) * N.x
192
+ assert (test1 & N.x) != (x + y) / (x * y)
193
+ test1 = test1.simplify()
194
+ assert (test1 & N.x) == (x + y) / (x * y)
195
+
196
+ test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * N.x
197
+ test2 = test2.simplify()
198
+ assert (test2 & N.x) == (A**2 * s**4 / (4 * pi * k * m**3))
199
+
200
+ test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * N.x
201
+ test3 = test3.simplify()
202
+ assert (test3 & N.x) == 0
203
+
204
+ test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * N.x
205
+ test4 = test4.simplify()
206
+ assert (test4 & N.x) == -2 * y
207
+
208
+
209
+ def test_vector_evalf():
210
+ a, b = symbols('a b')
211
+ v = pi * A.x
212
+ assert v.evalf(2) == Float('3.1416', 2) * A.x
213
+ v = pi * A.x + 5 * a * A.y - b * A.z
214
+ assert v.evalf(3) == Float('3.1416', 3) * A.x + Float('5', 3) * a * A.y - b * A.z
215
+ assert v.evalf(5, subs={a: 1.234, b:5.8973}) == Float('3.1415926536', 5) * A.x + Float('6.17', 5) * A.y - Float('5.8973', 5) * A.z
216
+
217
+
218
+ def test_vector_angle():
219
+ A = ReferenceFrame('A')
220
+ v1 = A.x + A.y
221
+ v2 = A.z
222
+ assert v1.angle_between(v2) == pi/2
223
+ B = ReferenceFrame('B')
224
+ B.orient_axis(A, A.x, pi)
225
+ v3 = A.x
226
+ v4 = B.x
227
+ assert v3.angle_between(v4) == 0
228
+
229
+
230
+ def test_vector_xreplace():
231
+ x, y, z = symbols('x y z')
232
+ v = x**2 * A.x + x*y * A.y + x*y*z * A.z
233
+ assert v.xreplace({x : cos(x)}) == cos(x)**2 * A.x + y*cos(x) * A.y + y*z*cos(x) * A.z
234
+ assert v.xreplace({x*y : pi}) == x**2 * A.x + pi * A.y + x*y*z * A.z
235
+ assert v.xreplace({x*y*z : 1}) == x**2*A.x + x*y*A.y + A.z
236
+ assert v.xreplace({x:1, z:0}) == A.x + y * A.y
237
+ raises(TypeError, lambda: v.xreplace())
238
+ raises(TypeError, lambda: v.xreplace([x, y]))
239
+
240
+ def test_issue_23366():
241
+ u1 = dynamicsymbols('u1')
242
+ N = ReferenceFrame('N')
243
+ N_v_A = u1*N.x
244
+ raises(VectorTypeError, lambda: N_v_A.diff(N, u1))
245
+
246
+
247
+ def test_vector_outer():
248
+ a, b, c, d, e, f = symbols('a, b, c, d, e, f')
249
+ N = ReferenceFrame('N')
250
+ v1 = a*N.x + b*N.y + c*N.z
251
+ v2 = d*N.x + e*N.y + f*N.z
252
+ v1v2 = Matrix([[a*d, a*e, a*f],
253
+ [b*d, b*e, b*f],
254
+ [c*d, c*e, c*f]])
255
+ assert v1.outer(v2).to_matrix(N) == v1v2
256
+ assert (v1 | v2).to_matrix(N) == v1v2
257
+ v2v1 = Matrix([[d*a, d*b, d*c],
258
+ [e*a, e*b, e*c],
259
+ [f*a, f*b, f*c]])
260
+ assert v2.outer(v1).to_matrix(N) == v2v1
261
+ assert (v2 | v1).to_matrix(N) == v2v1
262
+
263
+
264
+ def test_overloaded_operators():
265
+ a, b, c, d, e, f = symbols('a, b, c, d, e, f')
266
+ N = ReferenceFrame('N')
267
+ v1 = a*N.x + b*N.y + c*N.z
268
+ v2 = d*N.x + e*N.y + f*N.z
269
+
270
+ assert v1 + v2 == v2 + v1
271
+ assert v1 - v2 == -v2 + v1
272
+ assert v1 & v2 == v2 & v1
273
+ assert v1 ^ v2 == v1.cross(v2)
274
+ assert v2 ^ v1 == v2.cross(v1)
.venv/lib/python3.12/site-packages/sympy/physics/vector/vector.py ADDED
@@ -0,0 +1,806 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy import (S, sympify, expand, sqrt, Add, zeros, acos,
2
+ ImmutableMatrix as Matrix, simplify)
3
+ from sympy.simplify.trigsimp import trigsimp
4
+ from sympy.printing.defaults import Printable
5
+ from sympy.utilities.misc import filldedent
6
+ from sympy.core.evalf import EvalfMixin
7
+
8
+ from mpmath.libmp.libmpf import prec_to_dps
9
+
10
+
11
+ __all__ = ['Vector']
12
+
13
+
14
+ class Vector(Printable, EvalfMixin):
15
+ """The class used to define vectors.
16
+
17
+ It along with ReferenceFrame are the building blocks of describing a
18
+ classical mechanics system in PyDy and sympy.physics.vector.
19
+
20
+ Attributes
21
+ ==========
22
+
23
+ simp : Boolean
24
+ Let certain methods use trigsimp on their outputs
25
+
26
+ """
27
+
28
+ simp = False
29
+ is_number = False
30
+
31
+ def __init__(self, inlist):
32
+ """This is the constructor for the Vector class. You should not be
33
+ calling this, it should only be used by other functions. You should be
34
+ treating Vectors like you would with if you were doing the math by
35
+ hand, and getting the first 3 from the standard basis vectors from a
36
+ ReferenceFrame.
37
+
38
+ The only exception is to create a zero vector:
39
+ zv = Vector(0)
40
+
41
+ """
42
+
43
+ self.args = []
44
+ if inlist == 0:
45
+ inlist = []
46
+ if isinstance(inlist, dict):
47
+ d = inlist
48
+ else:
49
+ d = {}
50
+ for inp in inlist:
51
+ if inp[1] in d:
52
+ d[inp[1]] += inp[0]
53
+ else:
54
+ d[inp[1]] = inp[0]
55
+
56
+ for k, v in d.items():
57
+ if v != Matrix([0, 0, 0]):
58
+ self.args.append((v, k))
59
+
60
+ @property
61
+ def func(self):
62
+ """Returns the class Vector. """
63
+ return Vector
64
+
65
+ def __hash__(self):
66
+ return hash(tuple(self.args))
67
+
68
+ def __add__(self, other):
69
+ """The add operator for Vector. """
70
+ if other == 0:
71
+ return self
72
+ other = _check_vector(other)
73
+ return Vector(self.args + other.args)
74
+
75
+ def dot(self, other):
76
+ """Dot product of two vectors.
77
+
78
+ Returns a scalar, the dot product of the two Vectors
79
+
80
+ Parameters
81
+ ==========
82
+
83
+ other : Vector
84
+ The Vector which we are dotting with
85
+
86
+ Examples
87
+ ========
88
+
89
+ >>> from sympy.physics.vector import ReferenceFrame, dot
90
+ >>> from sympy import symbols
91
+ >>> q1 = symbols('q1')
92
+ >>> N = ReferenceFrame('N')
93
+ >>> dot(N.x, N.x)
94
+ 1
95
+ >>> dot(N.x, N.y)
96
+ 0
97
+ >>> A = N.orientnew('A', 'Axis', [q1, N.x])
98
+ >>> dot(N.y, A.y)
99
+ cos(q1)
100
+
101
+ """
102
+
103
+ from sympy.physics.vector.dyadic import Dyadic, _check_dyadic
104
+ if isinstance(other, Dyadic):
105
+ other = _check_dyadic(other)
106
+ ol = Vector(0)
107
+ for v in other.args:
108
+ ol += v[0] * v[2] * (v[1].dot(self))
109
+ return ol
110
+ other = _check_vector(other)
111
+ out = S.Zero
112
+ for v1 in self.args:
113
+ for v2 in other.args:
114
+ out += ((v2[0].T) * (v2[1].dcm(v1[1])) * (v1[0]))[0]
115
+ if Vector.simp:
116
+ return trigsimp(out, recursive=True)
117
+ else:
118
+ return out
119
+
120
+ def __truediv__(self, other):
121
+ """This uses mul and inputs self and 1 divided by other. """
122
+ return self.__mul__(S.One / other)
123
+
124
+ def __eq__(self, other):
125
+ """Tests for equality.
126
+
127
+ It is very import to note that this is only as good as the SymPy
128
+ equality test; False does not always mean they are not equivalent
129
+ Vectors.
130
+ If other is 0, and self is empty, returns True.
131
+ If other is 0 and self is not empty, returns False.
132
+ If none of the above, only accepts other as a Vector.
133
+
134
+ """
135
+
136
+ if other == 0:
137
+ other = Vector(0)
138
+ try:
139
+ other = _check_vector(other)
140
+ except TypeError:
141
+ return False
142
+ if (self.args == []) and (other.args == []):
143
+ return True
144
+ elif (self.args == []) or (other.args == []):
145
+ return False
146
+
147
+ frame = self.args[0][1]
148
+ for v in frame:
149
+ if expand((self - other).dot(v)) != 0:
150
+ return False
151
+ return True
152
+
153
+ def __mul__(self, other):
154
+ """Multiplies the Vector by a sympifyable expression.
155
+
156
+ Parameters
157
+ ==========
158
+
159
+ other : Sympifyable
160
+ The scalar to multiply this Vector with
161
+
162
+ Examples
163
+ ========
164
+
165
+ >>> from sympy.physics.vector import ReferenceFrame
166
+ >>> from sympy import Symbol
167
+ >>> N = ReferenceFrame('N')
168
+ >>> b = Symbol('b')
169
+ >>> V = 10 * b * N.x
170
+ >>> print(V)
171
+ 10*b*N.x
172
+
173
+ """
174
+
175
+ newlist = list(self.args)
176
+ other = sympify(other)
177
+ for i in range(len(newlist)):
178
+ newlist[i] = (other * newlist[i][0], newlist[i][1])
179
+ return Vector(newlist)
180
+
181
+ def __neg__(self):
182
+ return self * -1
183
+
184
+ def outer(self, other):
185
+ """Outer product between two Vectors.
186
+
187
+ A rank increasing operation, which returns a Dyadic from two Vectors
188
+
189
+ Parameters
190
+ ==========
191
+
192
+ other : Vector
193
+ The Vector to take the outer product with
194
+
195
+ Examples
196
+ ========
197
+
198
+ >>> from sympy.physics.vector import ReferenceFrame, outer
199
+ >>> N = ReferenceFrame('N')
200
+ >>> outer(N.x, N.x)
201
+ (N.x|N.x)
202
+
203
+ """
204
+
205
+ from sympy.physics.vector.dyadic import Dyadic
206
+ other = _check_vector(other)
207
+ ol = Dyadic(0)
208
+ for v in self.args:
209
+ for v2 in other.args:
210
+ # it looks this way because if we are in the same frame and
211
+ # use the enumerate function on the same frame in a nested
212
+ # fashion, then bad things happen
213
+ ol += Dyadic([(v[0][0] * v2[0][0], v[1].x, v2[1].x)])
214
+ ol += Dyadic([(v[0][0] * v2[0][1], v[1].x, v2[1].y)])
215
+ ol += Dyadic([(v[0][0] * v2[0][2], v[1].x, v2[1].z)])
216
+ ol += Dyadic([(v[0][1] * v2[0][0], v[1].y, v2[1].x)])
217
+ ol += Dyadic([(v[0][1] * v2[0][1], v[1].y, v2[1].y)])
218
+ ol += Dyadic([(v[0][1] * v2[0][2], v[1].y, v2[1].z)])
219
+ ol += Dyadic([(v[0][2] * v2[0][0], v[1].z, v2[1].x)])
220
+ ol += Dyadic([(v[0][2] * v2[0][1], v[1].z, v2[1].y)])
221
+ ol += Dyadic([(v[0][2] * v2[0][2], v[1].z, v2[1].z)])
222
+ return ol
223
+
224
+ def _latex(self, printer):
225
+ """Latex Printing method. """
226
+
227
+ ar = self.args # just to shorten things
228
+ if len(ar) == 0:
229
+ return str(0)
230
+ ol = [] # output list, to be concatenated to a string
231
+ for v in ar:
232
+ for j in 0, 1, 2:
233
+ # if the coef of the basis vector is 1, we skip the 1
234
+ if v[0][j] == 1:
235
+ ol.append(' + ' + v[1].latex_vecs[j])
236
+ # if the coef of the basis vector is -1, we skip the 1
237
+ elif v[0][j] == -1:
238
+ ol.append(' - ' + v[1].latex_vecs[j])
239
+ elif v[0][j] != 0:
240
+ # If the coefficient of the basis vector is not 1 or -1;
241
+ # also, we might wrap it in parentheses, for readability.
242
+ arg_str = printer._print(v[0][j])
243
+ if isinstance(v[0][j], Add):
244
+ arg_str = "(%s)" % arg_str
245
+ if arg_str[0] == '-':
246
+ arg_str = arg_str[1:]
247
+ str_start = ' - '
248
+ else:
249
+ str_start = ' + '
250
+ ol.append(str_start + arg_str + v[1].latex_vecs[j])
251
+ outstr = ''.join(ol)
252
+ if outstr.startswith(' + '):
253
+ outstr = outstr[3:]
254
+ elif outstr.startswith(' '):
255
+ outstr = outstr[1:]
256
+ return outstr
257
+
258
+ def _pretty(self, printer):
259
+ """Pretty Printing method. """
260
+ from sympy.printing.pretty.stringpict import prettyForm
261
+
262
+ terms = []
263
+
264
+ def juxtapose(a, b):
265
+ pa = printer._print(a)
266
+ pb = printer._print(b)
267
+ if a.is_Add:
268
+ pa = prettyForm(*pa.parens())
269
+ return printer._print_seq([pa, pb], delimiter=' ')
270
+
271
+ for M, N in self.args:
272
+ for i in range(3):
273
+ if M[i] == 0:
274
+ continue
275
+ elif M[i] == 1:
276
+ terms.append(prettyForm(N.pretty_vecs[i]))
277
+ elif M[i] == -1:
278
+ terms.append(prettyForm("-1") * prettyForm(N.pretty_vecs[i]))
279
+ else:
280
+ terms.append(juxtapose(M[i], N.pretty_vecs[i]))
281
+
282
+ if terms:
283
+ pretty_result = prettyForm.__add__(*terms)
284
+ else:
285
+ pretty_result = prettyForm("0")
286
+
287
+ return pretty_result
288
+
289
+ def __rsub__(self, other):
290
+ return (-1 * self) + other
291
+
292
+ def _sympystr(self, printer, order=True):
293
+ """Printing method. """
294
+ if not order or len(self.args) == 1:
295
+ ar = list(self.args)
296
+ elif len(self.args) == 0:
297
+ return printer._print(0)
298
+ else:
299
+ d = {v[1]: v[0] for v in self.args}
300
+ keys = sorted(d.keys(), key=lambda x: x.index)
301
+ ar = []
302
+ for key in keys:
303
+ ar.append((d[key], key))
304
+ ol = [] # output list, to be concatenated to a string
305
+ for v in ar:
306
+ for j in 0, 1, 2:
307
+ # if the coef of the basis vector is 1, we skip the 1
308
+ if v[0][j] == 1:
309
+ ol.append(' + ' + v[1].str_vecs[j])
310
+ # if the coef of the basis vector is -1, we skip the 1
311
+ elif v[0][j] == -1:
312
+ ol.append(' - ' + v[1].str_vecs[j])
313
+ elif v[0][j] != 0:
314
+ # If the coefficient of the basis vector is not 1 or -1;
315
+ # also, we might wrap it in parentheses, for readability.
316
+ arg_str = printer._print(v[0][j])
317
+ if isinstance(v[0][j], Add):
318
+ arg_str = "(%s)" % arg_str
319
+ if arg_str[0] == '-':
320
+ arg_str = arg_str[1:]
321
+ str_start = ' - '
322
+ else:
323
+ str_start = ' + '
324
+ ol.append(str_start + arg_str + '*' + v[1].str_vecs[j])
325
+ outstr = ''.join(ol)
326
+ if outstr.startswith(' + '):
327
+ outstr = outstr[3:]
328
+ elif outstr.startswith(' '):
329
+ outstr = outstr[1:]
330
+ return outstr
331
+
332
+ def __sub__(self, other):
333
+ """The subtraction operator. """
334
+ return self.__add__(other * -1)
335
+
336
+ def cross(self, other):
337
+ """The cross product operator for two Vectors.
338
+
339
+ Returns a Vector, expressed in the same ReferenceFrames as self.
340
+
341
+ Parameters
342
+ ==========
343
+
344
+ other : Vector
345
+ The Vector which we are crossing with
346
+
347
+ Examples
348
+ ========
349
+
350
+ >>> from sympy import symbols
351
+ >>> from sympy.physics.vector import ReferenceFrame, cross
352
+ >>> q1 = symbols('q1')
353
+ >>> N = ReferenceFrame('N')
354
+ >>> cross(N.x, N.y)
355
+ N.z
356
+ >>> A = ReferenceFrame('A')
357
+ >>> A.orient_axis(N, q1, N.x)
358
+ >>> cross(A.x, N.y)
359
+ N.z
360
+ >>> cross(N.y, A.x)
361
+ - sin(q1)*A.y - cos(q1)*A.z
362
+
363
+ """
364
+
365
+ from sympy.physics.vector.dyadic import Dyadic, _check_dyadic
366
+ if isinstance(other, Dyadic):
367
+ other = _check_dyadic(other)
368
+ ol = Dyadic(0)
369
+ for i, v in enumerate(other.args):
370
+ ol += v[0] * ((self.cross(v[1])).outer(v[2]))
371
+ return ol
372
+ other = _check_vector(other)
373
+ if other.args == []:
374
+ return Vector(0)
375
+
376
+ def _det(mat):
377
+ """This is needed as a little method for to find the determinant
378
+ of a list in python; needs to work for a 3x3 list.
379
+ SymPy's Matrix will not take in Vector, so need a custom function.
380
+ You should not be calling this.
381
+
382
+ """
383
+
384
+ return (mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1])
385
+ + mat[0][1] * (mat[1][2] * mat[2][0] - mat[1][0] *
386
+ mat[2][2]) + mat[0][2] * (mat[1][0] * mat[2][1] -
387
+ mat[1][1] * mat[2][0]))
388
+
389
+ outlist = []
390
+ ar = other.args # For brevity
391
+ for v in ar:
392
+ tempx = v[1].x
393
+ tempy = v[1].y
394
+ tempz = v[1].z
395
+ tempm = ([[tempx, tempy, tempz],
396
+ [self.dot(tempx), self.dot(tempy), self.dot(tempz)],
397
+ [Vector([v]).dot(tempx), Vector([v]).dot(tempy),
398
+ Vector([v]).dot(tempz)]])
399
+ outlist += _det(tempm).args
400
+ return Vector(outlist)
401
+
402
+ __radd__ = __add__
403
+ __rmul__ = __mul__
404
+
405
+ def separate(self):
406
+ """
407
+ The constituents of this vector in different reference frames,
408
+ as per its definition.
409
+
410
+ Returns a dict mapping each ReferenceFrame to the corresponding
411
+ constituent Vector.
412
+
413
+ Examples
414
+ ========
415
+
416
+ >>> from sympy.physics.vector import ReferenceFrame
417
+ >>> R1 = ReferenceFrame('R1')
418
+ >>> R2 = ReferenceFrame('R2')
419
+ >>> v = R1.x + R2.x
420
+ >>> v.separate() == {R1: R1.x, R2: R2.x}
421
+ True
422
+
423
+ """
424
+
425
+ components = {}
426
+ for x in self.args:
427
+ components[x[1]] = Vector([x])
428
+ return components
429
+
430
+ def __and__(self, other):
431
+ return self.dot(other)
432
+ __and__.__doc__ = dot.__doc__
433
+ __rand__ = __and__
434
+
435
+ def __xor__(self, other):
436
+ return self.cross(other)
437
+ __xor__.__doc__ = cross.__doc__
438
+
439
+ def __or__(self, other):
440
+ return self.outer(other)
441
+ __or__.__doc__ = outer.__doc__
442
+
443
+ def diff(self, var, frame, var_in_dcm=True):
444
+ """Returns the partial derivative of the vector with respect to a
445
+ variable in the provided reference frame.
446
+
447
+ Parameters
448
+ ==========
449
+ var : Symbol
450
+ What the partial derivative is taken with respect to.
451
+ frame : ReferenceFrame
452
+ The reference frame that the partial derivative is taken in.
453
+ var_in_dcm : boolean
454
+ If true, the differentiation algorithm assumes that the variable
455
+ may be present in any of the direction cosine matrices that relate
456
+ the frame to the frames of any component of the vector. But if it
457
+ is known that the variable is not present in the direction cosine
458
+ matrices, false can be set to skip full reexpression in the desired
459
+ frame.
460
+
461
+ Examples
462
+ ========
463
+
464
+ >>> from sympy import Symbol
465
+ >>> from sympy.physics.vector import dynamicsymbols, ReferenceFrame
466
+ >>> from sympy.physics.vector import init_vprinting
467
+ >>> init_vprinting(pretty_print=False)
468
+ >>> t = Symbol('t')
469
+ >>> q1 = dynamicsymbols('q1')
470
+ >>> N = ReferenceFrame('N')
471
+ >>> A = N.orientnew('A', 'Axis', [q1, N.y])
472
+ >>> A.x.diff(t, N)
473
+ - sin(q1)*q1'*N.x - cos(q1)*q1'*N.z
474
+ >>> A.x.diff(t, N).express(A).simplify()
475
+ - q1'*A.z
476
+ >>> B = ReferenceFrame('B')
477
+ >>> u1, u2 = dynamicsymbols('u1, u2')
478
+ >>> v = u1 * A.x + u2 * B.y
479
+ >>> v.diff(u2, N, var_in_dcm=False)
480
+ B.y
481
+
482
+ """
483
+
484
+ from sympy.physics.vector.frame import _check_frame
485
+
486
+ _check_frame(frame)
487
+ var = sympify(var)
488
+
489
+ inlist = []
490
+
491
+ for vector_component in self.args:
492
+ measure_number = vector_component[0]
493
+ component_frame = vector_component[1]
494
+ if component_frame == frame:
495
+ inlist += [(measure_number.diff(var), frame)]
496
+ else:
497
+ # If the direction cosine matrix relating the component frame
498
+ # with the derivative frame does not contain the variable.
499
+ if not var_in_dcm or (frame.dcm(component_frame).diff(var) ==
500
+ zeros(3, 3)):
501
+ inlist += [(measure_number.diff(var), component_frame)]
502
+ else: # else express in the frame
503
+ reexp_vec_comp = Vector([vector_component]).express(frame)
504
+ deriv = reexp_vec_comp.args[0][0].diff(var)
505
+ inlist += Vector([(deriv, frame)]).args
506
+
507
+ return Vector(inlist)
508
+
509
+ def express(self, otherframe, variables=False):
510
+ """
511
+ Returns a Vector equivalent to this one, expressed in otherframe.
512
+ Uses the global express method.
513
+
514
+ Parameters
515
+ ==========
516
+
517
+ otherframe : ReferenceFrame
518
+ The frame for this Vector to be described in
519
+
520
+ variables : boolean
521
+ If True, the coordinate symbols(if present) in this Vector
522
+ are re-expressed in terms otherframe
523
+
524
+ Examples
525
+ ========
526
+
527
+ >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
528
+ >>> from sympy.physics.vector import init_vprinting
529
+ >>> init_vprinting(pretty_print=False)
530
+ >>> q1 = dynamicsymbols('q1')
531
+ >>> N = ReferenceFrame('N')
532
+ >>> A = N.orientnew('A', 'Axis', [q1, N.y])
533
+ >>> A.x.express(N)
534
+ cos(q1)*N.x - sin(q1)*N.z
535
+
536
+ """
537
+ from sympy.physics.vector import express
538
+ return express(self, otherframe, variables=variables)
539
+
540
+ def to_matrix(self, reference_frame):
541
+ """Returns the matrix form of the vector with respect to the given
542
+ frame.
543
+
544
+ Parameters
545
+ ----------
546
+ reference_frame : ReferenceFrame
547
+ The reference frame that the rows of the matrix correspond to.
548
+
549
+ Returns
550
+ -------
551
+ matrix : ImmutableMatrix, shape(3,1)
552
+ The matrix that gives the 1D vector.
553
+
554
+ Examples
555
+ ========
556
+
557
+ >>> from sympy import symbols
558
+ >>> from sympy.physics.vector import ReferenceFrame
559
+ >>> a, b, c = symbols('a, b, c')
560
+ >>> N = ReferenceFrame('N')
561
+ >>> vector = a * N.x + b * N.y + c * N.z
562
+ >>> vector.to_matrix(N)
563
+ Matrix([
564
+ [a],
565
+ [b],
566
+ [c]])
567
+ >>> beta = symbols('beta')
568
+ >>> A = N.orientnew('A', 'Axis', (beta, N.x))
569
+ >>> vector.to_matrix(A)
570
+ Matrix([
571
+ [ a],
572
+ [ b*cos(beta) + c*sin(beta)],
573
+ [-b*sin(beta) + c*cos(beta)]])
574
+
575
+ """
576
+
577
+ return Matrix([self.dot(unit_vec) for unit_vec in
578
+ reference_frame]).reshape(3, 1)
579
+
580
+ def doit(self, **hints):
581
+ """Calls .doit() on each term in the Vector"""
582
+ d = {}
583
+ for v in self.args:
584
+ d[v[1]] = v[0].applyfunc(lambda x: x.doit(**hints))
585
+ return Vector(d)
586
+
587
+ def dt(self, otherframe):
588
+ """
589
+ Returns a Vector which is the time derivative of
590
+ the self Vector, taken in frame otherframe.
591
+
592
+ Calls the global time_derivative method
593
+
594
+ Parameters
595
+ ==========
596
+
597
+ otherframe : ReferenceFrame
598
+ The frame to calculate the time derivative in
599
+
600
+ """
601
+ from sympy.physics.vector import time_derivative
602
+ return time_derivative(self, otherframe)
603
+
604
+ def simplify(self):
605
+ """Returns a simplified Vector."""
606
+ d = {}
607
+ for v in self.args:
608
+ d[v[1]] = simplify(v[0])
609
+ return Vector(d)
610
+
611
+ def subs(self, *args, **kwargs):
612
+ """Substitution on the Vector.
613
+
614
+ Examples
615
+ ========
616
+
617
+ >>> from sympy.physics.vector import ReferenceFrame
618
+ >>> from sympy import Symbol
619
+ >>> N = ReferenceFrame('N')
620
+ >>> s = Symbol('s')
621
+ >>> a = N.x * s
622
+ >>> a.subs({s: 2})
623
+ 2*N.x
624
+
625
+ """
626
+
627
+ d = {}
628
+ for v in self.args:
629
+ d[v[1]] = v[0].subs(*args, **kwargs)
630
+ return Vector(d)
631
+
632
+ def magnitude(self):
633
+ """Returns the magnitude (Euclidean norm) of self.
634
+
635
+ Warnings
636
+ ========
637
+
638
+ Python ignores the leading negative sign so that might
639
+ give wrong results.
640
+ ``-A.x.magnitude()`` would be treated as ``-(A.x.magnitude())``,
641
+ instead of ``(-A.x).magnitude()``.
642
+
643
+ """
644
+ return sqrt(self.dot(self))
645
+
646
+ def normalize(self):
647
+ """Returns a Vector of magnitude 1, codirectional with self."""
648
+ return Vector(self.args + []) / self.magnitude()
649
+
650
+ def applyfunc(self, f):
651
+ """Apply a function to each component of a vector."""
652
+ if not callable(f):
653
+ raise TypeError("`f` must be callable.")
654
+
655
+ d = {}
656
+ for v in self.args:
657
+ d[v[1]] = v[0].applyfunc(f)
658
+ return Vector(d)
659
+
660
+ def angle_between(self, vec):
661
+ """
662
+ Returns the smallest angle between Vector 'vec' and self.
663
+
664
+ Parameter
665
+ =========
666
+
667
+ vec : Vector
668
+ The Vector between which angle is needed.
669
+
670
+ Examples
671
+ ========
672
+
673
+ >>> from sympy.physics.vector import ReferenceFrame
674
+ >>> A = ReferenceFrame("A")
675
+ >>> v1 = A.x
676
+ >>> v2 = A.y
677
+ >>> v1.angle_between(v2)
678
+ pi/2
679
+
680
+ >>> v3 = A.x + A.y + A.z
681
+ >>> v1.angle_between(v3)
682
+ acos(sqrt(3)/3)
683
+
684
+ Warnings
685
+ ========
686
+
687
+ Python ignores the leading negative sign so that might give wrong
688
+ results. ``-A.x.angle_between()`` would be treated as
689
+ ``-(A.x.angle_between())``, instead of ``(-A.x).angle_between()``.
690
+
691
+ """
692
+
693
+ vec1 = self.normalize()
694
+ vec2 = vec.normalize()
695
+ angle = acos(vec1.dot(vec2))
696
+ return angle
697
+
698
+ def free_symbols(self, reference_frame):
699
+ """Returns the free symbols in the measure numbers of the vector
700
+ expressed in the given reference frame.
701
+
702
+ Parameters
703
+ ==========
704
+ reference_frame : ReferenceFrame
705
+ The frame with respect to which the free symbols of the given
706
+ vector is to be determined.
707
+
708
+ Returns
709
+ =======
710
+ set of Symbol
711
+ set of symbols present in the measure numbers of
712
+ ``reference_frame``.
713
+
714
+ """
715
+
716
+ return self.to_matrix(reference_frame).free_symbols
717
+
718
+ def free_dynamicsymbols(self, reference_frame):
719
+ """Returns the free dynamic symbols (functions of time ``t``) in the
720
+ measure numbers of the vector expressed in the given reference frame.
721
+
722
+ Parameters
723
+ ==========
724
+ reference_frame : ReferenceFrame
725
+ The frame with respect to which the free dynamic symbols of the
726
+ given vector is to be determined.
727
+
728
+ Returns
729
+ =======
730
+ set
731
+ Set of functions of time ``t``, e.g.
732
+ ``Function('f')(me.dynamicsymbols._t)``.
733
+
734
+ """
735
+ # TODO : Circular dependency if imported at top. Should move
736
+ # find_dynamicsymbols into physics.vector.functions.
737
+ from sympy.physics.mechanics.functions import find_dynamicsymbols
738
+
739
+ return find_dynamicsymbols(self, reference_frame=reference_frame)
740
+
741
+ def _eval_evalf(self, prec):
742
+ if not self.args:
743
+ return self
744
+ new_args = []
745
+ dps = prec_to_dps(prec)
746
+ for mat, frame in self.args:
747
+ new_args.append([mat.evalf(n=dps), frame])
748
+ return Vector(new_args)
749
+
750
+ def xreplace(self, rule):
751
+ """Replace occurrences of objects within the measure numbers of the
752
+ vector.
753
+
754
+ Parameters
755
+ ==========
756
+
757
+ rule : dict-like
758
+ Expresses a replacement rule.
759
+
760
+ Returns
761
+ =======
762
+
763
+ Vector
764
+ Result of the replacement.
765
+
766
+ Examples
767
+ ========
768
+
769
+ >>> from sympy import symbols, pi
770
+ >>> from sympy.physics.vector import ReferenceFrame
771
+ >>> A = ReferenceFrame('A')
772
+ >>> x, y, z = symbols('x y z')
773
+ >>> ((1 + x*y) * A.x).xreplace({x: pi})
774
+ (pi*y + 1)*A.x
775
+ >>> ((1 + x*y) * A.x).xreplace({x: pi, y: 2})
776
+ (1 + 2*pi)*A.x
777
+
778
+ Replacements occur only if an entire node in the expression tree is
779
+ matched:
780
+
781
+ >>> ((x*y + z) * A.x).xreplace({x*y: pi})
782
+ (z + pi)*A.x
783
+ >>> ((x*y*z) * A.x).xreplace({x*y: pi})
784
+ x*y*z*A.x
785
+
786
+ """
787
+
788
+ new_args = []
789
+ for mat, frame in self.args:
790
+ mat = mat.xreplace(rule)
791
+ new_args.append([mat, frame])
792
+ return Vector(new_args)
793
+
794
+
795
+ class VectorTypeError(TypeError):
796
+
797
+ def __init__(self, other, want):
798
+ msg = filldedent("Expected an instance of %s, but received object "
799
+ "'%s' of %s." % (type(want), other, type(other)))
800
+ super().__init__(msg)
801
+
802
+
803
+ def _check_vector(other):
804
+ if not isinstance(other, Vector):
805
+ raise TypeError('A Vector must be supplied')
806
+ return other
.venv/lib/python3.12/site-packages/sympy/physics/wigner.py ADDED
@@ -0,0 +1,1213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ r"""
3
+ Wigner, Clebsch-Gordan, Racah, and Gaunt coefficients
4
+
5
+ Collection of functions for calculating Wigner 3j, 6j, 9j,
6
+ Clebsch-Gordan, Racah as well as Gaunt coefficients exactly, all
7
+ evaluating to a rational number times the square root of a rational
8
+ number [Rasch03]_.
9
+
10
+ Please see the description of the individual functions for further
11
+ details and examples.
12
+
13
+ References
14
+ ==========
15
+
16
+ .. [Regge58] 'Symmetry Properties of Clebsch-Gordan Coefficients',
17
+ T. Regge, Nuovo Cimento, Volume 10, pp. 544 (1958)
18
+ .. [Regge59] 'Symmetry Properties of Racah Coefficients',
19
+ T. Regge, Nuovo Cimento, Volume 11, pp. 116 (1959)
20
+ .. [Edmonds74] A. R. Edmonds. Angular momentum in quantum mechanics.
21
+ Investigations in physics, 4.; Investigations in physics, no. 4.
22
+ Princeton, N.J., Princeton University Press, 1957.
23
+ .. [Rasch03] J. Rasch and A. C. H. Yu, 'Efficient Storage Scheme for
24
+ Pre-calculated Wigner 3j, 6j and Gaunt Coefficients', SIAM
25
+ J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
26
+ .. [Liberatodebrito82] 'FORTRAN program for the integral of three
27
+ spherical harmonics', A. Liberato de Brito,
28
+ Comput. Phys. Commun., Volume 25, pp. 81-85 (1982)
29
+ .. [Homeier96] 'Some Properties of the Coupling Coefficients of Real
30
+ Spherical Harmonics and Their Relation to Gaunt Coefficients',
31
+ H. H. H. Homeier and E. O. Steinborn J. Mol. Struct., Volume 368,
32
+ pp. 31-37 (1996)
33
+
34
+ Credits and Copyright
35
+ =====================
36
+
37
+ This code was taken from Sage with the permission of all authors:
38
+
39
+ https://groups.google.com/forum/#!topic/sage-devel/M4NZdu-7O38
40
+
41
+ Authors
42
+ =======
43
+
44
+ - Jens Rasch (2009-03-24): initial version for Sage
45
+
46
+ - Jens Rasch (2009-05-31): updated to sage-4.0
47
+
48
+ - Oscar Gerardo Lazo Arjona (2017-06-18): added Wigner D matrices
49
+
50
+ - Phil Adam LeMaitre (2022-09-19): added real Gaunt coefficient
51
+
52
+ Copyright (C) 2008 Jens Rasch <jyr2000@gmail.com>
53
+
54
+ """
55
+ from sympy.concrete.summations import Sum
56
+ from sympy.core.add import Add
57
+ from sympy.core.numbers import int_valued
58
+ from sympy.core.function import Function
59
+ from sympy.core.numbers import (Float, I, Integer, pi, Rational)
60
+ from sympy.core.singleton import S
61
+ from sympy.core.symbol import Dummy
62
+ from sympy.core.sympify import sympify
63
+ from sympy.functions.combinatorial.factorials import (binomial, factorial)
64
+ from sympy.functions.elementary.complexes import re
65
+ from sympy.functions.elementary.exponential import exp
66
+ from sympy.functions.elementary.miscellaneous import sqrt
67
+ from sympy.functions.elementary.trigonometric import (cos, sin)
68
+ from sympy.functions.special.spherical_harmonics import Ynm
69
+ from sympy.matrices.dense import zeros
70
+ from sympy.matrices.immutable import ImmutableMatrix
71
+ from sympy.utilities.misc import as_int
72
+
73
+ # This list of precomputed factorials is needed to massively
74
+ # accelerate future calculations of the various coefficients
75
+ _Factlist = [1]
76
+
77
+
78
+ def _calc_factlist(nn):
79
+ r"""
80
+ Function calculates a list of precomputed factorials in order to
81
+ massively accelerate future calculations of the various
82
+ coefficients.
83
+
84
+ Parameters
85
+ ==========
86
+
87
+ nn : integer
88
+ Highest factorial to be computed.
89
+
90
+ Returns
91
+ =======
92
+
93
+ list of integers :
94
+ The list of precomputed factorials.
95
+
96
+ Examples
97
+ ========
98
+
99
+ Calculate list of factorials::
100
+
101
+ sage: from sage.functions.wigner import _calc_factlist
102
+ sage: _calc_factlist(10)
103
+ [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
104
+ """
105
+ if nn >= len(_Factlist):
106
+ for ii in range(len(_Factlist), int(nn + 1)):
107
+ _Factlist.append(_Factlist[ii - 1] * ii)
108
+ return _Factlist[:int(nn) + 1]
109
+
110
+
111
+ def _int_or_halfint(value):
112
+ """return Python int unless value is half-int (then return float)"""
113
+ if isinstance(value, int):
114
+ return value
115
+ elif type(value) is float:
116
+ if value.is_integer():
117
+ return int(value) # an int
118
+ if (2*value).is_integer():
119
+ return value # a float
120
+ elif isinstance(value, Rational):
121
+ if value.q == 2:
122
+ return value.p/value.q # a float
123
+ elif value.q == 1:
124
+ return value.p # an int
125
+ elif isinstance(value, Float):
126
+ return _int_or_halfint(float(value))
127
+ raise ValueError("expecting integer or half-integer, got %s" % value)
128
+
129
+
130
+ def wigner_3j(j_1, j_2, j_3, m_1, m_2, m_3):
131
+ r"""
132
+ Calculate the Wigner 3j symbol `\operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)`.
133
+
134
+ Parameters
135
+ ==========
136
+
137
+ j_1, j_2, j_3, m_1, m_2, m_3 :
138
+ Integer or half integer.
139
+
140
+ Returns
141
+ =======
142
+
143
+ Rational number times the square root of a rational number.
144
+
145
+ Examples
146
+ ========
147
+
148
+ >>> from sympy.physics.wigner import wigner_3j
149
+ >>> wigner_3j(2, 6, 4, 0, 0, 0)
150
+ sqrt(715)/143
151
+ >>> wigner_3j(2, 6, 4, 0, 0, 1)
152
+ 0
153
+
154
+ It is an error to have arguments that are not integer or half
155
+ integer values::
156
+
157
+ sage: wigner_3j(2.1, 6, 4, 0, 0, 0)
158
+ Traceback (most recent call last):
159
+ ...
160
+ ValueError: j values must be integer or half integer
161
+ sage: wigner_3j(2, 6, 4, 1, 0, -1.1)
162
+ Traceback (most recent call last):
163
+ ...
164
+ ValueError: m values must be integer or half integer
165
+
166
+ Notes
167
+ =====
168
+
169
+ The Wigner 3j symbol obeys the following symmetry rules:
170
+
171
+ - invariant under any permutation of the columns (with the
172
+ exception of a sign change where `J:=j_1+j_2+j_3`):
173
+
174
+ .. math::
175
+
176
+ \begin{aligned}
177
+ \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)
178
+ &=\operatorname{Wigner3j}(j_3,j_1,j_2,m_3,m_1,m_2) \\
179
+ &=\operatorname{Wigner3j}(j_2,j_3,j_1,m_2,m_3,m_1) \\
180
+ &=(-1)^J \operatorname{Wigner3j}(j_3,j_2,j_1,m_3,m_2,m_1) \\
181
+ &=(-1)^J \operatorname{Wigner3j}(j_1,j_3,j_2,m_1,m_3,m_2) \\
182
+ &=(-1)^J \operatorname{Wigner3j}(j_2,j_1,j_3,m_2,m_1,m_3)
183
+ \end{aligned}
184
+
185
+ - invariant under space inflection, i.e.
186
+
187
+ .. math::
188
+
189
+ \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,m_3)
190
+ =(-1)^J \operatorname{Wigner3j}(j_1,j_2,j_3,-m_1,-m_2,-m_3)
191
+
192
+ - symmetric with respect to the 72 additional symmetries based on
193
+ the work by [Regge58]_
194
+
195
+ - zero for `j_1`, `j_2`, `j_3` not fulfilling triangle relation
196
+
197
+ - zero for `m_1 + m_2 + m_3 \neq 0`
198
+
199
+ - zero for violating any one of the conditions
200
+ `m_1 \in \{-|j_1|, \ldots, |j_1|\}`,
201
+ `m_2 \in \{-|j_2|, \ldots, |j_2|\}`,
202
+ `m_3 \in \{-|j_3|, \ldots, |j_3|\}`
203
+
204
+ Algorithm
205
+ =========
206
+
207
+ This function uses the algorithm of [Edmonds74]_ to calculate the
208
+ value of the 3j symbol exactly. Note that the formula contains
209
+ alternating sums over large factorials and is therefore unsuitable
210
+ for finite precision arithmetic and only useful for a computer
211
+ algebra system [Rasch03]_.
212
+
213
+ Authors
214
+ =======
215
+
216
+ - Jens Rasch (2009-03-24): initial version
217
+ """
218
+
219
+ j_1, j_2, j_3, m_1, m_2, m_3 = \
220
+ map(_int_or_halfint, map(sympify,
221
+ [j_1, j_2, j_3, m_1, m_2, m_3]))
222
+
223
+ if m_1 + m_2 + m_3 != 0:
224
+ return S.Zero
225
+ a1 = j_1 + j_2 - j_3
226
+ if a1 < 0:
227
+ return S.Zero
228
+ a2 = j_1 - j_2 + j_3
229
+ if a2 < 0:
230
+ return S.Zero
231
+ a3 = -j_1 + j_2 + j_3
232
+ if a3 < 0:
233
+ return S.Zero
234
+ if (abs(m_1) > j_1) or (abs(m_2) > j_2) or (abs(m_3) > j_3):
235
+ return S.Zero
236
+ if not (int_valued(j_1 - m_1) and \
237
+ int_valued(j_2 - m_2) and \
238
+ int_valued(j_3 - m_3)):
239
+ return S.Zero
240
+
241
+ maxfact = max(j_1 + j_2 + j_3 + 1, j_1 + abs(m_1), j_2 + abs(m_2),
242
+ j_3 + abs(m_3))
243
+ _calc_factlist(int(maxfact))
244
+
245
+ argsqrt = Integer(_Factlist[int(j_1 + j_2 - j_3)] *
246
+ _Factlist[int(j_1 - j_2 + j_3)] *
247
+ _Factlist[int(-j_1 + j_2 + j_3)] *
248
+ _Factlist[int(j_1 - m_1)] *
249
+ _Factlist[int(j_1 + m_1)] *
250
+ _Factlist[int(j_2 - m_2)] *
251
+ _Factlist[int(j_2 + m_2)] *
252
+ _Factlist[int(j_3 - m_3)] *
253
+ _Factlist[int(j_3 + m_3)]) / \
254
+ _Factlist[int(j_1 + j_2 + j_3 + 1)]
255
+
256
+ ressqrt = sqrt(argsqrt)
257
+ if ressqrt.is_complex or ressqrt.is_infinite:
258
+ ressqrt = ressqrt.as_real_imag()[0]
259
+
260
+ imin = max(-j_3 + j_1 + m_2, -j_3 + j_2 - m_1, 0)
261
+ imax = min(j_2 + m_2, j_1 - m_1, j_1 + j_2 - j_3)
262
+ sumres = 0
263
+ for ii in range(int(imin), int(imax) + 1):
264
+ den = _Factlist[ii] * \
265
+ _Factlist[int(ii + j_3 - j_1 - m_2)] * \
266
+ _Factlist[int(j_2 + m_2 - ii)] * \
267
+ _Factlist[int(j_1 - ii - m_1)] * \
268
+ _Factlist[int(ii + j_3 - j_2 + m_1)] * \
269
+ _Factlist[int(j_1 + j_2 - j_3 - ii)]
270
+ sumres = sumres + Integer((-1) ** ii) / den
271
+
272
+ prefid = Integer((-1) ** int(j_1 - j_2 - m_3))
273
+ res = ressqrt * sumres * prefid
274
+ return res
275
+
276
+
277
+ def clebsch_gordan(j_1, j_2, j_3, m_1, m_2, m_3):
278
+ r"""
279
+ Calculates the Clebsch-Gordan coefficient.
280
+ `\left\langle j_1 m_1 \; j_2 m_2 | j_3 m_3 \right\rangle`.
281
+
282
+ The reference for this function is [Edmonds74]_.
283
+
284
+ Parameters
285
+ ==========
286
+
287
+ j_1, j_2, j_3, m_1, m_2, m_3 :
288
+ Integer or half integer.
289
+
290
+ Returns
291
+ =======
292
+
293
+ Rational number times the square root of a rational number.
294
+
295
+ Examples
296
+ ========
297
+
298
+ >>> from sympy import S
299
+ >>> from sympy.physics.wigner import clebsch_gordan
300
+ >>> clebsch_gordan(S(3)/2, S(1)/2, 2, S(3)/2, S(1)/2, 2)
301
+ 1
302
+ >>> clebsch_gordan(S(3)/2, S(1)/2, 1, S(3)/2, -S(1)/2, 1)
303
+ sqrt(3)/2
304
+ >>> clebsch_gordan(S(3)/2, S(1)/2, 1, -S(1)/2, S(1)/2, 0)
305
+ -sqrt(2)/2
306
+
307
+ Notes
308
+ =====
309
+
310
+ The Clebsch-Gordan coefficient will be evaluated via its relation
311
+ to Wigner 3j symbols:
312
+
313
+ .. math::
314
+
315
+ \left\langle j_1 m_1 \; j_2 m_2 | j_3 m_3 \right\rangle
316
+ =(-1)^{j_1-j_2+m_3} \sqrt{2j_3+1}
317
+ \operatorname{Wigner3j}(j_1,j_2,j_3,m_1,m_2,-m_3)
318
+
319
+ See also the documentation on Wigner 3j symbols which exhibit much
320
+ higher symmetry relations than the Clebsch-Gordan coefficient.
321
+
322
+ Authors
323
+ =======
324
+
325
+ - Jens Rasch (2009-03-24): initial version
326
+ """
327
+ j_1 = sympify(j_1)
328
+ j_2 = sympify(j_2)
329
+ j_3 = sympify(j_3)
330
+ m_1 = sympify(m_1)
331
+ m_2 = sympify(m_2)
332
+ m_3 = sympify(m_3)
333
+
334
+ w = wigner_3j(j_1, j_2, j_3, m_1, m_2, -m_3)
335
+
336
+ return (-1) ** (j_1 - j_2 + m_3) * sqrt(2 * j_3 + 1) * w
337
+
338
+
339
+ def _big_delta_coeff(aa, bb, cc, prec=None):
340
+ r"""
341
+ Calculates the Delta coefficient of the 3 angular momenta for
342
+ Racah symbols. Also checks that the differences are of integer
343
+ value.
344
+
345
+ Parameters
346
+ ==========
347
+
348
+ aa :
349
+ First angular momentum, integer or half integer.
350
+ bb :
351
+ Second angular momentum, integer or half integer.
352
+ cc :
353
+ Third angular momentum, integer or half integer.
354
+ prec :
355
+ Precision of the ``sqrt()`` calculation.
356
+
357
+ Returns
358
+ =======
359
+
360
+ double : Value of the Delta coefficient.
361
+
362
+ Examples
363
+ ========
364
+
365
+ sage: from sage.functions.wigner import _big_delta_coeff
366
+ sage: _big_delta_coeff(1,1,1)
367
+ 1/2*sqrt(1/6)
368
+ """
369
+
370
+ # the triangle test will only pass if a) all 3 values are ints or
371
+ # b) 1 is an int and the other two are half-ints
372
+ if not int_valued(aa + bb - cc):
373
+ raise ValueError("j values must be integer or half integer and fulfill the triangle relation")
374
+ if not int_valued(aa + cc - bb):
375
+ raise ValueError("j values must be integer or half integer and fulfill the triangle relation")
376
+ if not int_valued(bb + cc - aa):
377
+ raise ValueError("j values must be integer or half integer and fulfill the triangle relation")
378
+ if (aa + bb - cc) < 0:
379
+ return S.Zero
380
+ if (aa + cc - bb) < 0:
381
+ return S.Zero
382
+ if (bb + cc - aa) < 0:
383
+ return S.Zero
384
+
385
+ maxfact = max(aa + bb - cc, aa + cc - bb, bb + cc - aa, aa + bb + cc + 1)
386
+ _calc_factlist(maxfact)
387
+
388
+ argsqrt = Integer(_Factlist[int(aa + bb - cc)] *
389
+ _Factlist[int(aa + cc - bb)] *
390
+ _Factlist[int(bb + cc - aa)]) / \
391
+ Integer(_Factlist[int(aa + bb + cc + 1)])
392
+
393
+ ressqrt = sqrt(argsqrt)
394
+ if prec:
395
+ ressqrt = ressqrt.evalf(prec).as_real_imag()[0]
396
+ return ressqrt
397
+
398
+
399
+ def racah(aa, bb, cc, dd, ee, ff, prec=None):
400
+ r"""
401
+ Calculate the Racah symbol `W(a,b,c,d;e,f)`.
402
+
403
+ Parameters
404
+ ==========
405
+
406
+ a, ..., f :
407
+ Integer or half integer.
408
+ prec :
409
+ Precision, default: ``None``. Providing a precision can
410
+ drastically speed up the calculation.
411
+
412
+ Returns
413
+ =======
414
+
415
+ Rational number times the square root of a rational number
416
+ (if ``prec=None``), or real number if a precision is given.
417
+
418
+ Examples
419
+ ========
420
+
421
+ >>> from sympy.physics.wigner import racah
422
+ >>> racah(3,3,3,3,3,3)
423
+ -1/14
424
+
425
+ Notes
426
+ =====
427
+
428
+ The Racah symbol is related to the Wigner 6j symbol:
429
+
430
+ .. math::
431
+
432
+ \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
433
+ =(-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)
434
+
435
+ Please see the 6j symbol for its much richer symmetries and for
436
+ additional properties.
437
+
438
+ Algorithm
439
+ =========
440
+
441
+ This function uses the algorithm of [Edmonds74]_ to calculate the
442
+ value of the 6j symbol exactly. Note that the formula contains
443
+ alternating sums over large factorials and is therefore unsuitable
444
+ for finite precision arithmetic and only useful for a computer
445
+ algebra system [Rasch03]_.
446
+
447
+ Authors
448
+ =======
449
+
450
+ - Jens Rasch (2009-03-24): initial version
451
+ """
452
+ prefac = _big_delta_coeff(aa, bb, ee, prec) * \
453
+ _big_delta_coeff(cc, dd, ee, prec) * \
454
+ _big_delta_coeff(aa, cc, ff, prec) * \
455
+ _big_delta_coeff(bb, dd, ff, prec)
456
+ if prefac == 0:
457
+ return S.Zero
458
+ imin = max(aa + bb + ee, cc + dd + ee, aa + cc + ff, bb + dd + ff)
459
+ imax = min(aa + bb + cc + dd, aa + dd + ee + ff, bb + cc + ee + ff)
460
+
461
+ maxfact = max(imax + 1, aa + bb + cc + dd, aa + dd + ee + ff,
462
+ bb + cc + ee + ff)
463
+ _calc_factlist(maxfact)
464
+
465
+ sumres = 0
466
+ for kk in range(int(imin), int(imax) + 1):
467
+ den = _Factlist[int(kk - aa - bb - ee)] * \
468
+ _Factlist[int(kk - cc - dd - ee)] * \
469
+ _Factlist[int(kk - aa - cc - ff)] * \
470
+ _Factlist[int(kk - bb - dd - ff)] * \
471
+ _Factlist[int(aa + bb + cc + dd - kk)] * \
472
+ _Factlist[int(aa + dd + ee + ff - kk)] * \
473
+ _Factlist[int(bb + cc + ee + ff - kk)]
474
+ sumres = sumres + Integer((-1) ** kk * _Factlist[kk + 1]) / den
475
+
476
+ res = prefac * sumres * (-1) ** int(aa + bb + cc + dd)
477
+ return res
478
+
479
+
480
+ def wigner_6j(j_1, j_2, j_3, j_4, j_5, j_6, prec=None):
481
+ r"""
482
+ Calculate the Wigner 6j symbol `\operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)`.
483
+
484
+ Parameters
485
+ ==========
486
+
487
+ j_1, ..., j_6 :
488
+ Integer or half integer.
489
+ prec :
490
+ Precision, default: ``None``. Providing a precision can
491
+ drastically speed up the calculation.
492
+
493
+ Returns
494
+ =======
495
+
496
+ Rational number times the square root of a rational number
497
+ (if ``prec=None``), or real number if a precision is given.
498
+
499
+ Examples
500
+ ========
501
+
502
+ >>> from sympy.physics.wigner import wigner_6j
503
+ >>> wigner_6j(3,3,3,3,3,3)
504
+ -1/14
505
+ >>> wigner_6j(5,5,5,5,5,5)
506
+ 1/52
507
+
508
+ It is an error to have arguments that are not integer or half
509
+ integer values or do not fulfill the triangle relation::
510
+
511
+ sage: wigner_6j(2.5,2.5,2.5,2.5,2.5,2.5)
512
+ Traceback (most recent call last):
513
+ ...
514
+ ValueError: j values must be integer or half integer and fulfill the triangle relation
515
+ sage: wigner_6j(0.5,0.5,1.1,0.5,0.5,1.1)
516
+ Traceback (most recent call last):
517
+ ...
518
+ ValueError: j values must be integer or half integer and fulfill the triangle relation
519
+
520
+ Notes
521
+ =====
522
+
523
+ The Wigner 6j symbol is related to the Racah symbol but exhibits
524
+ more symmetries as detailed below.
525
+
526
+ .. math::
527
+
528
+ \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
529
+ =(-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)
530
+
531
+ The Wigner 6j symbol obeys the following symmetry rules:
532
+
533
+ - Wigner 6j symbols are left invariant under any permutation of
534
+ the columns:
535
+
536
+ .. math::
537
+
538
+ \begin{aligned}
539
+ \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
540
+ &=\operatorname{Wigner6j}(j_3,j_1,j_2,j_6,j_4,j_5) \\
541
+ &=\operatorname{Wigner6j}(j_2,j_3,j_1,j_5,j_6,j_4) \\
542
+ &=\operatorname{Wigner6j}(j_3,j_2,j_1,j_6,j_5,j_4) \\
543
+ &=\operatorname{Wigner6j}(j_1,j_3,j_2,j_4,j_6,j_5) \\
544
+ &=\operatorname{Wigner6j}(j_2,j_1,j_3,j_5,j_4,j_6)
545
+ \end{aligned}
546
+
547
+ - They are invariant under the exchange of the upper and lower
548
+ arguments in each of any two columns, i.e.
549
+
550
+ .. math::
551
+
552
+ \begin{aligned}
553
+ \operatorname{Wigner6j}(j_1,j_2,j_3,j_4,j_5,j_6)
554
+ &=\operatorname{Wigner6j}(j_1,j_5,j_6,j_4,j_2,j_3)\\
555
+ &=\operatorname{Wigner6j}(j_4,j_2,j_6,j_1,j_5,j_3)\\
556
+ &=\operatorname{Wigner6j}(j_4,j_5,j_3,j_1,j_2,j_6)
557
+ \end{aligned}
558
+
559
+ - additional 6 symmetries [Regge59]_ giving rise to 144 symmetries
560
+ in total
561
+
562
+ - only non-zero if any triple of `j`'s fulfill a triangle relation
563
+
564
+ Algorithm
565
+ =========
566
+
567
+ This function uses the algorithm of [Edmonds74]_ to calculate the
568
+ value of the 6j symbol exactly. Note that the formula contains
569
+ alternating sums over large factorials and is therefore unsuitable
570
+ for finite precision arithmetic and only useful for a computer
571
+ algebra system [Rasch03]_.
572
+
573
+ """
574
+ j_1, j_2, j_3, j_4, j_5, j_6 = map(sympify, \
575
+ [j_1, j_2, j_3, j_4, j_5, j_6])
576
+ res = (-1) ** int(j_1 + j_2 + j_4 + j_5) * \
577
+ racah(j_1, j_2, j_5, j_4, j_3, j_6, prec)
578
+ return res
579
+
580
+
581
+ def wigner_9j(j_1, j_2, j_3, j_4, j_5, j_6, j_7, j_8, j_9, prec=None):
582
+ r"""
583
+ Calculate the Wigner 9j symbol
584
+ `\operatorname{Wigner9j}(j_1,j_2,j_3,j_4,j_5,j_6,j_7,j_8,j_9)`.
585
+
586
+ Parameters
587
+ ==========
588
+
589
+ j_1, ..., j_9 :
590
+ Integer or half integer.
591
+ prec : precision, default
592
+ ``None``. Providing a precision can
593
+ drastically speed up the calculation.
594
+
595
+ Returns
596
+ =======
597
+
598
+ Rational number times the square root of a rational number
599
+ (if ``prec=None``), or real number if a precision is given.
600
+
601
+ Examples
602
+ ========
603
+
604
+ >>> from sympy.physics.wigner import wigner_9j
605
+ >>> wigner_9j(1,1,1, 1,1,1, 1,1,0, prec=64)
606
+ 0.05555555555555555555555555555555555555555555555555555555555555555
607
+
608
+ >>> wigner_9j(1/2,1/2,0, 1/2,3/2,1, 0,1,1, prec=64)
609
+ 0.1666666666666666666666666666666666666666666666666666666666666667
610
+
611
+ It is an error to have arguments that are not integer or half
612
+ integer values or do not fulfill the triangle relation::
613
+
614
+ sage: wigner_9j(0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,0.5,0.5,prec=64)
615
+ Traceback (most recent call last):
616
+ ...
617
+ ValueError: j values must be integer or half integer and fulfill the triangle relation
618
+ sage: wigner_9j(1,1,1, 0.5,1,1.5, 0.5,1,2.5,prec=64)
619
+ Traceback (most recent call last):
620
+ ...
621
+ ValueError: j values must be integer or half integer and fulfill the triangle relation
622
+
623
+ Algorithm
624
+ =========
625
+
626
+ This function uses the algorithm of [Edmonds74]_ to calculate the
627
+ value of the 3j symbol exactly. Note that the formula contains
628
+ alternating sums over large factorials and is therefore unsuitable
629
+ for finite precision arithmetic and only useful for a computer
630
+ algebra system [Rasch03]_.
631
+ """
632
+ j_1, j_2, j_3, j_4, j_5, j_6, j_7, j_8, j_9 = map(sympify, \
633
+ [j_1, j_2, j_3, j_4, j_5, j_6, j_7, j_8, j_9])
634
+ imax = int(min(j_1 + j_9, j_2 + j_6, j_4 + j_8) * 2)
635
+ imin = imax % 2
636
+ sumres = 0
637
+ for kk in range(imin, int(imax) + 1, 2):
638
+ sumres = sumres + (kk + 1) * \
639
+ racah(j_1, j_2, j_9, j_6, j_3, kk / 2, prec) * \
640
+ racah(j_4, j_6, j_8, j_2, j_5, kk / 2, prec) * \
641
+ racah(j_1, j_4, j_9, j_8, j_7, kk / 2, prec)
642
+ return sumres
643
+
644
+
645
+ def gaunt(l_1, l_2, l_3, m_1, m_2, m_3, prec=None):
646
+ r"""
647
+ Calculate the Gaunt coefficient.
648
+
649
+ Explanation
650
+ ===========
651
+
652
+ The Gaunt coefficient is defined as the integral over three
653
+ spherical harmonics:
654
+
655
+ .. math::
656
+
657
+ \begin{aligned}
658
+ \operatorname{Gaunt}(l_1,l_2,l_3,m_1,m_2,m_3)
659
+ &=\int Y_{l_1,m_1}(\Omega)
660
+ Y_{l_2,m_2}(\Omega) Y_{l_3,m_3}(\Omega) \,d\Omega \\
661
+ &=\sqrt{\frac{(2l_1+1)(2l_2+1)(2l_3+1)}{4\pi}}
662
+ \operatorname{Wigner3j}(l_1,l_2,l_3,0,0,0)
663
+ \operatorname{Wigner3j}(l_1,l_2,l_3,m_1,m_2,m_3)
664
+ \end{aligned}
665
+
666
+ Parameters
667
+ ==========
668
+
669
+ l_1, l_2, l_3, m_1, m_2, m_3 :
670
+ Integer.
671
+ prec - precision, default: ``None``.
672
+ Providing a precision can
673
+ drastically speed up the calculation.
674
+
675
+ Returns
676
+ =======
677
+
678
+ Rational number times the square root of a rational number
679
+ (if ``prec=None``), or real number if a precision is given.
680
+
681
+ Examples
682
+ ========
683
+
684
+ >>> from sympy.physics.wigner import gaunt
685
+ >>> gaunt(1,0,1,1,0,-1)
686
+ -1/(2*sqrt(pi))
687
+ >>> gaunt(1000,1000,1200,9,3,-12).n(64)
688
+ 0.006895004219221134484332976156744208248842039317638217822322799675
689
+
690
+ It is an error to use non-integer values for `l` and `m`::
691
+
692
+ sage: gaunt(1.2,0,1.2,0,0,0)
693
+ Traceback (most recent call last):
694
+ ...
695
+ ValueError: l values must be integer
696
+ sage: gaunt(1,0,1,1.1,0,-1.1)
697
+ Traceback (most recent call last):
698
+ ...
699
+ ValueError: m values must be integer
700
+
701
+ Notes
702
+ =====
703
+
704
+ The Gaunt coefficient obeys the following symmetry rules:
705
+
706
+ - invariant under any permutation of the columns
707
+
708
+ .. math::
709
+ \begin{aligned}
710
+ Y(l_1,l_2,l_3,m_1,m_2,m_3)
711
+ &=Y(l_3,l_1,l_2,m_3,m_1,m_2) \\
712
+ &=Y(l_2,l_3,l_1,m_2,m_3,m_1) \\
713
+ &=Y(l_3,l_2,l_1,m_3,m_2,m_1) \\
714
+ &=Y(l_1,l_3,l_2,m_1,m_3,m_2) \\
715
+ &=Y(l_2,l_1,l_3,m_2,m_1,m_3)
716
+ \end{aligned}
717
+
718
+ - invariant under space inflection, i.e.
719
+
720
+ .. math::
721
+ Y(l_1,l_2,l_3,m_1,m_2,m_3)
722
+ =Y(l_1,l_2,l_3,-m_1,-m_2,-m_3)
723
+
724
+ - symmetric with respect to the 72 Regge symmetries as inherited
725
+ for the `3j` symbols [Regge58]_
726
+
727
+ - zero for `l_1`, `l_2`, `l_3` not fulfilling triangle relation
728
+
729
+ - zero for violating any one of the conditions: `l_1 \ge |m_1|`,
730
+ `l_2 \ge |m_2|`, `l_3 \ge |m_3|`
731
+
732
+ - non-zero only for an even sum of the `l_i`, i.e.
733
+ `L = l_1 + l_2 + l_3 = 2n` for `n` in `\mathbb{N}`
734
+
735
+ Algorithms
736
+ ==========
737
+
738
+ This function uses the algorithm of [Liberatodebrito82]_ to
739
+ calculate the value of the Gaunt coefficient exactly. Note that
740
+ the formula contains alternating sums over large factorials and is
741
+ therefore unsuitable for finite precision arithmetic and only
742
+ useful for a computer algebra system [Rasch03]_.
743
+
744
+ Authors
745
+ =======
746
+
747
+ Jens Rasch (2009-03-24): initial version for Sage.
748
+ """
749
+ l_1, l_2, l_3, m_1, m_2, m_3 = [
750
+ as_int(i) for i in (l_1, l_2, l_3, m_1, m_2, m_3)]
751
+
752
+ if l_1 + l_2 - l_3 < 0:
753
+ return S.Zero
754
+ if l_1 - l_2 + l_3 < 0:
755
+ return S.Zero
756
+ if -l_1 + l_2 + l_3 < 0:
757
+ return S.Zero
758
+ if (m_1 + m_2 + m_3) != 0:
759
+ return S.Zero
760
+ if (abs(m_1) > l_1) or (abs(m_2) > l_2) or (abs(m_3) > l_3):
761
+ return S.Zero
762
+ bigL, remL = divmod(l_1 + l_2 + l_3, 2)
763
+ if remL % 2:
764
+ return S.Zero
765
+
766
+ imin = max(-l_3 + l_1 + m_2, -l_3 + l_2 - m_1, 0)
767
+ imax = min(l_2 + m_2, l_1 - m_1, l_1 + l_2 - l_3)
768
+
769
+ _calc_factlist(max(l_1 + l_2 + l_3 + 1, imax + 1))
770
+
771
+ ressqrt = sqrt((2 * l_1 + 1) * (2 * l_2 + 1) * (2 * l_3 + 1) * \
772
+ _Factlist[l_1 - m_1] * _Factlist[l_1 + m_1] * _Factlist[l_2 - m_2] * \
773
+ _Factlist[l_2 + m_2] * _Factlist[l_3 - m_3] * _Factlist[l_3 + m_3] / \
774
+ (4*pi))
775
+
776
+ prefac = Integer(_Factlist[bigL] * _Factlist[l_2 - l_1 + l_3] *
777
+ _Factlist[l_1 - l_2 + l_3] * _Factlist[l_1 + l_2 - l_3])/ \
778
+ _Factlist[2 * bigL + 1]/ \
779
+ (_Factlist[bigL - l_1] *
780
+ _Factlist[bigL - l_2] * _Factlist[bigL - l_3])
781
+
782
+ sumres = 0
783
+ for ii in range(int(imin), int(imax) + 1):
784
+ den = _Factlist[ii] * _Factlist[ii + l_3 - l_1 - m_2] * \
785
+ _Factlist[l_2 + m_2 - ii] * _Factlist[l_1 - ii - m_1] * \
786
+ _Factlist[ii + l_3 - l_2 + m_1] * _Factlist[l_1 + l_2 - l_3 - ii]
787
+ sumres = sumres + Integer((-1) ** ii) / den
788
+
789
+ res = ressqrt * prefac * sumres * Integer((-1) ** (bigL + l_3 + m_1 - m_2))
790
+ if prec is not None:
791
+ res = res.n(prec)
792
+ return res
793
+
794
+
795
+ def real_gaunt(l_1, l_2, l_3, mu_1, mu_2, mu_3, prec=None):
796
+ r"""
797
+ Calculate the real Gaunt coefficient.
798
+
799
+ Explanation
800
+ ===========
801
+
802
+ The real Gaunt coefficient is defined as the integral over three
803
+ real spherical harmonics:
804
+
805
+ .. math::
806
+ \begin{aligned}
807
+ \operatorname{RealGaunt}(l_1,l_2,l_3,\mu_1,\mu_2,\mu_3)
808
+ &=\int Z^{\mu_1}_{l_1}(\Omega)
809
+ Z^{\mu_2}_{l_2}(\Omega) Z^{\mu_3}_{l_3}(\Omega) \,d\Omega \\
810
+ \end{aligned}
811
+
812
+ Alternatively, it can be defined in terms of the standard Gaunt
813
+ coefficient by relating the real spherical harmonics to the standard
814
+ spherical harmonics via a unitary transformation `U`, i.e.
815
+ `Z^{\mu}_{l}(\Omega)=\sum_{m'}U^{\mu}_{m'}Y^{m'}_{l}(\Omega)` [Homeier96]_.
816
+ The real Gaunt coefficient is then defined as
817
+
818
+ .. math::
819
+ \begin{aligned}
820
+ \operatorname{RealGaunt}(l_1,l_2,l_3,\mu_1,\mu_2,\mu_3)
821
+ &=\int Z^{\mu_1}_{l_1}(\Omega)
822
+ Z^{\mu_2}_{l_2}(\Omega) Z^{\mu_3}_{l_3}(\Omega) \,d\Omega \\
823
+ &=\sum_{m'_1 m'_2 m'_3} U^{\mu_1}_{m'_1}U^{\mu_2}_{m'_2}U^{\mu_3}_{m'_3}
824
+ \operatorname{Gaunt}(l_1,l_2,l_3,m'_1,m'_2,m'_3)
825
+ \end{aligned}
826
+
827
+ The unitary matrix `U` has components
828
+
829
+ .. math::
830
+ \begin{aligned}
831
+ U^\mu_{m} = \delta_{|\mu||m|}*(\delta_{m0}\delta_{\mu 0} + \frac{1}{\sqrt{2}}\big[\Theta(\mu)\big(\delta_{m\mu}+(-1)^{m}\delta_{m-\mu}\big)
832
+ +i \Theta(-\mu)\big((-1)^{m}\delta_{m\mu}-\delta_{m-\mu}\big)\big])
833
+ \end{aligned}
834
+
835
+
836
+ where `\delta_{ij}` is the Kronecker delta symbol and `\Theta` is a step
837
+ function defined as
838
+
839
+ .. math::
840
+ \begin{aligned}
841
+ \Theta(x) = \begin{cases} 1 \,\text{for}\, x > 0 \\ 0 \,\text{for}\, x \leq 0 \end{cases}
842
+ \end{aligned}
843
+
844
+ Parameters
845
+ ==========
846
+
847
+ l_1, l_2, l_3, mu_1, mu_2, mu_3 :
848
+ Integer degree and order
849
+
850
+ prec - precision, default: ``None``.
851
+ Providing a precision can
852
+ drastically speed up the calculation.
853
+
854
+ Returns
855
+ =======
856
+
857
+ Rational number times the square root of a rational number.
858
+
859
+ Examples
860
+ ========
861
+ >>> from sympy.physics.wigner import real_gaunt
862
+ >>> real_gaunt(1,1,2,-1,1,-2)
863
+ sqrt(15)/(10*sqrt(pi))
864
+ >>> real_gaunt(10,10,20,-9,-9,0,prec=64)
865
+ -0.00002480019791932209313156167176797577821140084216297395518482071448
866
+
867
+ It is an error to use non-integer values for `l` and `\mu`::
868
+ real_gaunt(2.8,0.5,1.3,0,0,0)
869
+ Traceback (most recent call last):
870
+ ...
871
+ ValueError: l values must be integer
872
+
873
+ real_gaunt(2,2,4,0.7,1,-3.4)
874
+ Traceback (most recent call last):
875
+ ...
876
+ ValueError: mu values must be integer
877
+
878
+ Notes
879
+ =====
880
+
881
+ The real Gaunt coefficient inherits from the standard Gaunt coefficient,
882
+ the invariance under any permutation of the pairs `(l_i, \mu_i)` and the
883
+ requirement that the sum of the `l_i` be even to yield a non-zero value.
884
+ It also obeys the following symmetry rules:
885
+
886
+ - zero for `l_1`, `l_2`, `l_3` not fulfilling the condition
887
+ `l_1 \in \{l_{\text{max}}, l_{\text{max}}-2, \ldots, l_{\text{min}}\}`,
888
+ where `l_{\text{max}} = l_2+l_3`,
889
+
890
+ .. math::
891
+ \begin{aligned}
892
+ l_{\text{min}} = \begin{cases} \kappa(l_2, l_3, \mu_2, \mu_3) & \text{if}\,
893
+ \kappa(l_2, l_3, \mu_2, \mu_3) + l_{\text{max}}\, \text{is even} \\
894
+ \kappa(l_2, l_3, \mu_2, \mu_3)+1 & \text{if}\, \kappa(l_2, l_3, \mu_2, \mu_3) +
895
+ l_{\text{max}}\, \text{is odd}\end{cases}
896
+ \end{aligned}
897
+
898
+ and `\kappa(l_2, l_3, \mu_2, \mu_3) = \max{\big(|l_2-l_3|, \min{\big(|\mu_2+\mu_3|,
899
+ |\mu_2-\mu_3|\big)}\big)}`
900
+
901
+ - zero for an odd number of negative `\mu_i`
902
+
903
+ Algorithms
904
+ ==========
905
+
906
+ This function uses the algorithms of [Homeier96]_ and [Rasch03]_ to
907
+ calculate the value of the real Gaunt coefficient exactly. Note that
908
+ the formula used in [Rasch03]_ contains alternating sums over large
909
+ factorials and is therefore unsuitable for finite precision arithmetic
910
+ and only useful for a computer algebra system [Rasch03]_. However, this
911
+ function can in principle use any algorithm that computes the Gaunt
912
+ coefficient, so it is suitable for finite precision arithmetic in so far
913
+ as the algorithm which computes the Gaunt coefficient is.
914
+ """
915
+ l_1, l_2, l_3, mu_1, mu_2, mu_3 = [
916
+ as_int(i) for i in (l_1, l_2, l_3, mu_1, mu_2, mu_3)]
917
+
918
+ # check for quick exits
919
+ if sum(1 for i in (mu_1, mu_2, mu_3) if i < 0) % 2:
920
+ return S.Zero # odd number of negative m
921
+ if (l_1 + l_2 + l_3) % 2:
922
+ return S.Zero # sum of l is odd
923
+ lmax = l_2 + l_3
924
+ lmin = max(abs(l_2 - l_3), min(abs(mu_2 + mu_3), abs(mu_2 - mu_3)))
925
+ if (lmin + lmax) % 2:
926
+ lmin += 1
927
+ if lmin not in range(lmax, lmin - 2, -2):
928
+ return S.Zero
929
+
930
+ kron_del = lambda i, j: 1 if i == j else 0
931
+ s = lambda e: -1 if e % 2 else 1 # (-1)**e to give +/-1, avoiding float when e<0
932
+
933
+ t = lambda x: 1 if x > 0 else 0
934
+ A = lambda mu, m: t(-mu) * (s(m) * kron_del(m, mu) - kron_del(m, -mu))
935
+ B = lambda mu, m: t(mu) * (kron_del(m, mu) + s(m) * kron_del(m, -mu))
936
+ U = lambda mu, m: kron_del(abs(mu), abs(m)) * (kron_del(mu, 0) * kron_del(m, 0) + (B(mu, m) + I * A(mu, m))/sqrt(2))
937
+
938
+ ugnt = 0
939
+ for m1 in range(-l_1, l_1+1):
940
+ U1 = U(mu_1, m1)
941
+ for m2 in range(-l_2, l_2+1):
942
+ U2 = U(mu_2, m2)
943
+ U3 = U(mu_3,-m1-m2)
944
+ ugnt = ugnt + re(U1*U2*U3)*gaunt(l_1, l_2, l_3, m1, m2, -m1 - m2, prec=prec)
945
+
946
+ return ugnt
947
+
948
+
949
+ class Wigner3j(Function):
950
+
951
+ def doit(self, **hints):
952
+ if all(obj.is_number for obj in self.args):
953
+ return wigner_3j(*self.args)
954
+ else:
955
+ return self
956
+
957
+ def dot_rot_grad_Ynm(j, p, l, m, theta, phi):
958
+ r"""
959
+ Returns dot product of rotational gradients of spherical harmonics.
960
+
961
+ Explanation
962
+ ===========
963
+
964
+ This function returns the right hand side of the following expression:
965
+
966
+ .. math ::
967
+ \vec{R}Y{_j^{p}} \cdot \vec{R}Y{_l^{m}} = (-1)^{m+p}
968
+ \sum\limits_{k=|l-j|}^{l+j}Y{_k^{m+p}} * \alpha_{l,m,j,p,k} *
969
+ \frac{1}{2} (k^2-j^2-l^2+k-j-l)
970
+
971
+
972
+ Arguments
973
+ =========
974
+
975
+ j, p, l, m .... indices in spherical harmonics (expressions or integers)
976
+ theta, phi .... angle arguments in spherical harmonics
977
+
978
+ Example
979
+ =======
980
+
981
+ >>> from sympy import symbols
982
+ >>> from sympy.physics.wigner import dot_rot_grad_Ynm
983
+ >>> theta, phi = symbols("theta phi")
984
+ >>> dot_rot_grad_Ynm(3, 2, 2, 0, theta, phi).doit()
985
+ 3*sqrt(55)*Ynm(5, 2, theta, phi)/(11*sqrt(pi))
986
+
987
+ """
988
+ j = sympify(j)
989
+ p = sympify(p)
990
+ l = sympify(l)
991
+ m = sympify(m)
992
+ theta = sympify(theta)
993
+ phi = sympify(phi)
994
+ k = Dummy("k")
995
+
996
+ def alpha(l,m,j,p,k):
997
+ return sqrt((2*l+1)*(2*j+1)*(2*k+1)/(4*pi)) * \
998
+ Wigner3j(j, l, k, S.Zero, S.Zero, S.Zero) * \
999
+ Wigner3j(j, l, k, p, m, -m-p)
1000
+
1001
+ return (S.NegativeOne)**(m+p) * Sum(Ynm(k, m+p, theta, phi) * alpha(l,m,j,p,k) / 2 \
1002
+ *(k**2-j**2-l**2+k-j-l), (k, abs(l-j), l+j))
1003
+
1004
+
1005
+ def wigner_d_small(J, beta):
1006
+ """Return the small Wigner d matrix for angular momentum J.
1007
+
1008
+ Explanation
1009
+ ===========
1010
+
1011
+ J : An integer, half-integer, or SymPy symbol for the total angular
1012
+ momentum of the angular momentum space being rotated.
1013
+ beta : A real number representing the Euler angle of rotation about
1014
+ the so-called line of nodes. See [Edmonds74]_.
1015
+
1016
+ Returns
1017
+ =======
1018
+
1019
+ A matrix representing the corresponding Euler angle rotation( in the basis
1020
+ of eigenvectors of `J_z`).
1021
+
1022
+ .. math ::
1023
+ \\mathcal{d}_{\\beta} = \\exp\\big( \\frac{i\\beta}{\\hbar} J_y\\big)
1024
+
1025
+ such that
1026
+
1027
+ .. math ::
1028
+ d^{(J)}_{m',m}(\\beta) = \\mathtt{wigner\\_d\\_small(J,beta)[J-mprime,J-m]}
1029
+
1030
+ The components are calculated using the general form [Edmonds74]_,
1031
+ equation 4.1.15.
1032
+
1033
+ Examples
1034
+ ========
1035
+
1036
+ >>> from sympy import Integer, symbols, pi, pprint
1037
+ >>> from sympy.physics.wigner import wigner_d_small
1038
+ >>> half = 1/Integer(2)
1039
+ >>> beta = symbols("beta", real=True)
1040
+ >>> pprint(wigner_d_small(half, beta), use_unicode=True)
1041
+ ⎑ βŽ›Ξ²βŽž βŽ›Ξ²βŽžβŽ€
1042
+ ⎒cosβŽœβ”€βŽŸ sinβŽœβ”€βŽŸβŽ₯
1043
+ ⎒ ⎝2⎠ ⎝2⎠βŽ₯
1044
+ ⎒ βŽ₯
1045
+ ⎒ βŽ›Ξ²βŽž βŽ›Ξ²βŽžβŽ₯
1046
+ ⎒-sinβŽœβ”€βŽŸ cosβŽœβ”€βŽŸβŽ₯
1047
+ ⎣ ⎝2⎠ ⎝2⎠⎦
1048
+
1049
+ >>> pprint(wigner_d_small(2*half, beta), use_unicode=True)
1050
+ ⎑ 2βŽ›Ξ²βŽž βŽ›Ξ²βŽž βŽ›Ξ²βŽž 2βŽ›Ξ²βŽž ⎀
1051
+ ⎒ cos βŽœβ”€βŽŸ √2β‹…sinβŽœβ”€βŽŸβ‹…cosβŽœβ”€βŽŸ sin βŽœβ”€βŽŸ βŽ₯
1052
+ ⎒ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠ βŽ₯
1053
+ ⎒ βŽ₯
1054
+ ⎒ βŽ›Ξ²βŽž βŽ›Ξ²βŽž 2βŽ›Ξ²βŽž 2βŽ›Ξ²βŽž βŽ›Ξ²βŽž βŽ›Ξ²βŽžβŽ₯
1055
+ ⎒-√2β‹…sinβŽœβ”€βŽŸβ‹…cosβŽœβ”€βŽŸ - sin βŽœβ”€βŽŸ + cos βŽœβ”€βŽŸ √2β‹…sinβŽœβ”€βŽŸβ‹…cosβŽœβ”€βŽŸβŽ₯
1056
+ ⎒ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠βŽ₯
1057
+ ⎒ βŽ₯
1058
+ ⎒ 2βŽ›Ξ²βŽž βŽ›Ξ²βŽž βŽ›Ξ²βŽž 2βŽ›Ξ²βŽž βŽ₯
1059
+ ⎒ sin βŽœβ”€βŽŸ -√2β‹…sinβŽœβ”€βŽŸβ‹…cosβŽœβ”€βŽŸ cos βŽœβ”€βŽŸ βŽ₯
1060
+ ⎣ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠ ⎦
1061
+
1062
+ From table 4 in [Edmonds74]_
1063
+
1064
+ >>> pprint(wigner_d_small(half, beta).subs({beta:pi/2}), use_unicode=True)
1065
+ ⎑ √2 √2⎀
1066
+ ⎒ ── ──βŽ₯
1067
+ ⎒ 2 2 βŽ₯
1068
+ ⎒ βŽ₯
1069
+ ⎒-√2 √2βŽ₯
1070
+ βŽ’β”€β”€β”€β”€ ──βŽ₯
1071
+ ⎣ 2 2 ⎦
1072
+
1073
+ >>> pprint(wigner_d_small(2*half, beta).subs({beta:pi/2}),
1074
+ ... use_unicode=True)
1075
+ ⎑ √2 ⎀
1076
+ ⎒1/2 ── 1/2βŽ₯
1077
+ ⎒ 2 βŽ₯
1078
+ ⎒ βŽ₯
1079
+ ⎒-√2 √2 βŽ₯
1080
+ βŽ’β”€β”€β”€β”€ 0 ── βŽ₯
1081
+ ⎒ 2 2 βŽ₯
1082
+ ⎒ βŽ₯
1083
+ ⎒ -√2 βŽ₯
1084
+ ⎒1/2 ──── 1/2βŽ₯
1085
+ ⎣ 2 ⎦
1086
+
1087
+ >>> pprint(wigner_d_small(3*half, beta).subs({beta:pi/2}),
1088
+ ... use_unicode=True)
1089
+ ⎑ √2 √6 √6 √2⎀
1090
+ ⎒ ── ── ── ──βŽ₯
1091
+ ⎒ 4 4 4 4 βŽ₯
1092
+ ⎒ βŽ₯
1093
+ ⎒-√6 -√2 √2 √6βŽ₯
1094
+ βŽ’β”€β”€β”€β”€ ──── ── ──βŽ₯
1095
+ ⎒ 4 4 4 4 βŽ₯
1096
+ ⎒ βŽ₯
1097
+ ⎒ √6 -√2 -√2 √6βŽ₯
1098
+ ⎒ ── ──── ──── ──βŽ₯
1099
+ ⎒ 4 4 4 4 βŽ₯
1100
+ ⎒ βŽ₯
1101
+ ⎒-√2 √6 -√6 √2βŽ₯
1102
+ βŽ’β”€β”€β”€β”€ ── ──── ──βŽ₯
1103
+ ⎣ 4 4 4 4 ⎦
1104
+
1105
+ >>> pprint(wigner_d_small(4*half, beta).subs({beta:pi/2}),
1106
+ ... use_unicode=True)
1107
+ ⎑ √6 ⎀
1108
+ ⎒1/4 1/2 ── 1/2 1/4βŽ₯
1109
+ ⎒ 4 βŽ₯
1110
+ ⎒ βŽ₯
1111
+ ⎒-1/2 -1/2 0 1/2 1/2βŽ₯
1112
+ ⎒ βŽ₯
1113
+ ⎒ √6 √6 βŽ₯
1114
+ ⎒ ── 0 -1/2 0 ── βŽ₯
1115
+ ⎒ 4 4 βŽ₯
1116
+ ⎒ βŽ₯
1117
+ ⎒-1/2 1/2 0 -1/2 1/2βŽ₯
1118
+ ⎒ βŽ₯
1119
+ ⎒ √6 βŽ₯
1120
+ ⎒1/4 -1/2 ── -1/2 1/4βŽ₯
1121
+ ⎣ 4 ⎦
1122
+
1123
+ """
1124
+ M = [J-i for i in range(2*J+1)]
1125
+ d = zeros(2*J+1)
1126
+
1127
+ # Mi corresponds to Edmonds' $m'$, and Mj to $m$.
1128
+ for i, Mi in enumerate(M):
1129
+ for j, Mj in enumerate(M):
1130
+
1131
+ # We get the maximum and minimum value of sigma.
1132
+ sigmamax = min([J-Mi, J-Mj])
1133
+ sigmamin = max([0, -Mi-Mj])
1134
+
1135
+ dij = sqrt(factorial(J+Mi)*factorial(J-Mi) /
1136
+ factorial(J+Mj)/factorial(J-Mj))
1137
+ terms = [(-1)**(J-Mi-s) *
1138
+ binomial(J+Mj, J-Mi-s) *
1139
+ binomial(J-Mj, s) *
1140
+ cos(beta/2)**(2*s+Mi+Mj) *
1141
+ sin(beta/2)**(2*J-2*s-Mj-Mi)
1142
+ for s in range(sigmamin, sigmamax+1)]
1143
+
1144
+ d[i, j] = dij*Add(*terms)
1145
+
1146
+ return ImmutableMatrix(d)
1147
+
1148
+
1149
+ def wigner_d(J, alpha, beta, gamma):
1150
+ """Return the Wigner D matrix for angular momentum J.
1151
+
1152
+ Explanation
1153
+ ===========
1154
+
1155
+ J :
1156
+ An integer, half-integer, or SymPy symbol for the total angular
1157
+ momentum of the angular momentum space being rotated.
1158
+ alpha, beta, gamma - Real numbers representing the Euler.
1159
+ Angles of rotation about the so-called figure axis, line of nodes,
1160
+ and vertical. See [Edmonds74]_, however note that the symbols alpha
1161
+ and gamma are swapped in this implementation.
1162
+
1163
+ Returns
1164
+ =======
1165
+
1166
+ A matrix representing the corresponding Euler angle rotation (in the basis
1167
+ of eigenvectors of `J_z`).
1168
+
1169
+ .. math ::
1170
+ \\mathcal{D}_{\\alpha \\beta \\gamma} =
1171
+ \\exp\\big( \\frac{i\\alpha}{\\hbar} J_z\\big)
1172
+ \\exp\\big( \\frac{i\\beta}{\\hbar} J_y\\big)
1173
+ \\exp\\big( \\frac{i\\gamma}{\\hbar} J_z\\big)
1174
+
1175
+ such that
1176
+
1177
+ .. math ::
1178
+ \\mathcal{D}^{(J)}_{m',m}(\\alpha, \\beta, \\gamma) =
1179
+ \\mathtt{wigner_d(J, alpha, beta, gamma)[J-mprime,J-m]}
1180
+
1181
+ The components are calculated using the general form [Edmonds74]_,
1182
+ equation 4.1.12, however note that the angles alpha and gamma are swapped
1183
+ in this implementation.
1184
+
1185
+ Examples
1186
+ ========
1187
+
1188
+ The simplest possible example:
1189
+
1190
+ >>> from sympy.physics.wigner import wigner_d
1191
+ >>> from sympy import Integer, symbols, pprint
1192
+ >>> half = 1/Integer(2)
1193
+ >>> alpha, beta, gamma = symbols("alpha, beta, gamma", real=True)
1194
+ >>> pprint(wigner_d(half, alpha, beta, gamma), use_unicode=True)
1195
+ ⎑ β…ˆβ‹…Ξ± β…ˆβ‹…Ξ³ β…ˆβ‹…Ξ± -β…ˆβ‹…Ξ³ ⎀
1196
+ ⎒ ─── ─── ─── ───── βŽ₯
1197
+ ⎒ 2 2 βŽ›Ξ²βŽž 2 2 βŽ›Ξ²βŽž βŽ₯
1198
+ ⎒ β„― β‹…β„― β‹…cosβŽœβ”€βŽŸ β„― β‹…β„― β‹…sinβŽœβ”€βŽŸ βŽ₯
1199
+ ⎒ ⎝2⎠ ⎝2⎠ βŽ₯
1200
+ ⎒ βŽ₯
1201
+ ⎒ -β…ˆβ‹…Ξ± β…ˆβ‹…Ξ³ -β…ˆβ‹…Ξ± -β…ˆβ‹…Ξ³ βŽ₯
1202
+ ⎒ ───── ─── ───── ───── βŽ₯
1203
+ ⎒ 2 2 βŽ›Ξ²βŽž 2 2 βŽ›Ξ²βŽžβŽ₯
1204
+ ⎒-β„― β‹…β„― β‹…sinβŽœβ”€βŽŸ β„― β‹…β„― β‹…cosβŽœβ”€βŽŸβŽ₯
1205
+ ⎣ ⎝2⎠ ⎝2⎠⎦
1206
+
1207
+ """
1208
+ d = wigner_d_small(J, beta)
1209
+ M = [J-i for i in range(2*J+1)]
1210
+ # Mi corresponds to Edmonds' $m'$, and Mj to $m$.
1211
+ D = [[exp(I*Mi*alpha)*d[i, j]*exp(I*Mj*gamma)
1212
+ for j, Mj in enumerate(M)] for i, Mi in enumerate(M)]
1213
+ return ImmutableMatrix(D)
.venv/lib/python3.12/site-packages/sympy/plotting/__init__.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .plot import plot_backends
2
+ from .plot_implicit import plot_implicit
3
+ from .textplot import textplot
4
+ from .pygletplot import PygletPlot
5
+ from .plot import PlotGrid
6
+ from .plot import (plot, plot_parametric, plot3d, plot3d_parametric_surface,
7
+ plot3d_parametric_line, plot_contour)
8
+
9
+ __all__ = [
10
+ 'plot_backends',
11
+
12
+ 'plot_implicit',
13
+
14
+ 'textplot',
15
+
16
+ 'PygletPlot',
17
+
18
+ 'PlotGrid',
19
+
20
+ 'plot', 'plot_parametric', 'plot3d', 'plot3d_parametric_surface',
21
+ 'plot3d_parametric_line', 'plot_contour'
22
+ ]
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (626 Bytes). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/experimental_lambdify.cpython-312.pyc ADDED
Binary file (20.7 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plot.cpython-312.pyc ADDED
Binary file (43.9 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plot_implicit.cpython-312.pyc ADDED
Binary file (8.5 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/plotgrid.cpython-312.pyc ADDED
Binary file (8.35 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/series.cpython-312.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:85d3d0a35ea20954f21fa7ed28cefdd2eb212cbc31f9b878c353b66ed5905c3a
3
+ size 121802
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/textplot.cpython-312.pyc ADDED
Binary file (7.07 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/__pycache__/utils.cpython-312.pyc ADDED
Binary file (14.4 kB). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/backends/__init__.py ADDED
File without changes
.venv/lib/python3.12/site-packages/sympy/plotting/backends/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (198 Bytes). View file
 
.venv/lib/python3.12/site-packages/sympy/plotting/backends/__pycache__/base_backend.cpython-312.pyc ADDED
Binary file (18.1 kB). View file