Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- mgm/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/__init__.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/physics/continuum_mechanics/__pycache__/beam.cpython-310.pyc +3 -0
- mgm/lib/python3.10/site-packages/sympy/unify/__init__.py +15 -0
- mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/__init__.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/core.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/rewrite.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/usympy.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/core.py +234 -0
- mgm/lib/python3.10/site-packages/sympy/unify/rewrite.py +55 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/__init__.py +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_rewrite.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_sympy.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_unify.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/test_rewrite.py +74 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/test_sympy.py +162 -0
- mgm/lib/python3.10/site-packages/sympy/unify/tests/test_unify.py +88 -0
- mgm/lib/python3.10/site-packages/sympy/unify/usympy.py +124 -0
- mgm/lib/python3.10/site-packages/sympy/vector/__init__.py +47 -0
- mgm/lib/python3.10/site-packages/sympy/vector/basisdependent.py +365 -0
- mgm/lib/python3.10/site-packages/sympy/vector/coordsysrect.py +1034 -0
- mgm/lib/python3.10/site-packages/sympy/vector/deloperator.py +121 -0
- mgm/lib/python3.10/site-packages/sympy/vector/dyadic.py +285 -0
- mgm/lib/python3.10/site-packages/sympy/vector/functions.py +517 -0
- mgm/lib/python3.10/site-packages/sympy/vector/implicitregion.py +506 -0
- mgm/lib/python3.10/site-packages/sympy/vector/integrals.py +206 -0
- mgm/lib/python3.10/site-packages/sympy/vector/operators.py +335 -0
- mgm/lib/python3.10/site-packages/sympy/vector/orienters.py +398 -0
- mgm/lib/python3.10/site-packages/sympy/vector/parametricregion.py +189 -0
- mgm/lib/python3.10/site-packages/sympy/vector/point.py +151 -0
- mgm/lib/python3.10/site-packages/sympy/vector/scalar.py +69 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/__init__.py +0 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_dyadic.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_functions.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_integrals.cpython-310.pyc +0 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_coordsysrect.py +464 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_dyadic.py +134 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_field_functions.py +321 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_functions.py +184 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_implicitregion.py +90 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_integrals.py +106 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_operators.py +43 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_parametricregion.py +97 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_printing.py +221 -0
- mgm/lib/python3.10/site-packages/sympy/vector/tests/test_vector.py +266 -0
- mgm/lib/python3.10/site-packages/sympy/vector/vector.py +623 -0
- wemm/lib/python3.10/site-packages/torch/include/torch/csrc/jit/jit_opt_limit.h +39 -0
- wemm/lib/python3.10/site-packages/torch/include/torch/csrc/jit/passes/common_subexpression_elimination.h +11 -0
.gitattributes
CHANGED
|
@@ -1083,3 +1083,4 @@ vila/lib/python3.10/site-packages/scipy/optimize/_moduleTNC.cpython-310-x86_64-l
|
|
| 1083 |
mantis_evalkit/lib/python3.10/site-packages/pyarrow/libarrow.so.1900 filter=lfs diff=lfs merge=lfs -text
|
| 1084 |
wemm/lib/python3.10/site-packages/nvfuser/_C.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1085 |
mgm/lib/python3.10/site-packages/torchvision/_C.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1083 |
mantis_evalkit/lib/python3.10/site-packages/pyarrow/libarrow.so.1900 filter=lfs diff=lfs merge=lfs -text
|
| 1084 |
wemm/lib/python3.10/site-packages/nvfuser/_C.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1085 |
mgm/lib/python3.10/site-packages/torchvision/_C.so filter=lfs diff=lfs merge=lfs -text
|
| 1086 |
+
mgm/lib/python3.10/site-packages/sympy/physics/continuum_mechanics/__pycache__/beam.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
mgm/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (174 Bytes). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc
ADDED
|
Binary file (882 Bytes). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/physics/continuum_mechanics/__pycache__/beam.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0edd50d6210aa3e13cb2258869ebbfafd4c02b119609793f8d98dca1dd19c598
|
| 3 |
+
size 122349
|
mgm/lib/python3.10/site-packages/sympy/unify/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Unification in SymPy
|
| 2 |
+
|
| 3 |
+
See sympy.unify.core docstring for algorithmic details
|
| 4 |
+
|
| 5 |
+
See http://matthewrocklin.com/blog/work/2012/11/01/Unification/ for discussion
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from .usympy import unify, rebuild
|
| 9 |
+
from .rewrite import rewriterule
|
| 10 |
+
|
| 11 |
+
__all__ = [
|
| 12 |
+
'unify', 'rebuild',
|
| 13 |
+
|
| 14 |
+
'rewriterule',
|
| 15 |
+
]
|
mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (468 Bytes). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/core.cpython-310.pyc
ADDED
|
Binary file (8.78 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/rewrite.cpython-310.pyc
ADDED
|
Binary file (2.15 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/__pycache__/usympy.cpython-310.pyc
ADDED
|
Binary file (5.41 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/core.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Generic Unification algorithm for expression trees with lists of children
|
| 2 |
+
|
| 3 |
+
This implementation is a direct translation of
|
| 4 |
+
|
| 5 |
+
Artificial Intelligence: A Modern Approach by Stuart Russel and Peter Norvig
|
| 6 |
+
Second edition, section 9.2, page 276
|
| 7 |
+
|
| 8 |
+
It is modified in the following ways:
|
| 9 |
+
|
| 10 |
+
1. We allow associative and commutative Compound expressions. This results in
|
| 11 |
+
combinatorial blowup.
|
| 12 |
+
2. We explore the tree lazily.
|
| 13 |
+
3. We provide generic interfaces to symbolic algebra libraries in Python.
|
| 14 |
+
|
| 15 |
+
A more traditional version can be found here
|
| 16 |
+
http://aima.cs.berkeley.edu/python/logic.html
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from sympy.utilities.iterables import kbins
|
| 20 |
+
|
| 21 |
+
class Compound:
|
| 22 |
+
""" A little class to represent an interior node in the tree
|
| 23 |
+
|
| 24 |
+
This is analogous to SymPy.Basic for non-Atoms
|
| 25 |
+
"""
|
| 26 |
+
def __init__(self, op, args):
|
| 27 |
+
self.op = op
|
| 28 |
+
self.args = args
|
| 29 |
+
|
| 30 |
+
def __eq__(self, other):
|
| 31 |
+
return (type(self) is type(other) and self.op == other.op and
|
| 32 |
+
self.args == other.args)
|
| 33 |
+
|
| 34 |
+
def __hash__(self):
|
| 35 |
+
return hash((type(self), self.op, self.args))
|
| 36 |
+
|
| 37 |
+
def __str__(self):
|
| 38 |
+
return "%s[%s]" % (str(self.op), ', '.join(map(str, self.args)))
|
| 39 |
+
|
| 40 |
+
class Variable:
|
| 41 |
+
""" A Wild token """
|
| 42 |
+
def __init__(self, arg):
|
| 43 |
+
self.arg = arg
|
| 44 |
+
|
| 45 |
+
def __eq__(self, other):
|
| 46 |
+
return type(self) is type(other) and self.arg == other.arg
|
| 47 |
+
|
| 48 |
+
def __hash__(self):
|
| 49 |
+
return hash((type(self), self.arg))
|
| 50 |
+
|
| 51 |
+
def __str__(self):
|
| 52 |
+
return "Variable(%s)" % str(self.arg)
|
| 53 |
+
|
| 54 |
+
class CondVariable:
|
| 55 |
+
""" A wild token that matches conditionally.
|
| 56 |
+
|
| 57 |
+
arg - a wild token.
|
| 58 |
+
valid - an additional constraining function on a match.
|
| 59 |
+
"""
|
| 60 |
+
def __init__(self, arg, valid):
|
| 61 |
+
self.arg = arg
|
| 62 |
+
self.valid = valid
|
| 63 |
+
|
| 64 |
+
def __eq__(self, other):
|
| 65 |
+
return (type(self) is type(other) and
|
| 66 |
+
self.arg == other.arg and
|
| 67 |
+
self.valid == other.valid)
|
| 68 |
+
|
| 69 |
+
def __hash__(self):
|
| 70 |
+
return hash((type(self), self.arg, self.valid))
|
| 71 |
+
|
| 72 |
+
def __str__(self):
|
| 73 |
+
return "CondVariable(%s)" % str(self.arg)
|
| 74 |
+
|
| 75 |
+
def unify(x, y, s=None, **fns):
|
| 76 |
+
""" Unify two expressions.
|
| 77 |
+
|
| 78 |
+
Parameters
|
| 79 |
+
==========
|
| 80 |
+
|
| 81 |
+
x, y - expression trees containing leaves, Compounds and Variables.
|
| 82 |
+
s - a mapping of variables to subtrees.
|
| 83 |
+
|
| 84 |
+
Returns
|
| 85 |
+
=======
|
| 86 |
+
|
| 87 |
+
lazy sequence of mappings {Variable: subtree}
|
| 88 |
+
|
| 89 |
+
Examples
|
| 90 |
+
========
|
| 91 |
+
|
| 92 |
+
>>> from sympy.unify.core import unify, Compound, Variable
|
| 93 |
+
>>> expr = Compound("Add", ("x", "y"))
|
| 94 |
+
>>> pattern = Compound("Add", ("x", Variable("a")))
|
| 95 |
+
>>> next(unify(expr, pattern, {}))
|
| 96 |
+
{Variable(a): 'y'}
|
| 97 |
+
"""
|
| 98 |
+
s = s or {}
|
| 99 |
+
|
| 100 |
+
if x == y:
|
| 101 |
+
yield s
|
| 102 |
+
elif isinstance(x, (Variable, CondVariable)):
|
| 103 |
+
yield from unify_var(x, y, s, **fns)
|
| 104 |
+
elif isinstance(y, (Variable, CondVariable)):
|
| 105 |
+
yield from unify_var(y, x, s, **fns)
|
| 106 |
+
elif isinstance(x, Compound) and isinstance(y, Compound):
|
| 107 |
+
is_commutative = fns.get('is_commutative', lambda x: False)
|
| 108 |
+
is_associative = fns.get('is_associative', lambda x: False)
|
| 109 |
+
for sop in unify(x.op, y.op, s, **fns):
|
| 110 |
+
if is_associative(x) and is_associative(y):
|
| 111 |
+
a, b = (x, y) if len(x.args) < len(y.args) else (y, x)
|
| 112 |
+
if is_commutative(x) and is_commutative(y):
|
| 113 |
+
combs = allcombinations(a.args, b.args, 'commutative')
|
| 114 |
+
else:
|
| 115 |
+
combs = allcombinations(a.args, b.args, 'associative')
|
| 116 |
+
for aaargs, bbargs in combs:
|
| 117 |
+
aa = [unpack(Compound(a.op, arg)) for arg in aaargs]
|
| 118 |
+
bb = [unpack(Compound(b.op, arg)) for arg in bbargs]
|
| 119 |
+
yield from unify(aa, bb, sop, **fns)
|
| 120 |
+
elif len(x.args) == len(y.args):
|
| 121 |
+
yield from unify(x.args, y.args, sop, **fns)
|
| 122 |
+
|
| 123 |
+
elif is_args(x) and is_args(y) and len(x) == len(y):
|
| 124 |
+
if len(x) == 0:
|
| 125 |
+
yield s
|
| 126 |
+
else:
|
| 127 |
+
for shead in unify(x[0], y[0], s, **fns):
|
| 128 |
+
yield from unify(x[1:], y[1:], shead, **fns)
|
| 129 |
+
|
| 130 |
+
def unify_var(var, x, s, **fns):
|
| 131 |
+
if var in s:
|
| 132 |
+
yield from unify(s[var], x, s, **fns)
|
| 133 |
+
elif occur_check(var, x):
|
| 134 |
+
pass
|
| 135 |
+
elif isinstance(var, CondVariable) and var.valid(x):
|
| 136 |
+
yield assoc(s, var, x)
|
| 137 |
+
elif isinstance(var, Variable):
|
| 138 |
+
yield assoc(s, var, x)
|
| 139 |
+
|
| 140 |
+
def occur_check(var, x):
|
| 141 |
+
""" var occurs in subtree owned by x? """
|
| 142 |
+
if var == x:
|
| 143 |
+
return True
|
| 144 |
+
elif isinstance(x, Compound):
|
| 145 |
+
return occur_check(var, x.args)
|
| 146 |
+
elif is_args(x):
|
| 147 |
+
if any(occur_check(var, xi) for xi in x): return True
|
| 148 |
+
return False
|
| 149 |
+
|
| 150 |
+
def assoc(d, key, val):
|
| 151 |
+
""" Return copy of d with key associated to val """
|
| 152 |
+
d = d.copy()
|
| 153 |
+
d[key] = val
|
| 154 |
+
return d
|
| 155 |
+
|
| 156 |
+
def is_args(x):
|
| 157 |
+
""" Is x a traditional iterable? """
|
| 158 |
+
return type(x) in (tuple, list, set)
|
| 159 |
+
|
| 160 |
+
def unpack(x):
|
| 161 |
+
if isinstance(x, Compound) and len(x.args) == 1:
|
| 162 |
+
return x.args[0]
|
| 163 |
+
else:
|
| 164 |
+
return x
|
| 165 |
+
|
| 166 |
+
def allcombinations(A, B, ordered):
|
| 167 |
+
"""
|
| 168 |
+
Restructure A and B to have the same number of elements.
|
| 169 |
+
|
| 170 |
+
Parameters
|
| 171 |
+
==========
|
| 172 |
+
|
| 173 |
+
ordered must be either 'commutative' or 'associative'.
|
| 174 |
+
|
| 175 |
+
A and B can be rearranged so that the larger of the two lists is
|
| 176 |
+
reorganized into smaller sublists.
|
| 177 |
+
|
| 178 |
+
Examples
|
| 179 |
+
========
|
| 180 |
+
|
| 181 |
+
>>> from sympy.unify.core import allcombinations
|
| 182 |
+
>>> for x in allcombinations((1, 2, 3), (5, 6), 'associative'): print(x)
|
| 183 |
+
(((1,), (2, 3)), ((5,), (6,)))
|
| 184 |
+
(((1, 2), (3,)), ((5,), (6,)))
|
| 185 |
+
|
| 186 |
+
>>> for x in allcombinations((1, 2, 3), (5, 6), 'commutative'): print(x)
|
| 187 |
+
(((1,), (2, 3)), ((5,), (6,)))
|
| 188 |
+
(((1, 2), (3,)), ((5,), (6,)))
|
| 189 |
+
(((1,), (3, 2)), ((5,), (6,)))
|
| 190 |
+
(((1, 3), (2,)), ((5,), (6,)))
|
| 191 |
+
(((2,), (1, 3)), ((5,), (6,)))
|
| 192 |
+
(((2, 1), (3,)), ((5,), (6,)))
|
| 193 |
+
(((2,), (3, 1)), ((5,), (6,)))
|
| 194 |
+
(((2, 3), (1,)), ((5,), (6,)))
|
| 195 |
+
(((3,), (1, 2)), ((5,), (6,)))
|
| 196 |
+
(((3, 1), (2,)), ((5,), (6,)))
|
| 197 |
+
(((3,), (2, 1)), ((5,), (6,)))
|
| 198 |
+
(((3, 2), (1,)), ((5,), (6,)))
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
if ordered == "commutative":
|
| 202 |
+
ordered = 11
|
| 203 |
+
if ordered == "associative":
|
| 204 |
+
ordered = None
|
| 205 |
+
sm, bg = (A, B) if len(A) < len(B) else (B, A)
|
| 206 |
+
for part in kbins(list(range(len(bg))), len(sm), ordered=ordered):
|
| 207 |
+
if bg == B:
|
| 208 |
+
yield tuple((a,) for a in A), partition(B, part)
|
| 209 |
+
else:
|
| 210 |
+
yield partition(A, part), tuple((b,) for b in B)
|
| 211 |
+
|
| 212 |
+
def partition(it, part):
|
| 213 |
+
""" Partition a tuple/list into pieces defined by indices.
|
| 214 |
+
|
| 215 |
+
Examples
|
| 216 |
+
========
|
| 217 |
+
|
| 218 |
+
>>> from sympy.unify.core import partition
|
| 219 |
+
>>> partition((10, 20, 30, 40), [[0, 1, 2], [3]])
|
| 220 |
+
((10, 20, 30), (40,))
|
| 221 |
+
"""
|
| 222 |
+
return type(it)([index(it, ind) for ind in part])
|
| 223 |
+
|
| 224 |
+
def index(it, ind):
|
| 225 |
+
""" Fancy indexing into an indexable iterable (tuple, list).
|
| 226 |
+
|
| 227 |
+
Examples
|
| 228 |
+
========
|
| 229 |
+
|
| 230 |
+
>>> from sympy.unify.core import index
|
| 231 |
+
>>> index([10, 20, 30], (1, 2, 0))
|
| 232 |
+
[20, 30, 10]
|
| 233 |
+
"""
|
| 234 |
+
return type(it)([it[i] for i in ind])
|
mgm/lib/python3.10/site-packages/sympy/unify/rewrite.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Functions to support rewriting of SymPy expressions """
|
| 2 |
+
|
| 3 |
+
from sympy.core.expr import Expr
|
| 4 |
+
from sympy.assumptions import ask
|
| 5 |
+
from sympy.strategies.tools import subs
|
| 6 |
+
from sympy.unify.usympy import rebuild, unify
|
| 7 |
+
|
| 8 |
+
def rewriterule(source, target, variables=(), condition=None, assume=None):
|
| 9 |
+
""" Rewrite rule.
|
| 10 |
+
|
| 11 |
+
Transform expressions that match source into expressions that match target
|
| 12 |
+
treating all ``variables`` as wilds.
|
| 13 |
+
|
| 14 |
+
Examples
|
| 15 |
+
========
|
| 16 |
+
|
| 17 |
+
>>> from sympy.abc import w, x, y, z
|
| 18 |
+
>>> from sympy.unify.rewrite import rewriterule
|
| 19 |
+
>>> from sympy import default_sort_key
|
| 20 |
+
>>> rl = rewriterule(x + y, x**y, [x, y])
|
| 21 |
+
>>> sorted(rl(z + 3), key=default_sort_key)
|
| 22 |
+
[3**z, z**3]
|
| 23 |
+
|
| 24 |
+
Use ``condition`` to specify additional requirements. Inputs are taken in
|
| 25 |
+
the same order as is found in variables.
|
| 26 |
+
|
| 27 |
+
>>> rl = rewriterule(x + y, x**y, [x, y], lambda x, y: x.is_integer)
|
| 28 |
+
>>> list(rl(z + 3))
|
| 29 |
+
[3**z]
|
| 30 |
+
|
| 31 |
+
Use ``assume`` to specify additional requirements using new assumptions.
|
| 32 |
+
|
| 33 |
+
>>> from sympy.assumptions import Q
|
| 34 |
+
>>> rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
|
| 35 |
+
>>> list(rl(z + 3))
|
| 36 |
+
[3**z]
|
| 37 |
+
|
| 38 |
+
Assumptions for the local context are provided at rule runtime
|
| 39 |
+
|
| 40 |
+
>>> list(rl(w + z, Q.integer(z)))
|
| 41 |
+
[z**w]
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
def rewrite_rl(expr, assumptions=True):
|
| 45 |
+
for match in unify(source, expr, {}, variables=variables):
|
| 46 |
+
if (condition and
|
| 47 |
+
not condition(*[match.get(var, var) for var in variables])):
|
| 48 |
+
continue
|
| 49 |
+
if (assume and not ask(assume.xreplace(match), assumptions)):
|
| 50 |
+
continue
|
| 51 |
+
expr2 = subs(match)(target)
|
| 52 |
+
if isinstance(expr2, Expr):
|
| 53 |
+
expr2 = rebuild(expr2)
|
| 54 |
+
yield expr2
|
| 55 |
+
return rewrite_rl
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/__init__.py
ADDED
|
File without changes
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (166 Bytes). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_rewrite.cpython-310.pyc
ADDED
|
Binary file (3.17 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_sympy.cpython-310.pyc
ADDED
|
Binary file (6.52 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/__pycache__/test_unify.cpython-310.pyc
ADDED
|
Binary file (4.04 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/test_rewrite.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.unify.rewrite import rewriterule
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.symbol import Symbol
|
| 5 |
+
from sympy.functions.elementary.trigonometric import sin
|
| 6 |
+
from sympy.abc import x, y
|
| 7 |
+
from sympy.strategies.rl import rebuild
|
| 8 |
+
from sympy.assumptions import Q
|
| 9 |
+
|
| 10 |
+
p, q = Symbol('p'), Symbol('q')
|
| 11 |
+
|
| 12 |
+
def test_simple():
|
| 13 |
+
rl = rewriterule(Basic(p, S(1)), Basic(p, S(2)), variables=(p,))
|
| 14 |
+
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
|
| 15 |
+
|
| 16 |
+
p1 = p**2
|
| 17 |
+
p2 = p**3
|
| 18 |
+
rl = rewriterule(p1, p2, variables=(p,))
|
| 19 |
+
|
| 20 |
+
expr = x**2
|
| 21 |
+
assert list(rl(expr)) == [x**3]
|
| 22 |
+
|
| 23 |
+
def test_simple_variables():
|
| 24 |
+
rl = rewriterule(Basic(x, S(1)), Basic(x, S(2)), variables=(x,))
|
| 25 |
+
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
|
| 26 |
+
|
| 27 |
+
rl = rewriterule(x**2, x**3, variables=(x,))
|
| 28 |
+
assert list(rl(y**2)) == [y**3]
|
| 29 |
+
|
| 30 |
+
def test_moderate():
|
| 31 |
+
p1 = p**2 + q**3
|
| 32 |
+
p2 = (p*q)**4
|
| 33 |
+
rl = rewriterule(p1, p2, (p, q))
|
| 34 |
+
|
| 35 |
+
expr = x**2 + y**3
|
| 36 |
+
assert list(rl(expr)) == [(x*y)**4]
|
| 37 |
+
|
| 38 |
+
def test_sincos():
|
| 39 |
+
p1 = sin(p)**2 + sin(p)**2
|
| 40 |
+
p2 = 1
|
| 41 |
+
rl = rewriterule(p1, p2, (p, q))
|
| 42 |
+
|
| 43 |
+
assert list(rl(sin(x)**2 + sin(x)**2)) == [1]
|
| 44 |
+
assert list(rl(sin(y)**2 + sin(y)**2)) == [1]
|
| 45 |
+
|
| 46 |
+
def test_Exprs_ok():
|
| 47 |
+
rl = rewriterule(p+q, q+p, (p, q))
|
| 48 |
+
next(rl(x+y)).is_commutative
|
| 49 |
+
str(next(rl(x+y)))
|
| 50 |
+
|
| 51 |
+
def test_condition_simple():
|
| 52 |
+
rl = rewriterule(x, x+1, [x], lambda x: x < 10)
|
| 53 |
+
assert not list(rl(S(15)))
|
| 54 |
+
assert rebuild(next(rl(S(5)))) == 6
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_condition_multiple():
|
| 58 |
+
rl = rewriterule(x + y, x**y, [x,y], lambda x, y: x.is_integer)
|
| 59 |
+
|
| 60 |
+
a = Symbol('a')
|
| 61 |
+
b = Symbol('b', integer=True)
|
| 62 |
+
expr = a + b
|
| 63 |
+
assert list(rl(expr)) == [b**a]
|
| 64 |
+
|
| 65 |
+
c = Symbol('c', integer=True)
|
| 66 |
+
d = Symbol('d', integer=True)
|
| 67 |
+
assert set(rl(c + d)) == {c**d, d**c}
|
| 68 |
+
|
| 69 |
+
def test_assumptions():
|
| 70 |
+
rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
|
| 71 |
+
|
| 72 |
+
a, b = map(Symbol, 'ab')
|
| 73 |
+
expr = a + b
|
| 74 |
+
assert list(rl(expr, Q.integer(b))) == [b**a]
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/test_sympy.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.add import Add
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.containers import Tuple
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 6 |
+
from sympy.logic.boolalg import And
|
| 7 |
+
from sympy.core.symbol import Str
|
| 8 |
+
from sympy.unify.core import Compound, Variable
|
| 9 |
+
from sympy.unify.usympy import (deconstruct, construct, unify, is_associative,
|
| 10 |
+
is_commutative)
|
| 11 |
+
from sympy.abc import x, y, z, n
|
| 12 |
+
|
| 13 |
+
def test_deconstruct():
|
| 14 |
+
expr = Basic(S(1), S(2), S(3))
|
| 15 |
+
expected = Compound(Basic, (1, 2, 3))
|
| 16 |
+
assert deconstruct(expr) == expected
|
| 17 |
+
|
| 18 |
+
assert deconstruct(1) == 1
|
| 19 |
+
assert deconstruct(x) == x
|
| 20 |
+
assert deconstruct(x, variables=(x,)) == Variable(x)
|
| 21 |
+
assert deconstruct(Add(1, x, evaluate=False)) == Compound(Add, (1, x))
|
| 22 |
+
assert deconstruct(Add(1, x, evaluate=False), variables=(x,)) == \
|
| 23 |
+
Compound(Add, (1, Variable(x)))
|
| 24 |
+
|
| 25 |
+
def test_construct():
|
| 26 |
+
expr = Compound(Basic, (S(1), S(2), S(3)))
|
| 27 |
+
expected = Basic(S(1), S(2), S(3))
|
| 28 |
+
assert construct(expr) == expected
|
| 29 |
+
|
| 30 |
+
def test_nested():
|
| 31 |
+
expr = Basic(S(1), Basic(S(2)), S(3))
|
| 32 |
+
cmpd = Compound(Basic, (S(1), Compound(Basic, Tuple(2)), S(3)))
|
| 33 |
+
assert deconstruct(expr) == cmpd
|
| 34 |
+
assert construct(cmpd) == expr
|
| 35 |
+
|
| 36 |
+
def test_unify():
|
| 37 |
+
expr = Basic(S(1), S(2), S(3))
|
| 38 |
+
a, b, c = map(Symbol, 'abc')
|
| 39 |
+
pattern = Basic(a, b, c)
|
| 40 |
+
assert list(unify(expr, pattern, {}, (a, b, c))) == [{a: 1, b: 2, c: 3}]
|
| 41 |
+
assert list(unify(expr, pattern, variables=(a, b, c))) == \
|
| 42 |
+
[{a: 1, b: 2, c: 3}]
|
| 43 |
+
|
| 44 |
+
def test_unify_variables():
|
| 45 |
+
assert list(unify(Basic(S(1), S(2)), Basic(S(1), x), {}, variables=(x,))) == [{x: 2}]
|
| 46 |
+
|
| 47 |
+
def test_s_input():
|
| 48 |
+
expr = Basic(S(1), S(2))
|
| 49 |
+
a, b = map(Symbol, 'ab')
|
| 50 |
+
pattern = Basic(a, b)
|
| 51 |
+
assert list(unify(expr, pattern, {}, (a, b))) == [{a: 1, b: 2}]
|
| 52 |
+
assert list(unify(expr, pattern, {a: 5}, (a, b))) == []
|
| 53 |
+
|
| 54 |
+
def iterdicteq(a, b):
|
| 55 |
+
a = tuple(a)
|
| 56 |
+
b = tuple(b)
|
| 57 |
+
return len(a) == len(b) and all(x in b for x in a)
|
| 58 |
+
|
| 59 |
+
def test_unify_commutative():
|
| 60 |
+
expr = Add(1, 2, 3, evaluate=False)
|
| 61 |
+
a, b, c = map(Symbol, 'abc')
|
| 62 |
+
pattern = Add(a, b, c, evaluate=False)
|
| 63 |
+
|
| 64 |
+
result = tuple(unify(expr, pattern, {}, (a, b, c)))
|
| 65 |
+
expected = ({a: 1, b: 2, c: 3},
|
| 66 |
+
{a: 1, b: 3, c: 2},
|
| 67 |
+
{a: 2, b: 1, c: 3},
|
| 68 |
+
{a: 2, b: 3, c: 1},
|
| 69 |
+
{a: 3, b: 1, c: 2},
|
| 70 |
+
{a: 3, b: 2, c: 1})
|
| 71 |
+
|
| 72 |
+
assert iterdicteq(result, expected)
|
| 73 |
+
|
| 74 |
+
def test_unify_iter():
|
| 75 |
+
expr = Add(1, 2, 3, evaluate=False)
|
| 76 |
+
a, b, c = map(Symbol, 'abc')
|
| 77 |
+
pattern = Add(a, c, evaluate=False)
|
| 78 |
+
assert is_associative(deconstruct(pattern))
|
| 79 |
+
assert is_commutative(deconstruct(pattern))
|
| 80 |
+
|
| 81 |
+
result = list(unify(expr, pattern, {}, (a, c)))
|
| 82 |
+
expected = [{a: 1, c: Add(2, 3, evaluate=False)},
|
| 83 |
+
{a: 1, c: Add(3, 2, evaluate=False)},
|
| 84 |
+
{a: 2, c: Add(1, 3, evaluate=False)},
|
| 85 |
+
{a: 2, c: Add(3, 1, evaluate=False)},
|
| 86 |
+
{a: 3, c: Add(1, 2, evaluate=False)},
|
| 87 |
+
{a: 3, c: Add(2, 1, evaluate=False)},
|
| 88 |
+
{a: Add(1, 2, evaluate=False), c: 3},
|
| 89 |
+
{a: Add(2, 1, evaluate=False), c: 3},
|
| 90 |
+
{a: Add(1, 3, evaluate=False), c: 2},
|
| 91 |
+
{a: Add(3, 1, evaluate=False), c: 2},
|
| 92 |
+
{a: Add(2, 3, evaluate=False), c: 1},
|
| 93 |
+
{a: Add(3, 2, evaluate=False), c: 1}]
|
| 94 |
+
|
| 95 |
+
assert iterdicteq(result, expected)
|
| 96 |
+
|
| 97 |
+
def test_hard_match():
|
| 98 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 99 |
+
expr = sin(x) + cos(x)**2
|
| 100 |
+
p, q = map(Symbol, 'pq')
|
| 101 |
+
pattern = sin(p) + cos(p)**2
|
| 102 |
+
assert list(unify(expr, pattern, {}, (p, q))) == [{p: x}]
|
| 103 |
+
|
| 104 |
+
def test_matrix():
|
| 105 |
+
from sympy.matrices.expressions.matexpr import MatrixSymbol
|
| 106 |
+
X = MatrixSymbol('X', n, n)
|
| 107 |
+
Y = MatrixSymbol('Y', 2, 2)
|
| 108 |
+
Z = MatrixSymbol('Z', 2, 3)
|
| 109 |
+
assert list(unify(X, Y, {}, variables=[n, Str('X')])) == [{Str('X'): Str('Y'), n: 2}]
|
| 110 |
+
assert list(unify(X, Z, {}, variables=[n, Str('X')])) == []
|
| 111 |
+
|
| 112 |
+
def test_non_frankenAdds():
|
| 113 |
+
# the is_commutative property used to fail because of Basic.__new__
|
| 114 |
+
# This caused is_commutative and str calls to fail
|
| 115 |
+
expr = x+y*2
|
| 116 |
+
rebuilt = construct(deconstruct(expr))
|
| 117 |
+
# Ensure that we can run these commands without causing an error
|
| 118 |
+
str(rebuilt)
|
| 119 |
+
rebuilt.is_commutative
|
| 120 |
+
|
| 121 |
+
def test_FiniteSet_commutivity():
|
| 122 |
+
from sympy.sets.sets import FiniteSet
|
| 123 |
+
a, b, c, x, y = symbols('a,b,c,x,y')
|
| 124 |
+
s = FiniteSet(a, b, c)
|
| 125 |
+
t = FiniteSet(x, y)
|
| 126 |
+
variables = (x, y)
|
| 127 |
+
assert {x: FiniteSet(a, c), y: b} in tuple(unify(s, t, variables=variables))
|
| 128 |
+
|
| 129 |
+
def test_FiniteSet_complex():
|
| 130 |
+
from sympy.sets.sets import FiniteSet
|
| 131 |
+
a, b, c, x, y, z = symbols('a,b,c,x,y,z')
|
| 132 |
+
expr = FiniteSet(Basic(S(1), x), y, Basic(x, z))
|
| 133 |
+
pattern = FiniteSet(a, Basic(x, b))
|
| 134 |
+
variables = a, b
|
| 135 |
+
expected = ({b: 1, a: FiniteSet(y, Basic(x, z))},
|
| 136 |
+
{b: z, a: FiniteSet(y, Basic(S(1), x))})
|
| 137 |
+
assert iterdicteq(unify(expr, pattern, variables=variables), expected)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_and():
|
| 141 |
+
variables = x, y
|
| 142 |
+
expected = ({x: z > 0, y: n < 3},)
|
| 143 |
+
assert iterdicteq(unify((z>0) & (n<3), And(x, y), variables=variables),
|
| 144 |
+
expected)
|
| 145 |
+
|
| 146 |
+
def test_Union():
|
| 147 |
+
from sympy.sets.sets import Interval
|
| 148 |
+
assert list(unify(Interval(0, 1) + Interval(10, 11),
|
| 149 |
+
Interval(0, 1) + Interval(12, 13),
|
| 150 |
+
variables=(Interval(12, 13),)))
|
| 151 |
+
|
| 152 |
+
def test_is_commutative():
|
| 153 |
+
assert is_commutative(deconstruct(x+y))
|
| 154 |
+
assert is_commutative(deconstruct(x*y))
|
| 155 |
+
assert not is_commutative(deconstruct(x**y))
|
| 156 |
+
|
| 157 |
+
def test_commutative_in_commutative():
|
| 158 |
+
from sympy.abc import a,b,c,d
|
| 159 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 160 |
+
eq = sin(3)*sin(4)*sin(5) + 4*cos(3)*cos(4)
|
| 161 |
+
pat = a*cos(b)*cos(c) + d*sin(b)*sin(c)
|
| 162 |
+
assert next(unify(eq, pat, variables=(a,b,c,d)))
|
mgm/lib/python3.10/site-packages/sympy/unify/tests/test_unify.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.unify.core import Compound, Variable, CondVariable, allcombinations
|
| 2 |
+
from sympy.unify import core
|
| 3 |
+
|
| 4 |
+
a,b,c = 'a', 'b', 'c'
|
| 5 |
+
w,x,y,z = map(Variable, 'wxyz')
|
| 6 |
+
|
| 7 |
+
C = Compound
|
| 8 |
+
|
| 9 |
+
def is_associative(x):
|
| 10 |
+
return isinstance(x, Compound) and (x.op in ('Add', 'Mul', 'CAdd', 'CMul'))
|
| 11 |
+
def is_commutative(x):
|
| 12 |
+
return isinstance(x, Compound) and (x.op in ('CAdd', 'CMul'))
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def unify(a, b, s={}):
|
| 16 |
+
return core.unify(a, b, s=s, is_associative=is_associative,
|
| 17 |
+
is_commutative=is_commutative)
|
| 18 |
+
|
| 19 |
+
def test_basic():
|
| 20 |
+
assert list(unify(a, x, {})) == [{x: a}]
|
| 21 |
+
assert list(unify(a, x, {x: 10})) == []
|
| 22 |
+
assert list(unify(1, x, {})) == [{x: 1}]
|
| 23 |
+
assert list(unify(a, a, {})) == [{}]
|
| 24 |
+
assert list(unify((w, x), (y, z), {})) == [{w: y, x: z}]
|
| 25 |
+
assert list(unify(x, (a, b), {})) == [{x: (a, b)}]
|
| 26 |
+
|
| 27 |
+
assert list(unify((a, b), (x, x), {})) == []
|
| 28 |
+
assert list(unify((y, z), (x, x), {}))!= []
|
| 29 |
+
assert list(unify((a, (b, c)), (a, (x, y)), {})) == [{x: b, y: c}]
|
| 30 |
+
|
| 31 |
+
def test_ops():
|
| 32 |
+
assert list(unify(C('Add', (a,b,c)), C('Add', (a,x,y)), {})) == \
|
| 33 |
+
[{x:b, y:c}]
|
| 34 |
+
assert list(unify(C('Add', (C('Mul', (1,2)), b,c)), C('Add', (x,y,c)), {})) == \
|
| 35 |
+
[{x: C('Mul', (1,2)), y:b}]
|
| 36 |
+
|
| 37 |
+
def test_associative():
|
| 38 |
+
c1 = C('Add', (1,2,3))
|
| 39 |
+
c2 = C('Add', (x,y))
|
| 40 |
+
assert tuple(unify(c1, c2, {})) == ({x: 1, y: C('Add', (2, 3))},
|
| 41 |
+
{x: C('Add', (1, 2)), y: 3})
|
| 42 |
+
|
| 43 |
+
def test_commutative():
|
| 44 |
+
c1 = C('CAdd', (1,2,3))
|
| 45 |
+
c2 = C('CAdd', (x,y))
|
| 46 |
+
result = list(unify(c1, c2, {}))
|
| 47 |
+
assert {x: 1, y: C('CAdd', (2, 3))} in result
|
| 48 |
+
assert ({x: 2, y: C('CAdd', (1, 3))} in result or
|
| 49 |
+
{x: 2, y: C('CAdd', (3, 1))} in result)
|
| 50 |
+
|
| 51 |
+
def _test_combinations_assoc():
|
| 52 |
+
assert set(allcombinations((1,2,3), (a,b), True)) == \
|
| 53 |
+
{(((1, 2), (3,)), (a, b)), (((1,), (2, 3)), (a, b))}
|
| 54 |
+
|
| 55 |
+
def _test_combinations_comm():
|
| 56 |
+
assert set(allcombinations((1,2,3), (a,b), None)) == \
|
| 57 |
+
{(((1,), (2, 3)), ('a', 'b')), (((2,), (3, 1)), ('a', 'b')),
|
| 58 |
+
(((3,), (1, 2)), ('a', 'b')), (((1, 2), (3,)), ('a', 'b')),
|
| 59 |
+
(((2, 3), (1,)), ('a', 'b')), (((3, 1), (2,)), ('a', 'b'))}
|
| 60 |
+
|
| 61 |
+
def test_allcombinations():
|
| 62 |
+
assert set(allcombinations((1,2), (1,2), 'commutative')) ==\
|
| 63 |
+
{(((1,),(2,)), ((1,),(2,))), (((1,),(2,)), ((2,),(1,)))}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_commutativity():
|
| 67 |
+
c1 = Compound('CAdd', (a, b))
|
| 68 |
+
c2 = Compound('CAdd', (x, y))
|
| 69 |
+
assert is_commutative(c1) and is_commutative(c2)
|
| 70 |
+
assert len(list(unify(c1, c2, {}))) == 2
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_CondVariable():
|
| 74 |
+
expr = C('CAdd', (1, 2))
|
| 75 |
+
x = Variable('x')
|
| 76 |
+
y = CondVariable('y', lambda a: a % 2 == 0)
|
| 77 |
+
z = CondVariable('z', lambda a: a > 3)
|
| 78 |
+
pattern = C('CAdd', (x, y))
|
| 79 |
+
assert list(unify(expr, pattern, {})) == \
|
| 80 |
+
[{x: 1, y: 2}]
|
| 81 |
+
|
| 82 |
+
z = CondVariable('z', lambda a: a > 3)
|
| 83 |
+
pattern = C('CAdd', (z, y))
|
| 84 |
+
|
| 85 |
+
assert list(unify(expr, pattern, {})) == []
|
| 86 |
+
|
| 87 |
+
def test_defaultdict():
|
| 88 |
+
assert next(unify(Variable('x'), 'foo')) == {Variable('x'): 'foo'}
|
mgm/lib/python3.10/site-packages/sympy/unify/usympy.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" SymPy interface to Unification engine
|
| 2 |
+
|
| 3 |
+
See sympy.unify for module level docstring
|
| 4 |
+
See sympy.unify.core for algorithmic docstring """
|
| 5 |
+
|
| 6 |
+
from sympy.core import Basic, Add, Mul, Pow
|
| 7 |
+
from sympy.core.operations import AssocOp, LatticeOp
|
| 8 |
+
from sympy.matrices import MatAdd, MatMul, MatrixExpr
|
| 9 |
+
from sympy.sets.sets import Union, Intersection, FiniteSet
|
| 10 |
+
from sympy.unify.core import Compound, Variable, CondVariable
|
| 11 |
+
from sympy.unify import core
|
| 12 |
+
|
| 13 |
+
basic_new_legal = [MatrixExpr]
|
| 14 |
+
eval_false_legal = [AssocOp, Pow, FiniteSet]
|
| 15 |
+
illegal = [LatticeOp]
|
| 16 |
+
|
| 17 |
+
def sympy_associative(op):
|
| 18 |
+
assoc_ops = (AssocOp, MatAdd, MatMul, Union, Intersection, FiniteSet)
|
| 19 |
+
return any(issubclass(op, aop) for aop in assoc_ops)
|
| 20 |
+
|
| 21 |
+
def sympy_commutative(op):
|
| 22 |
+
comm_ops = (Add, MatAdd, Union, Intersection, FiniteSet)
|
| 23 |
+
return any(issubclass(op, cop) for cop in comm_ops)
|
| 24 |
+
|
| 25 |
+
def is_associative(x):
|
| 26 |
+
return isinstance(x, Compound) and sympy_associative(x.op)
|
| 27 |
+
|
| 28 |
+
def is_commutative(x):
|
| 29 |
+
if not isinstance(x, Compound):
|
| 30 |
+
return False
|
| 31 |
+
if sympy_commutative(x.op):
|
| 32 |
+
return True
|
| 33 |
+
if issubclass(x.op, Mul):
|
| 34 |
+
return all(construct(arg).is_commutative for arg in x.args)
|
| 35 |
+
|
| 36 |
+
def mk_matchtype(typ):
|
| 37 |
+
def matchtype(x):
|
| 38 |
+
return (isinstance(x, typ) or
|
| 39 |
+
isinstance(x, Compound) and issubclass(x.op, typ))
|
| 40 |
+
return matchtype
|
| 41 |
+
|
| 42 |
+
def deconstruct(s, variables=()):
|
| 43 |
+
""" Turn a SymPy object into a Compound """
|
| 44 |
+
if s in variables:
|
| 45 |
+
return Variable(s)
|
| 46 |
+
if isinstance(s, (Variable, CondVariable)):
|
| 47 |
+
return s
|
| 48 |
+
if not isinstance(s, Basic) or s.is_Atom:
|
| 49 |
+
return s
|
| 50 |
+
return Compound(s.__class__,
|
| 51 |
+
tuple(deconstruct(arg, variables) for arg in s.args))
|
| 52 |
+
|
| 53 |
+
def construct(t):
|
| 54 |
+
""" Turn a Compound into a SymPy object """
|
| 55 |
+
if isinstance(t, (Variable, CondVariable)):
|
| 56 |
+
return t.arg
|
| 57 |
+
if not isinstance(t, Compound):
|
| 58 |
+
return t
|
| 59 |
+
if any(issubclass(t.op, cls) for cls in eval_false_legal):
|
| 60 |
+
return t.op(*map(construct, t.args), evaluate=False)
|
| 61 |
+
elif any(issubclass(t.op, cls) for cls in basic_new_legal):
|
| 62 |
+
return Basic.__new__(t.op, *map(construct, t.args))
|
| 63 |
+
else:
|
| 64 |
+
return t.op(*map(construct, t.args))
|
| 65 |
+
|
| 66 |
+
def rebuild(s):
|
| 67 |
+
""" Rebuild a SymPy expression.
|
| 68 |
+
|
| 69 |
+
This removes harm caused by Expr-Rules interactions.
|
| 70 |
+
"""
|
| 71 |
+
return construct(deconstruct(s))
|
| 72 |
+
|
| 73 |
+
def unify(x, y, s=None, variables=(), **kwargs):
|
| 74 |
+
""" Structural unification of two expressions/patterns.
|
| 75 |
+
|
| 76 |
+
Examples
|
| 77 |
+
========
|
| 78 |
+
|
| 79 |
+
>>> from sympy.unify.usympy import unify
|
| 80 |
+
>>> from sympy import Basic, S
|
| 81 |
+
>>> from sympy.abc import x, y, z, p, q
|
| 82 |
+
|
| 83 |
+
>>> next(unify(Basic(S(1), S(2)), Basic(S(1), x), variables=[x]))
|
| 84 |
+
{x: 2}
|
| 85 |
+
|
| 86 |
+
>>> expr = 2*x + y + z
|
| 87 |
+
>>> pattern = 2*p + q
|
| 88 |
+
>>> next(unify(expr, pattern, {}, variables=(p, q)))
|
| 89 |
+
{p: x, q: y + z}
|
| 90 |
+
|
| 91 |
+
Unification supports commutative and associative matching
|
| 92 |
+
|
| 93 |
+
>>> expr = x + y + z
|
| 94 |
+
>>> pattern = p + q
|
| 95 |
+
>>> len(list(unify(expr, pattern, {}, variables=(p, q))))
|
| 96 |
+
12
|
| 97 |
+
|
| 98 |
+
Symbols not indicated to be variables are treated as literal,
|
| 99 |
+
else they are wild-like and match anything in a sub-expression.
|
| 100 |
+
|
| 101 |
+
>>> expr = x*y*z + 3
|
| 102 |
+
>>> pattern = x*y + 3
|
| 103 |
+
>>> next(unify(expr, pattern, {}, variables=[x, y]))
|
| 104 |
+
{x: y, y: x*z}
|
| 105 |
+
|
| 106 |
+
The x and y of the pattern above were in a Mul and matched factors
|
| 107 |
+
in the Mul of expr. Here, a single symbol matches an entire term:
|
| 108 |
+
|
| 109 |
+
>>> expr = x*y + 3
|
| 110 |
+
>>> pattern = p + 3
|
| 111 |
+
>>> next(unify(expr, pattern, {}, variables=[p]))
|
| 112 |
+
{p: x*y}
|
| 113 |
+
|
| 114 |
+
"""
|
| 115 |
+
decons = lambda x: deconstruct(x, variables)
|
| 116 |
+
s = s or {}
|
| 117 |
+
s = {decons(k): decons(v) for k, v in s.items()}
|
| 118 |
+
|
| 119 |
+
ds = core.unify(decons(x), decons(y), s,
|
| 120 |
+
is_associative=is_associative,
|
| 121 |
+
is_commutative=is_commutative,
|
| 122 |
+
**kwargs)
|
| 123 |
+
for d in ds:
|
| 124 |
+
yield {construct(k): construct(v) for k, v in d.items()}
|
mgm/lib/python3.10/site-packages/sympy/vector/__init__.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 2 |
+
from sympy.vector.vector import (Vector, VectorAdd, VectorMul,
|
| 3 |
+
BaseVector, VectorZero, Cross, Dot, cross, dot)
|
| 4 |
+
from sympy.vector.dyadic import (Dyadic, DyadicAdd, DyadicMul,
|
| 5 |
+
BaseDyadic, DyadicZero)
|
| 6 |
+
from sympy.vector.scalar import BaseScalar
|
| 7 |
+
from sympy.vector.deloperator import Del
|
| 8 |
+
from sympy.vector.functions import (express, matrix_to_vector,
|
| 9 |
+
laplacian, is_conservative,
|
| 10 |
+
is_solenoidal, scalar_potential,
|
| 11 |
+
directional_derivative,
|
| 12 |
+
scalar_potential_difference)
|
| 13 |
+
from sympy.vector.point import Point
|
| 14 |
+
from sympy.vector.orienters import (AxisOrienter, BodyOrienter,
|
| 15 |
+
SpaceOrienter, QuaternionOrienter)
|
| 16 |
+
from sympy.vector.operators import Gradient, Divergence, Curl, Laplacian, gradient, curl, divergence
|
| 17 |
+
from sympy.vector.implicitregion import ImplicitRegion
|
| 18 |
+
from sympy.vector.parametricregion import (ParametricRegion, parametric_region_list)
|
| 19 |
+
from sympy.vector.integrals import (ParametricIntegral, vector_integrate)
|
| 20 |
+
|
| 21 |
+
__all__ = [
|
| 22 |
+
'Vector', 'VectorAdd', 'VectorMul', 'BaseVector', 'VectorZero', 'Cross',
|
| 23 |
+
'Dot', 'cross', 'dot',
|
| 24 |
+
|
| 25 |
+
'Dyadic', 'DyadicAdd', 'DyadicMul', 'BaseDyadic', 'DyadicZero',
|
| 26 |
+
|
| 27 |
+
'BaseScalar',
|
| 28 |
+
|
| 29 |
+
'Del',
|
| 30 |
+
|
| 31 |
+
'CoordSys3D',
|
| 32 |
+
|
| 33 |
+
'express', 'matrix_to_vector', 'laplacian', 'is_conservative',
|
| 34 |
+
'is_solenoidal', 'scalar_potential', 'directional_derivative',
|
| 35 |
+
'scalar_potential_difference',
|
| 36 |
+
|
| 37 |
+
'Point',
|
| 38 |
+
|
| 39 |
+
'AxisOrienter', 'BodyOrienter', 'SpaceOrienter', 'QuaternionOrienter',
|
| 40 |
+
|
| 41 |
+
'Gradient', 'Divergence', 'Curl', 'Laplacian', 'gradient', 'curl',
|
| 42 |
+
'divergence',
|
| 43 |
+
|
| 44 |
+
'ParametricRegion', 'parametric_region_list', 'ImplicitRegion',
|
| 45 |
+
|
| 46 |
+
'ParametricIntegral', 'vector_integrate',
|
| 47 |
+
]
|
mgm/lib/python3.10/site-packages/sympy/vector/basisdependent.py
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from typing import TYPE_CHECKING
|
| 3 |
+
|
| 4 |
+
from sympy.simplify import simplify as simp, trigsimp as tsimp # type: ignore
|
| 5 |
+
from sympy.core.decorators import call_highest_priority, _sympifyit
|
| 6 |
+
from sympy.core.assumptions import StdFactKB
|
| 7 |
+
from sympy.core.function import diff as df
|
| 8 |
+
from sympy.integrals.integrals import Integral
|
| 9 |
+
from sympy.polys.polytools import factor as fctr
|
| 10 |
+
from sympy.core import S, Add, Mul
|
| 11 |
+
from sympy.core.expr import Expr
|
| 12 |
+
|
| 13 |
+
if TYPE_CHECKING:
|
| 14 |
+
from sympy.vector.vector import BaseVector
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class BasisDependent(Expr):
|
| 18 |
+
"""
|
| 19 |
+
Super class containing functionality common to vectors and
|
| 20 |
+
dyadics.
|
| 21 |
+
Named so because the representation of these quantities in
|
| 22 |
+
sympy.vector is dependent on the basis they are expressed in.
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
zero: BasisDependentZero
|
| 26 |
+
|
| 27 |
+
@call_highest_priority('__radd__')
|
| 28 |
+
def __add__(self, other):
|
| 29 |
+
return self._add_func(self, other)
|
| 30 |
+
|
| 31 |
+
@call_highest_priority('__add__')
|
| 32 |
+
def __radd__(self, other):
|
| 33 |
+
return self._add_func(other, self)
|
| 34 |
+
|
| 35 |
+
@call_highest_priority('__rsub__')
|
| 36 |
+
def __sub__(self, other):
|
| 37 |
+
return self._add_func(self, -other)
|
| 38 |
+
|
| 39 |
+
@call_highest_priority('__sub__')
|
| 40 |
+
def __rsub__(self, other):
|
| 41 |
+
return self._add_func(other, -self)
|
| 42 |
+
|
| 43 |
+
@_sympifyit('other', NotImplemented)
|
| 44 |
+
@call_highest_priority('__rmul__')
|
| 45 |
+
def __mul__(self, other):
|
| 46 |
+
return self._mul_func(self, other)
|
| 47 |
+
|
| 48 |
+
@_sympifyit('other', NotImplemented)
|
| 49 |
+
@call_highest_priority('__mul__')
|
| 50 |
+
def __rmul__(self, other):
|
| 51 |
+
return self._mul_func(other, self)
|
| 52 |
+
|
| 53 |
+
def __neg__(self):
|
| 54 |
+
return self._mul_func(S.NegativeOne, self)
|
| 55 |
+
|
| 56 |
+
@_sympifyit('other', NotImplemented)
|
| 57 |
+
@call_highest_priority('__rtruediv__')
|
| 58 |
+
def __truediv__(self, other):
|
| 59 |
+
return self._div_helper(other)
|
| 60 |
+
|
| 61 |
+
@call_highest_priority('__truediv__')
|
| 62 |
+
def __rtruediv__(self, other):
|
| 63 |
+
return TypeError("Invalid divisor for division")
|
| 64 |
+
|
| 65 |
+
def evalf(self, n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False):
|
| 66 |
+
"""
|
| 67 |
+
Implements the SymPy evalf routine for this quantity.
|
| 68 |
+
|
| 69 |
+
evalf's documentation
|
| 70 |
+
=====================
|
| 71 |
+
|
| 72 |
+
"""
|
| 73 |
+
options = {'subs':subs, 'maxn':maxn, 'chop':chop, 'strict':strict,
|
| 74 |
+
'quad':quad, 'verbose':verbose}
|
| 75 |
+
vec = self.zero
|
| 76 |
+
for k, v in self.components.items():
|
| 77 |
+
vec += v.evalf(n, **options) * k
|
| 78 |
+
return vec
|
| 79 |
+
|
| 80 |
+
evalf.__doc__ += Expr.evalf.__doc__ # type: ignore
|
| 81 |
+
|
| 82 |
+
n = evalf
|
| 83 |
+
|
| 84 |
+
def simplify(self, **kwargs):
|
| 85 |
+
"""
|
| 86 |
+
Implements the SymPy simplify routine for this quantity.
|
| 87 |
+
|
| 88 |
+
simplify's documentation
|
| 89 |
+
========================
|
| 90 |
+
|
| 91 |
+
"""
|
| 92 |
+
simp_components = [simp(v, **kwargs) * k for
|
| 93 |
+
k, v in self.components.items()]
|
| 94 |
+
return self._add_func(*simp_components)
|
| 95 |
+
|
| 96 |
+
simplify.__doc__ += simp.__doc__ # type: ignore
|
| 97 |
+
|
| 98 |
+
def trigsimp(self, **opts):
|
| 99 |
+
"""
|
| 100 |
+
Implements the SymPy trigsimp routine, for this quantity.
|
| 101 |
+
|
| 102 |
+
trigsimp's documentation
|
| 103 |
+
========================
|
| 104 |
+
|
| 105 |
+
"""
|
| 106 |
+
trig_components = [tsimp(v, **opts) * k for
|
| 107 |
+
k, v in self.components.items()]
|
| 108 |
+
return self._add_func(*trig_components)
|
| 109 |
+
|
| 110 |
+
trigsimp.__doc__ += tsimp.__doc__ # type: ignore
|
| 111 |
+
|
| 112 |
+
def _eval_simplify(self, **kwargs):
|
| 113 |
+
return self.simplify(**kwargs)
|
| 114 |
+
|
| 115 |
+
def _eval_trigsimp(self, **opts):
|
| 116 |
+
return self.trigsimp(**opts)
|
| 117 |
+
|
| 118 |
+
def _eval_derivative(self, wrt):
|
| 119 |
+
return self.diff(wrt)
|
| 120 |
+
|
| 121 |
+
def _eval_Integral(self, *symbols, **assumptions):
|
| 122 |
+
integral_components = [Integral(v, *symbols, **assumptions) * k
|
| 123 |
+
for k, v in self.components.items()]
|
| 124 |
+
return self._add_func(*integral_components)
|
| 125 |
+
|
| 126 |
+
def as_numer_denom(self):
|
| 127 |
+
"""
|
| 128 |
+
Returns the expression as a tuple wrt the following
|
| 129 |
+
transformation -
|
| 130 |
+
|
| 131 |
+
expression -> a/b -> a, b
|
| 132 |
+
|
| 133 |
+
"""
|
| 134 |
+
return self, S.One
|
| 135 |
+
|
| 136 |
+
def factor(self, *args, **kwargs):
|
| 137 |
+
"""
|
| 138 |
+
Implements the SymPy factor routine, on the scalar parts
|
| 139 |
+
of a basis-dependent expression.
|
| 140 |
+
|
| 141 |
+
factor's documentation
|
| 142 |
+
========================
|
| 143 |
+
|
| 144 |
+
"""
|
| 145 |
+
fctr_components = [fctr(v, *args, **kwargs) * k for
|
| 146 |
+
k, v in self.components.items()]
|
| 147 |
+
return self._add_func(*fctr_components)
|
| 148 |
+
|
| 149 |
+
factor.__doc__ += fctr.__doc__ # type: ignore
|
| 150 |
+
|
| 151 |
+
def as_coeff_Mul(self, rational=False):
|
| 152 |
+
"""Efficiently extract the coefficient of a product."""
|
| 153 |
+
return (S.One, self)
|
| 154 |
+
|
| 155 |
+
def as_coeff_add(self, *deps):
|
| 156 |
+
"""Efficiently extract the coefficient of a summation."""
|
| 157 |
+
l = [x * self.components[x] for x in self.components]
|
| 158 |
+
return 0, tuple(l)
|
| 159 |
+
|
| 160 |
+
def diff(self, *args, **kwargs):
|
| 161 |
+
"""
|
| 162 |
+
Implements the SymPy diff routine, for vectors.
|
| 163 |
+
|
| 164 |
+
diff's documentation
|
| 165 |
+
========================
|
| 166 |
+
|
| 167 |
+
"""
|
| 168 |
+
for x in args:
|
| 169 |
+
if isinstance(x, BasisDependent):
|
| 170 |
+
raise TypeError("Invalid arg for differentiation")
|
| 171 |
+
diff_components = [df(v, *args, **kwargs) * k for
|
| 172 |
+
k, v in self.components.items()]
|
| 173 |
+
return self._add_func(*diff_components)
|
| 174 |
+
|
| 175 |
+
diff.__doc__ += df.__doc__ # type: ignore
|
| 176 |
+
|
| 177 |
+
def doit(self, **hints):
|
| 178 |
+
"""Calls .doit() on each term in the Dyadic"""
|
| 179 |
+
doit_components = [self.components[x].doit(**hints) * x
|
| 180 |
+
for x in self.components]
|
| 181 |
+
return self._add_func(*doit_components)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
class BasisDependentAdd(BasisDependent, Add):
|
| 185 |
+
"""
|
| 186 |
+
Denotes sum of basis dependent quantities such that they cannot
|
| 187 |
+
be expressed as base or Mul instances.
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
def __new__(cls, *args, **options):
|
| 191 |
+
components = {}
|
| 192 |
+
|
| 193 |
+
# Check each arg and simultaneously learn the components
|
| 194 |
+
for arg in args:
|
| 195 |
+
if not isinstance(arg, cls._expr_type):
|
| 196 |
+
if isinstance(arg, Mul):
|
| 197 |
+
arg = cls._mul_func(*(arg.args))
|
| 198 |
+
elif isinstance(arg, Add):
|
| 199 |
+
arg = cls._add_func(*(arg.args))
|
| 200 |
+
else:
|
| 201 |
+
raise TypeError(str(arg) +
|
| 202 |
+
" cannot be interpreted correctly")
|
| 203 |
+
# If argument is zero, ignore
|
| 204 |
+
if arg == cls.zero:
|
| 205 |
+
continue
|
| 206 |
+
# Else, update components accordingly
|
| 207 |
+
if hasattr(arg, "components"):
|
| 208 |
+
for x in arg.components:
|
| 209 |
+
components[x] = components.get(x, 0) + arg.components[x]
|
| 210 |
+
|
| 211 |
+
temp = list(components.keys())
|
| 212 |
+
for x in temp:
|
| 213 |
+
if components[x] == 0:
|
| 214 |
+
del components[x]
|
| 215 |
+
|
| 216 |
+
# Handle case of zero vector
|
| 217 |
+
if len(components) == 0:
|
| 218 |
+
return cls.zero
|
| 219 |
+
|
| 220 |
+
# Build object
|
| 221 |
+
newargs = [x * components[x] for x in components]
|
| 222 |
+
obj = super().__new__(cls, *newargs, **options)
|
| 223 |
+
if isinstance(obj, Mul):
|
| 224 |
+
return cls._mul_func(*obj.args)
|
| 225 |
+
assumptions = {'commutative': True}
|
| 226 |
+
obj._assumptions = StdFactKB(assumptions)
|
| 227 |
+
obj._components = components
|
| 228 |
+
obj._sys = (list(components.keys()))[0]._sys
|
| 229 |
+
|
| 230 |
+
return obj
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
class BasisDependentMul(BasisDependent, Mul):
|
| 234 |
+
"""
|
| 235 |
+
Denotes product of base- basis dependent quantity with a scalar.
|
| 236 |
+
"""
|
| 237 |
+
|
| 238 |
+
def __new__(cls, *args, **options):
|
| 239 |
+
from sympy.vector import Cross, Dot, Curl, Gradient
|
| 240 |
+
count = 0
|
| 241 |
+
measure_number = S.One
|
| 242 |
+
zeroflag = False
|
| 243 |
+
extra_args = []
|
| 244 |
+
|
| 245 |
+
# Determine the component and check arguments
|
| 246 |
+
# Also keep a count to ensure two vectors aren't
|
| 247 |
+
# being multiplied
|
| 248 |
+
for arg in args:
|
| 249 |
+
if isinstance(arg, cls._zero_func):
|
| 250 |
+
count += 1
|
| 251 |
+
zeroflag = True
|
| 252 |
+
elif arg == S.Zero:
|
| 253 |
+
zeroflag = True
|
| 254 |
+
elif isinstance(arg, (cls._base_func, cls._mul_func)):
|
| 255 |
+
count += 1
|
| 256 |
+
expr = arg._base_instance
|
| 257 |
+
measure_number *= arg._measure_number
|
| 258 |
+
elif isinstance(arg, cls._add_func):
|
| 259 |
+
count += 1
|
| 260 |
+
expr = arg
|
| 261 |
+
elif isinstance(arg, (Cross, Dot, Curl, Gradient)):
|
| 262 |
+
extra_args.append(arg)
|
| 263 |
+
else:
|
| 264 |
+
measure_number *= arg
|
| 265 |
+
# Make sure incompatible types weren't multiplied
|
| 266 |
+
if count > 1:
|
| 267 |
+
raise ValueError("Invalid multiplication")
|
| 268 |
+
elif count == 0:
|
| 269 |
+
return Mul(*args, **options)
|
| 270 |
+
# Handle zero vector case
|
| 271 |
+
if zeroflag:
|
| 272 |
+
return cls.zero
|
| 273 |
+
|
| 274 |
+
# If one of the args was a VectorAdd, return an
|
| 275 |
+
# appropriate VectorAdd instance
|
| 276 |
+
if isinstance(expr, cls._add_func):
|
| 277 |
+
newargs = [cls._mul_func(measure_number, x) for
|
| 278 |
+
x in expr.args]
|
| 279 |
+
return cls._add_func(*newargs)
|
| 280 |
+
|
| 281 |
+
obj = super().__new__(cls, measure_number,
|
| 282 |
+
expr._base_instance,
|
| 283 |
+
*extra_args,
|
| 284 |
+
**options)
|
| 285 |
+
if isinstance(obj, Add):
|
| 286 |
+
return cls._add_func(*obj.args)
|
| 287 |
+
obj._base_instance = expr._base_instance
|
| 288 |
+
obj._measure_number = measure_number
|
| 289 |
+
assumptions = {'commutative': True}
|
| 290 |
+
obj._assumptions = StdFactKB(assumptions)
|
| 291 |
+
obj._components = {expr._base_instance: measure_number}
|
| 292 |
+
obj._sys = expr._base_instance._sys
|
| 293 |
+
|
| 294 |
+
return obj
|
| 295 |
+
|
| 296 |
+
def _sympystr(self, printer):
|
| 297 |
+
measure_str = printer._print(self._measure_number)
|
| 298 |
+
if ('(' in measure_str or '-' in measure_str or
|
| 299 |
+
'+' in measure_str):
|
| 300 |
+
measure_str = '(' + measure_str + ')'
|
| 301 |
+
return measure_str + '*' + printer._print(self._base_instance)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class BasisDependentZero(BasisDependent):
|
| 305 |
+
"""
|
| 306 |
+
Class to denote a zero basis dependent instance.
|
| 307 |
+
"""
|
| 308 |
+
components: dict['BaseVector', Expr] = {}
|
| 309 |
+
_latex_form: str
|
| 310 |
+
|
| 311 |
+
def __new__(cls):
|
| 312 |
+
obj = super().__new__(cls)
|
| 313 |
+
# Pre-compute a specific hash value for the zero vector
|
| 314 |
+
# Use the same one always
|
| 315 |
+
obj._hash = (S.Zero, cls).__hash__()
|
| 316 |
+
return obj
|
| 317 |
+
|
| 318 |
+
def __hash__(self):
|
| 319 |
+
return self._hash
|
| 320 |
+
|
| 321 |
+
@call_highest_priority('__req__')
|
| 322 |
+
def __eq__(self, other):
|
| 323 |
+
return isinstance(other, self._zero_func)
|
| 324 |
+
|
| 325 |
+
__req__ = __eq__
|
| 326 |
+
|
| 327 |
+
@call_highest_priority('__radd__')
|
| 328 |
+
def __add__(self, other):
|
| 329 |
+
if isinstance(other, self._expr_type):
|
| 330 |
+
return other
|
| 331 |
+
else:
|
| 332 |
+
raise TypeError("Invalid argument types for addition")
|
| 333 |
+
|
| 334 |
+
@call_highest_priority('__add__')
|
| 335 |
+
def __radd__(self, other):
|
| 336 |
+
if isinstance(other, self._expr_type):
|
| 337 |
+
return other
|
| 338 |
+
else:
|
| 339 |
+
raise TypeError("Invalid argument types for addition")
|
| 340 |
+
|
| 341 |
+
@call_highest_priority('__rsub__')
|
| 342 |
+
def __sub__(self, other):
|
| 343 |
+
if isinstance(other, self._expr_type):
|
| 344 |
+
return -other
|
| 345 |
+
else:
|
| 346 |
+
raise TypeError("Invalid argument types for subtraction")
|
| 347 |
+
|
| 348 |
+
@call_highest_priority('__sub__')
|
| 349 |
+
def __rsub__(self, other):
|
| 350 |
+
if isinstance(other, self._expr_type):
|
| 351 |
+
return other
|
| 352 |
+
else:
|
| 353 |
+
raise TypeError("Invalid argument types for subtraction")
|
| 354 |
+
|
| 355 |
+
def __neg__(self):
|
| 356 |
+
return self
|
| 357 |
+
|
| 358 |
+
def normalize(self):
|
| 359 |
+
"""
|
| 360 |
+
Returns the normalized version of this vector.
|
| 361 |
+
"""
|
| 362 |
+
return self
|
| 363 |
+
|
| 364 |
+
def _sympystr(self, printer):
|
| 365 |
+
return '0'
|
mgm/lib/python3.10/site-packages/sympy/v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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Callable
|
| 2 |
+
|
| 3 |
+
from sympy.core.basic import Basic
|
| 4 |
+
from sympy.core.cache import cacheit
|
| 5 |
+
from sympy.core import S, Dummy, Lambda
|
| 6 |
+
from sympy.core.symbol import Str
|
| 7 |
+
from sympy.core.symbol import symbols
|
| 8 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 9 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 10 |
+
from sympy.solvers import solve
|
| 11 |
+
from sympy.vector.scalar import BaseScalar
|
| 12 |
+
from sympy.core.containers import Tuple
|
| 13 |
+
from sympy.core.function import diff
|
| 14 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 15 |
+
from sympy.functions.elementary.trigonometric import (acos, atan2, cos, sin)
|
| 16 |
+
from sympy.matrices.dense import eye
|
| 17 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix
|
| 18 |
+
from sympy.simplify.simplify import simplify
|
| 19 |
+
from sympy.simplify.trigsimp import trigsimp
|
| 20 |
+
import sympy.vector
|
| 21 |
+
from sympy.vector.orienters import (Orienter, AxisOrienter, BodyOrienter,
|
| 22 |
+
SpaceOrienter, QuaternionOrienter)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class CoordSys3D(Basic):
|
| 26 |
+
"""
|
| 27 |
+
Represents a coordinate system in 3-D space.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
def __new__(cls, name, transformation=None, parent=None, location=None,
|
| 31 |
+
rotation_matrix=None, vector_names=None, variable_names=None):
|
| 32 |
+
"""
|
| 33 |
+
The orientation/location parameters are necessary if this system
|
| 34 |
+
is being defined at a certain orientation or location wrt another.
|
| 35 |
+
|
| 36 |
+
Parameters
|
| 37 |
+
==========
|
| 38 |
+
|
| 39 |
+
name : str
|
| 40 |
+
The name of the new CoordSys3D instance.
|
| 41 |
+
|
| 42 |
+
transformation : Lambda, Tuple, str
|
| 43 |
+
Transformation defined by transformation equations or chosen
|
| 44 |
+
from predefined ones.
|
| 45 |
+
|
| 46 |
+
location : Vector
|
| 47 |
+
The position vector of the new system's origin wrt the parent
|
| 48 |
+
instance.
|
| 49 |
+
|
| 50 |
+
rotation_matrix : SymPy ImmutableMatrix
|
| 51 |
+
The rotation matrix of the new coordinate system with respect
|
| 52 |
+
to the parent. In other words, the output of
|
| 53 |
+
new_system.rotation_matrix(parent).
|
| 54 |
+
|
| 55 |
+
parent : CoordSys3D
|
| 56 |
+
The coordinate system wrt which the orientation/location
|
| 57 |
+
(or both) is being defined.
|
| 58 |
+
|
| 59 |
+
vector_names, variable_names : iterable(optional)
|
| 60 |
+
Iterables of 3 strings each, with custom names for base
|
| 61 |
+
vectors and base scalars of the new system respectively.
|
| 62 |
+
Used for simple str printing.
|
| 63 |
+
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
name = str(name)
|
| 67 |
+
Vector = sympy.vector.Vector
|
| 68 |
+
Point = sympy.vector.Point
|
| 69 |
+
|
| 70 |
+
if not isinstance(name, str):
|
| 71 |
+
raise TypeError("name should be a string")
|
| 72 |
+
|
| 73 |
+
if transformation is not None:
|
| 74 |
+
if (location is not None) or (rotation_matrix is not None):
|
| 75 |
+
raise ValueError("specify either `transformation` or "
|
| 76 |
+
"`location`/`rotation_matrix`")
|
| 77 |
+
if isinstance(transformation, (Tuple, tuple, list)):
|
| 78 |
+
if isinstance(transformation[0], MatrixBase):
|
| 79 |
+
rotation_matrix = transformation[0]
|
| 80 |
+
location = transformation[1]
|
| 81 |
+
else:
|
| 82 |
+
transformation = Lambda(transformation[0],
|
| 83 |
+
transformation[1])
|
| 84 |
+
elif isinstance(transformation, Callable):
|
| 85 |
+
x1, x2, x3 = symbols('x1 x2 x3', cls=Dummy)
|
| 86 |
+
transformation = Lambda((x1, x2, x3),
|
| 87 |
+
transformation(x1, x2, x3))
|
| 88 |
+
elif isinstance(transformation, str):
|
| 89 |
+
transformation = Str(transformation)
|
| 90 |
+
elif isinstance(transformation, (Str, Lambda)):
|
| 91 |
+
pass
|
| 92 |
+
else:
|
| 93 |
+
raise TypeError("transformation: "
|
| 94 |
+
"wrong type {}".format(type(transformation)))
|
| 95 |
+
|
| 96 |
+
# If orientation information has been provided, store
|
| 97 |
+
# the rotation matrix accordingly
|
| 98 |
+
if rotation_matrix is None:
|
| 99 |
+
rotation_matrix = ImmutableDenseMatrix(eye(3))
|
| 100 |
+
else:
|
| 101 |
+
if not isinstance(rotation_matrix, MatrixBase):
|
| 102 |
+
raise TypeError("rotation_matrix should be an Immutable" +
|
| 103 |
+
"Matrix instance")
|
| 104 |
+
rotation_matrix = rotation_matrix.as_immutable()
|
| 105 |
+
|
| 106 |
+
# If location information is not given, adjust the default
|
| 107 |
+
# location as Vector.zero
|
| 108 |
+
if parent is not None:
|
| 109 |
+
if not isinstance(parent, CoordSys3D):
|
| 110 |
+
raise TypeError("parent should be a " +
|
| 111 |
+
"CoordSys3D/None")
|
| 112 |
+
if location is None:
|
| 113 |
+
location = Vector.zero
|
| 114 |
+
else:
|
| 115 |
+
if not isinstance(location, Vector):
|
| 116 |
+
raise TypeError("location should be a Vector")
|
| 117 |
+
# Check that location does not contain base
|
| 118 |
+
# scalars
|
| 119 |
+
for x in location.free_symbols:
|
| 120 |
+
if isinstance(x, BaseScalar):
|
| 121 |
+
raise ValueError("location should not contain" +
|
| 122 |
+
" BaseScalars")
|
| 123 |
+
origin = parent.origin.locate_new(name + '.origin',
|
| 124 |
+
location)
|
| 125 |
+
else:
|
| 126 |
+
location = Vector.zero
|
| 127 |
+
origin = Point(name + '.origin')
|
| 128 |
+
|
| 129 |
+
if transformation is None:
|
| 130 |
+
transformation = Tuple(rotation_matrix, location)
|
| 131 |
+
|
| 132 |
+
if isinstance(transformation, Tuple):
|
| 133 |
+
lambda_transformation = CoordSys3D._compose_rotation_and_translation(
|
| 134 |
+
transformation[0],
|
| 135 |
+
transformation[1],
|
| 136 |
+
parent
|
| 137 |
+
)
|
| 138 |
+
r, l = transformation
|
| 139 |
+
l = l._projections
|
| 140 |
+
lambda_lame = CoordSys3D._get_lame_coeff('cartesian')
|
| 141 |
+
lambda_inverse = lambda x, y, z: r.inv()*Matrix(
|
| 142 |
+
[x-l[0], y-l[1], z-l[2]])
|
| 143 |
+
elif isinstance(transformation, Str):
|
| 144 |
+
trname = transformation.name
|
| 145 |
+
lambda_transformation = CoordSys3D._get_transformation_lambdas(trname)
|
| 146 |
+
if parent is not None:
|
| 147 |
+
if parent.lame_coefficients() != (S.One, S.One, S.One):
|
| 148 |
+
raise ValueError('Parent for pre-defined coordinate '
|
| 149 |
+
'system should be Cartesian.')
|
| 150 |
+
lambda_lame = CoordSys3D._get_lame_coeff(trname)
|
| 151 |
+
lambda_inverse = CoordSys3D._set_inv_trans_equations(trname)
|
| 152 |
+
elif isinstance(transformation, Lambda):
|
| 153 |
+
if not CoordSys3D._check_orthogonality(transformation):
|
| 154 |
+
raise ValueError("The transformation equation does not "
|
| 155 |
+
"create orthogonal coordinate system")
|
| 156 |
+
lambda_transformation = transformation
|
| 157 |
+
lambda_lame = CoordSys3D._calculate_lame_coeff(lambda_transformation)
|
| 158 |
+
lambda_inverse = None
|
| 159 |
+
else:
|
| 160 |
+
lambda_transformation = lambda x, y, z: transformation(x, y, z)
|
| 161 |
+
lambda_lame = CoordSys3D._get_lame_coeff(transformation)
|
| 162 |
+
lambda_inverse = None
|
| 163 |
+
|
| 164 |
+
if variable_names is None:
|
| 165 |
+
if isinstance(transformation, Lambda):
|
| 166 |
+
variable_names = ["x1", "x2", "x3"]
|
| 167 |
+
elif isinstance(transformation, Str):
|
| 168 |
+
if transformation.name == 'spherical':
|
| 169 |
+
variable_names = ["r", "theta", "phi"]
|
| 170 |
+
elif transformation.name == 'cylindrical':
|
| 171 |
+
variable_names = ["r", "theta", "z"]
|
| 172 |
+
else:
|
| 173 |
+
variable_names = ["x", "y", "z"]
|
| 174 |
+
else:
|
| 175 |
+
variable_names = ["x", "y", "z"]
|
| 176 |
+
if vector_names is None:
|
| 177 |
+
vector_names = ["i", "j", "k"]
|
| 178 |
+
|
| 179 |
+
# All systems that are defined as 'roots' are unequal, unless
|
| 180 |
+
# they have the same name.
|
| 181 |
+
# Systems defined at same orientation/position wrt the same
|
| 182 |
+
# 'parent' are equal, irrespective of the name.
|
| 183 |
+
# This is true even if the same orientation is provided via
|
| 184 |
+
# different methods like Axis/Body/Space/Quaternion.
|
| 185 |
+
# However, coincident systems may be seen as unequal if
|
| 186 |
+
# positioned/oriented wrt different parents, even though
|
| 187 |
+
# they may actually be 'coincident' wrt the root system.
|
| 188 |
+
if parent is not None:
|
| 189 |
+
obj = super().__new__(
|
| 190 |
+
cls, Str(name), transformation, parent)
|
| 191 |
+
else:
|
| 192 |
+
obj = super().__new__(
|
| 193 |
+
cls, Str(name), transformation)
|
| 194 |
+
obj._name = name
|
| 195 |
+
# Initialize the base vectors
|
| 196 |
+
|
| 197 |
+
_check_strings('vector_names', vector_names)
|
| 198 |
+
vector_names = list(vector_names)
|
| 199 |
+
latex_vects = [(r'\mathbf{\hat{%s}_{%s}}' % (x, name)) for
|
| 200 |
+
x in vector_names]
|
| 201 |
+
pretty_vects = ['%s_%s' % (x, name) for x in vector_names]
|
| 202 |
+
|
| 203 |
+
obj._vector_names = vector_names
|
| 204 |
+
|
| 205 |
+
v1 = BaseVector(0, obj, pretty_vects[0], latex_vects[0])
|
| 206 |
+
v2 = BaseVector(1, obj, pretty_vects[1], latex_vects[1])
|
| 207 |
+
v3 = BaseVector(2, obj, pretty_vects[2], latex_vects[2])
|
| 208 |
+
|
| 209 |
+
obj._base_vectors = (v1, v2, v3)
|
| 210 |
+
|
| 211 |
+
# Initialize the base scalars
|
| 212 |
+
|
| 213 |
+
_check_strings('variable_names', vector_names)
|
| 214 |
+
variable_names = list(variable_names)
|
| 215 |
+
latex_scalars = [(r"\mathbf{{%s}_{%s}}" % (x, name)) for
|
| 216 |
+
x in variable_names]
|
| 217 |
+
pretty_scalars = ['%s_%s' % (x, name) for x in variable_names]
|
| 218 |
+
|
| 219 |
+
obj._variable_names = variable_names
|
| 220 |
+
obj._vector_names = vector_names
|
| 221 |
+
|
| 222 |
+
x1 = BaseScalar(0, obj, pretty_scalars[0], latex_scalars[0])
|
| 223 |
+
x2 = BaseScalar(1, obj, pretty_scalars[1], latex_scalars[1])
|
| 224 |
+
x3 = BaseScalar(2, obj, pretty_scalars[2], latex_scalars[2])
|
| 225 |
+
|
| 226 |
+
obj._base_scalars = (x1, x2, x3)
|
| 227 |
+
|
| 228 |
+
obj._transformation = transformation
|
| 229 |
+
obj._transformation_lambda = lambda_transformation
|
| 230 |
+
obj._lame_coefficients = lambda_lame(x1, x2, x3)
|
| 231 |
+
obj._transformation_from_parent_lambda = lambda_inverse
|
| 232 |
+
|
| 233 |
+
setattr(obj, variable_names[0], x1)
|
| 234 |
+
setattr(obj, variable_names[1], x2)
|
| 235 |
+
setattr(obj, variable_names[2], x3)
|
| 236 |
+
|
| 237 |
+
setattr(obj, vector_names[0], v1)
|
| 238 |
+
setattr(obj, vector_names[1], v2)
|
| 239 |
+
setattr(obj, vector_names[2], v3)
|
| 240 |
+
|
| 241 |
+
# Assign params
|
| 242 |
+
obj._parent = parent
|
| 243 |
+
if obj._parent is not None:
|
| 244 |
+
obj._root = obj._parent._root
|
| 245 |
+
else:
|
| 246 |
+
obj._root = obj
|
| 247 |
+
|
| 248 |
+
obj._parent_rotation_matrix = rotation_matrix
|
| 249 |
+
obj._origin = origin
|
| 250 |
+
|
| 251 |
+
# Return the instance
|
| 252 |
+
return obj
|
| 253 |
+
|
| 254 |
+
def _sympystr(self, printer):
|
| 255 |
+
return self._name
|
| 256 |
+
|
| 257 |
+
def __iter__(self):
|
| 258 |
+
return iter(self.base_vectors())
|
| 259 |
+
|
| 260 |
+
@staticmethod
|
| 261 |
+
def _check_orthogonality(equations):
|
| 262 |
+
"""
|
| 263 |
+
Helper method for _connect_to_cartesian. It checks if
|
| 264 |
+
set of transformation equations create orthogonal curvilinear
|
| 265 |
+
coordinate system
|
| 266 |
+
|
| 267 |
+
Parameters
|
| 268 |
+
==========
|
| 269 |
+
|
| 270 |
+
equations : Lambda
|
| 271 |
+
Lambda of transformation equations
|
| 272 |
+
|
| 273 |
+
"""
|
| 274 |
+
|
| 275 |
+
x1, x2, x3 = symbols("x1, x2, x3", cls=Dummy)
|
| 276 |
+
equations = equations(x1, x2, x3)
|
| 277 |
+
v1 = Matrix([diff(equations[0], x1),
|
| 278 |
+
diff(equations[1], x1), diff(equations[2], x1)])
|
| 279 |
+
|
| 280 |
+
v2 = Matrix([diff(equations[0], x2),
|
| 281 |
+
diff(equations[1], x2), diff(equations[2], x2)])
|
| 282 |
+
|
| 283 |
+
v3 = Matrix([diff(equations[0], x3),
|
| 284 |
+
diff(equations[1], x3), diff(equations[2], x3)])
|
| 285 |
+
|
| 286 |
+
if any(simplify(i[0] + i[1] + i[2]) == 0 for i in (v1, v2, v3)):
|
| 287 |
+
return False
|
| 288 |
+
else:
|
| 289 |
+
if simplify(v1.dot(v2)) == 0 and simplify(v2.dot(v3)) == 0 \
|
| 290 |
+
and simplify(v3.dot(v1)) == 0:
|
| 291 |
+
return True
|
| 292 |
+
else:
|
| 293 |
+
return False
|
| 294 |
+
|
| 295 |
+
@staticmethod
|
| 296 |
+
def _set_inv_trans_equations(curv_coord_name):
|
| 297 |
+
"""
|
| 298 |
+
Store information about inverse transformation equations for
|
| 299 |
+
pre-defined coordinate systems.
|
| 300 |
+
|
| 301 |
+
Parameters
|
| 302 |
+
==========
|
| 303 |
+
|
| 304 |
+
curv_coord_name : str
|
| 305 |
+
Name of coordinate system
|
| 306 |
+
|
| 307 |
+
"""
|
| 308 |
+
if curv_coord_name == 'cartesian':
|
| 309 |
+
return lambda x, y, z: (x, y, z)
|
| 310 |
+
|
| 311 |
+
if curv_coord_name == 'spherical':
|
| 312 |
+
return lambda x, y, z: (
|
| 313 |
+
sqrt(x**2 + y**2 + z**2),
|
| 314 |
+
acos(z/sqrt(x**2 + y**2 + z**2)),
|
| 315 |
+
atan2(y, x)
|
| 316 |
+
)
|
| 317 |
+
if curv_coord_name == 'cylindrical':
|
| 318 |
+
return lambda x, y, z: (
|
| 319 |
+
sqrt(x**2 + y**2),
|
| 320 |
+
atan2(y, x),
|
| 321 |
+
z
|
| 322 |
+
)
|
| 323 |
+
raise ValueError('Wrong set of parameters.'
|
| 324 |
+
'Type of coordinate system is defined')
|
| 325 |
+
|
| 326 |
+
def _calculate_inv_trans_equations(self):
|
| 327 |
+
"""
|
| 328 |
+
Helper method for set_coordinate_type. It calculates inverse
|
| 329 |
+
transformation equations for given transformations equations.
|
| 330 |
+
|
| 331 |
+
"""
|
| 332 |
+
x1, x2, x3 = symbols("x1, x2, x3", cls=Dummy, reals=True)
|
| 333 |
+
x, y, z = symbols("x, y, z", cls=Dummy)
|
| 334 |
+
|
| 335 |
+
equations = self._transformation(x1, x2, x3)
|
| 336 |
+
|
| 337 |
+
solved = solve([equations[0] - x,
|
| 338 |
+
equations[1] - y,
|
| 339 |
+
equations[2] - z], (x1, x2, x3), dict=True)[0]
|
| 340 |
+
solved = solved[x1], solved[x2], solved[x3]
|
| 341 |
+
self._transformation_from_parent_lambda = \
|
| 342 |
+
lambda x1, x2, x3: tuple(i.subs(list(zip((x, y, z), (x1, x2, x3)))) for i in solved)
|
| 343 |
+
|
| 344 |
+
@staticmethod
|
| 345 |
+
def _get_lame_coeff(curv_coord_name):
|
| 346 |
+
"""
|
| 347 |
+
Store information about Lame coefficients for pre-defined
|
| 348 |
+
coordinate systems.
|
| 349 |
+
|
| 350 |
+
Parameters
|
| 351 |
+
==========
|
| 352 |
+
|
| 353 |
+
curv_coord_name : str
|
| 354 |
+
Name of coordinate system
|
| 355 |
+
|
| 356 |
+
"""
|
| 357 |
+
if isinstance(curv_coord_name, str):
|
| 358 |
+
if curv_coord_name == 'cartesian':
|
| 359 |
+
return lambda x, y, z: (S.One, S.One, S.One)
|
| 360 |
+
if curv_coord_name == 'spherical':
|
| 361 |
+
return lambda r, theta, phi: (S.One, r, r*sin(theta))
|
| 362 |
+
if curv_coord_name == 'cylindrical':
|
| 363 |
+
return lambda r, theta, h: (S.One, r, S.One)
|
| 364 |
+
raise ValueError('Wrong set of parameters.'
|
| 365 |
+
' Type of coordinate system is not defined')
|
| 366 |
+
return CoordSys3D._calculate_lame_coefficients(curv_coord_name)
|
| 367 |
+
|
| 368 |
+
@staticmethod
|
| 369 |
+
def _calculate_lame_coeff(equations):
|
| 370 |
+
"""
|
| 371 |
+
It calculates Lame coefficients
|
| 372 |
+
for given transformations equations.
|
| 373 |
+
|
| 374 |
+
Parameters
|
| 375 |
+
==========
|
| 376 |
+
|
| 377 |
+
equations : Lambda
|
| 378 |
+
Lambda of transformation equations.
|
| 379 |
+
|
| 380 |
+
"""
|
| 381 |
+
return lambda x1, x2, x3: (
|
| 382 |
+
sqrt(diff(equations(x1, x2, x3)[0], x1)**2 +
|
| 383 |
+
diff(equations(x1, x2, x3)[1], x1)**2 +
|
| 384 |
+
diff(equations(x1, x2, x3)[2], x1)**2),
|
| 385 |
+
sqrt(diff(equations(x1, x2, x3)[0], x2)**2 +
|
| 386 |
+
diff(equations(x1, x2, x3)[1], x2)**2 +
|
| 387 |
+
diff(equations(x1, x2, x3)[2], x2)**2),
|
| 388 |
+
sqrt(diff(equations(x1, x2, x3)[0], x3)**2 +
|
| 389 |
+
diff(equations(x1, x2, x3)[1], x3)**2 +
|
| 390 |
+
diff(equations(x1, x2, x3)[2], x3)**2)
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
def _inverse_rotation_matrix(self):
|
| 394 |
+
"""
|
| 395 |
+
Returns inverse rotation matrix.
|
| 396 |
+
"""
|
| 397 |
+
return simplify(self._parent_rotation_matrix**-1)
|
| 398 |
+
|
| 399 |
+
@staticmethod
|
| 400 |
+
def _get_transformation_lambdas(curv_coord_name):
|
| 401 |
+
"""
|
| 402 |
+
Store information about transformation equations for pre-defined
|
| 403 |
+
coordinate systems.
|
| 404 |
+
|
| 405 |
+
Parameters
|
| 406 |
+
==========
|
| 407 |
+
|
| 408 |
+
curv_coord_name : str
|
| 409 |
+
Name of coordinate system
|
| 410 |
+
|
| 411 |
+
"""
|
| 412 |
+
if isinstance(curv_coord_name, str):
|
| 413 |
+
if curv_coord_name == 'cartesian':
|
| 414 |
+
return lambda x, y, z: (x, y, z)
|
| 415 |
+
if curv_coord_name == 'spherical':
|
| 416 |
+
return lambda r, theta, phi: (
|
| 417 |
+
r*sin(theta)*cos(phi),
|
| 418 |
+
r*sin(theta)*sin(phi),
|
| 419 |
+
r*cos(theta)
|
| 420 |
+
)
|
| 421 |
+
if curv_coord_name == 'cylindrical':
|
| 422 |
+
return lambda r, theta, h: (
|
| 423 |
+
r*cos(theta),
|
| 424 |
+
r*sin(theta),
|
| 425 |
+
h
|
| 426 |
+
)
|
| 427 |
+
raise ValueError('Wrong set of parameters.'
|
| 428 |
+
'Type of coordinate system is defined')
|
| 429 |
+
|
| 430 |
+
@classmethod
|
| 431 |
+
def _rotation_trans_equations(cls, matrix, equations):
|
| 432 |
+
"""
|
| 433 |
+
Returns the transformation equations obtained from rotation matrix.
|
| 434 |
+
|
| 435 |
+
Parameters
|
| 436 |
+
==========
|
| 437 |
+
|
| 438 |
+
matrix : Matrix
|
| 439 |
+
Rotation matrix
|
| 440 |
+
|
| 441 |
+
equations : tuple
|
| 442 |
+
Transformation equations
|
| 443 |
+
|
| 444 |
+
"""
|
| 445 |
+
return tuple(matrix * Matrix(equations))
|
| 446 |
+
|
| 447 |
+
@property
|
| 448 |
+
def origin(self):
|
| 449 |
+
return self._origin
|
| 450 |
+
|
| 451 |
+
def base_vectors(self):
|
| 452 |
+
return self._base_vectors
|
| 453 |
+
|
| 454 |
+
def base_scalars(self):
|
| 455 |
+
return self._base_scalars
|
| 456 |
+
|
| 457 |
+
def lame_coefficients(self):
|
| 458 |
+
return self._lame_coefficients
|
| 459 |
+
|
| 460 |
+
def transformation_to_parent(self):
|
| 461 |
+
return self._transformation_lambda(*self.base_scalars())
|
| 462 |
+
|
| 463 |
+
def transformation_from_parent(self):
|
| 464 |
+
if self._parent is None:
|
| 465 |
+
raise ValueError("no parent coordinate system, use "
|
| 466 |
+
"`transformation_from_parent_function()`")
|
| 467 |
+
return self._transformation_from_parent_lambda(
|
| 468 |
+
*self._parent.base_scalars())
|
| 469 |
+
|
| 470 |
+
def transformation_from_parent_function(self):
|
| 471 |
+
return self._transformation_from_parent_lambda
|
| 472 |
+
|
| 473 |
+
def rotation_matrix(self, other):
|
| 474 |
+
"""
|
| 475 |
+
Returns the direction cosine matrix(DCM), also known as the
|
| 476 |
+
'rotation matrix' of this coordinate system with respect to
|
| 477 |
+
another system.
|
| 478 |
+
|
| 479 |
+
If v_a is a vector defined in system 'A' (in matrix format)
|
| 480 |
+
and v_b is the same vector defined in system 'B', then
|
| 481 |
+
v_a = A.rotation_matrix(B) * v_b.
|
| 482 |
+
|
| 483 |
+
A SymPy Matrix is returned.
|
| 484 |
+
|
| 485 |
+
Parameters
|
| 486 |
+
==========
|
| 487 |
+
|
| 488 |
+
other : CoordSys3D
|
| 489 |
+
The system which the DCM is generated to.
|
| 490 |
+
|
| 491 |
+
Examples
|
| 492 |
+
========
|
| 493 |
+
|
| 494 |
+
>>> from sympy.vector import CoordSys3D
|
| 495 |
+
>>> from sympy import symbols
|
| 496 |
+
>>> q1 = symbols('q1')
|
| 497 |
+
>>> N = CoordSys3D('N')
|
| 498 |
+
>>> A = N.orient_new_axis('A', q1, N.i)
|
| 499 |
+
>>> N.rotation_matrix(A)
|
| 500 |
+
Matrix([
|
| 501 |
+
[1, 0, 0],
|
| 502 |
+
[0, cos(q1), -sin(q1)],
|
| 503 |
+
[0, sin(q1), cos(q1)]])
|
| 504 |
+
|
| 505 |
+
"""
|
| 506 |
+
from sympy.vector.functions import _path
|
| 507 |
+
if not isinstance(other, CoordSys3D):
|
| 508 |
+
raise TypeError(str(other) +
|
| 509 |
+
" is not a CoordSys3D")
|
| 510 |
+
# Handle special cases
|
| 511 |
+
if other == self:
|
| 512 |
+
return eye(3)
|
| 513 |
+
elif other == self._parent:
|
| 514 |
+
return self._parent_rotation_matrix
|
| 515 |
+
elif other._parent == self:
|
| 516 |
+
return other._parent_rotation_matrix.T
|
| 517 |
+
# Else, use tree to calculate position
|
| 518 |
+
rootindex, path = _path(self, other)
|
| 519 |
+
result = eye(3)
|
| 520 |
+
i = -1
|
| 521 |
+
for i in range(rootindex):
|
| 522 |
+
result *= path[i]._parent_rotation_matrix
|
| 523 |
+
i += 2
|
| 524 |
+
while i < len(path):
|
| 525 |
+
result *= path[i]._parent_rotation_matrix.T
|
| 526 |
+
i += 1
|
| 527 |
+
return result
|
| 528 |
+
|
| 529 |
+
@cacheit
|
| 530 |
+
def position_wrt(self, other):
|
| 531 |
+
"""
|
| 532 |
+
Returns the position vector of the origin of this coordinate
|
| 533 |
+
system with respect to another Point/CoordSys3D.
|
| 534 |
+
|
| 535 |
+
Parameters
|
| 536 |
+
==========
|
| 537 |
+
|
| 538 |
+
other : Point/CoordSys3D
|
| 539 |
+
If other is a Point, the position of this system's origin
|
| 540 |
+
wrt it is returned. If its an instance of CoordSyRect,
|
| 541 |
+
the position wrt its origin is returned.
|
| 542 |
+
|
| 543 |
+
Examples
|
| 544 |
+
========
|
| 545 |
+
|
| 546 |
+
>>> from sympy.vector import CoordSys3D
|
| 547 |
+
>>> N = CoordSys3D('N')
|
| 548 |
+
>>> N1 = N.locate_new('N1', 10 * N.i)
|
| 549 |
+
>>> N.position_wrt(N1)
|
| 550 |
+
(-10)*N.i
|
| 551 |
+
|
| 552 |
+
"""
|
| 553 |
+
return self.origin.position_wrt(other)
|
| 554 |
+
|
| 555 |
+
def scalar_map(self, other):
|
| 556 |
+
"""
|
| 557 |
+
Returns a dictionary which expresses the coordinate variables
|
| 558 |
+
(base scalars) of this frame in terms of the variables of
|
| 559 |
+
otherframe.
|
| 560 |
+
|
| 561 |
+
Parameters
|
| 562 |
+
==========
|
| 563 |
+
|
| 564 |
+
otherframe : CoordSys3D
|
| 565 |
+
The other system to map the variables to.
|
| 566 |
+
|
| 567 |
+
Examples
|
| 568 |
+
========
|
| 569 |
+
|
| 570 |
+
>>> from sympy.vector import CoordSys3D
|
| 571 |
+
>>> from sympy import Symbol
|
| 572 |
+
>>> A = CoordSys3D('A')
|
| 573 |
+
>>> q = Symbol('q')
|
| 574 |
+
>>> B = A.orient_new_axis('B', q, A.k)
|
| 575 |
+
>>> A.scalar_map(B)
|
| 576 |
+
{A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}
|
| 577 |
+
|
| 578 |
+
"""
|
| 579 |
+
|
| 580 |
+
origin_coords = tuple(self.position_wrt(other).to_matrix(other))
|
| 581 |
+
relocated_scalars = [x - origin_coords[i]
|
| 582 |
+
for i, x in enumerate(other.base_scalars())]
|
| 583 |
+
|
| 584 |
+
vars_matrix = (self.rotation_matrix(other) *
|
| 585 |
+
Matrix(relocated_scalars))
|
| 586 |
+
return {x: trigsimp(vars_matrix[i])
|
| 587 |
+
for i, x in enumerate(self.base_scalars())}
|
| 588 |
+
|
| 589 |
+
def locate_new(self, name, position, vector_names=None,
|
| 590 |
+
variable_names=None):
|
| 591 |
+
"""
|
| 592 |
+
Returns a CoordSys3D with its origin located at the given
|
| 593 |
+
position wrt this coordinate system's origin.
|
| 594 |
+
|
| 595 |
+
Parameters
|
| 596 |
+
==========
|
| 597 |
+
|
| 598 |
+
name : str
|
| 599 |
+
The name of the new CoordSys3D instance.
|
| 600 |
+
|
| 601 |
+
position : Vector
|
| 602 |
+
The position vector of the new system's origin wrt this
|
| 603 |
+
one.
|
| 604 |
+
|
| 605 |
+
vector_names, variable_names : iterable(optional)
|
| 606 |
+
Iterables of 3 strings each, with custom names for base
|
| 607 |
+
vectors and base scalars of the new system respectively.
|
| 608 |
+
Used for simple str printing.
|
| 609 |
+
|
| 610 |
+
Examples
|
| 611 |
+
========
|
| 612 |
+
|
| 613 |
+
>>> from sympy.vector import CoordSys3D
|
| 614 |
+
>>> A = CoordSys3D('A')
|
| 615 |
+
>>> B = A.locate_new('B', 10 * A.i)
|
| 616 |
+
>>> B.origin.position_wrt(A.origin)
|
| 617 |
+
10*A.i
|
| 618 |
+
|
| 619 |
+
"""
|
| 620 |
+
if variable_names is None:
|
| 621 |
+
variable_names = self._variable_names
|
| 622 |
+
if vector_names is None:
|
| 623 |
+
vector_names = self._vector_names
|
| 624 |
+
|
| 625 |
+
return CoordSys3D(name, location=position,
|
| 626 |
+
vector_names=vector_names,
|
| 627 |
+
variable_names=variable_names,
|
| 628 |
+
parent=self)
|
| 629 |
+
|
| 630 |
+
def orient_new(self, name, orienters, location=None,
|
| 631 |
+
vector_names=None, variable_names=None):
|
| 632 |
+
"""
|
| 633 |
+
Creates a new CoordSys3D oriented in the user-specified way
|
| 634 |
+
with respect to this system.
|
| 635 |
+
|
| 636 |
+
Please refer to the documentation of the orienter classes
|
| 637 |
+
for more information about the orientation procedure.
|
| 638 |
+
|
| 639 |
+
Parameters
|
| 640 |
+
==========
|
| 641 |
+
|
| 642 |
+
name : str
|
| 643 |
+
The name of the new CoordSys3D instance.
|
| 644 |
+
|
| 645 |
+
orienters : iterable/Orienter
|
| 646 |
+
An Orienter or an iterable of Orienters for orienting the
|
| 647 |
+
new coordinate system.
|
| 648 |
+
If an Orienter is provided, it is applied to get the new
|
| 649 |
+
system.
|
| 650 |
+
If an iterable is provided, the orienters will be applied
|
| 651 |
+
in the order in which they appear in the iterable.
|
| 652 |
+
|
| 653 |
+
location : Vector(optional)
|
| 654 |
+
The location of the new coordinate system's origin wrt this
|
| 655 |
+
system's origin. If not specified, the origins are taken to
|
| 656 |
+
be coincident.
|
| 657 |
+
|
| 658 |
+
vector_names, variable_names : iterable(optional)
|
| 659 |
+
Iterables of 3 strings each, with custom names for base
|
| 660 |
+
vectors and base scalars of the new system respectively.
|
| 661 |
+
Used for simple str printing.
|
| 662 |
+
|
| 663 |
+
Examples
|
| 664 |
+
========
|
| 665 |
+
|
| 666 |
+
>>> from sympy.vector import CoordSys3D
|
| 667 |
+
>>> from sympy import symbols
|
| 668 |
+
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 669 |
+
>>> N = CoordSys3D('N')
|
| 670 |
+
|
| 671 |
+
Using an AxisOrienter
|
| 672 |
+
|
| 673 |
+
>>> from sympy.vector import AxisOrienter
|
| 674 |
+
>>> axis_orienter = AxisOrienter(q1, N.i + 2 * N.j)
|
| 675 |
+
>>> A = N.orient_new('A', (axis_orienter, ))
|
| 676 |
+
|
| 677 |
+
Using a BodyOrienter
|
| 678 |
+
|
| 679 |
+
>>> from sympy.vector import BodyOrienter
|
| 680 |
+
>>> body_orienter = BodyOrienter(q1, q2, q3, '123')
|
| 681 |
+
>>> B = N.orient_new('B', (body_orienter, ))
|
| 682 |
+
|
| 683 |
+
Using a SpaceOrienter
|
| 684 |
+
|
| 685 |
+
>>> from sympy.vector import SpaceOrienter
|
| 686 |
+
>>> space_orienter = SpaceOrienter(q1, q2, q3, '312')
|
| 687 |
+
>>> C = N.orient_new('C', (space_orienter, ))
|
| 688 |
+
|
| 689 |
+
Using a QuaternionOrienter
|
| 690 |
+
|
| 691 |
+
>>> from sympy.vector import QuaternionOrienter
|
| 692 |
+
>>> q_orienter = QuaternionOrienter(q0, q1, q2, q3)
|
| 693 |
+
>>> D = N.orient_new('D', (q_orienter, ))
|
| 694 |
+
"""
|
| 695 |
+
if variable_names is None:
|
| 696 |
+
variable_names = self._variable_names
|
| 697 |
+
if vector_names is None:
|
| 698 |
+
vector_names = self._vector_names
|
| 699 |
+
|
| 700 |
+
if isinstance(orienters, Orienter):
|
| 701 |
+
if isinstance(orienters, AxisOrienter):
|
| 702 |
+
final_matrix = orienters.rotation_matrix(self)
|
| 703 |
+
else:
|
| 704 |
+
final_matrix = orienters.rotation_matrix()
|
| 705 |
+
# TODO: trigsimp is needed here so that the matrix becomes
|
| 706 |
+
# canonical (scalar_map also calls trigsimp; without this, you can
|
| 707 |
+
# end up with the same CoordinateSystem that compares differently
|
| 708 |
+
# due to a differently formatted matrix). However, this is
|
| 709 |
+
# probably not so good for performance.
|
| 710 |
+
final_matrix = trigsimp(final_matrix)
|
| 711 |
+
else:
|
| 712 |
+
final_matrix = Matrix(eye(3))
|
| 713 |
+
for orienter in orienters:
|
| 714 |
+
if isinstance(orienter, AxisOrienter):
|
| 715 |
+
final_matrix *= orienter.rotation_matrix(self)
|
| 716 |
+
else:
|
| 717 |
+
final_matrix *= orienter.rotation_matrix()
|
| 718 |
+
|
| 719 |
+
return CoordSys3D(name, rotation_matrix=final_matrix,
|
| 720 |
+
vector_names=vector_names,
|
| 721 |
+
variable_names=variable_names,
|
| 722 |
+
location=location,
|
| 723 |
+
parent=self)
|
| 724 |
+
|
| 725 |
+
def orient_new_axis(self, name, angle, axis, location=None,
|
| 726 |
+
vector_names=None, variable_names=None):
|
| 727 |
+
"""
|
| 728 |
+
Axis rotation is a rotation about an arbitrary axis by
|
| 729 |
+
some angle. The angle is supplied as a SymPy expr scalar, and
|
| 730 |
+
the axis is supplied as a Vector.
|
| 731 |
+
|
| 732 |
+
Parameters
|
| 733 |
+
==========
|
| 734 |
+
|
| 735 |
+
name : string
|
| 736 |
+
The name of the new coordinate system
|
| 737 |
+
|
| 738 |
+
angle : Expr
|
| 739 |
+
The angle by which the new system is to be rotated
|
| 740 |
+
|
| 741 |
+
axis : Vector
|
| 742 |
+
The axis around which the rotation has to be performed
|
| 743 |
+
|
| 744 |
+
location : Vector(optional)
|
| 745 |
+
The location of the new coordinate system's origin wrt this
|
| 746 |
+
system's origin. If not specified, the origins are taken to
|
| 747 |
+
be coincident.
|
| 748 |
+
|
| 749 |
+
vector_names, variable_names : iterable(optional)
|
| 750 |
+
Iterables of 3 strings each, with custom names for base
|
| 751 |
+
vectors and base scalars of the new system respectively.
|
| 752 |
+
Used for simple str printing.
|
| 753 |
+
|
| 754 |
+
Examples
|
| 755 |
+
========
|
| 756 |
+
|
| 757 |
+
>>> from sympy.vector import CoordSys3D
|
| 758 |
+
>>> from sympy import symbols
|
| 759 |
+
>>> q1 = symbols('q1')
|
| 760 |
+
>>> N = CoordSys3D('N')
|
| 761 |
+
>>> B = N.orient_new_axis('B', q1, N.i + 2 * N.j)
|
| 762 |
+
|
| 763 |
+
"""
|
| 764 |
+
if variable_names is None:
|
| 765 |
+
variable_names = self._variable_names
|
| 766 |
+
if vector_names is None:
|
| 767 |
+
vector_names = self._vector_names
|
| 768 |
+
|
| 769 |
+
orienter = AxisOrienter(angle, axis)
|
| 770 |
+
return self.orient_new(name, orienter,
|
| 771 |
+
location=location,
|
| 772 |
+
vector_names=vector_names,
|
| 773 |
+
variable_names=variable_names)
|
| 774 |
+
|
| 775 |
+
def orient_new_body(self, name, angle1, angle2, angle3,
|
| 776 |
+
rotation_order, location=None,
|
| 777 |
+
vector_names=None, variable_names=None):
|
| 778 |
+
"""
|
| 779 |
+
Body orientation takes this coordinate system through three
|
| 780 |
+
successive simple rotations.
|
| 781 |
+
|
| 782 |
+
Body fixed rotations include both Euler Angles and
|
| 783 |
+
Tait-Bryan Angles, see https://en.wikipedia.org/wiki/Euler_angles.
|
| 784 |
+
|
| 785 |
+
Parameters
|
| 786 |
+
==========
|
| 787 |
+
|
| 788 |
+
name : string
|
| 789 |
+
The name of the new coordinate system
|
| 790 |
+
|
| 791 |
+
angle1, angle2, angle3 : Expr
|
| 792 |
+
Three successive angles to rotate the coordinate system by
|
| 793 |
+
|
| 794 |
+
rotation_order : string
|
| 795 |
+
String defining the order of axes for rotation
|
| 796 |
+
|
| 797 |
+
location : Vector(optional)
|
| 798 |
+
The location of the new coordinate system's origin wrt this
|
| 799 |
+
system's origin. If not specified, the origins are taken to
|
| 800 |
+
be coincident.
|
| 801 |
+
|
| 802 |
+
vector_names, variable_names : iterable(optional)
|
| 803 |
+
Iterables of 3 strings each, with custom names for base
|
| 804 |
+
vectors and base scalars of the new system respectively.
|
| 805 |
+
Used for simple str printing.
|
| 806 |
+
|
| 807 |
+
Examples
|
| 808 |
+
========
|
| 809 |
+
|
| 810 |
+
>>> from sympy.vector import CoordSys3D
|
| 811 |
+
>>> from sympy import symbols
|
| 812 |
+
>>> q1, q2, q3 = symbols('q1 q2 q3')
|
| 813 |
+
>>> N = CoordSys3D('N')
|
| 814 |
+
|
| 815 |
+
A 'Body' fixed rotation is described by three angles and
|
| 816 |
+
three body-fixed rotation axes. To orient a coordinate system D
|
| 817 |
+
with respect to N, each sequential rotation is always about
|
| 818 |
+
the orthogonal unit vectors fixed to D. For example, a '123'
|
| 819 |
+
rotation will specify rotations about N.i, then D.j, then
|
| 820 |
+
D.k. (Initially, D.i is same as N.i)
|
| 821 |
+
Therefore,
|
| 822 |
+
|
| 823 |
+
>>> D = N.orient_new_body('D', q1, q2, q3, '123')
|
| 824 |
+
|
| 825 |
+
is same as
|
| 826 |
+
|
| 827 |
+
>>> D = N.orient_new_axis('D', q1, N.i)
|
| 828 |
+
>>> D = D.orient_new_axis('D', q2, D.j)
|
| 829 |
+
>>> D = D.orient_new_axis('D', q3, D.k)
|
| 830 |
+
|
| 831 |
+
Acceptable rotation orders are of length 3, expressed in XYZ or
|
| 832 |
+
123, and cannot have a rotation about about an axis twice in a row.
|
| 833 |
+
|
| 834 |
+
>>> B = N.orient_new_body('B', q1, q2, q3, '123')
|
| 835 |
+
>>> B = N.orient_new_body('B', q1, q2, 0, 'ZXZ')
|
| 836 |
+
>>> B = N.orient_new_body('B', 0, 0, 0, 'XYX')
|
| 837 |
+
|
| 838 |
+
"""
|
| 839 |
+
|
| 840 |
+
orienter = BodyOrienter(angle1, angle2, angle3, rotation_order)
|
| 841 |
+
return self.orient_new(name, orienter,
|
| 842 |
+
location=location,
|
| 843 |
+
vector_names=vector_names,
|
| 844 |
+
variable_names=variable_names)
|
| 845 |
+
|
| 846 |
+
def orient_new_space(self, name, angle1, angle2, angle3,
|
| 847 |
+
rotation_order, location=None,
|
| 848 |
+
vector_names=None, variable_names=None):
|
| 849 |
+
"""
|
| 850 |
+
Space rotation is similar to Body rotation, but the rotations
|
| 851 |
+
are applied in the opposite order.
|
| 852 |
+
|
| 853 |
+
Parameters
|
| 854 |
+
==========
|
| 855 |
+
|
| 856 |
+
name : string
|
| 857 |
+
The name of the new coordinate system
|
| 858 |
+
|
| 859 |
+
angle1, angle2, angle3 : Expr
|
| 860 |
+
Three successive angles to rotate the coordinate system by
|
| 861 |
+
|
| 862 |
+
rotation_order : string
|
| 863 |
+
String defining the order of axes for rotation
|
| 864 |
+
|
| 865 |
+
location : Vector(optional)
|
| 866 |
+
The location of the new coordinate system's origin wrt this
|
| 867 |
+
system's origin. If not specified, the origins are taken to
|
| 868 |
+
be coincident.
|
| 869 |
+
|
| 870 |
+
vector_names, variable_names : iterable(optional)
|
| 871 |
+
Iterables of 3 strings each, with custom names for base
|
| 872 |
+
vectors and base scalars of the new system respectively.
|
| 873 |
+
Used for simple str printing.
|
| 874 |
+
|
| 875 |
+
See Also
|
| 876 |
+
========
|
| 877 |
+
|
| 878 |
+
CoordSys3D.orient_new_body : method to orient via Euler
|
| 879 |
+
angles
|
| 880 |
+
|
| 881 |
+
Examples
|
| 882 |
+
========
|
| 883 |
+
|
| 884 |
+
>>> from sympy.vector import CoordSys3D
|
| 885 |
+
>>> from sympy import symbols
|
| 886 |
+
>>> q1, q2, q3 = symbols('q1 q2 q3')
|
| 887 |
+
>>> N = CoordSys3D('N')
|
| 888 |
+
|
| 889 |
+
To orient a coordinate system D with respect to N, each
|
| 890 |
+
sequential rotation is always about N's orthogonal unit vectors.
|
| 891 |
+
For example, a '123' rotation will specify rotations about
|
| 892 |
+
N.i, then N.j, then N.k.
|
| 893 |
+
Therefore,
|
| 894 |
+
|
| 895 |
+
>>> D = N.orient_new_space('D', q1, q2, q3, '312')
|
| 896 |
+
|
| 897 |
+
is same as
|
| 898 |
+
|
| 899 |
+
>>> B = N.orient_new_axis('B', q1, N.i)
|
| 900 |
+
>>> C = B.orient_new_axis('C', q2, N.j)
|
| 901 |
+
>>> D = C.orient_new_axis('D', q3, N.k)
|
| 902 |
+
|
| 903 |
+
"""
|
| 904 |
+
|
| 905 |
+
orienter = SpaceOrienter(angle1, angle2, angle3, rotation_order)
|
| 906 |
+
return self.orient_new(name, orienter,
|
| 907 |
+
location=location,
|
| 908 |
+
vector_names=vector_names,
|
| 909 |
+
variable_names=variable_names)
|
| 910 |
+
|
| 911 |
+
def orient_new_quaternion(self, name, q0, q1, q2, q3, location=None,
|
| 912 |
+
vector_names=None, variable_names=None):
|
| 913 |
+
"""
|
| 914 |
+
Quaternion orientation orients the new CoordSys3D with
|
| 915 |
+
Quaternions, defined as a finite rotation about lambda, a unit
|
| 916 |
+
vector, by some amount theta.
|
| 917 |
+
|
| 918 |
+
This orientation is described by four parameters:
|
| 919 |
+
|
| 920 |
+
q0 = cos(theta/2)
|
| 921 |
+
|
| 922 |
+
q1 = lambda_x sin(theta/2)
|
| 923 |
+
|
| 924 |
+
q2 = lambda_y sin(theta/2)
|
| 925 |
+
|
| 926 |
+
q3 = lambda_z sin(theta/2)
|
| 927 |
+
|
| 928 |
+
Quaternion does not take in a rotation order.
|
| 929 |
+
|
| 930 |
+
Parameters
|
| 931 |
+
==========
|
| 932 |
+
|
| 933 |
+
name : string
|
| 934 |
+
The name of the new coordinate system
|
| 935 |
+
|
| 936 |
+
q0, q1, q2, q3 : Expr
|
| 937 |
+
The quaternions to rotate the coordinate system by
|
| 938 |
+
|
| 939 |
+
location : Vector(optional)
|
| 940 |
+
The location of the new coordinate system's origin wrt this
|
| 941 |
+
system's origin. If not specified, the origins are taken to
|
| 942 |
+
be coincident.
|
| 943 |
+
|
| 944 |
+
vector_names, variable_names : iterable(optional)
|
| 945 |
+
Iterables of 3 strings each, with custom names for base
|
| 946 |
+
vectors and base scalars of the new system respectively.
|
| 947 |
+
Used for simple str printing.
|
| 948 |
+
|
| 949 |
+
Examples
|
| 950 |
+
========
|
| 951 |
+
|
| 952 |
+
>>> from sympy.vector import CoordSys3D
|
| 953 |
+
>>> from sympy import symbols
|
| 954 |
+
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 955 |
+
>>> N = CoordSys3D('N')
|
| 956 |
+
>>> B = N.orient_new_quaternion('B', q0, q1, q2, q3)
|
| 957 |
+
|
| 958 |
+
"""
|
| 959 |
+
|
| 960 |
+
orienter = QuaternionOrienter(q0, q1, q2, q3)
|
| 961 |
+
return self.orient_new(name, orienter,
|
| 962 |
+
location=location,
|
| 963 |
+
vector_names=vector_names,
|
| 964 |
+
variable_names=variable_names)
|
| 965 |
+
|
| 966 |
+
def create_new(self, name, transformation, variable_names=None, vector_names=None):
|
| 967 |
+
"""
|
| 968 |
+
Returns a CoordSys3D which is connected to self by transformation.
|
| 969 |
+
|
| 970 |
+
Parameters
|
| 971 |
+
==========
|
| 972 |
+
|
| 973 |
+
name : str
|
| 974 |
+
The name of the new CoordSys3D instance.
|
| 975 |
+
|
| 976 |
+
transformation : Lambda, Tuple, str
|
| 977 |
+
Transformation defined by transformation equations or chosen
|
| 978 |
+
from predefined ones.
|
| 979 |
+
|
| 980 |
+
vector_names, variable_names : iterable(optional)
|
| 981 |
+
Iterables of 3 strings each, with custom names for base
|
| 982 |
+
vectors and base scalars of the new system respectively.
|
| 983 |
+
Used for simple str printing.
|
| 984 |
+
|
| 985 |
+
Examples
|
| 986 |
+
========
|
| 987 |
+
|
| 988 |
+
>>> from sympy.vector import CoordSys3D
|
| 989 |
+
>>> a = CoordSys3D('a')
|
| 990 |
+
>>> b = a.create_new('b', transformation='spherical')
|
| 991 |
+
>>> b.transformation_to_parent()
|
| 992 |
+
(b.r*sin(b.theta)*cos(b.phi), b.r*sin(b.phi)*sin(b.theta), b.r*cos(b.theta))
|
| 993 |
+
>>> b.transformation_from_parent()
|
| 994 |
+
(sqrt(a.x**2 + a.y**2 + a.z**2), acos(a.z/sqrt(a.x**2 + a.y**2 + a.z**2)), atan2(a.y, a.x))
|
| 995 |
+
|
| 996 |
+
"""
|
| 997 |
+
return CoordSys3D(name, parent=self, transformation=transformation,
|
| 998 |
+
variable_names=variable_names, vector_names=vector_names)
|
| 999 |
+
|
| 1000 |
+
def __init__(self, name, location=None, rotation_matrix=None,
|
| 1001 |
+
parent=None, vector_names=None, variable_names=None,
|
| 1002 |
+
latex_vects=None, pretty_vects=None, latex_scalars=None,
|
| 1003 |
+
pretty_scalars=None, transformation=None):
|
| 1004 |
+
# Dummy initializer for setting docstring
|
| 1005 |
+
pass
|
| 1006 |
+
|
| 1007 |
+
__init__.__doc__ = __new__.__doc__
|
| 1008 |
+
|
| 1009 |
+
@staticmethod
|
| 1010 |
+
def _compose_rotation_and_translation(rot, translation, parent):
|
| 1011 |
+
r = lambda x, y, z: CoordSys3D._rotation_trans_equations(rot, (x, y, z))
|
| 1012 |
+
if parent is None:
|
| 1013 |
+
return r
|
| 1014 |
+
|
| 1015 |
+
dx, dy, dz = [translation.dot(i) for i in parent.base_vectors()]
|
| 1016 |
+
t = lambda x, y, z: (
|
| 1017 |
+
x + dx,
|
| 1018 |
+
y + dy,
|
| 1019 |
+
z + dz,
|
| 1020 |
+
)
|
| 1021 |
+
return lambda x, y, z: t(*r(x, y, z))
|
| 1022 |
+
|
| 1023 |
+
|
| 1024 |
+
def _check_strings(arg_name, arg):
|
| 1025 |
+
errorstr = arg_name + " must be an iterable of 3 string-types"
|
| 1026 |
+
if len(arg) != 3:
|
| 1027 |
+
raise ValueError(errorstr)
|
| 1028 |
+
for s in arg:
|
| 1029 |
+
if not isinstance(s, str):
|
| 1030 |
+
raise TypeError(errorstr)
|
| 1031 |
+
|
| 1032 |
+
|
| 1033 |
+
# Delayed import to avoid cyclic import problems:
|
| 1034 |
+
from sympy.vector.vector import BaseVector
|
mgm/lib/python3.10/site-packages/sympy/vector/deloperator.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Basic
|
| 2 |
+
from sympy.vector.operators import gradient, divergence, curl
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class Del(Basic):
|
| 6 |
+
"""
|
| 7 |
+
Represents the vector differential operator, usually represented in
|
| 8 |
+
mathematical expressions as the 'nabla' symbol.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __new__(cls):
|
| 12 |
+
obj = super().__new__(cls)
|
| 13 |
+
obj._name = "delop"
|
| 14 |
+
return obj
|
| 15 |
+
|
| 16 |
+
def gradient(self, scalar_field, doit=False):
|
| 17 |
+
"""
|
| 18 |
+
Returns the gradient of the given scalar field, as a
|
| 19 |
+
Vector instance.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
==========
|
| 23 |
+
|
| 24 |
+
scalar_field : SymPy expression
|
| 25 |
+
The scalar field to calculate the gradient of.
|
| 26 |
+
|
| 27 |
+
doit : bool
|
| 28 |
+
If True, the result is returned after calling .doit() on
|
| 29 |
+
each component. Else, the returned expression contains
|
| 30 |
+
Derivative instances
|
| 31 |
+
|
| 32 |
+
Examples
|
| 33 |
+
========
|
| 34 |
+
|
| 35 |
+
>>> from sympy.vector import CoordSys3D, Del
|
| 36 |
+
>>> C = CoordSys3D('C')
|
| 37 |
+
>>> delop = Del()
|
| 38 |
+
>>> delop.gradient(9)
|
| 39 |
+
0
|
| 40 |
+
>>> delop(C.x*C.y*C.z).doit()
|
| 41 |
+
C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k
|
| 42 |
+
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
return gradient(scalar_field, doit=doit)
|
| 46 |
+
|
| 47 |
+
__call__ = gradient
|
| 48 |
+
__call__.__doc__ = gradient.__doc__
|
| 49 |
+
|
| 50 |
+
def dot(self, vect, doit=False):
|
| 51 |
+
"""
|
| 52 |
+
Represents the dot product between this operator and a given
|
| 53 |
+
vector - equal to the divergence of the vector field.
|
| 54 |
+
|
| 55 |
+
Parameters
|
| 56 |
+
==========
|
| 57 |
+
|
| 58 |
+
vect : Vector
|
| 59 |
+
The vector whose divergence is to be calculated.
|
| 60 |
+
|
| 61 |
+
doit : bool
|
| 62 |
+
If True, the result is returned after calling .doit() on
|
| 63 |
+
each component. Else, the returned expression contains
|
| 64 |
+
Derivative instances
|
| 65 |
+
|
| 66 |
+
Examples
|
| 67 |
+
========
|
| 68 |
+
|
| 69 |
+
>>> from sympy.vector import CoordSys3D, Del
|
| 70 |
+
>>> delop = Del()
|
| 71 |
+
>>> C = CoordSys3D('C')
|
| 72 |
+
>>> delop.dot(C.x*C.i)
|
| 73 |
+
Derivative(C.x, C.x)
|
| 74 |
+
>>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
|
| 75 |
+
>>> (delop & v).doit()
|
| 76 |
+
C.x*C.y + C.x*C.z + C.y*C.z
|
| 77 |
+
|
| 78 |
+
"""
|
| 79 |
+
return divergence(vect, doit=doit)
|
| 80 |
+
|
| 81 |
+
__and__ = dot
|
| 82 |
+
__and__.__doc__ = dot.__doc__
|
| 83 |
+
|
| 84 |
+
def cross(self, vect, doit=False):
|
| 85 |
+
"""
|
| 86 |
+
Represents the cross product between this operator and a given
|
| 87 |
+
vector - equal to the curl of the vector field.
|
| 88 |
+
|
| 89 |
+
Parameters
|
| 90 |
+
==========
|
| 91 |
+
|
| 92 |
+
vect : Vector
|
| 93 |
+
The vector whose curl is to be calculated.
|
| 94 |
+
|
| 95 |
+
doit : bool
|
| 96 |
+
If True, the result is returned after calling .doit() on
|
| 97 |
+
each component. Else, the returned expression contains
|
| 98 |
+
Derivative instances
|
| 99 |
+
|
| 100 |
+
Examples
|
| 101 |
+
========
|
| 102 |
+
|
| 103 |
+
>>> from sympy.vector import CoordSys3D, Del
|
| 104 |
+
>>> C = CoordSys3D('C')
|
| 105 |
+
>>> delop = Del()
|
| 106 |
+
>>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
|
| 107 |
+
>>> delop.cross(v, doit = True)
|
| 108 |
+
(-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j +
|
| 109 |
+
(-C.x*C.z + C.y*C.z)*C.k
|
| 110 |
+
>>> (delop ^ C.i).doit()
|
| 111 |
+
0
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
return curl(vect, doit=doit)
|
| 116 |
+
|
| 117 |
+
__xor__ = cross
|
| 118 |
+
__xor__.__doc__ = cross.__doc__
|
| 119 |
+
|
| 120 |
+
def _sympystr(self, printer):
|
| 121 |
+
return self._name
|
mgm/lib/python3.10/site-packages/sympy/vector/dyadic.py
ADDED
|
@@ -0,0 +1,285 @@
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from sympy.vector.basisdependent import (BasisDependent, BasisDependentAdd,
|
| 4 |
+
BasisDependentMul, BasisDependentZero)
|
| 5 |
+
from sympy.core import S, Pow
|
| 6 |
+
from sympy.core.expr import AtomicExpr
|
| 7 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 8 |
+
import sympy.vector
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Dyadic(BasisDependent):
|
| 12 |
+
"""
|
| 13 |
+
Super class for all Dyadic-classes.
|
| 14 |
+
|
| 15 |
+
References
|
| 16 |
+
==========
|
| 17 |
+
|
| 18 |
+
.. [1] https://en.wikipedia.org/wiki/Dyadic_tensor
|
| 19 |
+
.. [2] Kane, T., Levinson, D. Dynamics Theory and Applications. 1985
|
| 20 |
+
McGraw-Hill
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
_op_priority = 13.0
|
| 25 |
+
|
| 26 |
+
_expr_type: type[Dyadic]
|
| 27 |
+
_mul_func: type[Dyadic]
|
| 28 |
+
_add_func: type[Dyadic]
|
| 29 |
+
_zero_func: type[Dyadic]
|
| 30 |
+
_base_func: type[Dyadic]
|
| 31 |
+
zero: DyadicZero
|
| 32 |
+
|
| 33 |
+
@property
|
| 34 |
+
def components(self):
|
| 35 |
+
"""
|
| 36 |
+
Returns the components of this dyadic in the form of a
|
| 37 |
+
Python dictionary mapping BaseDyadic instances to the
|
| 38 |
+
corresponding measure numbers.
|
| 39 |
+
|
| 40 |
+
"""
|
| 41 |
+
# The '_components' attribute is defined according to the
|
| 42 |
+
# subclass of Dyadic the instance belongs to.
|
| 43 |
+
return self._components
|
| 44 |
+
|
| 45 |
+
def dot(self, other):
|
| 46 |
+
"""
|
| 47 |
+
Returns the dot product(also called inner product) of this
|
| 48 |
+
Dyadic, with another Dyadic or Vector.
|
| 49 |
+
If 'other' is a Dyadic, this returns a Dyadic. Else, it returns
|
| 50 |
+
a Vector (unless an error is encountered).
|
| 51 |
+
|
| 52 |
+
Parameters
|
| 53 |
+
==========
|
| 54 |
+
|
| 55 |
+
other : Dyadic/Vector
|
| 56 |
+
The other Dyadic or Vector to take the inner product with
|
| 57 |
+
|
| 58 |
+
Examples
|
| 59 |
+
========
|
| 60 |
+
|
| 61 |
+
>>> from sympy.vector import CoordSys3D
|
| 62 |
+
>>> N = CoordSys3D('N')
|
| 63 |
+
>>> D1 = N.i.outer(N.j)
|
| 64 |
+
>>> D2 = N.j.outer(N.j)
|
| 65 |
+
>>> D1.dot(D2)
|
| 66 |
+
(N.i|N.j)
|
| 67 |
+
>>> D1.dot(N.j)
|
| 68 |
+
N.i
|
| 69 |
+
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
Vector = sympy.vector.Vector
|
| 73 |
+
if isinstance(other, BasisDependentZero):
|
| 74 |
+
return Vector.zero
|
| 75 |
+
elif isinstance(other, Vector):
|
| 76 |
+
outvec = Vector.zero
|
| 77 |
+
for k, v in self.components.items():
|
| 78 |
+
vect_dot = k.args[1].dot(other)
|
| 79 |
+
outvec += vect_dot * v * k.args[0]
|
| 80 |
+
return outvec
|
| 81 |
+
elif isinstance(other, Dyadic):
|
| 82 |
+
outdyad = Dyadic.zero
|
| 83 |
+
for k1, v1 in self.components.items():
|
| 84 |
+
for k2, v2 in other.components.items():
|
| 85 |
+
vect_dot = k1.args[1].dot(k2.args[0])
|
| 86 |
+
outer_product = k1.args[0].outer(k2.args[1])
|
| 87 |
+
outdyad += vect_dot * v1 * v2 * outer_product
|
| 88 |
+
return outdyad
|
| 89 |
+
else:
|
| 90 |
+
raise TypeError("Inner product is not defined for " +
|
| 91 |
+
str(type(other)) + " and Dyadics.")
|
| 92 |
+
|
| 93 |
+
def __and__(self, other):
|
| 94 |
+
return self.dot(other)
|
| 95 |
+
|
| 96 |
+
__and__.__doc__ = dot.__doc__
|
| 97 |
+
|
| 98 |
+
def cross(self, other):
|
| 99 |
+
"""
|
| 100 |
+
Returns the cross product between this Dyadic, and a Vector, as a
|
| 101 |
+
Vector instance.
|
| 102 |
+
|
| 103 |
+
Parameters
|
| 104 |
+
==========
|
| 105 |
+
|
| 106 |
+
other : Vector
|
| 107 |
+
The Vector that we are crossing this Dyadic with
|
| 108 |
+
|
| 109 |
+
Examples
|
| 110 |
+
========
|
| 111 |
+
|
| 112 |
+
>>> from sympy.vector import CoordSys3D
|
| 113 |
+
>>> N = CoordSys3D('N')
|
| 114 |
+
>>> d = N.i.outer(N.i)
|
| 115 |
+
>>> d.cross(N.j)
|
| 116 |
+
(N.i|N.k)
|
| 117 |
+
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
Vector = sympy.vector.Vector
|
| 121 |
+
if other == Vector.zero:
|
| 122 |
+
return Dyadic.zero
|
| 123 |
+
elif isinstance(other, Vector):
|
| 124 |
+
outdyad = Dyadic.zero
|
| 125 |
+
for k, v in self.components.items():
|
| 126 |
+
cross_product = k.args[1].cross(other)
|
| 127 |
+
outer = k.args[0].outer(cross_product)
|
| 128 |
+
outdyad += v * outer
|
| 129 |
+
return outdyad
|
| 130 |
+
else:
|
| 131 |
+
raise TypeError(str(type(other)) + " not supported for " +
|
| 132 |
+
"cross with dyadics")
|
| 133 |
+
|
| 134 |
+
def __xor__(self, other):
|
| 135 |
+
return self.cross(other)
|
| 136 |
+
|
| 137 |
+
__xor__.__doc__ = cross.__doc__
|
| 138 |
+
|
| 139 |
+
def to_matrix(self, system, second_system=None):
|
| 140 |
+
"""
|
| 141 |
+
Returns the matrix form of the dyadic with respect to one or two
|
| 142 |
+
coordinate systems.
|
| 143 |
+
|
| 144 |
+
Parameters
|
| 145 |
+
==========
|
| 146 |
+
|
| 147 |
+
system : CoordSys3D
|
| 148 |
+
The coordinate system that the rows and columns of the matrix
|
| 149 |
+
correspond to. If a second system is provided, this
|
| 150 |
+
only corresponds to the rows of the matrix.
|
| 151 |
+
second_system : CoordSys3D, optional, default=None
|
| 152 |
+
The coordinate system that the columns of the matrix correspond
|
| 153 |
+
to.
|
| 154 |
+
|
| 155 |
+
Examples
|
| 156 |
+
========
|
| 157 |
+
|
| 158 |
+
>>> from sympy.vector import CoordSys3D
|
| 159 |
+
>>> N = CoordSys3D('N')
|
| 160 |
+
>>> v = N.i + 2*N.j
|
| 161 |
+
>>> d = v.outer(N.i)
|
| 162 |
+
>>> d.to_matrix(N)
|
| 163 |
+
Matrix([
|
| 164 |
+
[1, 0, 0],
|
| 165 |
+
[2, 0, 0],
|
| 166 |
+
[0, 0, 0]])
|
| 167 |
+
>>> from sympy import Symbol
|
| 168 |
+
>>> q = Symbol('q')
|
| 169 |
+
>>> P = N.orient_new_axis('P', q, N.k)
|
| 170 |
+
>>> d.to_matrix(N, P)
|
| 171 |
+
Matrix([
|
| 172 |
+
[ cos(q), -sin(q), 0],
|
| 173 |
+
[2*cos(q), -2*sin(q), 0],
|
| 174 |
+
[ 0, 0, 0]])
|
| 175 |
+
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
if second_system is None:
|
| 179 |
+
second_system = system
|
| 180 |
+
|
| 181 |
+
return Matrix([i.dot(self).dot(j) for i in system for j in
|
| 182 |
+
second_system]).reshape(3, 3)
|
| 183 |
+
|
| 184 |
+
def _div_helper(one, other):
|
| 185 |
+
""" Helper for division involving dyadics """
|
| 186 |
+
if isinstance(one, Dyadic) and isinstance(other, Dyadic):
|
| 187 |
+
raise TypeError("Cannot divide two dyadics")
|
| 188 |
+
elif isinstance(one, Dyadic):
|
| 189 |
+
return DyadicMul(one, Pow(other, S.NegativeOne))
|
| 190 |
+
else:
|
| 191 |
+
raise TypeError("Cannot divide by a dyadic")
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class BaseDyadic(Dyadic, AtomicExpr):
|
| 195 |
+
"""
|
| 196 |
+
Class to denote a base dyadic tensor component.
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
+
def __new__(cls, vector1, vector2):
|
| 200 |
+
Vector = sympy.vector.Vector
|
| 201 |
+
BaseVector = sympy.vector.BaseVector
|
| 202 |
+
VectorZero = sympy.vector.VectorZero
|
| 203 |
+
# Verify arguments
|
| 204 |
+
if not isinstance(vector1, (BaseVector, VectorZero)) or \
|
| 205 |
+
not isinstance(vector2, (BaseVector, VectorZero)):
|
| 206 |
+
raise TypeError("BaseDyadic cannot be composed of non-base " +
|
| 207 |
+
"vectors")
|
| 208 |
+
# Handle special case of zero vector
|
| 209 |
+
elif vector1 == Vector.zero or vector2 == Vector.zero:
|
| 210 |
+
return Dyadic.zero
|
| 211 |
+
# Initialize instance
|
| 212 |
+
obj = super().__new__(cls, vector1, vector2)
|
| 213 |
+
obj._base_instance = obj
|
| 214 |
+
obj._measure_number = 1
|
| 215 |
+
obj._components = {obj: S.One}
|
| 216 |
+
obj._sys = vector1._sys
|
| 217 |
+
obj._pretty_form = ('(' + vector1._pretty_form + '|' +
|
| 218 |
+
vector2._pretty_form + ')')
|
| 219 |
+
obj._latex_form = (r'\left(' + vector1._latex_form + r"{\middle|}" +
|
| 220 |
+
vector2._latex_form + r'\right)')
|
| 221 |
+
|
| 222 |
+
return obj
|
| 223 |
+
|
| 224 |
+
def _sympystr(self, printer):
|
| 225 |
+
return "({}|{})".format(
|
| 226 |
+
printer._print(self.args[0]), printer._print(self.args[1]))
|
| 227 |
+
|
| 228 |
+
def _sympyrepr(self, printer):
|
| 229 |
+
return "BaseDyadic({}, {})".format(
|
| 230 |
+
printer._print(self.args[0]), printer._print(self.args[1]))
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
class DyadicMul(BasisDependentMul, Dyadic):
|
| 234 |
+
""" Products of scalars and BaseDyadics """
|
| 235 |
+
|
| 236 |
+
def __new__(cls, *args, **options):
|
| 237 |
+
obj = BasisDependentMul.__new__(cls, *args, **options)
|
| 238 |
+
return obj
|
| 239 |
+
|
| 240 |
+
@property
|
| 241 |
+
def base_dyadic(self):
|
| 242 |
+
""" The BaseDyadic involved in the product. """
|
| 243 |
+
return self._base_instance
|
| 244 |
+
|
| 245 |
+
@property
|
| 246 |
+
def measure_number(self):
|
| 247 |
+
""" The scalar expression involved in the definition of
|
| 248 |
+
this DyadicMul.
|
| 249 |
+
"""
|
| 250 |
+
return self._measure_number
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
class DyadicAdd(BasisDependentAdd, Dyadic):
|
| 254 |
+
""" Class to hold dyadic sums """
|
| 255 |
+
|
| 256 |
+
def __new__(cls, *args, **options):
|
| 257 |
+
obj = BasisDependentAdd.__new__(cls, *args, **options)
|
| 258 |
+
return obj
|
| 259 |
+
|
| 260 |
+
def _sympystr(self, printer):
|
| 261 |
+
items = list(self.components.items())
|
| 262 |
+
items.sort(key=lambda x: x[0].__str__())
|
| 263 |
+
return " + ".join(printer._print(k * v) for k, v in items)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
class DyadicZero(BasisDependentZero, Dyadic):
|
| 267 |
+
"""
|
| 268 |
+
Class to denote a zero dyadic
|
| 269 |
+
"""
|
| 270 |
+
|
| 271 |
+
_op_priority = 13.1
|
| 272 |
+
_pretty_form = '(0|0)'
|
| 273 |
+
_latex_form = r'(\mathbf{\hat{0}}|\mathbf{\hat{0}})'
|
| 274 |
+
|
| 275 |
+
def __new__(cls):
|
| 276 |
+
obj = BasisDependentZero.__new__(cls)
|
| 277 |
+
return obj
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
Dyadic._expr_type = Dyadic
|
| 281 |
+
Dyadic._mul_func = DyadicMul
|
| 282 |
+
Dyadic._add_func = DyadicAdd
|
| 283 |
+
Dyadic._zero_func = DyadicZero
|
| 284 |
+
Dyadic._base_func = BaseDyadic
|
| 285 |
+
Dyadic.zero = DyadicZero()
|
mgm/lib/python3.10/site-packages/sympy/vector/functions.py
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 2 |
+
from sympy.vector.deloperator import Del
|
| 3 |
+
from sympy.vector.scalar import BaseScalar
|
| 4 |
+
from sympy.vector.vector import Vector, BaseVector
|
| 5 |
+
from sympy.vector.operators import gradient, curl, divergence
|
| 6 |
+
from sympy.core.function import diff
|
| 7 |
+
from sympy.core.singleton import S
|
| 8 |
+
from sympy.integrals.integrals import integrate
|
| 9 |
+
from sympy.simplify.simplify import simplify
|
| 10 |
+
from sympy.core import sympify
|
| 11 |
+
from sympy.vector.dyadic import Dyadic
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def express(expr, system, system2=None, variables=False):
|
| 15 |
+
"""
|
| 16 |
+
Global function for 'express' functionality.
|
| 17 |
+
|
| 18 |
+
Re-expresses a Vector, Dyadic or scalar(sympyfiable) in the given
|
| 19 |
+
coordinate system.
|
| 20 |
+
|
| 21 |
+
If 'variables' is True, then the coordinate variables (base scalars)
|
| 22 |
+
of other coordinate systems present in the vector/scalar field or
|
| 23 |
+
dyadic are also substituted in terms of the base scalars of the
|
| 24 |
+
given system.
|
| 25 |
+
|
| 26 |
+
Parameters
|
| 27 |
+
==========
|
| 28 |
+
|
| 29 |
+
expr : Vector/Dyadic/scalar(sympyfiable)
|
| 30 |
+
The expression to re-express in CoordSys3D 'system'
|
| 31 |
+
|
| 32 |
+
system: CoordSys3D
|
| 33 |
+
The coordinate system the expr is to be expressed in
|
| 34 |
+
|
| 35 |
+
system2: CoordSys3D
|
| 36 |
+
The other coordinate system required for re-expression
|
| 37 |
+
(only for a Dyadic Expr)
|
| 38 |
+
|
| 39 |
+
variables : boolean
|
| 40 |
+
Specifies whether to substitute the coordinate variables present
|
| 41 |
+
in expr, in terms of those of parameter system
|
| 42 |
+
|
| 43 |
+
Examples
|
| 44 |
+
========
|
| 45 |
+
|
| 46 |
+
>>> from sympy.vector import CoordSys3D
|
| 47 |
+
>>> from sympy import Symbol, cos, sin
|
| 48 |
+
>>> N = CoordSys3D('N')
|
| 49 |
+
>>> q = Symbol('q')
|
| 50 |
+
>>> B = N.orient_new_axis('B', q, N.k)
|
| 51 |
+
>>> from sympy.vector import express
|
| 52 |
+
>>> express(B.i, N)
|
| 53 |
+
(cos(q))*N.i + (sin(q))*N.j
|
| 54 |
+
>>> express(N.x, B, variables=True)
|
| 55 |
+
B.x*cos(q) - B.y*sin(q)
|
| 56 |
+
>>> d = N.i.outer(N.i)
|
| 57 |
+
>>> express(d, B, N) == (cos(q))*(B.i|N.i) + (-sin(q))*(B.j|N.i)
|
| 58 |
+
True
|
| 59 |
+
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
if expr in (0, Vector.zero):
|
| 63 |
+
return expr
|
| 64 |
+
|
| 65 |
+
if not isinstance(system, CoordSys3D):
|
| 66 |
+
raise TypeError("system should be a CoordSys3D \
|
| 67 |
+
instance")
|
| 68 |
+
|
| 69 |
+
if isinstance(expr, Vector):
|
| 70 |
+
if system2 is not None:
|
| 71 |
+
raise ValueError("system2 should not be provided for \
|
| 72 |
+
Vectors")
|
| 73 |
+
# Given expr is a Vector
|
| 74 |
+
if variables:
|
| 75 |
+
# If variables attribute is True, substitute
|
| 76 |
+
# the coordinate variables in the Vector
|
| 77 |
+
system_list = {x.system for x in expr.atoms(BaseScalar, BaseVector)} - {system}
|
| 78 |
+
subs_dict = {}
|
| 79 |
+
for f in system_list:
|
| 80 |
+
subs_dict.update(f.scalar_map(system))
|
| 81 |
+
expr = expr.subs(subs_dict)
|
| 82 |
+
# Re-express in this coordinate system
|
| 83 |
+
outvec = Vector.zero
|
| 84 |
+
parts = expr.separate()
|
| 85 |
+
for x in parts:
|
| 86 |
+
if x != system:
|
| 87 |
+
temp = system.rotation_matrix(x) * parts[x].to_matrix(x)
|
| 88 |
+
outvec += matrix_to_vector(temp, system)
|
| 89 |
+
else:
|
| 90 |
+
outvec += parts[x]
|
| 91 |
+
return outvec
|
| 92 |
+
|
| 93 |
+
elif isinstance(expr, Dyadic):
|
| 94 |
+
if system2 is None:
|
| 95 |
+
system2 = system
|
| 96 |
+
if not isinstance(system2, CoordSys3D):
|
| 97 |
+
raise TypeError("system2 should be a CoordSys3D \
|
| 98 |
+
instance")
|
| 99 |
+
outdyad = Dyadic.zero
|
| 100 |
+
var = variables
|
| 101 |
+
for k, v in expr.components.items():
|
| 102 |
+
outdyad += (express(v, system, variables=var) *
|
| 103 |
+
(express(k.args[0], system, variables=var) |
|
| 104 |
+
express(k.args[1], system2, variables=var)))
|
| 105 |
+
|
| 106 |
+
return outdyad
|
| 107 |
+
|
| 108 |
+
else:
|
| 109 |
+
if system2 is not None:
|
| 110 |
+
raise ValueError("system2 should not be provided for \
|
| 111 |
+
Vectors")
|
| 112 |
+
if variables:
|
| 113 |
+
# Given expr is a scalar field
|
| 114 |
+
system_set = set()
|
| 115 |
+
expr = sympify(expr)
|
| 116 |
+
# Substitute all the coordinate variables
|
| 117 |
+
for x in expr.atoms(BaseScalar):
|
| 118 |
+
if x.system != system:
|
| 119 |
+
system_set.add(x.system)
|
| 120 |
+
subs_dict = {}
|
| 121 |
+
for f in system_set:
|
| 122 |
+
subs_dict.update(f.scalar_map(system))
|
| 123 |
+
return expr.subs(subs_dict)
|
| 124 |
+
return expr
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def directional_derivative(field, direction_vector):
|
| 128 |
+
"""
|
| 129 |
+
Returns the directional derivative of a scalar or vector field computed
|
| 130 |
+
along a given vector in coordinate system which parameters are expressed.
|
| 131 |
+
|
| 132 |
+
Parameters
|
| 133 |
+
==========
|
| 134 |
+
|
| 135 |
+
field : Vector or Scalar
|
| 136 |
+
The scalar or vector field to compute the directional derivative of
|
| 137 |
+
|
| 138 |
+
direction_vector : Vector
|
| 139 |
+
The vector to calculated directional derivative along them.
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
Examples
|
| 143 |
+
========
|
| 144 |
+
|
| 145 |
+
>>> from sympy.vector import CoordSys3D, directional_derivative
|
| 146 |
+
>>> R = CoordSys3D('R')
|
| 147 |
+
>>> f1 = R.x*R.y*R.z
|
| 148 |
+
>>> v1 = 3*R.i + 4*R.j + R.k
|
| 149 |
+
>>> directional_derivative(f1, v1)
|
| 150 |
+
R.x*R.y + 4*R.x*R.z + 3*R.y*R.z
|
| 151 |
+
>>> f2 = 5*R.x**2*R.z
|
| 152 |
+
>>> directional_derivative(f2, v1)
|
| 153 |
+
5*R.x**2 + 30*R.x*R.z
|
| 154 |
+
|
| 155 |
+
"""
|
| 156 |
+
from sympy.vector.operators import _get_coord_systems
|
| 157 |
+
coord_sys = _get_coord_systems(field)
|
| 158 |
+
if len(coord_sys) > 0:
|
| 159 |
+
# TODO: This gets a random coordinate system in case of multiple ones:
|
| 160 |
+
coord_sys = next(iter(coord_sys))
|
| 161 |
+
field = express(field, coord_sys, variables=True)
|
| 162 |
+
i, j, k = coord_sys.base_vectors()
|
| 163 |
+
x, y, z = coord_sys.base_scalars()
|
| 164 |
+
out = Vector.dot(direction_vector, i) * diff(field, x)
|
| 165 |
+
out += Vector.dot(direction_vector, j) * diff(field, y)
|
| 166 |
+
out += Vector.dot(direction_vector, k) * diff(field, z)
|
| 167 |
+
if out == 0 and isinstance(field, Vector):
|
| 168 |
+
out = Vector.zero
|
| 169 |
+
return out
|
| 170 |
+
elif isinstance(field, Vector):
|
| 171 |
+
return Vector.zero
|
| 172 |
+
else:
|
| 173 |
+
return S.Zero
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def laplacian(expr):
|
| 177 |
+
"""
|
| 178 |
+
Return the laplacian of the given field computed in terms of
|
| 179 |
+
the base scalars of the given coordinate system.
|
| 180 |
+
|
| 181 |
+
Parameters
|
| 182 |
+
==========
|
| 183 |
+
|
| 184 |
+
expr : SymPy Expr or Vector
|
| 185 |
+
expr denotes a scalar or vector field.
|
| 186 |
+
|
| 187 |
+
Examples
|
| 188 |
+
========
|
| 189 |
+
|
| 190 |
+
>>> from sympy.vector import CoordSys3D, laplacian
|
| 191 |
+
>>> R = CoordSys3D('R')
|
| 192 |
+
>>> f = R.x**2*R.y**5*R.z
|
| 193 |
+
>>> laplacian(f)
|
| 194 |
+
20*R.x**2*R.y**3*R.z + 2*R.y**5*R.z
|
| 195 |
+
>>> f = R.x**2*R.i + R.y**3*R.j + R.z**4*R.k
|
| 196 |
+
>>> laplacian(f)
|
| 197 |
+
2*R.i + 6*R.y*R.j + 12*R.z**2*R.k
|
| 198 |
+
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
delop = Del()
|
| 202 |
+
if expr.is_Vector:
|
| 203 |
+
return (gradient(divergence(expr)) - curl(curl(expr))).doit()
|
| 204 |
+
return delop.dot(delop(expr)).doit()
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def is_conservative(field):
|
| 208 |
+
"""
|
| 209 |
+
Checks if a field is conservative.
|
| 210 |
+
|
| 211 |
+
Parameters
|
| 212 |
+
==========
|
| 213 |
+
|
| 214 |
+
field : Vector
|
| 215 |
+
The field to check for conservative property
|
| 216 |
+
|
| 217 |
+
Examples
|
| 218 |
+
========
|
| 219 |
+
|
| 220 |
+
>>> from sympy.vector import CoordSys3D
|
| 221 |
+
>>> from sympy.vector import is_conservative
|
| 222 |
+
>>> R = CoordSys3D('R')
|
| 223 |
+
>>> is_conservative(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
|
| 224 |
+
True
|
| 225 |
+
>>> is_conservative(R.z*R.j)
|
| 226 |
+
False
|
| 227 |
+
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
# Field is conservative irrespective of system
|
| 231 |
+
# Take the first coordinate system in the result of the
|
| 232 |
+
# separate method of Vector
|
| 233 |
+
if not isinstance(field, Vector):
|
| 234 |
+
raise TypeError("field should be a Vector")
|
| 235 |
+
if field == Vector.zero:
|
| 236 |
+
return True
|
| 237 |
+
return curl(field).simplify() == Vector.zero
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def is_solenoidal(field):
|
| 241 |
+
"""
|
| 242 |
+
Checks if a field is solenoidal.
|
| 243 |
+
|
| 244 |
+
Parameters
|
| 245 |
+
==========
|
| 246 |
+
|
| 247 |
+
field : Vector
|
| 248 |
+
The field to check for solenoidal property
|
| 249 |
+
|
| 250 |
+
Examples
|
| 251 |
+
========
|
| 252 |
+
|
| 253 |
+
>>> from sympy.vector import CoordSys3D
|
| 254 |
+
>>> from sympy.vector import is_solenoidal
|
| 255 |
+
>>> R = CoordSys3D('R')
|
| 256 |
+
>>> is_solenoidal(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
|
| 257 |
+
True
|
| 258 |
+
>>> is_solenoidal(R.y * R.j)
|
| 259 |
+
False
|
| 260 |
+
|
| 261 |
+
"""
|
| 262 |
+
|
| 263 |
+
# Field is solenoidal irrespective of system
|
| 264 |
+
# Take the first coordinate system in the result of the
|
| 265 |
+
# separate method in Vector
|
| 266 |
+
if not isinstance(field, Vector):
|
| 267 |
+
raise TypeError("field should be a Vector")
|
| 268 |
+
if field == Vector.zero:
|
| 269 |
+
return True
|
| 270 |
+
return divergence(field).simplify() is S.Zero
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def scalar_potential(field, coord_sys):
|
| 274 |
+
"""
|
| 275 |
+
Returns the scalar potential function of a field in a given
|
| 276 |
+
coordinate system (without the added integration constant).
|
| 277 |
+
|
| 278 |
+
Parameters
|
| 279 |
+
==========
|
| 280 |
+
|
| 281 |
+
field : Vector
|
| 282 |
+
The vector field whose scalar potential function is to be
|
| 283 |
+
calculated
|
| 284 |
+
|
| 285 |
+
coord_sys : CoordSys3D
|
| 286 |
+
The coordinate system to do the calculation in
|
| 287 |
+
|
| 288 |
+
Examples
|
| 289 |
+
========
|
| 290 |
+
|
| 291 |
+
>>> from sympy.vector import CoordSys3D
|
| 292 |
+
>>> from sympy.vector import scalar_potential, gradient
|
| 293 |
+
>>> R = CoordSys3D('R')
|
| 294 |
+
>>> scalar_potential(R.k, R) == R.z
|
| 295 |
+
True
|
| 296 |
+
>>> scalar_field = 2*R.x**2*R.y*R.z
|
| 297 |
+
>>> grad_field = gradient(scalar_field)
|
| 298 |
+
>>> scalar_potential(grad_field, R)
|
| 299 |
+
2*R.x**2*R.y*R.z
|
| 300 |
+
|
| 301 |
+
"""
|
| 302 |
+
|
| 303 |
+
# Check whether field is conservative
|
| 304 |
+
if not is_conservative(field):
|
| 305 |
+
raise ValueError("Field is not conservative")
|
| 306 |
+
if field == Vector.zero:
|
| 307 |
+
return S.Zero
|
| 308 |
+
# Express the field exntirely in coord_sys
|
| 309 |
+
# Substitute coordinate variables also
|
| 310 |
+
if not isinstance(coord_sys, CoordSys3D):
|
| 311 |
+
raise TypeError("coord_sys must be a CoordSys3D")
|
| 312 |
+
field = express(field, coord_sys, variables=True)
|
| 313 |
+
dimensions = coord_sys.base_vectors()
|
| 314 |
+
scalars = coord_sys.base_scalars()
|
| 315 |
+
# Calculate scalar potential function
|
| 316 |
+
temp_function = integrate(field.dot(dimensions[0]), scalars[0])
|
| 317 |
+
for i, dim in enumerate(dimensions[1:]):
|
| 318 |
+
partial_diff = diff(temp_function, scalars[i + 1])
|
| 319 |
+
partial_diff = field.dot(dim) - partial_diff
|
| 320 |
+
temp_function += integrate(partial_diff, scalars[i + 1])
|
| 321 |
+
return temp_function
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
def scalar_potential_difference(field, coord_sys, point1, point2):
|
| 325 |
+
"""
|
| 326 |
+
Returns the scalar potential difference between two points in a
|
| 327 |
+
certain coordinate system, wrt a given field.
|
| 328 |
+
|
| 329 |
+
If a scalar field is provided, its values at the two points are
|
| 330 |
+
considered. If a conservative vector field is provided, the values
|
| 331 |
+
of its scalar potential function at the two points are used.
|
| 332 |
+
|
| 333 |
+
Returns (potential at point2) - (potential at point1)
|
| 334 |
+
|
| 335 |
+
The position vectors of the two Points are calculated wrt the
|
| 336 |
+
origin of the coordinate system provided.
|
| 337 |
+
|
| 338 |
+
Parameters
|
| 339 |
+
==========
|
| 340 |
+
|
| 341 |
+
field : Vector/Expr
|
| 342 |
+
The field to calculate wrt
|
| 343 |
+
|
| 344 |
+
coord_sys : CoordSys3D
|
| 345 |
+
The coordinate system to do the calculations in
|
| 346 |
+
|
| 347 |
+
point1 : Point
|
| 348 |
+
The initial Point in given coordinate system
|
| 349 |
+
|
| 350 |
+
position2 : Point
|
| 351 |
+
The second Point in the given coordinate system
|
| 352 |
+
|
| 353 |
+
Examples
|
| 354 |
+
========
|
| 355 |
+
|
| 356 |
+
>>> from sympy.vector import CoordSys3D
|
| 357 |
+
>>> from sympy.vector import scalar_potential_difference
|
| 358 |
+
>>> R = CoordSys3D('R')
|
| 359 |
+
>>> P = R.origin.locate_new('P', R.x*R.i + R.y*R.j + R.z*R.k)
|
| 360 |
+
>>> vectfield = 4*R.x*R.y*R.i + 2*R.x**2*R.j
|
| 361 |
+
>>> scalar_potential_difference(vectfield, R, R.origin, P)
|
| 362 |
+
2*R.x**2*R.y
|
| 363 |
+
>>> Q = R.origin.locate_new('O', 3*R.i + R.j + 2*R.k)
|
| 364 |
+
>>> scalar_potential_difference(vectfield, R, P, Q)
|
| 365 |
+
-2*R.x**2*R.y + 18
|
| 366 |
+
|
| 367 |
+
"""
|
| 368 |
+
|
| 369 |
+
if not isinstance(coord_sys, CoordSys3D):
|
| 370 |
+
raise TypeError("coord_sys must be a CoordSys3D")
|
| 371 |
+
if isinstance(field, Vector):
|
| 372 |
+
# Get the scalar potential function
|
| 373 |
+
scalar_fn = scalar_potential(field, coord_sys)
|
| 374 |
+
else:
|
| 375 |
+
# Field is a scalar
|
| 376 |
+
scalar_fn = field
|
| 377 |
+
# Express positions in required coordinate system
|
| 378 |
+
origin = coord_sys.origin
|
| 379 |
+
position1 = express(point1.position_wrt(origin), coord_sys,
|
| 380 |
+
variables=True)
|
| 381 |
+
position2 = express(point2.position_wrt(origin), coord_sys,
|
| 382 |
+
variables=True)
|
| 383 |
+
# Get the two positions as substitution dicts for coordinate variables
|
| 384 |
+
subs_dict1 = {}
|
| 385 |
+
subs_dict2 = {}
|
| 386 |
+
scalars = coord_sys.base_scalars()
|
| 387 |
+
for i, x in enumerate(coord_sys.base_vectors()):
|
| 388 |
+
subs_dict1[scalars[i]] = x.dot(position1)
|
| 389 |
+
subs_dict2[scalars[i]] = x.dot(position2)
|
| 390 |
+
return scalar_fn.subs(subs_dict2) - scalar_fn.subs(subs_dict1)
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
def matrix_to_vector(matrix, system):
|
| 394 |
+
"""
|
| 395 |
+
Converts a vector in matrix form to a Vector instance.
|
| 396 |
+
|
| 397 |
+
It is assumed that the elements of the Matrix represent the
|
| 398 |
+
measure numbers of the components of the vector along basis
|
| 399 |
+
vectors of 'system'.
|
| 400 |
+
|
| 401 |
+
Parameters
|
| 402 |
+
==========
|
| 403 |
+
|
| 404 |
+
matrix : SymPy Matrix, Dimensions: (3, 1)
|
| 405 |
+
The matrix to be converted to a vector
|
| 406 |
+
|
| 407 |
+
system : CoordSys3D
|
| 408 |
+
The coordinate system the vector is to be defined in
|
| 409 |
+
|
| 410 |
+
Examples
|
| 411 |
+
========
|
| 412 |
+
|
| 413 |
+
>>> from sympy import ImmutableMatrix as Matrix
|
| 414 |
+
>>> m = Matrix([1, 2, 3])
|
| 415 |
+
>>> from sympy.vector import CoordSys3D, matrix_to_vector
|
| 416 |
+
>>> C = CoordSys3D('C')
|
| 417 |
+
>>> v = matrix_to_vector(m, C)
|
| 418 |
+
>>> v
|
| 419 |
+
C.i + 2*C.j + 3*C.k
|
| 420 |
+
>>> v.to_matrix(C) == m
|
| 421 |
+
True
|
| 422 |
+
|
| 423 |
+
"""
|
| 424 |
+
|
| 425 |
+
outvec = Vector.zero
|
| 426 |
+
vects = system.base_vectors()
|
| 427 |
+
for i, x in enumerate(matrix):
|
| 428 |
+
outvec += x * vects[i]
|
| 429 |
+
return outvec
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
def _path(from_object, to_object):
|
| 433 |
+
"""
|
| 434 |
+
Calculates the 'path' of objects starting from 'from_object'
|
| 435 |
+
to 'to_object', along with the index of the first common
|
| 436 |
+
ancestor in the tree.
|
| 437 |
+
|
| 438 |
+
Returns (index, list) tuple.
|
| 439 |
+
"""
|
| 440 |
+
|
| 441 |
+
if from_object._root != to_object._root:
|
| 442 |
+
raise ValueError("No connecting path found between " +
|
| 443 |
+
str(from_object) + " and " + str(to_object))
|
| 444 |
+
|
| 445 |
+
other_path = []
|
| 446 |
+
obj = to_object
|
| 447 |
+
while obj._parent is not None:
|
| 448 |
+
other_path.append(obj)
|
| 449 |
+
obj = obj._parent
|
| 450 |
+
other_path.append(obj)
|
| 451 |
+
object_set = set(other_path)
|
| 452 |
+
from_path = []
|
| 453 |
+
obj = from_object
|
| 454 |
+
while obj not in object_set:
|
| 455 |
+
from_path.append(obj)
|
| 456 |
+
obj = obj._parent
|
| 457 |
+
index = len(from_path)
|
| 458 |
+
i = other_path.index(obj)
|
| 459 |
+
while i >= 0:
|
| 460 |
+
from_path.append(other_path[i])
|
| 461 |
+
i -= 1
|
| 462 |
+
return index, from_path
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def orthogonalize(*vlist, orthonormal=False):
|
| 466 |
+
"""
|
| 467 |
+
Takes a sequence of independent vectors and orthogonalizes them
|
| 468 |
+
using the Gram - Schmidt process. Returns a list of
|
| 469 |
+
orthogonal or orthonormal vectors.
|
| 470 |
+
|
| 471 |
+
Parameters
|
| 472 |
+
==========
|
| 473 |
+
|
| 474 |
+
vlist : sequence of independent vectors to be made orthogonal.
|
| 475 |
+
|
| 476 |
+
orthonormal : Optional parameter
|
| 477 |
+
Set to True if the vectors returned should be
|
| 478 |
+
orthonormal.
|
| 479 |
+
Default: False
|
| 480 |
+
|
| 481 |
+
Examples
|
| 482 |
+
========
|
| 483 |
+
|
| 484 |
+
>>> from sympy.vector.coordsysrect import CoordSys3D
|
| 485 |
+
>>> from sympy.vector.functions import orthogonalize
|
| 486 |
+
>>> C = CoordSys3D('C')
|
| 487 |
+
>>> i, j, k = C.base_vectors()
|
| 488 |
+
>>> v1 = i + 2*j
|
| 489 |
+
>>> v2 = 2*i + 3*j
|
| 490 |
+
>>> orthogonalize(v1, v2)
|
| 491 |
+
[C.i + 2*C.j, 2/5*C.i + (-1/5)*C.j]
|
| 492 |
+
|
| 493 |
+
References
|
| 494 |
+
==========
|
| 495 |
+
|
| 496 |
+
.. [1] https://en.wikipedia.org/wiki/Gram-Schmidt_process
|
| 497 |
+
|
| 498 |
+
"""
|
| 499 |
+
|
| 500 |
+
if not all(isinstance(vec, Vector) for vec in vlist):
|
| 501 |
+
raise TypeError('Each element must be of Type Vector')
|
| 502 |
+
|
| 503 |
+
ortho_vlist = []
|
| 504 |
+
for i, term in enumerate(vlist):
|
| 505 |
+
for j in range(i):
|
| 506 |
+
term -= ortho_vlist[j].projection(vlist[i])
|
| 507 |
+
# TODO : The following line introduces a performance issue
|
| 508 |
+
# and needs to be changed once a good solution for issue #10279 is
|
| 509 |
+
# found.
|
| 510 |
+
if simplify(term).equals(Vector.zero):
|
| 511 |
+
raise ValueError("Vector set not linearly independent")
|
| 512 |
+
ortho_vlist.append(term)
|
| 513 |
+
|
| 514 |
+
if orthonormal:
|
| 515 |
+
ortho_vlist = [vec.normalize() for vec in ortho_vlist]
|
| 516 |
+
|
| 517 |
+
return ortho_vlist
|
mgm/lib/python3.10/site-packages/sympy/vector/implicitregion.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import Rational
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.core.symbol import symbols
|
| 4 |
+
from sympy.functions.elementary.complexes import sign
|
| 5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 6 |
+
from sympy.polys.polytools import gcd
|
| 7 |
+
from sympy.sets.sets import Complement
|
| 8 |
+
from sympy.core import Basic, Tuple, diff, expand, Eq, Integer
|
| 9 |
+
from sympy.core.sorting import ordered
|
| 10 |
+
from sympy.core.symbol import _symbol
|
| 11 |
+
from sympy.solvers import solveset, nonlinsolve, diophantine
|
| 12 |
+
from sympy.polys import total_degree
|
| 13 |
+
from sympy.geometry import Point
|
| 14 |
+
from sympy.ntheory.factor_ import core
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ImplicitRegion(Basic):
|
| 18 |
+
"""
|
| 19 |
+
Represents an implicit region in space.
|
| 20 |
+
|
| 21 |
+
Examples
|
| 22 |
+
========
|
| 23 |
+
|
| 24 |
+
>>> from sympy import Eq
|
| 25 |
+
>>> from sympy.abc import x, y, z, t
|
| 26 |
+
>>> from sympy.vector import ImplicitRegion
|
| 27 |
+
|
| 28 |
+
>>> ImplicitRegion((x, y), x**2 + y**2 - 4)
|
| 29 |
+
ImplicitRegion((x, y), x**2 + y**2 - 4)
|
| 30 |
+
>>> ImplicitRegion((x, y), Eq(y*x, 1))
|
| 31 |
+
ImplicitRegion((x, y), x*y - 1)
|
| 32 |
+
|
| 33 |
+
>>> parabola = ImplicitRegion((x, y), y**2 - 4*x)
|
| 34 |
+
>>> parabola.degree
|
| 35 |
+
2
|
| 36 |
+
>>> parabola.equation
|
| 37 |
+
-4*x + y**2
|
| 38 |
+
>>> parabola.rational_parametrization(t)
|
| 39 |
+
(4/t**2, 4/t)
|
| 40 |
+
|
| 41 |
+
>>> r = ImplicitRegion((x, y, z), Eq(z, x**2 + y**2))
|
| 42 |
+
>>> r.variables
|
| 43 |
+
(x, y, z)
|
| 44 |
+
>>> r.singular_points()
|
| 45 |
+
EmptySet
|
| 46 |
+
>>> r.regular_point()
|
| 47 |
+
(-10, -10, 200)
|
| 48 |
+
|
| 49 |
+
Parameters
|
| 50 |
+
==========
|
| 51 |
+
|
| 52 |
+
variables : tuple to map variables in implicit equation to base scalars.
|
| 53 |
+
|
| 54 |
+
equation : An expression or Eq denoting the implicit equation of the region.
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
def __new__(cls, variables, equation):
|
| 58 |
+
if not isinstance(variables, Tuple):
|
| 59 |
+
variables = Tuple(*variables)
|
| 60 |
+
|
| 61 |
+
if isinstance(equation, Eq):
|
| 62 |
+
equation = equation.lhs - equation.rhs
|
| 63 |
+
|
| 64 |
+
return super().__new__(cls, variables, equation)
|
| 65 |
+
|
| 66 |
+
@property
|
| 67 |
+
def variables(self):
|
| 68 |
+
return self.args[0]
|
| 69 |
+
|
| 70 |
+
@property
|
| 71 |
+
def equation(self):
|
| 72 |
+
return self.args[1]
|
| 73 |
+
|
| 74 |
+
@property
|
| 75 |
+
def degree(self):
|
| 76 |
+
return total_degree(self.equation)
|
| 77 |
+
|
| 78 |
+
def regular_point(self):
|
| 79 |
+
"""
|
| 80 |
+
Returns a point on the implicit region.
|
| 81 |
+
|
| 82 |
+
Examples
|
| 83 |
+
========
|
| 84 |
+
|
| 85 |
+
>>> from sympy.abc import x, y, z
|
| 86 |
+
>>> from sympy.vector import ImplicitRegion
|
| 87 |
+
>>> circle = ImplicitRegion((x, y), (x + 2)**2 + (y - 3)**2 - 16)
|
| 88 |
+
>>> circle.regular_point()
|
| 89 |
+
(-2, -1)
|
| 90 |
+
>>> parabola = ImplicitRegion((x, y), x**2 - 4*y)
|
| 91 |
+
>>> parabola.regular_point()
|
| 92 |
+
(0, 0)
|
| 93 |
+
>>> r = ImplicitRegion((x, y, z), (x + y + z)**4)
|
| 94 |
+
>>> r.regular_point()
|
| 95 |
+
(-10, -10, 20)
|
| 96 |
+
|
| 97 |
+
References
|
| 98 |
+
==========
|
| 99 |
+
|
| 100 |
+
- Erik Hillgarter, "Rational Points on Conics", Diploma Thesis, RISC-Linz,
|
| 101 |
+
J. Kepler Universitat Linz, 1996. Available:
|
| 102 |
+
https://www3.risc.jku.at/publications/download/risc_1355/Rational%20Points%20on%20Conics.pdf
|
| 103 |
+
|
| 104 |
+
"""
|
| 105 |
+
equation = self.equation
|
| 106 |
+
|
| 107 |
+
if len(self.variables) == 1:
|
| 108 |
+
return (list(solveset(equation, self.variables[0], domain=S.Reals))[0],)
|
| 109 |
+
elif len(self.variables) == 2:
|
| 110 |
+
|
| 111 |
+
if self.degree == 2:
|
| 112 |
+
coeffs = a, b, c, d, e, f = conic_coeff(self.variables, equation)
|
| 113 |
+
|
| 114 |
+
if b**2 == 4*a*c:
|
| 115 |
+
x_reg, y_reg = self._regular_point_parabola(*coeffs)
|
| 116 |
+
else:
|
| 117 |
+
x_reg, y_reg = self._regular_point_ellipse(*coeffs)
|
| 118 |
+
return x_reg, y_reg
|
| 119 |
+
|
| 120 |
+
if len(self.variables) == 3:
|
| 121 |
+
x, y, z = self.variables
|
| 122 |
+
|
| 123 |
+
for x_reg in range(-10, 10):
|
| 124 |
+
for y_reg in range(-10, 10):
|
| 125 |
+
if not solveset(equation.subs({x: x_reg, y: y_reg}), self.variables[2], domain=S.Reals).is_empty:
|
| 126 |
+
return (x_reg, y_reg, list(solveset(equation.subs({x: x_reg, y: y_reg})))[0])
|
| 127 |
+
|
| 128 |
+
if len(self.singular_points()) != 0:
|
| 129 |
+
return list[self.singular_points()][0]
|
| 130 |
+
|
| 131 |
+
raise NotImplementedError()
|
| 132 |
+
|
| 133 |
+
def _regular_point_parabola(self, a, b, c, d, e, f):
|
| 134 |
+
ok = (a, d) != (0, 0) and (c, e) != (0, 0) and b**2 == 4*a*c and (a, c) != (0, 0)
|
| 135 |
+
|
| 136 |
+
if not ok:
|
| 137 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 138 |
+
|
| 139 |
+
if a != 0:
|
| 140 |
+
d_dash, f_dash = (4*a*e - 2*b*d, 4*a*f - d**2)
|
| 141 |
+
if d_dash != 0:
|
| 142 |
+
y_reg = -f_dash/d_dash
|
| 143 |
+
x_reg = -(d + b*y_reg)/(2*a)
|
| 144 |
+
else:
|
| 145 |
+
ok = False
|
| 146 |
+
elif c != 0:
|
| 147 |
+
d_dash, f_dash = (4*c*d - 2*b*e, 4*c*f - e**2)
|
| 148 |
+
if d_dash != 0:
|
| 149 |
+
x_reg = -f_dash/d_dash
|
| 150 |
+
y_reg = -(e + b*x_reg)/(2*c)
|
| 151 |
+
else:
|
| 152 |
+
ok = False
|
| 153 |
+
|
| 154 |
+
if ok:
|
| 155 |
+
return x_reg, y_reg
|
| 156 |
+
else:
|
| 157 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 158 |
+
|
| 159 |
+
def _regular_point_ellipse(self, a, b, c, d, e, f):
|
| 160 |
+
D = 4*a*c - b**2
|
| 161 |
+
ok = D
|
| 162 |
+
|
| 163 |
+
if not ok:
|
| 164 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 165 |
+
|
| 166 |
+
if a == 0 and c == 0:
|
| 167 |
+
K = -1
|
| 168 |
+
L = 4*(d*e - b*f)
|
| 169 |
+
elif c != 0:
|
| 170 |
+
K = D
|
| 171 |
+
L = 4*c**2*d**2 - 4*b*c*d*e + 4*a*c*e**2 + 4*b**2*c*f - 16*a*c**2*f
|
| 172 |
+
else:
|
| 173 |
+
K = D
|
| 174 |
+
L = 4*a**2*e**2 - 4*b*a*d*e + 4*b**2*a*f
|
| 175 |
+
|
| 176 |
+
ok = L != 0 and not(K > 0 and L < 0)
|
| 177 |
+
if not ok:
|
| 178 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 179 |
+
|
| 180 |
+
K = Rational(K).limit_denominator(10**12)
|
| 181 |
+
L = Rational(L).limit_denominator(10**12)
|
| 182 |
+
|
| 183 |
+
k1, k2 = K.p, K.q
|
| 184 |
+
l1, l2 = L.p, L.q
|
| 185 |
+
g = gcd(k2, l2)
|
| 186 |
+
|
| 187 |
+
a1 = (l2*k2)/g
|
| 188 |
+
b1 = (k1*l2)/g
|
| 189 |
+
c1 = -(l1*k2)/g
|
| 190 |
+
a2 = sign(a1)*core(abs(a1), 2)
|
| 191 |
+
r1 = sqrt(a1/a2)
|
| 192 |
+
b2 = sign(b1)*core(abs(b1), 2)
|
| 193 |
+
r2 = sqrt(b1/b2)
|
| 194 |
+
c2 = sign(c1)*core(abs(c1), 2)
|
| 195 |
+
r3 = sqrt(c1/c2)
|
| 196 |
+
|
| 197 |
+
g = gcd(gcd(a2, b2), c2)
|
| 198 |
+
a2 = a2/g
|
| 199 |
+
b2 = b2/g
|
| 200 |
+
c2 = c2/g
|
| 201 |
+
|
| 202 |
+
g1 = gcd(a2, b2)
|
| 203 |
+
a2 = a2/g1
|
| 204 |
+
b2 = b2/g1
|
| 205 |
+
c2 = c2*g1
|
| 206 |
+
|
| 207 |
+
g2 = gcd(a2,c2)
|
| 208 |
+
a2 = a2/g2
|
| 209 |
+
b2 = b2*g2
|
| 210 |
+
c2 = c2/g2
|
| 211 |
+
|
| 212 |
+
g3 = gcd(b2, c2)
|
| 213 |
+
a2 = a2*g3
|
| 214 |
+
b2 = b2/g3
|
| 215 |
+
c2 = c2/g3
|
| 216 |
+
|
| 217 |
+
x, y, z = symbols("x y z")
|
| 218 |
+
eq = a2*x**2 + b2*y**2 + c2*z**2
|
| 219 |
+
|
| 220 |
+
solutions = diophantine(eq)
|
| 221 |
+
|
| 222 |
+
if len(solutions) == 0:
|
| 223 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 224 |
+
|
| 225 |
+
flag = False
|
| 226 |
+
for sol in solutions:
|
| 227 |
+
syms = Tuple(*sol).free_symbols
|
| 228 |
+
rep = dict.fromkeys(syms, 3)
|
| 229 |
+
sol_z = sol[2]
|
| 230 |
+
|
| 231 |
+
if sol_z == 0:
|
| 232 |
+
flag = True
|
| 233 |
+
continue
|
| 234 |
+
|
| 235 |
+
if not isinstance(sol_z, (int, Integer)):
|
| 236 |
+
syms_z = sol_z.free_symbols
|
| 237 |
+
|
| 238 |
+
if len(syms_z) == 1:
|
| 239 |
+
p = next(iter(syms_z))
|
| 240 |
+
p_values = Complement(S.Integers, solveset(Eq(sol_z, 0), p, S.Integers))
|
| 241 |
+
rep[p] = next(iter(p_values))
|
| 242 |
+
|
| 243 |
+
if len(syms_z) == 2:
|
| 244 |
+
p, q = list(ordered(syms_z))
|
| 245 |
+
|
| 246 |
+
for i in S.Integers:
|
| 247 |
+
subs_sol_z = sol_z.subs(p, i)
|
| 248 |
+
q_values = Complement(S.Integers, solveset(Eq(subs_sol_z, 0), q, S.Integers))
|
| 249 |
+
|
| 250 |
+
if not q_values.is_empty:
|
| 251 |
+
rep[p] = i
|
| 252 |
+
rep[q] = next(iter(q_values))
|
| 253 |
+
break
|
| 254 |
+
|
| 255 |
+
if len(syms) != 0:
|
| 256 |
+
x, y, z = tuple(s.subs(rep) for s in sol)
|
| 257 |
+
else:
|
| 258 |
+
x, y, z = sol
|
| 259 |
+
flag = False
|
| 260 |
+
break
|
| 261 |
+
|
| 262 |
+
if flag:
|
| 263 |
+
raise ValueError("Rational Point on the conic does not exist")
|
| 264 |
+
|
| 265 |
+
x = (x*g3)/r1
|
| 266 |
+
y = (y*g2)/r2
|
| 267 |
+
z = (z*g1)/r3
|
| 268 |
+
x = x/z
|
| 269 |
+
y = y/z
|
| 270 |
+
|
| 271 |
+
if a == 0 and c == 0:
|
| 272 |
+
x_reg = (x + y - 2*e)/(2*b)
|
| 273 |
+
y_reg = (x - y - 2*d)/(2*b)
|
| 274 |
+
elif c != 0:
|
| 275 |
+
x_reg = (x - 2*d*c + b*e)/K
|
| 276 |
+
y_reg = (y - b*x_reg - e)/(2*c)
|
| 277 |
+
else:
|
| 278 |
+
y_reg = (x - 2*e*a + b*d)/K
|
| 279 |
+
x_reg = (y - b*y_reg - d)/(2*a)
|
| 280 |
+
|
| 281 |
+
return x_reg, y_reg
|
| 282 |
+
|
| 283 |
+
def singular_points(self):
|
| 284 |
+
"""
|
| 285 |
+
Returns a set of singular points of the region.
|
| 286 |
+
|
| 287 |
+
The singular points are those points on the region
|
| 288 |
+
where all partial derivatives vanish.
|
| 289 |
+
|
| 290 |
+
Examples
|
| 291 |
+
========
|
| 292 |
+
|
| 293 |
+
>>> from sympy.abc import x, y
|
| 294 |
+
>>> from sympy.vector import ImplicitRegion
|
| 295 |
+
>>> I = ImplicitRegion((x, y), (y-1)**2 -x**3 + 2*x**2 -x)
|
| 296 |
+
>>> I.singular_points()
|
| 297 |
+
{(1, 1)}
|
| 298 |
+
|
| 299 |
+
"""
|
| 300 |
+
eq_list = [self.equation]
|
| 301 |
+
for var in self.variables:
|
| 302 |
+
eq_list += [diff(self.equation, var)]
|
| 303 |
+
|
| 304 |
+
return nonlinsolve(eq_list, list(self.variables))
|
| 305 |
+
|
| 306 |
+
def multiplicity(self, point):
|
| 307 |
+
"""
|
| 308 |
+
Returns the multiplicity of a singular point on the region.
|
| 309 |
+
|
| 310 |
+
A singular point (x,y) of region is said to be of multiplicity m
|
| 311 |
+
if all the partial derivatives off to order m - 1 vanish there.
|
| 312 |
+
|
| 313 |
+
Examples
|
| 314 |
+
========
|
| 315 |
+
|
| 316 |
+
>>> from sympy.abc import x, y, z
|
| 317 |
+
>>> from sympy.vector import ImplicitRegion
|
| 318 |
+
>>> I = ImplicitRegion((x, y, z), x**2 + y**3 - z**4)
|
| 319 |
+
>>> I.singular_points()
|
| 320 |
+
{(0, 0, 0)}
|
| 321 |
+
>>> I.multiplicity((0, 0, 0))
|
| 322 |
+
2
|
| 323 |
+
|
| 324 |
+
"""
|
| 325 |
+
if isinstance(point, Point):
|
| 326 |
+
point = point.args
|
| 327 |
+
|
| 328 |
+
modified_eq = self.equation
|
| 329 |
+
|
| 330 |
+
for i, var in enumerate(self.variables):
|
| 331 |
+
modified_eq = modified_eq.subs(var, var + point[i])
|
| 332 |
+
modified_eq = expand(modified_eq)
|
| 333 |
+
|
| 334 |
+
if len(modified_eq.args) != 0:
|
| 335 |
+
terms = modified_eq.args
|
| 336 |
+
m = min(total_degree(term) for term in terms)
|
| 337 |
+
else:
|
| 338 |
+
terms = modified_eq
|
| 339 |
+
m = total_degree(terms)
|
| 340 |
+
|
| 341 |
+
return m
|
| 342 |
+
|
| 343 |
+
def rational_parametrization(self, parameters=('t', 's'), reg_point=None):
|
| 344 |
+
"""
|
| 345 |
+
Returns the rational parametrization of implicit region.
|
| 346 |
+
|
| 347 |
+
Examples
|
| 348 |
+
========
|
| 349 |
+
|
| 350 |
+
>>> from sympy import Eq
|
| 351 |
+
>>> from sympy.abc import x, y, z, s, t
|
| 352 |
+
>>> from sympy.vector import ImplicitRegion
|
| 353 |
+
|
| 354 |
+
>>> parabola = ImplicitRegion((x, y), y**2 - 4*x)
|
| 355 |
+
>>> parabola.rational_parametrization()
|
| 356 |
+
(4/t**2, 4/t)
|
| 357 |
+
|
| 358 |
+
>>> circle = ImplicitRegion((x, y), Eq(x**2 + y**2, 4))
|
| 359 |
+
>>> circle.rational_parametrization()
|
| 360 |
+
(4*t/(t**2 + 1), 4*t**2/(t**2 + 1) - 2)
|
| 361 |
+
|
| 362 |
+
>>> I = ImplicitRegion((x, y), x**3 + x**2 - y**2)
|
| 363 |
+
>>> I.rational_parametrization()
|
| 364 |
+
(t**2 - 1, t*(t**2 - 1))
|
| 365 |
+
|
| 366 |
+
>>> cubic_curve = ImplicitRegion((x, y), x**3 + x**2 - y**2)
|
| 367 |
+
>>> cubic_curve.rational_parametrization(parameters=(t))
|
| 368 |
+
(t**2 - 1, t*(t**2 - 1))
|
| 369 |
+
|
| 370 |
+
>>> sphere = ImplicitRegion((x, y, z), x**2 + y**2 + z**2 - 4)
|
| 371 |
+
>>> sphere.rational_parametrization(parameters=(t, s))
|
| 372 |
+
(-2 + 4/(s**2 + t**2 + 1), 4*s/(s**2 + t**2 + 1), 4*t/(s**2 + t**2 + 1))
|
| 373 |
+
|
| 374 |
+
For some conics, regular_points() is unable to find a point on curve.
|
| 375 |
+
To calulcate the parametric representation in such cases, user need
|
| 376 |
+
to determine a point on the region and pass it using reg_point.
|
| 377 |
+
|
| 378 |
+
>>> c = ImplicitRegion((x, y), (x - 1/2)**2 + (y)**2 - (1/4)**2)
|
| 379 |
+
>>> c.rational_parametrization(reg_point=(3/4, 0))
|
| 380 |
+
(0.75 - 0.5/(t**2 + 1), -0.5*t/(t**2 + 1))
|
| 381 |
+
|
| 382 |
+
References
|
| 383 |
+
==========
|
| 384 |
+
|
| 385 |
+
- Christoph M. Hoffmann, "Conversion Methods between Parametric and
|
| 386 |
+
Implicit Curves and Surfaces", Purdue e-Pubs, 1990. Available:
|
| 387 |
+
https://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1827&context=cstech
|
| 388 |
+
|
| 389 |
+
"""
|
| 390 |
+
equation = self.equation
|
| 391 |
+
degree = self.degree
|
| 392 |
+
|
| 393 |
+
if degree == 1:
|
| 394 |
+
if len(self.variables) == 1:
|
| 395 |
+
return (equation,)
|
| 396 |
+
elif len(self.variables) == 2:
|
| 397 |
+
x, y = self.variables
|
| 398 |
+
y_par = list(solveset(equation, y))[0]
|
| 399 |
+
return x, y_par
|
| 400 |
+
else:
|
| 401 |
+
raise NotImplementedError()
|
| 402 |
+
|
| 403 |
+
point = ()
|
| 404 |
+
|
| 405 |
+
# Finding the (n - 1) fold point of the monoid of degree
|
| 406 |
+
if degree == 2:
|
| 407 |
+
# For degree 2 curves, either a regular point or a singular point can be used.
|
| 408 |
+
if reg_point is not None:
|
| 409 |
+
# Using point provided by the user as regular point
|
| 410 |
+
point = reg_point
|
| 411 |
+
else:
|
| 412 |
+
if len(self.singular_points()) != 0:
|
| 413 |
+
point = list(self.singular_points())[0]
|
| 414 |
+
else:
|
| 415 |
+
point = self.regular_point()
|
| 416 |
+
|
| 417 |
+
if len(self.singular_points()) != 0:
|
| 418 |
+
singular_points = self.singular_points()
|
| 419 |
+
for spoint in singular_points:
|
| 420 |
+
syms = Tuple(*spoint).free_symbols
|
| 421 |
+
rep = dict.fromkeys(syms, 2)
|
| 422 |
+
|
| 423 |
+
if len(syms) != 0:
|
| 424 |
+
spoint = tuple(s.subs(rep) for s in spoint)
|
| 425 |
+
|
| 426 |
+
if self.multiplicity(spoint) == degree - 1:
|
| 427 |
+
point = spoint
|
| 428 |
+
break
|
| 429 |
+
|
| 430 |
+
if len(point) == 0:
|
| 431 |
+
# The region in not a monoid
|
| 432 |
+
raise NotImplementedError()
|
| 433 |
+
|
| 434 |
+
modified_eq = equation
|
| 435 |
+
|
| 436 |
+
# Shifting the region such that fold point moves to origin
|
| 437 |
+
for i, var in enumerate(self.variables):
|
| 438 |
+
modified_eq = modified_eq.subs(var, var + point[i])
|
| 439 |
+
modified_eq = expand(modified_eq)
|
| 440 |
+
|
| 441 |
+
hn = hn_1 = 0
|
| 442 |
+
for term in modified_eq.args:
|
| 443 |
+
if total_degree(term) == degree:
|
| 444 |
+
hn += term
|
| 445 |
+
else:
|
| 446 |
+
hn_1 += term
|
| 447 |
+
|
| 448 |
+
hn_1 = -1*hn_1
|
| 449 |
+
|
| 450 |
+
if not isinstance(parameters, tuple):
|
| 451 |
+
parameters = (parameters,)
|
| 452 |
+
|
| 453 |
+
if len(self.variables) == 2:
|
| 454 |
+
|
| 455 |
+
parameter1 = parameters[0]
|
| 456 |
+
if parameter1 == 's':
|
| 457 |
+
# To avoid name conflict between parameters
|
| 458 |
+
s = _symbol('s_', real=True)
|
| 459 |
+
else:
|
| 460 |
+
s = _symbol('s', real=True)
|
| 461 |
+
t = _symbol(parameter1, real=True)
|
| 462 |
+
|
| 463 |
+
hn = hn.subs({self.variables[0]: s, self.variables[1]: t})
|
| 464 |
+
hn_1 = hn_1.subs({self.variables[0]: s, self.variables[1]: t})
|
| 465 |
+
|
| 466 |
+
x_par = (s*(hn_1/hn)).subs(s, 1) + point[0]
|
| 467 |
+
y_par = (t*(hn_1/hn)).subs(s, 1) + point[1]
|
| 468 |
+
|
| 469 |
+
return x_par, y_par
|
| 470 |
+
|
| 471 |
+
elif len(self.variables) == 3:
|
| 472 |
+
|
| 473 |
+
parameter1, parameter2 = parameters
|
| 474 |
+
if 'r' in parameters:
|
| 475 |
+
# To avoid name conflict between parameters
|
| 476 |
+
r = _symbol('r_', real=True)
|
| 477 |
+
else:
|
| 478 |
+
r = _symbol('r', real=True)
|
| 479 |
+
s = _symbol(parameter2, real=True)
|
| 480 |
+
t = _symbol(parameter1, real=True)
|
| 481 |
+
|
| 482 |
+
hn = hn.subs({self.variables[0]: r, self.variables[1]: s, self.variables[2]: t})
|
| 483 |
+
hn_1 = hn_1.subs({self.variables[0]: r, self.variables[1]: s, self.variables[2]: t})
|
| 484 |
+
|
| 485 |
+
x_par = (r*(hn_1/hn)).subs(r, 1) + point[0]
|
| 486 |
+
y_par = (s*(hn_1/hn)).subs(r, 1) + point[1]
|
| 487 |
+
z_par = (t*(hn_1/hn)).subs(r, 1) + point[2]
|
| 488 |
+
|
| 489 |
+
return x_par, y_par, z_par
|
| 490 |
+
|
| 491 |
+
raise NotImplementedError()
|
| 492 |
+
|
| 493 |
+
def conic_coeff(variables, equation):
|
| 494 |
+
if total_degree(equation) != 2:
|
| 495 |
+
raise ValueError()
|
| 496 |
+
x = variables[0]
|
| 497 |
+
y = variables[1]
|
| 498 |
+
|
| 499 |
+
equation = expand(equation)
|
| 500 |
+
a = equation.coeff(x**2)
|
| 501 |
+
b = equation.coeff(x*y)
|
| 502 |
+
c = equ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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Basic, diff
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.core.sorting import default_sort_key
|
| 4 |
+
from sympy.matrices import Matrix
|
| 5 |
+
from sympy.integrals import Integral, integrate
|
| 6 |
+
from sympy.geometry.entity import GeometryEntity
|
| 7 |
+
from sympy.simplify.simplify import simplify
|
| 8 |
+
from sympy.utilities.iterables import topological_sort
|
| 9 |
+
from sympy.vector import (CoordSys3D, Vector, ParametricRegion,
|
| 10 |
+
parametric_region_list, ImplicitRegion)
|
| 11 |
+
from sympy.vector.operators import _get_coord_systems
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ParametricIntegral(Basic):
|
| 15 |
+
"""
|
| 16 |
+
Represents integral of a scalar or vector field
|
| 17 |
+
over a Parametric Region
|
| 18 |
+
|
| 19 |
+
Examples
|
| 20 |
+
========
|
| 21 |
+
|
| 22 |
+
>>> from sympy import cos, sin, pi
|
| 23 |
+
>>> from sympy.vector import CoordSys3D, ParametricRegion, ParametricIntegral
|
| 24 |
+
>>> from sympy.abc import r, t, theta, phi
|
| 25 |
+
|
| 26 |
+
>>> C = CoordSys3D('C')
|
| 27 |
+
>>> curve = ParametricRegion((3*t - 2, t + 1), (t, 1, 2))
|
| 28 |
+
>>> ParametricIntegral(C.x, curve)
|
| 29 |
+
5*sqrt(10)/2
|
| 30 |
+
>>> length = ParametricIntegral(1, curve)
|
| 31 |
+
>>> length
|
| 32 |
+
sqrt(10)
|
| 33 |
+
>>> semisphere = ParametricRegion((2*sin(phi)*cos(theta), 2*sin(phi)*sin(theta), 2*cos(phi)),\
|
| 34 |
+
(theta, 0, 2*pi), (phi, 0, pi/2))
|
| 35 |
+
>>> ParametricIntegral(C.z, semisphere)
|
| 36 |
+
8*pi
|
| 37 |
+
|
| 38 |
+
>>> ParametricIntegral(C.j + C.k, ParametricRegion((r*cos(theta), r*sin(theta)), r, theta))
|
| 39 |
+
0
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def __new__(cls, field, parametricregion):
|
| 44 |
+
|
| 45 |
+
coord_set = _get_coord_systems(field)
|
| 46 |
+
|
| 47 |
+
if len(coord_set) == 0:
|
| 48 |
+
coord_sys = CoordSys3D('C')
|
| 49 |
+
elif len(coord_set) > 1:
|
| 50 |
+
raise ValueError
|
| 51 |
+
else:
|
| 52 |
+
coord_sys = next(iter(coord_set))
|
| 53 |
+
|
| 54 |
+
if parametricregion.dimensions == 0:
|
| 55 |
+
return S.Zero
|
| 56 |
+
|
| 57 |
+
base_vectors = coord_sys.base_vectors()
|
| 58 |
+
base_scalars = coord_sys.base_scalars()
|
| 59 |
+
|
| 60 |
+
parametricfield = field
|
| 61 |
+
|
| 62 |
+
r = Vector.zero
|
| 63 |
+
for i in range(len(parametricregion.definition)):
|
| 64 |
+
r += base_vectors[i]*parametricregion.definition[i]
|
| 65 |
+
|
| 66 |
+
if len(coord_set) != 0:
|
| 67 |
+
for i in range(len(parametricregion.definition)):
|
| 68 |
+
parametricfield = parametricfield.subs(base_scalars[i], parametricregion.definition[i])
|
| 69 |
+
|
| 70 |
+
if parametricregion.dimensions == 1:
|
| 71 |
+
parameter = parametricregion.parameters[0]
|
| 72 |
+
|
| 73 |
+
r_diff = diff(r, parameter)
|
| 74 |
+
lower, upper = parametricregion.limits[parameter][0], parametricregion.limits[parameter][1]
|
| 75 |
+
|
| 76 |
+
if isinstance(parametricfield, Vector):
|
| 77 |
+
integrand = simplify(r_diff.dot(parametricfield))
|
| 78 |
+
else:
|
| 79 |
+
integrand = simplify(r_diff.magnitude()*parametricfield)
|
| 80 |
+
|
| 81 |
+
result = integrate(integrand, (parameter, lower, upper))
|
| 82 |
+
|
| 83 |
+
elif parametricregion.dimensions == 2:
|
| 84 |
+
u, v = cls._bounds_case(parametricregion.parameters, parametricregion.limits)
|
| 85 |
+
|
| 86 |
+
r_u = diff(r, u)
|
| 87 |
+
r_v = diff(r, v)
|
| 88 |
+
normal_vector = simplify(r_u.cross(r_v))
|
| 89 |
+
|
| 90 |
+
if isinstance(parametricfield, Vector):
|
| 91 |
+
integrand = parametricfield.dot(normal_vector)
|
| 92 |
+
else:
|
| 93 |
+
integrand = parametricfield*normal_vector.magnitude()
|
| 94 |
+
|
| 95 |
+
integrand = simplify(integrand)
|
| 96 |
+
|
| 97 |
+
lower_u, upper_u = parametricregion.limits[u][0], parametricregion.limits[u][1]
|
| 98 |
+
lower_v, upper_v = parametricregion.limits[v][0], parametricregion.limits[v][1]
|
| 99 |
+
|
| 100 |
+
result = integrate(integrand, (u, lower_u, upper_u), (v, lower_v, upper_v))
|
| 101 |
+
|
| 102 |
+
else:
|
| 103 |
+
variables = cls._bounds_case(parametricregion.parameters, parametricregion.limits)
|
| 104 |
+
coeff = Matrix(parametricregion.definition).jacobian(variables).det()
|
| 105 |
+
integrand = simplify(parametricfield*coeff)
|
| 106 |
+
|
| 107 |
+
l = [(var, parametricregion.limits[var][0], parametricregion.limits[var][1]) for var in variables]
|
| 108 |
+
result = integrate(integrand, *l)
|
| 109 |
+
|
| 110 |
+
if not isinstance(result, Integral):
|
| 111 |
+
return result
|
| 112 |
+
else:
|
| 113 |
+
return super().__new__(cls, field, parametricregion)
|
| 114 |
+
|
| 115 |
+
@classmethod
|
| 116 |
+
def _bounds_case(cls, parameters, limits):
|
| 117 |
+
|
| 118 |
+
V = list(limits.keys())
|
| 119 |
+
E = []
|
| 120 |
+
|
| 121 |
+
for p in V:
|
| 122 |
+
lower_p = limits[p][0]
|
| 123 |
+
upper_p = limits[p][1]
|
| 124 |
+
|
| 125 |
+
lower_p = lower_p.atoms()
|
| 126 |
+
upper_p = upper_p.atoms()
|
| 127 |
+
E.extend((p, q) for q in V if p != q and
|
| 128 |
+
(lower_p.issuperset({q}) or upper_p.issuperset({q})))
|
| 129 |
+
|
| 130 |
+
if not E:
|
| 131 |
+
return parameters
|
| 132 |
+
else:
|
| 133 |
+
return topological_sort((V, E), key=default_sort_key)
|
| 134 |
+
|
| 135 |
+
@property
|
| 136 |
+
def field(self):
|
| 137 |
+
return self.args[0]
|
| 138 |
+
|
| 139 |
+
@property
|
| 140 |
+
def parametricregion(self):
|
| 141 |
+
return self.args[1]
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def vector_integrate(field, *region):
|
| 145 |
+
"""
|
| 146 |
+
Compute the integral of a vector/scalar field
|
| 147 |
+
over a a region or a set of parameters.
|
| 148 |
+
|
| 149 |
+
Examples
|
| 150 |
+
========
|
| 151 |
+
>>> from sympy.vector import CoordSys3D, ParametricRegion, vector_integrate
|
| 152 |
+
>>> from sympy.abc import x, y, t
|
| 153 |
+
>>> C = CoordSys3D('C')
|
| 154 |
+
|
| 155 |
+
>>> region = ParametricRegion((t, t**2), (t, 1, 5))
|
| 156 |
+
>>> vector_integrate(C.x*C.i, region)
|
| 157 |
+
12
|
| 158 |
+
|
| 159 |
+
Integrals over some objects of geometry module can also be calculated.
|
| 160 |
+
|
| 161 |
+
>>> from sympy.geometry import Point, Circle, Triangle
|
| 162 |
+
>>> c = Circle(Point(0, 2), 5)
|
| 163 |
+
>>> vector_integrate(C.x**2 + C.y**2, c)
|
| 164 |
+
290*pi
|
| 165 |
+
>>> triangle = Triangle(Point(-2, 3), Point(2, 3), Point(0, 5))
|
| 166 |
+
>>> vector_integrate(3*C.x**2*C.y*C.i + C.j, triangle)
|
| 167 |
+
-8
|
| 168 |
+
|
| 169 |
+
Integrals over some simple implicit regions can be computed. But in most cases,
|
| 170 |
+
it takes too long to compute over them. This is due to the expressions of parametric
|
| 171 |
+
representation becoming large.
|
| 172 |
+
|
| 173 |
+
>>> from sympy.vector import ImplicitRegion
|
| 174 |
+
>>> c2 = ImplicitRegion((x, y), (x - 2)**2 + (y - 1)**2 - 9)
|
| 175 |
+
>>> vector_integrate(1, c2)
|
| 176 |
+
6*pi
|
| 177 |
+
|
| 178 |
+
Integral of fields with respect to base scalars:
|
| 179 |
+
|
| 180 |
+
>>> vector_integrate(12*C.y**3, (C.y, 1, 3))
|
| 181 |
+
240
|
| 182 |
+
>>> vector_integrate(C.x**2*C.z, C.x)
|
| 183 |
+
C.x**3*C.z/3
|
| 184 |
+
>>> vector_integrate(C.x*C.i - C.y*C.k, C.x)
|
| 185 |
+
(Integral(C.x, C.x))*C.i + (Integral(-C.y, C.x))*C.k
|
| 186 |
+
>>> _.doit()
|
| 187 |
+
C.x**2/2*C.i + (-C.x*C.y)*C.k
|
| 188 |
+
|
| 189 |
+
"""
|
| 190 |
+
if len(region) == 1:
|
| 191 |
+
if isinstance(region[0], ParametricRegion):
|
| 192 |
+
return ParametricIntegral(field, region[0])
|
| 193 |
+
|
| 194 |
+
if isinstance(region[0], ImplicitRegion):
|
| 195 |
+
region = parametric_region_list(region[0])[0]
|
| 196 |
+
return vector_integrate(field, region)
|
| 197 |
+
|
| 198 |
+
if isinstance(region[0], GeometryEntity):
|
| 199 |
+
regions_list = parametric_region_list(region[0])
|
| 200 |
+
|
| 201 |
+
result = 0
|
| 202 |
+
for reg in regions_list:
|
| 203 |
+
result += vector_integrate(field, reg)
|
| 204 |
+
return result
|
| 205 |
+
|
| 206 |
+
return integrate(field, *region)
|
mgm/lib/python3.10/site-packages/sympy/vector/operators.py
ADDED
|
@@ -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|
+
from sympy.core.expr import Expr
|
| 3 |
+
from sympy.core import sympify, S, preorder_traversal
|
| 4 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 5 |
+
from sympy.vector.vector import Vector, VectorMul, VectorAdd, Cross, Dot
|
| 6 |
+
from sympy.core.function import Derivative
|
| 7 |
+
from sympy.core.add import Add
|
| 8 |
+
from sympy.core.mul import Mul
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _get_coord_systems(expr):
|
| 12 |
+
g = preorder_traversal(expr)
|
| 13 |
+
ret = set()
|
| 14 |
+
for i in g:
|
| 15 |
+
if isinstance(i, CoordSys3D):
|
| 16 |
+
ret.add(i)
|
| 17 |
+
g.skip()
|
| 18 |
+
return frozenset(ret)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _split_mul_args_wrt_coordsys(expr):
|
| 22 |
+
d = collections.defaultdict(lambda: S.One)
|
| 23 |
+
for i in expr.args:
|
| 24 |
+
d[_get_coord_systems(i)] *= i
|
| 25 |
+
return list(d.values())
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class Gradient(Expr):
|
| 29 |
+
"""
|
| 30 |
+
Represents unevaluated Gradient.
|
| 31 |
+
|
| 32 |
+
Examples
|
| 33 |
+
========
|
| 34 |
+
|
| 35 |
+
>>> from sympy.vector import CoordSys3D, Gradient
|
| 36 |
+
>>> R = CoordSys3D('R')
|
| 37 |
+
>>> s = R.x*R.y*R.z
|
| 38 |
+
>>> Gradient(s)
|
| 39 |
+
Gradient(R.x*R.y*R.z)
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def __new__(cls, expr):
|
| 44 |
+
expr = sympify(expr)
|
| 45 |
+
obj = Expr.__new__(cls, expr)
|
| 46 |
+
obj._expr = expr
|
| 47 |
+
return obj
|
| 48 |
+
|
| 49 |
+
def doit(self, **hints):
|
| 50 |
+
return gradient(self._expr, doit=True)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class Divergence(Expr):
|
| 54 |
+
"""
|
| 55 |
+
Represents unevaluated Divergence.
|
| 56 |
+
|
| 57 |
+
Examples
|
| 58 |
+
========
|
| 59 |
+
|
| 60 |
+
>>> from sympy.vector import CoordSys3D, Divergence
|
| 61 |
+
>>> R = CoordSys3D('R')
|
| 62 |
+
>>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
|
| 63 |
+
>>> Divergence(v)
|
| 64 |
+
Divergence(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
def __new__(cls, expr):
|
| 69 |
+
expr = sympify(expr)
|
| 70 |
+
obj = Expr.__new__(cls, expr)
|
| 71 |
+
obj._expr = expr
|
| 72 |
+
return obj
|
| 73 |
+
|
| 74 |
+
def doit(self, **hints):
|
| 75 |
+
return divergence(self._expr, doit=True)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class Curl(Expr):
|
| 79 |
+
"""
|
| 80 |
+
Represents unevaluated Curl.
|
| 81 |
+
|
| 82 |
+
Examples
|
| 83 |
+
========
|
| 84 |
+
|
| 85 |
+
>>> from sympy.vector import CoordSys3D, Curl
|
| 86 |
+
>>> R = CoordSys3D('R')
|
| 87 |
+
>>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
|
| 88 |
+
>>> Curl(v)
|
| 89 |
+
Curl(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
|
| 90 |
+
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
def __new__(cls, expr):
|
| 94 |
+
expr = sympify(expr)
|
| 95 |
+
obj = Expr.__new__(cls, expr)
|
| 96 |
+
obj._expr = expr
|
| 97 |
+
return obj
|
| 98 |
+
|
| 99 |
+
def doit(self, **hints):
|
| 100 |
+
return curl(self._expr, doit=True)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def curl(vect, doit=True):
|
| 104 |
+
"""
|
| 105 |
+
Returns the curl of a vector field computed wrt the base scalars
|
| 106 |
+
of the given coordinate system.
|
| 107 |
+
|
| 108 |
+
Parameters
|
| 109 |
+
==========
|
| 110 |
+
|
| 111 |
+
vect : Vector
|
| 112 |
+
The vector operand
|
| 113 |
+
|
| 114 |
+
doit : bool
|
| 115 |
+
If True, the result is returned after calling .doit() on
|
| 116 |
+
each component. Else, the returned expression contains
|
| 117 |
+
Derivative instances
|
| 118 |
+
|
| 119 |
+
Examples
|
| 120 |
+
========
|
| 121 |
+
|
| 122 |
+
>>> from sympy.vector import CoordSys3D, curl
|
| 123 |
+
>>> R = CoordSys3D('R')
|
| 124 |
+
>>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
|
| 125 |
+
>>> curl(v1)
|
| 126 |
+
0
|
| 127 |
+
>>> v2 = R.x*R.y*R.z*R.i
|
| 128 |
+
>>> curl(v2)
|
| 129 |
+
R.x*R.y*R.j + (-R.x*R.z)*R.k
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
coord_sys = _get_coord_systems(vect)
|
| 134 |
+
|
| 135 |
+
if len(coord_sys) == 0:
|
| 136 |
+
return Vector.zero
|
| 137 |
+
elif len(coord_sys) == 1:
|
| 138 |
+
coord_sys = next(iter(coord_sys))
|
| 139 |
+
i, j, k = coord_sys.base_vectors()
|
| 140 |
+
x, y, z = coord_sys.base_scalars()
|
| 141 |
+
h1, h2, h3 = coord_sys.lame_coefficients()
|
| 142 |
+
vectx = vect.dot(i)
|
| 143 |
+
vecty = vect.dot(j)
|
| 144 |
+
vectz = vect.dot(k)
|
| 145 |
+
outvec = Vector.zero
|
| 146 |
+
outvec += (Derivative(vectz * h3, y) -
|
| 147 |
+
Derivative(vecty * h2, z)) * i / (h2 * h3)
|
| 148 |
+
outvec += (Derivative(vectx * h1, z) -
|
| 149 |
+
Derivative(vectz * h3, x)) * j / (h1 * h3)
|
| 150 |
+
outvec += (Derivative(vecty * h2, x) -
|
| 151 |
+
Derivative(vectx * h1, y)) * k / (h2 * h1)
|
| 152 |
+
|
| 153 |
+
if doit:
|
| 154 |
+
return outvec.doit()
|
| 155 |
+
return outvec
|
| 156 |
+
else:
|
| 157 |
+
if isinstance(vect, (Add, VectorAdd)):
|
| 158 |
+
from sympy.vector import express
|
| 159 |
+
try:
|
| 160 |
+
cs = next(iter(coord_sys))
|
| 161 |
+
args = [express(i, cs, variables=True) for i in vect.args]
|
| 162 |
+
except ValueError:
|
| 163 |
+
args = vect.args
|
| 164 |
+
return VectorAdd.fromiter(curl(i, doit=doit) for i in args)
|
| 165 |
+
elif isinstance(vect, (Mul, VectorMul)):
|
| 166 |
+
vector = [i for i in vect.args if isinstance(i, (Vector, Cross, Gradient))][0]
|
| 167 |
+
scalar = Mul.fromiter(i for i in vect.args if not isinstance(i, (Vector, Cross, Gradient)))
|
| 168 |
+
res = Cross(gradient(scalar), vector).doit() + scalar*curl(vector, doit=doit)
|
| 169 |
+
if doit:
|
| 170 |
+
return res.doit()
|
| 171 |
+
return res
|
| 172 |
+
elif isinstance(vect, (Cross, Curl, Gradient)):
|
| 173 |
+
return Curl(vect)
|
| 174 |
+
else:
|
| 175 |
+
raise Curl(vect)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def divergence(vect, doit=True):
|
| 179 |
+
"""
|
| 180 |
+
Returns the divergence of a vector field computed wrt the base
|
| 181 |
+
scalars of the given coordinate system.
|
| 182 |
+
|
| 183 |
+
Parameters
|
| 184 |
+
==========
|
| 185 |
+
|
| 186 |
+
vector : Vector
|
| 187 |
+
The vector operand
|
| 188 |
+
|
| 189 |
+
doit : bool
|
| 190 |
+
If True, the result is returned after calling .doit() on
|
| 191 |
+
each component. Else, the returned expression contains
|
| 192 |
+
Derivative instances
|
| 193 |
+
|
| 194 |
+
Examples
|
| 195 |
+
========
|
| 196 |
+
|
| 197 |
+
>>> from sympy.vector import CoordSys3D, divergence
|
| 198 |
+
>>> R = CoordSys3D('R')
|
| 199 |
+
>>> v1 = R.x*R.y*R.z * (R.i+R.j+R.k)
|
| 200 |
+
|
| 201 |
+
>>> divergence(v1)
|
| 202 |
+
R.x*R.y + R.x*R.z + R.y*R.z
|
| 203 |
+
>>> v2 = 2*R.y*R.z*R.j
|
| 204 |
+
>>> divergence(v2)
|
| 205 |
+
2*R.z
|
| 206 |
+
|
| 207 |
+
"""
|
| 208 |
+
coord_sys = _get_coord_systems(vect)
|
| 209 |
+
if len(coord_sys) == 0:
|
| 210 |
+
return S.Zero
|
| 211 |
+
elif len(coord_sys) == 1:
|
| 212 |
+
if isinstance(vect, (Cross, Curl, Gradient)):
|
| 213 |
+
return Divergence(vect)
|
| 214 |
+
# TODO: is case of many coord systems, this gets a random one:
|
| 215 |
+
coord_sys = next(iter(coord_sys))
|
| 216 |
+
i, j, k = coord_sys.base_vectors()
|
| 217 |
+
x, y, z = coord_sys.base_scalars()
|
| 218 |
+
h1, h2, h3 = coord_sys.lame_coefficients()
|
| 219 |
+
vx = _diff_conditional(vect.dot(i), x, h2, h3) \
|
| 220 |
+
/ (h1 * h2 * h3)
|
| 221 |
+
vy = _diff_conditional(vect.dot(j), y, h3, h1) \
|
| 222 |
+
/ (h1 * h2 * h3)
|
| 223 |
+
vz = _diff_conditional(vect.dot(k), z, h1, h2) \
|
| 224 |
+
/ (h1 * h2 * h3)
|
| 225 |
+
res = vx + vy + vz
|
| 226 |
+
if doit:
|
| 227 |
+
return res.doit()
|
| 228 |
+
return res
|
| 229 |
+
else:
|
| 230 |
+
if isinstance(vect, (Add, VectorAdd)):
|
| 231 |
+
return Add.fromiter(divergence(i, doit=doit) for i in vect.args)
|
| 232 |
+
elif isinstance(vect, (Mul, VectorMul)):
|
| 233 |
+
vector = [i for i in vect.args if isinstance(i, (Vector, Cross, Gradient))][0]
|
| 234 |
+
scalar = Mul.fromiter(i for i in vect.args if not isinstance(i, (Vector, Cross, Gradient)))
|
| 235 |
+
res = Dot(vector, gradient(scalar)) + scalar*divergence(vector, doit=doit)
|
| 236 |
+
if doit:
|
| 237 |
+
return res.doit()
|
| 238 |
+
return res
|
| 239 |
+
elif isinstance(vect, (Cross, Curl, Gradient)):
|
| 240 |
+
return Divergence(vect)
|
| 241 |
+
else:
|
| 242 |
+
raise Divergence(vect)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def gradient(scalar_field, doit=True):
|
| 246 |
+
"""
|
| 247 |
+
Returns the vector gradient of a scalar field computed wrt the
|
| 248 |
+
base scalars of the given coordinate system.
|
| 249 |
+
|
| 250 |
+
Parameters
|
| 251 |
+
==========
|
| 252 |
+
|
| 253 |
+
scalar_field : SymPy Expr
|
| 254 |
+
The scalar field to compute the gradient of
|
| 255 |
+
|
| 256 |
+
doit : bool
|
| 257 |
+
If True, the result is returned after calling .doit() on
|
| 258 |
+
each component. Else, the returned expression contains
|
| 259 |
+
Derivative instances
|
| 260 |
+
|
| 261 |
+
Examples
|
| 262 |
+
========
|
| 263 |
+
|
| 264 |
+
>>> from sympy.vector import CoordSys3D, gradient
|
| 265 |
+
>>> R = CoordSys3D('R')
|
| 266 |
+
>>> s1 = R.x*R.y*R.z
|
| 267 |
+
>>> gradient(s1)
|
| 268 |
+
R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
|
| 269 |
+
>>> s2 = 5*R.x**2*R.z
|
| 270 |
+
>>> gradient(s2)
|
| 271 |
+
10*R.x*R.z*R.i + 5*R.x**2*R.k
|
| 272 |
+
|
| 273 |
+
"""
|
| 274 |
+
coord_sys = _get_coord_systems(scalar_field)
|
| 275 |
+
|
| 276 |
+
if len(coord_sys) == 0:
|
| 277 |
+
return Vector.zero
|
| 278 |
+
elif len(coord_sys) == 1:
|
| 279 |
+
coord_sys = next(iter(coord_sys))
|
| 280 |
+
h1, h2, h3 = coord_sys.lame_coefficients()
|
| 281 |
+
i, j, k = coord_sys.base_vectors()
|
| 282 |
+
x, y, z = coord_sys.base_scalars()
|
| 283 |
+
vx = Derivative(scalar_field, x) / h1
|
| 284 |
+
vy = Derivative(scalar_field, y) / h2
|
| 285 |
+
vz = Derivative(scalar_field, z) / h3
|
| 286 |
+
|
| 287 |
+
if doit:
|
| 288 |
+
return (vx * i + vy * j + vz * k).doit()
|
| 289 |
+
return vx * i + vy * j + vz * k
|
| 290 |
+
else:
|
| 291 |
+
if isinstance(scalar_field, (Add, VectorAdd)):
|
| 292 |
+
return VectorAdd.fromiter(gradient(i) for i in scalar_field.args)
|
| 293 |
+
if isinstance(scalar_field, (Mul, VectorMul)):
|
| 294 |
+
s = _split_mul_args_wrt_coordsys(scalar_field)
|
| 295 |
+
return VectorAdd.fromiter(scalar_field / i * gradient(i) for i in s)
|
| 296 |
+
return Gradient(scalar_field)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
class Laplacian(Expr):
|
| 300 |
+
"""
|
| 301 |
+
Represents unevaluated Laplacian.
|
| 302 |
+
|
| 303 |
+
Examples
|
| 304 |
+
========
|
| 305 |
+
|
| 306 |
+
>>> from sympy.vector import CoordSys3D, Laplacian
|
| 307 |
+
>>> R = CoordSys3D('R')
|
| 308 |
+
>>> v = 3*R.x**3*R.y**2*R.z**3
|
| 309 |
+
>>> Laplacian(v)
|
| 310 |
+
Laplacian(3*R.x**3*R.y**2*R.z**3)
|
| 311 |
+
|
| 312 |
+
"""
|
| 313 |
+
|
| 314 |
+
def __new__(cls, expr):
|
| 315 |
+
expr = sympify(expr)
|
| 316 |
+
obj = Expr.__new__(cls, expr)
|
| 317 |
+
obj._expr = expr
|
| 318 |
+
return obj
|
| 319 |
+
|
| 320 |
+
def doit(self, **hints):
|
| 321 |
+
from sympy.vector.functions import laplacian
|
| 322 |
+
return laplacian(self._expr)
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
def _diff_conditional(expr, base_scalar, coeff_1, coeff_2):
|
| 326 |
+
"""
|
| 327 |
+
First re-expresses expr in the system that base_scalar belongs to.
|
| 328 |
+
If base_scalar appears in the re-expressed form, differentiates
|
| 329 |
+
it wrt base_scalar.
|
| 330 |
+
Else, returns 0
|
| 331 |
+
"""
|
| 332 |
+
from sympy.vector.functions import express
|
| 333 |
+
new_expr = express(expr, base_scalar.system, variables=True)
|
| 334 |
+
arg = coeff_1 * coeff_2 * new_expr
|
| 335 |
+
return Derivative(arg, base_scalar) if arg else S.Zero
|
mgm/lib/python3.10/site-packages/sympy/vector/orienters.py
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.basic import Basic
|
| 2 |
+
from sympy.core.sympify import sympify
|
| 3 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 4 |
+
from sympy.matrices.dense import (eye, rot_axis1, rot_axis2, rot_axis3)
|
| 5 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 6 |
+
from sympy.core.cache import cacheit
|
| 7 |
+
from sympy.core.symbol import Str
|
| 8 |
+
import sympy.vector
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Orienter(Basic):
|
| 12 |
+
"""
|
| 13 |
+
Super-class for all orienter classes.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def rotation_matrix(self):
|
| 17 |
+
"""
|
| 18 |
+
The rotation matrix corresponding to this orienter
|
| 19 |
+
instance.
|
| 20 |
+
"""
|
| 21 |
+
return self._parent_orient
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class AxisOrienter(Orienter):
|
| 25 |
+
"""
|
| 26 |
+
Class to denote an axis orienter.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def __new__(cls, angle, axis):
|
| 30 |
+
if not isinstance(axis, sympy.vector.Vector):
|
| 31 |
+
raise TypeError("axis should be a Vector")
|
| 32 |
+
angle = sympify(angle)
|
| 33 |
+
|
| 34 |
+
obj = super().__new__(cls, angle, axis)
|
| 35 |
+
obj._angle = angle
|
| 36 |
+
obj._axis = axis
|
| 37 |
+
|
| 38 |
+
return obj
|
| 39 |
+
|
| 40 |
+
def __init__(self, angle, axis):
|
| 41 |
+
"""
|
| 42 |
+
Axis rotation is a rotation about an arbitrary axis by
|
| 43 |
+
some angle. The angle is supplied as a SymPy expr scalar, and
|
| 44 |
+
the axis is supplied as a Vector.
|
| 45 |
+
|
| 46 |
+
Parameters
|
| 47 |
+
==========
|
| 48 |
+
|
| 49 |
+
angle : Expr
|
| 50 |
+
The angle by which the new system is to be rotated
|
| 51 |
+
|
| 52 |
+
axis : Vector
|
| 53 |
+
The axis around which the rotation has to be performed
|
| 54 |
+
|
| 55 |
+
Examples
|
| 56 |
+
========
|
| 57 |
+
|
| 58 |
+
>>> from sympy.vector import CoordSys3D
|
| 59 |
+
>>> from sympy import symbols
|
| 60 |
+
>>> q1 = symbols('q1')
|
| 61 |
+
>>> N = CoordSys3D('N')
|
| 62 |
+
>>> from sympy.vector import AxisOrienter
|
| 63 |
+
>>> orienter = AxisOrienter(q1, N.i + 2 * N.j)
|
| 64 |
+
>>> B = N.orient_new('B', (orienter, ))
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
# Dummy initializer for docstrings
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
@cacheit
|
| 71 |
+
def rotation_matrix(self, system):
|
| 72 |
+
"""
|
| 73 |
+
The rotation matrix corresponding to this orienter
|
| 74 |
+
instance.
|
| 75 |
+
|
| 76 |
+
Parameters
|
| 77 |
+
==========
|
| 78 |
+
|
| 79 |
+
system : CoordSys3D
|
| 80 |
+
The coordinate system wrt which the rotation matrix
|
| 81 |
+
is to be computed
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
axis = sympy.vector.express(self.axis, system).normalize()
|
| 85 |
+
axis = axis.to_matrix(system)
|
| 86 |
+
theta = self.angle
|
| 87 |
+
parent_orient = ((eye(3) - axis * axis.T) * cos(theta) +
|
| 88 |
+
Matrix([[0, -axis[2], axis[1]],
|
| 89 |
+
[axis[2], 0, -axis[0]],
|
| 90 |
+
[-axis[1], axis[0], 0]]) * sin(theta) +
|
| 91 |
+
axis * axis.T)
|
| 92 |
+
parent_orient = parent_orient.T
|
| 93 |
+
return parent_orient
|
| 94 |
+
|
| 95 |
+
@property
|
| 96 |
+
def angle(self):
|
| 97 |
+
return self._angle
|
| 98 |
+
|
| 99 |
+
@property
|
| 100 |
+
def axis(self):
|
| 101 |
+
return self._axis
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
class ThreeAngleOrienter(Orienter):
|
| 105 |
+
"""
|
| 106 |
+
Super-class for Body and Space orienters.
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
def __new__(cls, angle1, angle2, angle3, rot_order):
|
| 110 |
+
if isinstance(rot_order, Str):
|
| 111 |
+
rot_order = rot_order.name
|
| 112 |
+
|
| 113 |
+
approved_orders = ('123', '231', '312', '132', '213',
|
| 114 |
+
'321', '121', '131', '212', '232',
|
| 115 |
+
'313', '323', '')
|
| 116 |
+
original_rot_order = rot_order
|
| 117 |
+
rot_order = str(rot_order).upper()
|
| 118 |
+
if not (len(rot_order) == 3):
|
| 119 |
+
raise TypeError('rot_order should be a str of length 3')
|
| 120 |
+
rot_order = [i.replace('X', '1') for i in rot_order]
|
| 121 |
+
rot_order = [i.replace('Y', '2') for i in rot_order]
|
| 122 |
+
rot_order = [i.replace('Z', '3') for i in rot_order]
|
| 123 |
+
rot_order = ''.join(rot_order)
|
| 124 |
+
if rot_order not in approved_orders:
|
| 125 |
+
raise TypeError('Invalid rot_type parameter')
|
| 126 |
+
a1 = int(rot_order[0])
|
| 127 |
+
a2 = int(rot_order[1])
|
| 128 |
+
a3 = int(rot_order[2])
|
| 129 |
+
angle1 = sympify(angle1)
|
| 130 |
+
angle2 = sympify(angle2)
|
| 131 |
+
angle3 = sympify(angle3)
|
| 132 |
+
if cls._in_order:
|
| 133 |
+
parent_orient = (_rot(a1, angle1) *
|
| 134 |
+
_rot(a2, angle2) *
|
| 135 |
+
_rot(a3, angle3))
|
| 136 |
+
else:
|
| 137 |
+
parent_orient = (_rot(a3, angle3) *
|
| 138 |
+
_rot(a2, angle2) *
|
| 139 |
+
_rot(a1, angle1))
|
| 140 |
+
parent_orient = parent_orient.T
|
| 141 |
+
|
| 142 |
+
obj = super().__new__(
|
| 143 |
+
cls, angle1, angle2, angle3, Str(rot_order))
|
| 144 |
+
obj._angle1 = angle1
|
| 145 |
+
obj._angle2 = angle2
|
| 146 |
+
obj._angle3 = angle3
|
| 147 |
+
obj._rot_order = original_rot_order
|
| 148 |
+
obj._parent_orient = parent_orient
|
| 149 |
+
|
| 150 |
+
return obj
|
| 151 |
+
|
| 152 |
+
@property
|
| 153 |
+
def angle1(self):
|
| 154 |
+
return self._angle1
|
| 155 |
+
|
| 156 |
+
@property
|
| 157 |
+
def angle2(self):
|
| 158 |
+
return self._angle2
|
| 159 |
+
|
| 160 |
+
@property
|
| 161 |
+
def angle3(self):
|
| 162 |
+
return self._angle3
|
| 163 |
+
|
| 164 |
+
@property
|
| 165 |
+
def rot_order(self):
|
| 166 |
+
return self._rot_order
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
class BodyOrienter(ThreeAngleOrienter):
|
| 170 |
+
"""
|
| 171 |
+
Class to denote a body-orienter.
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
_in_order = True
|
| 175 |
+
|
| 176 |
+
def __new__(cls, angle1, angle2, angle3, rot_order):
|
| 177 |
+
obj = ThreeAngleOrienter.__new__(cls, angle1, angle2, angle3,
|
| 178 |
+
rot_order)
|
| 179 |
+
return obj
|
| 180 |
+
|
| 181 |
+
def __init__(self, angle1, angle2, angle3, rot_order):
|
| 182 |
+
"""
|
| 183 |
+
Body orientation takes this coordinate system through three
|
| 184 |
+
successive simple rotations.
|
| 185 |
+
|
| 186 |
+
Body fixed rotations include both Euler Angles and
|
| 187 |
+
Tait-Bryan Angles, see https://en.wikipedia.org/wiki/Euler_angles.
|
| 188 |
+
|
| 189 |
+
Parameters
|
| 190 |
+
==========
|
| 191 |
+
|
| 192 |
+
angle1, angle2, angle3 : Expr
|
| 193 |
+
Three successive angles to rotate the coordinate system by
|
| 194 |
+
|
| 195 |
+
rotation_order : string
|
| 196 |
+
String defining the order of axes for rotation
|
| 197 |
+
|
| 198 |
+
Examples
|
| 199 |
+
========
|
| 200 |
+
|
| 201 |
+
>>> from sympy.vector import CoordSys3D, BodyOrienter
|
| 202 |
+
>>> from sympy import symbols
|
| 203 |
+
>>> q1, q2, q3 = symbols('q1 q2 q3')
|
| 204 |
+
>>> N = CoordSys3D('N')
|
| 205 |
+
|
| 206 |
+
A 'Body' fixed rotation is described by three angles and
|
| 207 |
+
three body-fixed rotation axes. To orient a coordinate system D
|
| 208 |
+
with respect to N, each sequential rotation is always about
|
| 209 |
+
the orthogonal unit vectors fixed to D. For example, a '123'
|
| 210 |
+
rotation will specify rotations about N.i, then D.j, then
|
| 211 |
+
D.k. (Initially, D.i is same as N.i)
|
| 212 |
+
Therefore,
|
| 213 |
+
|
| 214 |
+
>>> body_orienter = BodyOrienter(q1, q2, q3, '123')
|
| 215 |
+
>>> D = N.orient_new('D', (body_orienter, ))
|
| 216 |
+
|
| 217 |
+
is same as
|
| 218 |
+
|
| 219 |
+
>>> from sympy.vector import AxisOrienter
|
| 220 |
+
>>> axis_orienter1 = AxisOrienter(q1, N.i)
|
| 221 |
+
>>> D = N.orient_new('D', (axis_orienter1, ))
|
| 222 |
+
>>> axis_orienter2 = AxisOrienter(q2, D.j)
|
| 223 |
+
>>> D = D.orient_new('D', (axis_orienter2, ))
|
| 224 |
+
>>> axis_orienter3 = AxisOrienter(q3, D.k)
|
| 225 |
+
>>> D = D.orient_new('D', (axis_orienter3, ))
|
| 226 |
+
|
| 227 |
+
Acceptable rotation orders are of length 3, expressed in XYZ or
|
| 228 |
+
123, and cannot have a rotation about about an axis twice in a row.
|
| 229 |
+
|
| 230 |
+
>>> body_orienter1 = BodyOrienter(q1, q2, q3, '123')
|
| 231 |
+
>>> body_orienter2 = BodyOrienter(q1, q2, 0, 'ZXZ')
|
| 232 |
+
>>> body_orienter3 = BodyOrienter(0, 0, 0, 'XYX')
|
| 233 |
+
|
| 234 |
+
"""
|
| 235 |
+
# Dummy initializer for docstrings
|
| 236 |
+
pass
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
class SpaceOrienter(ThreeAngleOrienter):
|
| 240 |
+
"""
|
| 241 |
+
Class to denote a space-orienter.
|
| 242 |
+
"""
|
| 243 |
+
|
| 244 |
+
_in_order = False
|
| 245 |
+
|
| 246 |
+
def __new__(cls, angle1, angle2, angle3, rot_order):
|
| 247 |
+
obj = ThreeAngleOrienter.__new__(cls, angle1, angle2, angle3,
|
| 248 |
+
rot_order)
|
| 249 |
+
return obj
|
| 250 |
+
|
| 251 |
+
def __init__(self, angle1, angle2, angle3, rot_order):
|
| 252 |
+
"""
|
| 253 |
+
Space rotation is similar to Body rotation, but the rotations
|
| 254 |
+
are applied in the opposite order.
|
| 255 |
+
|
| 256 |
+
Parameters
|
| 257 |
+
==========
|
| 258 |
+
|
| 259 |
+
angle1, angle2, angle3 : Expr
|
| 260 |
+
Three successive angles to rotate the coordinate system by
|
| 261 |
+
|
| 262 |
+
rotation_order : string
|
| 263 |
+
String defining the order of axes for rotation
|
| 264 |
+
|
| 265 |
+
See Also
|
| 266 |
+
========
|
| 267 |
+
|
| 268 |
+
BodyOrienter : Orienter to orient systems wrt Euler angles.
|
| 269 |
+
|
| 270 |
+
Examples
|
| 271 |
+
========
|
| 272 |
+
|
| 273 |
+
>>> from sympy.vector import CoordSys3D, SpaceOrienter
|
| 274 |
+
>>> from sympy import symbols
|
| 275 |
+
>>> q1, q2, q3 = symbols('q1 q2 q3')
|
| 276 |
+
>>> N = CoordSys3D('N')
|
| 277 |
+
|
| 278 |
+
To orient a coordinate system D with respect to N, each
|
| 279 |
+
sequential rotation is always about N's orthogonal unit vectors.
|
| 280 |
+
For example, a '123' rotation will specify rotations about
|
| 281 |
+
N.i, then N.j, then N.k.
|
| 282 |
+
Therefore,
|
| 283 |
+
|
| 284 |
+
>>> space_orienter = SpaceOrienter(q1, q2, q3, '312')
|
| 285 |
+
>>> D = N.orient_new('D', (space_orienter, ))
|
| 286 |
+
|
| 287 |
+
is same as
|
| 288 |
+
|
| 289 |
+
>>> from sympy.vector import AxisOrienter
|
| 290 |
+
>>> axis_orienter1 = AxisOrienter(q1, N.i)
|
| 291 |
+
>>> B = N.orient_new('B', (axis_orienter1, ))
|
| 292 |
+
>>> axis_orienter2 = AxisOrienter(q2, N.j)
|
| 293 |
+
>>> C = B.orient_new('C', (axis_orienter2, ))
|
| 294 |
+
>>> axis_orienter3 = AxisOrienter(q3, N.k)
|
| 295 |
+
>>> D = C.orient_new('C', (axis_orienter3, ))
|
| 296 |
+
|
| 297 |
+
"""
|
| 298 |
+
# Dummy initializer for docstrings
|
| 299 |
+
pass
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
class QuaternionOrienter(Orienter):
|
| 303 |
+
"""
|
| 304 |
+
Class to denote a quaternion-orienter.
|
| 305 |
+
"""
|
| 306 |
+
|
| 307 |
+
def __new__(cls, q0, q1, q2, q3):
|
| 308 |
+
q0 = sympify(q0)
|
| 309 |
+
q1 = sympify(q1)
|
| 310 |
+
q2 = sympify(q2)
|
| 311 |
+
q3 = sympify(q3)
|
| 312 |
+
parent_orient = (Matrix([[q0 ** 2 + q1 ** 2 - q2 ** 2 -
|
| 313 |
+
q3 ** 2,
|
| 314 |
+
2 * (q1 * q2 - q0 * q3),
|
| 315 |
+
2 * (q0 * q2 + q1 * q3)],
|
| 316 |
+
[2 * (q1 * q2 + q0 * q3),
|
| 317 |
+
q0 ** 2 - q1 ** 2 +
|
| 318 |
+
q2 ** 2 - q3 ** 2,
|
| 319 |
+
2 * (q2 * q3 - q0 * q1)],
|
| 320 |
+
[2 * (q1 * q3 - q0 * q2),
|
| 321 |
+
2 * (q0 * q1 + q2 * q3),
|
| 322 |
+
q0 ** 2 - q1 ** 2 -
|
| 323 |
+
q2 ** 2 + q3 ** 2]]))
|
| 324 |
+
parent_orient = parent_orient.T
|
| 325 |
+
|
| 326 |
+
obj = super().__new__(cls, q0, q1, q2, q3)
|
| 327 |
+
obj._q0 = q0
|
| 328 |
+
obj._q1 = q1
|
| 329 |
+
obj._q2 = q2
|
| 330 |
+
obj._q3 = q3
|
| 331 |
+
obj._parent_orient = parent_orient
|
| 332 |
+
|
| 333 |
+
return obj
|
| 334 |
+
|
| 335 |
+
def __init__(self, angle1, angle2, angle3, rot_order):
|
| 336 |
+
"""
|
| 337 |
+
Quaternion orientation orients the new CoordSys3D with
|
| 338 |
+
Quaternions, defined as a finite rotation about lambda, a unit
|
| 339 |
+
vector, by some amount theta.
|
| 340 |
+
|
| 341 |
+
This orientation is described by four parameters:
|
| 342 |
+
|
| 343 |
+
q0 = cos(theta/2)
|
| 344 |
+
|
| 345 |
+
q1 = lambda_x sin(theta/2)
|
| 346 |
+
|
| 347 |
+
q2 = lambda_y sin(theta/2)
|
| 348 |
+
|
| 349 |
+
q3 = lambda_z sin(theta/2)
|
| 350 |
+
|
| 351 |
+
Quaternion does not take in a rotation order.
|
| 352 |
+
|
| 353 |
+
Parameters
|
| 354 |
+
==========
|
| 355 |
+
|
| 356 |
+
q0, q1, q2, q3 : Expr
|
| 357 |
+
The quaternions to rotate the coordinate system by
|
| 358 |
+
|
| 359 |
+
Examples
|
| 360 |
+
========
|
| 361 |
+
|
| 362 |
+
>>> from sympy.vector import CoordSys3D
|
| 363 |
+
>>> from sympy import symbols
|
| 364 |
+
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 365 |
+
>>> N = CoordSys3D('N')
|
| 366 |
+
>>> from sympy.vector import QuaternionOrienter
|
| 367 |
+
>>> q_orienter = QuaternionOrienter(q0, q1, q2, q3)
|
| 368 |
+
>>> B = N.orient_new('B', (q_orienter, ))
|
| 369 |
+
|
| 370 |
+
"""
|
| 371 |
+
# Dummy initializer for docstrings
|
| 372 |
+
pass
|
| 373 |
+
|
| 374 |
+
@property
|
| 375 |
+
def q0(self):
|
| 376 |
+
return self._q0
|
| 377 |
+
|
| 378 |
+
@property
|
| 379 |
+
def q1(self):
|
| 380 |
+
return self._q1
|
| 381 |
+
|
| 382 |
+
@property
|
| 383 |
+
def q2(self):
|
| 384 |
+
return self._q2
|
| 385 |
+
|
| 386 |
+
@property
|
| 387 |
+
def q3(self):
|
| 388 |
+
return self._q3
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def _rot(axis, angle):
|
| 392 |
+
"""DCM for simple axis 1, 2 or 3 rotations. """
|
| 393 |
+
if axis == 1:
|
| 394 |
+
return Matrix(rot_axis1(angle).T)
|
| 395 |
+
elif axis == 2:
|
| 396 |
+
return Matrix(rot_axis2(angle).T)
|
| 397 |
+
elif axis == 3:
|
| 398 |
+
return Matrix(rot_axis3(angle).T)
|
mgm/lib/python3.10/site-packages/sympy/vector/parametricregion.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import singledispatch
|
| 2 |
+
from sympy.core.numbers import pi
|
| 3 |
+
from sympy.functions.elementary.trigonometric import tan
|
| 4 |
+
from sympy.simplify import trigsimp
|
| 5 |
+
from sympy.core import Basic, Tuple
|
| 6 |
+
from sympy.core.symbol import _symbol
|
| 7 |
+
from sympy.solvers import solve
|
| 8 |
+
from sympy.geometry import Point, Segment, Curve, Ellipse, Polygon
|
| 9 |
+
from sympy.vector import ImplicitRegion
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ParametricRegion(Basic):
|
| 13 |
+
"""
|
| 14 |
+
Represents a parametric region in space.
|
| 15 |
+
|
| 16 |
+
Examples
|
| 17 |
+
========
|
| 18 |
+
|
| 19 |
+
>>> from sympy import cos, sin, pi
|
| 20 |
+
>>> from sympy.abc import r, theta, t, a, b, x, y
|
| 21 |
+
>>> from sympy.vector import ParametricRegion
|
| 22 |
+
|
| 23 |
+
>>> ParametricRegion((t, t**2), (t, -1, 2))
|
| 24 |
+
ParametricRegion((t, t**2), (t, -1, 2))
|
| 25 |
+
>>> ParametricRegion((x, y), (x, 3, 4), (y, 5, 6))
|
| 26 |
+
ParametricRegion((x, y), (x, 3, 4), (y, 5, 6))
|
| 27 |
+
>>> ParametricRegion((r*cos(theta), r*sin(theta)), (r, -2, 2), (theta, 0, pi))
|
| 28 |
+
ParametricRegion((r*cos(theta), r*sin(theta)), (r, -2, 2), (theta, 0, pi))
|
| 29 |
+
>>> ParametricRegion((a*cos(t), b*sin(t)), t)
|
| 30 |
+
ParametricRegion((a*cos(t), b*sin(t)), t)
|
| 31 |
+
|
| 32 |
+
>>> circle = ParametricRegion((r*cos(theta), r*sin(theta)), r, (theta, 0, pi))
|
| 33 |
+
>>> circle.parameters
|
| 34 |
+
(r, theta)
|
| 35 |
+
>>> circle.definition
|
| 36 |
+
(r*cos(theta), r*sin(theta))
|
| 37 |
+
>>> circle.limits
|
| 38 |
+
{theta: (0, pi)}
|
| 39 |
+
|
| 40 |
+
Dimension of a parametric region determines whether a region is a curve, surface
|
| 41 |
+
or volume region. It does not represent its dimensions in space.
|
| 42 |
+
|
| 43 |
+
>>> circle.dimensions
|
| 44 |
+
1
|
| 45 |
+
|
| 46 |
+
Parameters
|
| 47 |
+
==========
|
| 48 |
+
|
| 49 |
+
definition : tuple to define base scalars in terms of parameters.
|
| 50 |
+
|
| 51 |
+
bounds : Parameter or a tuple of length 3 to define parameter and corresponding lower and upper bound.
|
| 52 |
+
|
| 53 |
+
"""
|
| 54 |
+
def __new__(cls, definition, *bounds):
|
| 55 |
+
parameters = ()
|
| 56 |
+
limits = {}
|
| 57 |
+
|
| 58 |
+
if not isinstance(bounds, Tuple):
|
| 59 |
+
bounds = Tuple(*bounds)
|
| 60 |
+
|
| 61 |
+
for bound in bounds:
|
| 62 |
+
if isinstance(bound, (tuple, Tuple)):
|
| 63 |
+
if len(bound) != 3:
|
| 64 |
+
raise ValueError("Tuple should be in the form (parameter, lowerbound, upperbound)")
|
| 65 |
+
parameters += (bound[0],)
|
| 66 |
+
limits[bound[0]] = (bound[1], bound[2])
|
| 67 |
+
else:
|
| 68 |
+
parameters += (bound,)
|
| 69 |
+
|
| 70 |
+
if not isinstance(definition, (tuple, Tuple)):
|
| 71 |
+
definition = (definition,)
|
| 72 |
+
|
| 73 |
+
obj = super().__new__(cls, Tuple(*definition), *bounds)
|
| 74 |
+
obj._parameters = parameters
|
| 75 |
+
obj._limits = limits
|
| 76 |
+
|
| 77 |
+
return obj
|
| 78 |
+
|
| 79 |
+
@property
|
| 80 |
+
def definition(self):
|
| 81 |
+
return self.args[0]
|
| 82 |
+
|
| 83 |
+
@property
|
| 84 |
+
def limits(self):
|
| 85 |
+
return self._limits
|
| 86 |
+
|
| 87 |
+
@property
|
| 88 |
+
def parameters(self):
|
| 89 |
+
return self._parameters
|
| 90 |
+
|
| 91 |
+
@property
|
| 92 |
+
def dimensions(self):
|
| 93 |
+
return len(self.limits)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@singledispatch
|
| 97 |
+
def parametric_region_list(reg):
|
| 98 |
+
"""
|
| 99 |
+
Returns a list of ParametricRegion objects representing the geometric region.
|
| 100 |
+
|
| 101 |
+
Examples
|
| 102 |
+
========
|
| 103 |
+
|
| 104 |
+
>>> from sympy.abc import t
|
| 105 |
+
>>> from sympy.vector import parametric_region_list
|
| 106 |
+
>>> from sympy.geometry import Point, Curve, Ellipse, Segment, Polygon
|
| 107 |
+
|
| 108 |
+
>>> p = Point(2, 5)
|
| 109 |
+
>>> parametric_region_list(p)
|
| 110 |
+
[ParametricRegion((2, 5))]
|
| 111 |
+
|
| 112 |
+
>>> c = Curve((t**3, 4*t), (t, -3, 4))
|
| 113 |
+
>>> parametric_region_list(c)
|
| 114 |
+
[ParametricRegion((t**3, 4*t), (t, -3, 4))]
|
| 115 |
+
|
| 116 |
+
>>> e = Ellipse(Point(1, 3), 2, 3)
|
| 117 |
+
>>> parametric_region_list(e)
|
| 118 |
+
[ParametricRegion((2*cos(t) + 1, 3*sin(t) + 3), (t, 0, 2*pi))]
|
| 119 |
+
|
| 120 |
+
>>> s = Segment(Point(1, 3), Point(2, 6))
|
| 121 |
+
>>> parametric_region_list(s)
|
| 122 |
+
[ParametricRegion((t + 1, 3*t + 3), (t, 0, 1))]
|
| 123 |
+
|
| 124 |
+
>>> p1, p2, p3, p4 = [(0, 1), (2, -3), (5, 3), (-2, 3)]
|
| 125 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
| 126 |
+
>>> parametric_region_list(poly)
|
| 127 |
+
[ParametricRegion((2*t, 1 - 4*t), (t, 0, 1)), ParametricRegion((3*t + 2, 6*t - 3), (t, 0, 1)),\
|
| 128 |
+
ParametricRegion((5 - 7*t, 3), (t, 0, 1)), ParametricRegion((2*t - 2, 3 - 2*t), (t, 0, 1))]
|
| 129 |
+
|
| 130 |
+
"""
|
| 131 |
+
raise ValueError("SymPy cannot determine parametric representation of the region.")
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
@parametric_region_list.register(Point)
|
| 135 |
+
def _(obj):
|
| 136 |
+
return [ParametricRegion(obj.args)]
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@parametric_region_list.register(Curve) # type: ignore
|
| 140 |
+
def _(obj):
|
| 141 |
+
definition = obj.arbitrary_point(obj.parameter).args
|
| 142 |
+
bounds = obj.limits
|
| 143 |
+
return [ParametricRegion(definition, bounds)]
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@parametric_region_list.register(Ellipse) # type: ignore
|
| 147 |
+
def _(obj, parameter='t'):
|
| 148 |
+
definition = obj.arbitrary_point(parameter).args
|
| 149 |
+
t = _symbol(parameter, real=True)
|
| 150 |
+
bounds = (t, 0, 2*pi)
|
| 151 |
+
return [ParametricRegion(definition, bounds)]
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
@parametric_region_list.register(Segment) # type: ignore
|
| 155 |
+
def _(obj, parameter='t'):
|
| 156 |
+
t = _symbol(parameter, real=True)
|
| 157 |
+
definition = obj.arbitrary_point(t).args
|
| 158 |
+
|
| 159 |
+
for i in range(0, 3):
|
| 160 |
+
lower_bound = solve(definition[i] - obj.points[0].args[i], t)
|
| 161 |
+
upper_bound = solve(definition[i] - obj.points[1].args[i], t)
|
| 162 |
+
|
| 163 |
+
if len(lower_bound) == 1 and len(upper_bound) == 1:
|
| 164 |
+
bounds = t, lower_bound[0], upper_bound[0]
|
| 165 |
+
break
|
| 166 |
+
|
| 167 |
+
definition_tuple = obj.arbitrary_point(parameter).args
|
| 168 |
+
return [ParametricRegion(definition_tuple, bounds)]
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@parametric_region_list.register(Polygon) # type: ignore
|
| 172 |
+
def _(obj, parameter='t'):
|
| 173 |
+
l = [parametric_region_list(side, parameter)[0] for side in obj.sides]
|
| 174 |
+
return l
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@parametric_region_list.register(ImplicitRegion) # type: ignore
|
| 178 |
+
def _(obj, parameters=('t', 's')):
|
| 179 |
+
definition = obj.rational_parametrization(parameters)
|
| 180 |
+
bounds = []
|
| 181 |
+
|
| 182 |
+
for i in range(len(obj.variables) - 1):
|
| 183 |
+
# Each parameter is replaced by its tangent to simplify intergation
|
| 184 |
+
parameter = _symbol(parameters[i], real=True)
|
| 185 |
+
definition = [trigsimp(elem.subs(parameter, tan(parameter/2))) for elem in definition]
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.basic import Basic
|
| 2 |
+
from sympy.core.symbol import Str
|
| 3 |
+
from sympy.vector.vector import Vector
|
| 4 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 5 |
+
from sympy.vector.functions import _path
|
| 6 |
+
from sympy.core.cache import cacheit
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Point(Basic):
|
| 10 |
+
"""
|
| 11 |
+
Represents a point in 3-D space.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def __new__(cls, name, position=Vector.zero, parent_point=None):
|
| 15 |
+
name = str(name)
|
| 16 |
+
# Check the args first
|
| 17 |
+
if not isinstance(position, Vector):
|
| 18 |
+
raise TypeError(
|
| 19 |
+
"position should be an instance of Vector, not %s" % type(
|
| 20 |
+
position))
|
| 21 |
+
if (not isinstance(parent_point, Point) and
|
| 22 |
+
parent_point is not None):
|
| 23 |
+
raise TypeError(
|
| 24 |
+
"parent_point should be an instance of Point, not %s" % type(
|
| 25 |
+
parent_point))
|
| 26 |
+
# Super class construction
|
| 27 |
+
if parent_point is None:
|
| 28 |
+
obj = super().__new__(cls, Str(name), position)
|
| 29 |
+
else:
|
| 30 |
+
obj = super().__new__(cls, Str(name), position, parent_point)
|
| 31 |
+
# Decide the object parameters
|
| 32 |
+
obj._name = name
|
| 33 |
+
obj._pos = position
|
| 34 |
+
if parent_point is None:
|
| 35 |
+
obj._parent = None
|
| 36 |
+
obj._root = obj
|
| 37 |
+
else:
|
| 38 |
+
obj._parent = parent_point
|
| 39 |
+
obj._root = parent_point._root
|
| 40 |
+
# Return object
|
| 41 |
+
return obj
|
| 42 |
+
|
| 43 |
+
@cacheit
|
| 44 |
+
def position_wrt(self, other):
|
| 45 |
+
"""
|
| 46 |
+
Returns the position vector of this Point with respect to
|
| 47 |
+
another Point/CoordSys3D.
|
| 48 |
+
|
| 49 |
+
Parameters
|
| 50 |
+
==========
|
| 51 |
+
|
| 52 |
+
other : Point/CoordSys3D
|
| 53 |
+
If other is a Point, the position of this Point wrt it is
|
| 54 |
+
returned. If its an instance of CoordSyRect, the position
|
| 55 |
+
wrt its origin is returned.
|
| 56 |
+
|
| 57 |
+
Examples
|
| 58 |
+
========
|
| 59 |
+
|
| 60 |
+
>>> from sympy.vector import CoordSys3D
|
| 61 |
+
>>> N = CoordSys3D('N')
|
| 62 |
+
>>> p1 = N.origin.locate_new('p1', 10 * N.i)
|
| 63 |
+
>>> N.origin.position_wrt(p1)
|
| 64 |
+
(-10)*N.i
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
if (not isinstance(other, Point) and
|
| 69 |
+
not isinstance(other, CoordSys3D)):
|
| 70 |
+
raise TypeError(str(other) +
|
| 71 |
+
"is not a Point or CoordSys3D")
|
| 72 |
+
if isinstance(other, CoordSys3D):
|
| 73 |
+
other = other.origin
|
| 74 |
+
# Handle special cases
|
| 75 |
+
if other == self:
|
| 76 |
+
return Vector.zero
|
| 77 |
+
elif other == self._parent:
|
| 78 |
+
return self._pos
|
| 79 |
+
elif other._parent == self:
|
| 80 |
+
return -1 * other._pos
|
| 81 |
+
# Else, use point tree to calculate position
|
| 82 |
+
rootindex, path = _path(self, other)
|
| 83 |
+
result = Vector.zero
|
| 84 |
+
i = -1
|
| 85 |
+
for i in range(rootindex):
|
| 86 |
+
result += path[i]._pos
|
| 87 |
+
i += 2
|
| 88 |
+
while i < len(path):
|
| 89 |
+
result -= path[i]._pos
|
| 90 |
+
i += 1
|
| 91 |
+
return result
|
| 92 |
+
|
| 93 |
+
def locate_new(self, name, position):
|
| 94 |
+
"""
|
| 95 |
+
Returns a new Point located at the given position wrt this
|
| 96 |
+
Point.
|
| 97 |
+
Thus, the position vector of the new Point wrt this one will
|
| 98 |
+
be equal to the given 'position' parameter.
|
| 99 |
+
|
| 100 |
+
Parameters
|
| 101 |
+
==========
|
| 102 |
+
|
| 103 |
+
name : str
|
| 104 |
+
Name of the new point
|
| 105 |
+
|
| 106 |
+
position : Vector
|
| 107 |
+
The position vector of the new Point wrt this one
|
| 108 |
+
|
| 109 |
+
Examples
|
| 110 |
+
========
|
| 111 |
+
|
| 112 |
+
>>> from sympy.vector import CoordSys3D
|
| 113 |
+
>>> N = CoordSys3D('N')
|
| 114 |
+
>>> p1 = N.origin.locate_new('p1', 10 * N.i)
|
| 115 |
+
>>> p1.position_wrt(N.origin)
|
| 116 |
+
10*N.i
|
| 117 |
+
|
| 118 |
+
"""
|
| 119 |
+
return Point(name, position, self)
|
| 120 |
+
|
| 121 |
+
def express_coordinates(self, coordinate_system):
|
| 122 |
+
"""
|
| 123 |
+
Returns the Cartesian/rectangular coordinates of this point
|
| 124 |
+
wrt the origin of the given CoordSys3D instance.
|
| 125 |
+
|
| 126 |
+
Parameters
|
| 127 |
+
==========
|
| 128 |
+
|
| 129 |
+
coordinate_system : CoordSys3D
|
| 130 |
+
The coordinate system to express the coordinates of this
|
| 131 |
+
Point in.
|
| 132 |
+
|
| 133 |
+
Examples
|
| 134 |
+
========
|
| 135 |
+
|
| 136 |
+
>>> from sympy.vector import CoordSys3D
|
| 137 |
+
>>> N = CoordSys3D('N')
|
| 138 |
+
>>> p1 = N.origin.locate_new('p1', 10 * N.i)
|
| 139 |
+
>>> p2 = p1.locate_new('p2', 5 * N.j)
|
| 140 |
+
>>> p2.express_coordinates(N)
|
| 141 |
+
(10, 5, 0)
|
| 142 |
+
|
| 143 |
+
"""
|
| 144 |
+
|
| 145 |
+
# Determine the position vector
|
| 146 |
+
pos_vect = self.position_wrt(coordinate_system.origin)
|
| 147 |
+
# Express it in the given coordinate system
|
| 148 |
+
return tuple(pos_vect.to_matrix(coordinate_system))
|
| 149 |
+
|
| 150 |
+
def _sympystr(self, printer):
|
| 151 |
+
return self._name
|
mgm/lib/python3.10/site-packages/sympy/vector/scalar.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import AtomicExpr, Symbol, S
|
| 2 |
+
from sympy.core.sympify import _sympify
|
| 3 |
+
from sympy.printing.pretty.stringpict import prettyForm
|
| 4 |
+
from sympy.printing.precedence import PRECEDENCE
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BaseScalar(AtomicExpr):
|
| 8 |
+
"""
|
| 9 |
+
A coordinate symbol/base scalar.
|
| 10 |
+
|
| 11 |
+
Ideally, users should not instantiate this class.
|
| 12 |
+
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def __new__(cls, index, system, pretty_str=None, latex_str=None):
|
| 16 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 17 |
+
if pretty_str is None:
|
| 18 |
+
pretty_str = "x{}".format(index)
|
| 19 |
+
elif isinstance(pretty_str, Symbol):
|
| 20 |
+
pretty_str = pretty_str.name
|
| 21 |
+
if latex_str is None:
|
| 22 |
+
latex_str = "x_{}".format(index)
|
| 23 |
+
elif isinstance(latex_str, Symbol):
|
| 24 |
+
latex_str = latex_str.name
|
| 25 |
+
|
| 26 |
+
index = _sympify(index)
|
| 27 |
+
system = _sympify(system)
|
| 28 |
+
obj = super().__new__(cls, index, system)
|
| 29 |
+
if not isinstance(system, CoordSys3D):
|
| 30 |
+
raise TypeError("system should be a CoordSys3D")
|
| 31 |
+
if index not in range(0, 3):
|
| 32 |
+
raise ValueError("Invalid index specified.")
|
| 33 |
+
# The _id is used for equating purposes, and for hashing
|
| 34 |
+
obj._id = (index, system)
|
| 35 |
+
obj._name = obj.name = system._name + '.' + system._variable_names[index]
|
| 36 |
+
obj._pretty_form = '' + pretty_str
|
| 37 |
+
obj._latex_form = latex_str
|
| 38 |
+
obj._system = system
|
| 39 |
+
|
| 40 |
+
return obj
|
| 41 |
+
|
| 42 |
+
is_commutative = True
|
| 43 |
+
is_symbol = True
|
| 44 |
+
|
| 45 |
+
@property
|
| 46 |
+
def free_symbols(self):
|
| 47 |
+
return {self}
|
| 48 |
+
|
| 49 |
+
_diff_wrt = True
|
| 50 |
+
|
| 51 |
+
def _eval_derivative(self, s):
|
| 52 |
+
if self == s:
|
| 53 |
+
return S.One
|
| 54 |
+
return S.Zero
|
| 55 |
+
|
| 56 |
+
def _latex(self, printer=None):
|
| 57 |
+
return self._latex_form
|
| 58 |
+
|
| 59 |
+
def _pretty(self, printer=None):
|
| 60 |
+
return prettyForm(self._pretty_form)
|
| 61 |
+
|
| 62 |
+
precedence = PRECEDENCE['Atom']
|
| 63 |
+
|
| 64 |
+
@property
|
| 65 |
+
def system(self):
|
| 66 |
+
return self._system
|
| 67 |
+
|
| 68 |
+
def _sympystr(self, printer):
|
| 69 |
+
return self._name
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/__init__.py
ADDED
|
File without changes
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_dyadic.cpython-310.pyc
ADDED
|
Binary file (4.59 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_functions.cpython-310.pyc
ADDED
|
Binary file (7.55 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/__pycache__/test_integrals.cpython-310.pyc
ADDED
|
Binary file (5.15 kB). View file
|
|
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.testing.pytest import raises
|
| 2 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 3 |
+
from sympy.vector.scalar import BaseScalar
|
| 4 |
+
from sympy.core.function import expand
|
| 5 |
+
from sympy.core.numbers import pi
|
| 6 |
+
from sympy.core.symbol import symbols
|
| 7 |
+
from sympy.functions.elementary.hyperbolic import (cosh, sinh)
|
| 8 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 9 |
+
from sympy.functions.elementary.trigonometric import (acos, atan2, cos, sin)
|
| 10 |
+
from sympy.matrices.dense import zeros
|
| 11 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 12 |
+
from sympy.simplify.simplify import simplify
|
| 13 |
+
from sympy.vector.functions import express
|
| 14 |
+
from sympy.vector.point import Point
|
| 15 |
+
from sympy.vector.vector import Vector
|
| 16 |
+
from sympy.vector.orienters import (AxisOrienter, BodyOrienter,
|
| 17 |
+
SpaceOrienter, QuaternionOrienter)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
x, y, z = symbols('x y z')
|
| 21 |
+
a, b, c, q = symbols('a b c q')
|
| 22 |
+
q1, q2, q3, q4 = symbols('q1 q2 q3 q4')
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_func_args():
|
| 26 |
+
A = CoordSys3D('A')
|
| 27 |
+
assert A.x.func(*A.x.args) == A.x
|
| 28 |
+
expr = 3*A.x + 4*A.y
|
| 29 |
+
assert expr.func(*expr.args) == expr
|
| 30 |
+
assert A.i.func(*A.i.args) == A.i
|
| 31 |
+
v = A.x*A.i + A.y*A.j + A.z*A.k
|
| 32 |
+
assert v.func(*v.args) == v
|
| 33 |
+
assert A.origin.func(*A.origin.args) == A.origin
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_coordsys3d_equivalence():
|
| 37 |
+
A = CoordSys3D('A')
|
| 38 |
+
A1 = CoordSys3D('A')
|
| 39 |
+
assert A1 == A
|
| 40 |
+
B = CoordSys3D('B')
|
| 41 |
+
assert A != B
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_orienters():
|
| 45 |
+
A = CoordSys3D('A')
|
| 46 |
+
axis_orienter = AxisOrienter(a, A.k)
|
| 47 |
+
body_orienter = BodyOrienter(a, b, c, '123')
|
| 48 |
+
space_orienter = SpaceOrienter(a, b, c, '123')
|
| 49 |
+
q_orienter = QuaternionOrienter(q1, q2, q3, q4)
|
| 50 |
+
assert axis_orienter.rotation_matrix(A) == Matrix([
|
| 51 |
+
[ cos(a), sin(a), 0],
|
| 52 |
+
[-sin(a), cos(a), 0],
|
| 53 |
+
[ 0, 0, 1]])
|
| 54 |
+
assert body_orienter.rotation_matrix() == Matrix([
|
| 55 |
+
[ cos(b)*cos(c), sin(a)*sin(b)*cos(c) + sin(c)*cos(a),
|
| 56 |
+
sin(a)*sin(c) - sin(b)*cos(a)*cos(c)],
|
| 57 |
+
[-sin(c)*cos(b), -sin(a)*sin(b)*sin(c) + cos(a)*cos(c),
|
| 58 |
+
sin(a)*cos(c) + sin(b)*sin(c)*cos(a)],
|
| 59 |
+
[ sin(b), -sin(a)*cos(b),
|
| 60 |
+
cos(a)*cos(b)]])
|
| 61 |
+
assert space_orienter.rotation_matrix() == Matrix([
|
| 62 |
+
[cos(b)*cos(c), sin(c)*cos(b), -sin(b)],
|
| 63 |
+
[sin(a)*sin(b)*cos(c) - sin(c)*cos(a),
|
| 64 |
+
sin(a)*sin(b)*sin(c) + cos(a)*cos(c), sin(a)*cos(b)],
|
| 65 |
+
[sin(a)*sin(c) + sin(b)*cos(a)*cos(c), -sin(a)*cos(c) +
|
| 66 |
+
sin(b)*sin(c)*cos(a), cos(a)*cos(b)]])
|
| 67 |
+
assert q_orienter.rotation_matrix() == Matrix([
|
| 68 |
+
[q1**2 + q2**2 - q3**2 - q4**2, 2*q1*q4 + 2*q2*q3,
|
| 69 |
+
-2*q1*q3 + 2*q2*q4],
|
| 70 |
+
[-2*q1*q4 + 2*q2*q3, q1**2 - q2**2 + q3**2 - q4**2,
|
| 71 |
+
2*q1*q2 + 2*q3*q4],
|
| 72 |
+
[2*q1*q3 + 2*q2*q4,
|
| 73 |
+
-2*q1*q2 + 2*q3*q4, q1**2 - q2**2 - q3**2 + q4**2]])
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_coordinate_vars():
|
| 77 |
+
"""
|
| 78 |
+
Tests the coordinate variables functionality with respect to
|
| 79 |
+
reorientation of coordinate systems.
|
| 80 |
+
"""
|
| 81 |
+
A = CoordSys3D('A')
|
| 82 |
+
# Note that the name given on the lhs is different from A.x._name
|
| 83 |
+
assert BaseScalar(0, A, 'A_x', r'\mathbf{{x}_{A}}') == A.x
|
| 84 |
+
assert BaseScalar(1, A, 'A_y', r'\mathbf{{y}_{A}}') == A.y
|
| 85 |
+
assert BaseScalar(2, A, 'A_z', r'\mathbf{{z}_{A}}') == A.z
|
| 86 |
+
assert BaseScalar(0, A, 'A_x', r'\mathbf{{x}_{A}}').__hash__() == A.x.__hash__()
|
| 87 |
+
assert isinstance(A.x, BaseScalar) and \
|
| 88 |
+
isinstance(A.y, BaseScalar) and \
|
| 89 |
+
isinstance(A.z, BaseScalar)
|
| 90 |
+
assert A.x*A.y == A.y*A.x
|
| 91 |
+
assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
|
| 92 |
+
assert A.x.system == A
|
| 93 |
+
assert A.x.diff(A.x) == 1
|
| 94 |
+
B = A.orient_new_axis('B', q, A.k)
|
| 95 |
+
assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q),
|
| 96 |
+
B.x: A.x*cos(q) + A.y*sin(q)}
|
| 97 |
+
assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q),
|
| 98 |
+
A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}
|
| 99 |
+
assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q)
|
| 100 |
+
assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q)
|
| 101 |
+
assert express(B.z, A, variables=True) == A.z
|
| 102 |
+
assert expand(express(B.x*B.y*B.z, A, variables=True)) == \
|
| 103 |
+
expand(A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q)))
|
| 104 |
+
assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
|
| 105 |
+
(B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
|
| 106 |
+
B.y*cos(q))*A.j + B.z*A.k
|
| 107 |
+
assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
|
| 108 |
+
variables=True)) == \
|
| 109 |
+
A.x*A.i + A.y*A.j + A.z*A.k
|
| 110 |
+
assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
|
| 111 |
+
(A.x*cos(q) + A.y*sin(q))*B.i + \
|
| 112 |
+
(-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
|
| 113 |
+
assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
|
| 114 |
+
variables=True)) == \
|
| 115 |
+
B.x*B.i + B.y*B.j + B.z*B.k
|
| 116 |
+
N = B.orient_new_axis('N', -q, B.k)
|
| 117 |
+
assert N.scalar_map(A) == \
|
| 118 |
+
{N.x: A.x, N.z: A.z, N.y: A.y}
|
| 119 |
+
C = A.orient_new_axis('C', q, A.i + A.j + A.k)
|
| 120 |
+
mapping = A.scalar_map(C)
|
| 121 |
+
assert mapping[A.x].equals(C.x*(2*cos(q) + 1)/3 +
|
| 122 |
+
C.y*(-2*sin(q + pi/6) + 1)/3 +
|
| 123 |
+
C.z*(-2*cos(q + pi/3) + 1)/3)
|
| 124 |
+
assert mapping[A.y].equals(C.x*(-2*cos(q + pi/3) + 1)/3 +
|
| 125 |
+
C.y*(2*cos(q) + 1)/3 +
|
| 126 |
+
C.z*(-2*sin(q + pi/6) + 1)/3)
|
| 127 |
+
assert mapping[A.z].equals(C.x*(-2*sin(q + pi/6) + 1)/3 +
|
| 128 |
+
C.y*(-2*cos(q + pi/3) + 1)/3 +
|
| 129 |
+
C.z*(2*cos(q) + 1)/3)
|
| 130 |
+
D = A.locate_new('D', a*A.i + b*A.j + c*A.k)
|
| 131 |
+
assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
|
| 132 |
+
E = A.orient_new_axis('E', a, A.k, a*A.i + b*A.j + c*A.k)
|
| 133 |
+
assert A.scalar_map(E) == {A.z: E.z + c,
|
| 134 |
+
A.x: E.x*cos(a) - E.y*sin(a) + a,
|
| 135 |
+
A.y: E.x*sin(a) + E.y*cos(a) + b}
|
| 136 |
+
assert E.scalar_map(A) == {E.x: (A.x - a)*cos(a) + (A.y - b)*sin(a),
|
| 137 |
+
E.y: (-A.x + a)*sin(a) + (A.y - b)*cos(a),
|
| 138 |
+
E.z: A.z - c}
|
| 139 |
+
F = A.locate_new('F', Vector.zero)
|
| 140 |
+
assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_rotation_matrix():
|
| 144 |
+
N = CoordSys3D('N')
|
| 145 |
+
A = N.orient_new_axis('A', q1, N.k)
|
| 146 |
+
B = A.orient_new_axis('B', q2, A.i)
|
| 147 |
+
C = B.orient_new_axis('C', q3, B.j)
|
| 148 |
+
D = N.orient_new_axis('D', q4, N.j)
|
| 149 |
+
E = N.orient_new_space('E', q1, q2, q3, '123')
|
| 150 |
+
F = N.orient_new_quaternion('F', q1, q2, q3, q4)
|
| 151 |
+
G = N.orient_new_body('G', q1, q2, q3, '123')
|
| 152 |
+
assert N.rotation_matrix(C) == Matrix([
|
| 153 |
+
[- sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), - sin(q1) *
|
| 154 |
+
cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)], \
|
| 155 |
+
[sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), \
|
| 156 |
+
cos(q1) * cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * \
|
| 157 |
+
cos(q3)], [- sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
|
| 158 |
+
test_mat = D.rotation_matrix(C) - Matrix(
|
| 159 |
+
[[cos(q1) * cos(q3) * cos(q4) - sin(q3) * (- sin(q4) * cos(q2) +
|
| 160 |
+
sin(q1) * sin(q2) * cos(q4)), - sin(q2) * sin(q4) - sin(q1) *
|
| 161 |
+
cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) * \
|
| 162 |
+
(- sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4))], \
|
| 163 |
+
[sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * \
|
| 164 |
+
cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)], \
|
| 165 |
+
[sin(q4) * cos(q1) * cos(q3) - sin(q3) * (cos(q2) * cos(q4) + \
|
| 166 |
+
sin(q1) * sin(q2) * \
|
| 167 |
+
sin(q4)), sin(q2) *
|
| 168 |
+
cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) * \
|
| 169 |
+
sin(q4) * cos(q1) + cos(q3) * (cos(q2) * cos(q4) + \
|
| 170 |
+
sin(q1) * sin(q2) * sin(q4))]])
|
| 171 |
+
assert test_mat.expand() == zeros(3, 3)
|
| 172 |
+
assert E.rotation_matrix(N) == Matrix(
|
| 173 |
+
[[cos(q2)*cos(q3), sin(q3)*cos(q2), -sin(q2)],
|
| 174 |
+
[sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), \
|
| 175 |
+
sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2)], \
|
| 176 |
+
[sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), - \
|
| 177 |
+
sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2)]])
|
| 178 |
+
assert F.rotation_matrix(N) == Matrix([[
|
| 179 |
+
q1**2 + q2**2 - q3**2 - q4**2,
|
| 180 |
+
2*q1*q4 + 2*q2*q3, -2*q1*q3 + 2*q2*q4],[ -2*q1*q4 + 2*q2*q3,
|
| 181 |
+
q1**2 - q2**2 + q3**2 - q4**2, 2*q1*q2 + 2*q3*q4],
|
| 182 |
+
[2*q1*q3 + 2*q2*q4,
|
| 183 |
+
-2*q1*q2 + 2*q3*q4,
|
| 184 |
+
q1**2 - q2**2 - q3**2 + q4**2]])
|
| 185 |
+
assert G.rotation_matrix(N) == Matrix([[
|
| 186 |
+
cos(q2)*cos(q3), sin(q1)*sin(q2)*cos(q3) + sin(q3)*cos(q1),
|
| 187 |
+
sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3)], [
|
| 188 |
+
-sin(q3)*cos(q2), -sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3),
|
| 189 |
+
sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],[
|
| 190 |
+
sin(q2), -sin(q1)*cos(q2), cos(q1)*cos(q2)]])
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def test_vector_with_orientation():
|
| 194 |
+
"""
|
| 195 |
+
Tests the effects of orientation of coordinate systems on
|
| 196 |
+
basic vector operations.
|
| 197 |
+
"""
|
| 198 |
+
N = CoordSys3D('N')
|
| 199 |
+
A = N.orient_new_axis('A', q1, N.k)
|
| 200 |
+
B = A.orient_new_axis('B', q2, A.i)
|
| 201 |
+
C = B.orient_new_axis('C', q3, B.j)
|
| 202 |
+
|
| 203 |
+
# Test to_matrix
|
| 204 |
+
v1 = a*N.i + b*N.j + c*N.k
|
| 205 |
+
assert v1.to_matrix(A) == Matrix([[ a*cos(q1) + b*sin(q1)],
|
| 206 |
+
[-a*sin(q1) + b*cos(q1)],
|
| 207 |
+
[ c]])
|
| 208 |
+
|
| 209 |
+
# Test dot
|
| 210 |
+
assert N.i.dot(A.i) == cos(q1)
|
| 211 |
+
assert N.i.dot(A.j) == -sin(q1)
|
| 212 |
+
assert N.i.dot(A.k) == 0
|
| 213 |
+
assert N.j.dot(A.i) == sin(q1)
|
| 214 |
+
assert N.j.dot(A.j) == cos(q1)
|
| 215 |
+
assert N.j.dot(A.k) == 0
|
| 216 |
+
assert N.k.dot(A.i) == 0
|
| 217 |
+
assert N.k.dot(A.j) == 0
|
| 218 |
+
assert N.k.dot(A.k) == 1
|
| 219 |
+
|
| 220 |
+
assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == \
|
| 221 |
+
(A.i + A.j).dot(N.i)
|
| 222 |
+
|
| 223 |
+
assert A.i.dot(C.i) == cos(q3)
|
| 224 |
+
assert A.i.dot(C.j) == 0
|
| 225 |
+
assert A.i.dot(C.k) == sin(q3)
|
| 226 |
+
assert A.j.dot(C.i) == sin(q2)*sin(q3)
|
| 227 |
+
assert A.j.dot(C.j) == cos(q2)
|
| 228 |
+
assert A.j.dot(C.k) == -sin(q2)*cos(q3)
|
| 229 |
+
assert A.k.dot(C.i) == -cos(q2)*sin(q3)
|
| 230 |
+
assert A.k.dot(C.j) == sin(q2)
|
| 231 |
+
assert A.k.dot(C.k) == cos(q2)*cos(q3)
|
| 232 |
+
|
| 233 |
+
# Test cross
|
| 234 |
+
assert N.i.cross(A.i) == sin(q1)*A.k
|
| 235 |
+
assert N.i.cross(A.j) == cos(q1)*A.k
|
| 236 |
+
assert N.i.cross(A.k) == -sin(q1)*A.i - cos(q1)*A.j
|
| 237 |
+
assert N.j.cross(A.i) == -cos(q1)*A.k
|
| 238 |
+
assert N.j.cross(A.j) == sin(q1)*A.k
|
| 239 |
+
assert N.j.cross(A.k) == cos(q1)*A.i - sin(q1)*A.j
|
| 240 |
+
assert N.k.cross(A.i) == A.j
|
| 241 |
+
assert N.k.cross(A.j) == -A.i
|
| 242 |
+
assert N.k.cross(A.k) == Vector.zero
|
| 243 |
+
|
| 244 |
+
assert N.i.cross(A.i) == sin(q1)*A.k
|
| 245 |
+
assert N.i.cross(A.j) == cos(q1)*A.k
|
| 246 |
+
assert N.i.cross(A.i + A.j) == sin(q1)*A.k + cos(q1)*A.k
|
| 247 |
+
assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1))*N.k
|
| 248 |
+
|
| 249 |
+
assert A.i.cross(C.i) == sin(q3)*C.j
|
| 250 |
+
assert A.i.cross(C.j) == -sin(q3)*C.i + cos(q3)*C.k
|
| 251 |
+
assert A.i.cross(C.k) == -cos(q3)*C.j
|
| 252 |
+
assert C.i.cross(A.i) == (-sin(q3)*cos(q2))*A.j + \
|
| 253 |
+
(-sin(q2)*sin(q3))*A.k
|
| 254 |
+
assert C.j.cross(A.i) == (sin(q2))*A.j + (-cos(q2))*A.k
|
| 255 |
+
assert express(C.k.cross(A.i), C).trigsimp() == cos(q3)*C.j
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def test_orient_new_methods():
|
| 259 |
+
N = CoordSys3D('N')
|
| 260 |
+
orienter1 = AxisOrienter(q4, N.j)
|
| 261 |
+
orienter2 = SpaceOrienter(q1, q2, q3, '123')
|
| 262 |
+
orienter3 = QuaternionOrienter(q1, q2, q3, q4)
|
| 263 |
+
orienter4 = BodyOrienter(q1, q2, q3, '123')
|
| 264 |
+
D = N.orient_new('D', (orienter1, ))
|
| 265 |
+
E = N.orient_new('E', (orienter2, ))
|
| 266 |
+
F = N.orient_new('F', (orienter3, ))
|
| 267 |
+
G = N.orient_new('G', (orienter4, ))
|
| 268 |
+
assert D == N.orient_new_axis('D', q4, N.j)
|
| 269 |
+
assert E == N.orient_new_space('E', q1, q2, q3, '123')
|
| 270 |
+
assert F == N.orient_new_quaternion('F', q1, q2, q3, q4)
|
| 271 |
+
assert G == N.orient_new_body('G', q1, q2, q3, '123')
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def test_locatenew_point():
|
| 275 |
+
"""
|
| 276 |
+
Tests Point class, and locate_new method in CoordSys3D.
|
| 277 |
+
"""
|
| 278 |
+
A = CoordSys3D('A')
|
| 279 |
+
assert isinstance(A.origin, Point)
|
| 280 |
+
v = a*A.i + b*A.j + c*A.k
|
| 281 |
+
C = A.locate_new('C', v)
|
| 282 |
+
assert C.origin.position_wrt(A) == \
|
| 283 |
+
C.position_wrt(A) == \
|
| 284 |
+
C.origin.position_wrt(A.origin) == v
|
| 285 |
+
assert A.origin.position_wrt(C) == \
|
| 286 |
+
A.position_wrt(C) == \
|
| 287 |
+
A.origin.position_wrt(C.origin) == -v
|
| 288 |
+
assert A.origin.express_coordinates(C) == (-a, -b, -c)
|
| 289 |
+
p = A.origin.locate_new('p', -v)
|
| 290 |
+
assert p.express_coordinates(A) == (-a, -b, -c)
|
| 291 |
+
assert p.position_wrt(C.origin) == p.position_wrt(C) == \
|
| 292 |
+
-2 * v
|
| 293 |
+
p1 = p.locate_new('p1', 2*v)
|
| 294 |
+
assert p1.position_wrt(C.origin) == Vector.zero
|
| 295 |
+
assert p1.express_coordinates(C) == (0, 0, 0)
|
| 296 |
+
p2 = p.locate_new('p2', A.i)
|
| 297 |
+
assert p1.position_wrt(p2) == 2*v - A.i
|
| 298 |
+
assert p2.express_coordinates(C) == (-2*a + 1, -2*b, -2*c)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def test_create_new():
|
| 302 |
+
a = CoordSys3D('a')
|
| 303 |
+
c = a.create_new('c', transformation='spherical')
|
| 304 |
+
assert c._parent == a
|
| 305 |
+
assert c.transformation_to_parent() == \
|
| 306 |
+
(c.r*sin(c.theta)*cos(c.phi), c.r*sin(c.theta)*sin(c.phi), c.r*cos(c.theta))
|
| 307 |
+
assert c.transformation_from_parent() == \
|
| 308 |
+
(sqrt(a.x**2 + a.y**2 + a.z**2), acos(a.z/sqrt(a.x**2 + a.y**2 + a.z**2)), atan2(a.y, a.x))
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def test_evalf():
|
| 312 |
+
A = CoordSys3D('A')
|
| 313 |
+
v = 3*A.i + 4*A.j + a*A.k
|
| 314 |
+
assert v.n() == v.evalf()
|
| 315 |
+
assert v.evalf(subs={a:1}) == v.subs(a, 1).evalf()
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def test_lame_coefficients():
|
| 319 |
+
a = CoordSys3D('a', 'spherical')
|
| 320 |
+
assert a.lame_coefficients() == (1, a.r, sin(a.theta)*a.r)
|
| 321 |
+
a = CoordSys3D('a')
|
| 322 |
+
assert a.lame_coefficients() == (1, 1, 1)
|
| 323 |
+
a = CoordSys3D('a', 'cartesian')
|
| 324 |
+
assert a.lame_coefficients() == (1, 1, 1)
|
| 325 |
+
a = CoordSys3D('a', 'cylindrical')
|
| 326 |
+
assert a.lame_coefficients() == (1, a.r, 1)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def test_transformation_equations():
|
| 330 |
+
|
| 331 |
+
x, y, z = symbols('x y z')
|
| 332 |
+
# Str
|
| 333 |
+
a = CoordSys3D('a', transformation='spherical',
|
| 334 |
+
variable_names=["r", "theta", "phi"])
|
| 335 |
+
r, theta, phi = a.base_scalars()
|
| 336 |
+
|
| 337 |
+
assert r == a.r
|
| 338 |
+
assert theta == a.theta
|
| 339 |
+
assert phi == a.phi
|
| 340 |
+
|
| 341 |
+
raises(AttributeError, lambda: a.x)
|
| 342 |
+
raises(AttributeError, lambda: a.y)
|
| 343 |
+
raises(AttributeError, lambda: a.z)
|
| 344 |
+
|
| 345 |
+
assert a.transformation_to_parent() == (
|
| 346 |
+
r*sin(theta)*cos(phi),
|
| 347 |
+
r*sin(theta)*sin(phi),
|
| 348 |
+
r*cos(theta)
|
| 349 |
+
)
|
| 350 |
+
assert a.lame_coefficients() == (1, r, r*sin(theta))
|
| 351 |
+
assert a.transformation_from_parent_function()(x, y, z) == (
|
| 352 |
+
sqrt(x ** 2 + y ** 2 + z ** 2),
|
| 353 |
+
acos((z) / sqrt(x**2 + y**2 + z**2)),
|
| 354 |
+
atan2(y, x)
|
| 355 |
+
)
|
| 356 |
+
a = CoordSys3D('a', transformation='cylindrical',
|
| 357 |
+
variable_names=["r", "theta", "z"])
|
| 358 |
+
r, theta, z = a.base_scalars()
|
| 359 |
+
assert a.transformation_to_parent() == (
|
| 360 |
+
r*cos(theta),
|
| 361 |
+
r*sin(theta),
|
| 362 |
+
z
|
| 363 |
+
)
|
| 364 |
+
assert a.lame_coefficients() == (1, a.r, 1)
|
| 365 |
+
assert a.transformation_from_parent_function()(x, y, z) == (sqrt(x**2 + y**2),
|
| 366 |
+
atan2(y, x), z)
|
| 367 |
+
|
| 368 |
+
a = CoordSys3D('a', 'cartesian')
|
| 369 |
+
assert a.transformation_to_parent() == (a.x, a.y, a.z)
|
| 370 |
+
assert a.lame_coefficients() == (1, 1, 1)
|
| 371 |
+
assert a.transformation_from_parent_function()(x, y, z) == (x, y, z)
|
| 372 |
+
|
| 373 |
+
# Variables and expressions
|
| 374 |
+
|
| 375 |
+
# Cartesian with equation tuple:
|
| 376 |
+
x, y, z = symbols('x y z')
|
| 377 |
+
a = CoordSys3D('a', ((x, y, z), (x, y, z)))
|
| 378 |
+
a._calculate_inv_trans_equations()
|
| 379 |
+
assert a.transformation_to_parent() == (a.x1, a.x2, a.x3)
|
| 380 |
+
assert a.lame_coefficients() == (1, 1, 1)
|
| 381 |
+
assert a.transformation_from_parent_function()(x, y, z) == (x, y, z)
|
| 382 |
+
r, theta, z = symbols("r theta z")
|
| 383 |
+
|
| 384 |
+
# Cylindrical with equation tuple:
|
| 385 |
+
a = CoordSys3D('a', [(r, theta, z), (r*cos(theta), r*sin(theta), z)],
|
| 386 |
+
variable_names=["r", "theta", "z"])
|
| 387 |
+
r, theta, z = a.base_scalars()
|
| 388 |
+
assert a.transformation_to_parent() == (
|
| 389 |
+
r*cos(theta), r*sin(theta), z
|
| 390 |
+
)
|
| 391 |
+
assert a.lame_coefficients() == (
|
| 392 |
+
sqrt(sin(theta)**2 + cos(theta)**2),
|
| 393 |
+
sqrt(r**2*sin(theta)**2 + r**2*cos(theta)**2),
|
| 394 |
+
1
|
| 395 |
+
) # ==> this should simplify to (1, r, 1), tests are too slow with `simplify`.
|
| 396 |
+
|
| 397 |
+
# Definitions with `lambda`:
|
| 398 |
+
|
| 399 |
+
# Cartesian with `lambda`
|
| 400 |
+
a = CoordSys3D('a', lambda x, y, z: (x, y, z))
|
| 401 |
+
assert a.transformation_to_parent() == (a.x1, a.x2, a.x3)
|
| 402 |
+
assert a.lame_coefficients() == (1, 1, 1)
|
| 403 |
+
a._calculate_inv_trans_equations()
|
| 404 |
+
assert a.transformation_from_parent_function()(x, y, z) == (x, y, z)
|
| 405 |
+
|
| 406 |
+
# Spherical with `lambda`
|
| 407 |
+
a = CoordSys3D('a', lambda r, theta, phi: (r*sin(theta)*cos(phi), r*sin(theta)*sin(phi), r*cos(theta)),
|
| 408 |
+
variable_names=["r", "theta", "phi"])
|
| 409 |
+
r, theta, phi = a.base_scalars()
|
| 410 |
+
assert a.transformation_to_parent() == (
|
| 411 |
+
r*sin(theta)*cos(phi), r*sin(phi)*sin(theta), r*cos(theta)
|
| 412 |
+
)
|
| 413 |
+
assert a.lame_coefficients() == (
|
| 414 |
+
sqrt(sin(phi)**2*sin(theta)**2 + sin(theta)**2*cos(phi)**2 + cos(theta)**2),
|
| 415 |
+
sqrt(r**2*sin(phi)**2*cos(theta)**2 + r**2*sin(theta)**2 + r**2*cos(phi)**2*cos(theta)**2),
|
| 416 |
+
sqrt(r**2*sin(phi)**2*sin(theta)**2 + r**2*sin(theta)**2*cos(phi)**2)
|
| 417 |
+
) # ==> this should simplify to (1, r, sin(theta)*r), `simplify` is too slow.
|
| 418 |
+
|
| 419 |
+
# Cylindrical with `lambda`
|
| 420 |
+
a = CoordSys3D('a', lambda r, theta, z:
|
| 421 |
+
(r*cos(theta), r*sin(theta), z),
|
| 422 |
+
variable_names=["r", "theta", "z"]
|
| 423 |
+
)
|
| 424 |
+
r, theta, z = a.base_scalars()
|
| 425 |
+
assert a.transformation_to_parent() == (r*cos(theta), r*sin(theta), z)
|
| 426 |
+
assert a.lame_coefficients() == (
|
| 427 |
+
sqrt(sin(theta)**2 + cos(theta)**2),
|
| 428 |
+
sqrt(r**2*sin(theta)**2 + r**2*cos(theta)**2),
|
| 429 |
+
1
|
| 430 |
+
) # ==> this should simplify to (1, a.x, 1)
|
| 431 |
+
|
| 432 |
+
raises(TypeError, lambda: CoordSys3D('a', transformation={
|
| 433 |
+
x: x*sin(y)*cos(z), y:x*sin(y)*sin(z), z: x*cos(y)}))
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def test_check_orthogonality():
|
| 437 |
+
x, y, z = symbols('x y z')
|
| 438 |
+
u,v = symbols('u, v')
|
| 439 |
+
a = CoordSys3D('a', transformation=((x, y, z), (x*sin(y)*cos(z), x*sin(y)*sin(z), x*cos(y))))
|
| 440 |
+
assert a._check_orthogonality(a._transformation) is True
|
| 441 |
+
a = CoordSys3D('a', transformation=((x, y, z), (x * cos(y), x * sin(y), z)))
|
| 442 |
+
assert a._check_orthogonality(a._transformation) is True
|
| 443 |
+
a = CoordSys3D('a', transformation=((u, v, z), (cosh(u) * cos(v), sinh(u) * sin(v), z)))
|
| 444 |
+
assert a._check_orthogonality(a._transformation) is True
|
| 445 |
+
|
| 446 |
+
raises(ValueError, lambda: CoordSys3D('a', transformation=((x, y, z), (x, x, z))))
|
| 447 |
+
raises(ValueError, lambda: CoordSys3D('a', transformation=(
|
| 448 |
+
(x, y, z), (x*sin(y/2)*cos(z), x*sin(y)*sin(z), x*cos(y)))))
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def test_rotation_trans_equations():
|
| 452 |
+
a = CoordSys3D('a')
|
| 453 |
+
from sympy.core.symbol import symbols
|
| 454 |
+
q0 = symbols('q0')
|
| 455 |
+
assert a._rotation_trans_equations(a._parent_rotation_matrix, a.base_scalars()) == (a.x, a.y, a.z)
|
| 456 |
+
assert a._rotation_trans_equations(a._inverse_rotation_matrix(), a.base_scalars()) == (a.x, a.y, a.z)
|
| 457 |
+
b = a.orient_new_axis('b', 0, -a.k)
|
| 458 |
+
assert b._rotation_trans_equations(b._parent_rotation_matrix, b.base_scalars()) == (b.x, b.y, b.z)
|
| 459 |
+
assert b._rotation_trans_equations(b._inverse_rotation_matrix(), b.base_scalars()) == (b.x, b.y, b.z)
|
| 460 |
+
c = a.orient_new_axis('c', q0, -a.k)
|
| 461 |
+
assert c._rotation_trans_equations(c._parent_rotation_matrix, c.base_scalars()) == \
|
| 462 |
+
(-sin(q0) * c.y + cos(q0) * c.x, sin(q0) * c.x + cos(q0) * c.y, c.z)
|
| 463 |
+
assert c._rotation_trans_equations(c._inverse_rotation_matrix(), c.base_scalars()) == \
|
| 464 |
+
(sin(q0) * c.y + cos(q0) * c.x, -sin(q0) * c.x + cos(q0) * c.y, c.z)
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_dyadic.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import 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.simplify.simplify import simplify
|
| 6 |
+
from sympy.vector import (CoordSys3D, Vector, Dyadic,
|
| 7 |
+
DyadicAdd, DyadicMul, DyadicZero,
|
| 8 |
+
BaseDyadic, express)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
A = CoordSys3D('A')
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_dyadic():
|
| 15 |
+
a, b = symbols('a, b')
|
| 16 |
+
assert Dyadic.zero != 0
|
| 17 |
+
assert isinstance(Dyadic.zero, DyadicZero)
|
| 18 |
+
assert BaseDyadic(A.i, A.j) != BaseDyadic(A.j, A.i)
|
| 19 |
+
assert (BaseDyadic(Vector.zero, A.i) ==
|
| 20 |
+
BaseDyadic(A.i, Vector.zero) == Dyadic.zero)
|
| 21 |
+
|
| 22 |
+
d1 = A.i | A.i
|
| 23 |
+
d2 = A.j | A.j
|
| 24 |
+
d3 = A.i | A.j
|
| 25 |
+
|
| 26 |
+
assert isinstance(d1, BaseDyadic)
|
| 27 |
+
d_mul = a*d1
|
| 28 |
+
assert isinstance(d_mul, DyadicMul)
|
| 29 |
+
assert d_mul.base_dyadic == d1
|
| 30 |
+
assert d_mul.measure_number == a
|
| 31 |
+
assert isinstance(a*d1 + b*d3, DyadicAdd)
|
| 32 |
+
assert d1 == A.i.outer(A.i)
|
| 33 |
+
assert d3 == A.i.outer(A.j)
|
| 34 |
+
v1 = a*A.i - A.k
|
| 35 |
+
v2 = A.i + b*A.j
|
| 36 |
+
assert v1 | v2 == v1.outer(v2) == a * (A.i|A.i) + (a*b) * (A.i|A.j) +\
|
| 37 |
+
- (A.k|A.i) - b * (A.k|A.j)
|
| 38 |
+
assert d1 * 0 == Dyadic.zero
|
| 39 |
+
assert d1 != Dyadic.zero
|
| 40 |
+
assert d1 * 2 == 2 * (A.i | A.i)
|
| 41 |
+
assert d1 / 2. == 0.5 * d1
|
| 42 |
+
|
| 43 |
+
assert d1.dot(0 * d1) == Vector.zero
|
| 44 |
+
assert d1 & d2 == Dyadic.zero
|
| 45 |
+
assert d1.dot(A.i) == A.i == d1 & A.i
|
| 46 |
+
|
| 47 |
+
assert d1.cross(Vector.zero) == Dyadic.zero
|
| 48 |
+
assert d1.cross(A.i) == Dyadic.zero
|
| 49 |
+
assert d1 ^ A.j == d1.cross(A.j)
|
| 50 |
+
assert d1.cross(A.k) == - A.i | A.j
|
| 51 |
+
assert d2.cross(A.i) == - A.j | A.k == d2 ^ A.i
|
| 52 |
+
|
| 53 |
+
assert A.i ^ d1 == Dyadic.zero
|
| 54 |
+
assert A.j.cross(d1) == - A.k | A.i == A.j ^ d1
|
| 55 |
+
assert Vector.zero.cross(d1) == Dyadic.zero
|
| 56 |
+
assert A.k ^ d1 == A.j | A.i
|
| 57 |
+
assert A.i.dot(d1) == A.i & d1 == A.i
|
| 58 |
+
assert A.j.dot(d1) == Vector.zero
|
| 59 |
+
assert Vector.zero.dot(d1) == Vector.zero
|
| 60 |
+
assert A.j & d2 == A.j
|
| 61 |
+
|
| 62 |
+
assert d1.dot(d3) == d1 & d3 == A.i | A.j == d3
|
| 63 |
+
assert d3 & d1 == Dyadic.zero
|
| 64 |
+
|
| 65 |
+
q = symbols('q')
|
| 66 |
+
B = A.orient_new_axis('B', q, A.k)
|
| 67 |
+
assert express(d1, B) == express(d1, B, B)
|
| 68 |
+
|
| 69 |
+
expr1 = ((cos(q)**2) * (B.i | B.i) + (-sin(q) * cos(q)) *
|
| 70 |
+
(B.i | B.j) + (-sin(q) * cos(q)) * (B.j | B.i) + (sin(q)**2) *
|
| 71 |
+
(B.j | B.j))
|
| 72 |
+
assert (express(d1, B) - expr1).simplify() == Dyadic.zero
|
| 73 |
+
|
| 74 |
+
expr2 = (cos(q)) * (B.i | A.i) + (-sin(q)) * (B.j | A.i)
|
| 75 |
+
assert (express(d1, B, A) - expr2).simplify() == Dyadic.zero
|
| 76 |
+
|
| 77 |
+
expr3 = (cos(q)) * (A.i | B.i) + (-sin(q)) * (A.i | B.j)
|
| 78 |
+
assert (express(d1, A, B) - expr3).simplify() == Dyadic.zero
|
| 79 |
+
|
| 80 |
+
assert d1.to_matrix(A) == Matrix([[1, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 81 |
+
assert d1.to_matrix(A, B) == Matrix([[cos(q), -sin(q), 0],
|
| 82 |
+
[0, 0, 0],
|
| 83 |
+
[0, 0, 0]])
|
| 84 |
+
assert d3.to_matrix(A) == Matrix([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
|
| 85 |
+
a, b, c, d, e, f = symbols('a, b, c, d, e, f')
|
| 86 |
+
v1 = a * A.i + b * A.j + c * A.k
|
| 87 |
+
v2 = d * A.i + e * A.j + f * A.k
|
| 88 |
+
d4 = v1.outer(v2)
|
| 89 |
+
assert d4.to_matrix(A) == Matrix([[a * d, a * e, a * f],
|
| 90 |
+
[b * d, b * e, b * f],
|
| 91 |
+
[c * d, c * e, c * f]])
|
| 92 |
+
d5 = v1.outer(v1)
|
| 93 |
+
C = A.orient_new_axis('C', q, A.i)
|
| 94 |
+
for expected, actual in zip(C.rotation_matrix(A) * d5.to_matrix(A) * \
|
| 95 |
+
C.rotation_matrix(A).T, d5.to_matrix(C)):
|
| 96 |
+
assert (expected - actual).simplify() == 0
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_dyadic_simplify():
|
| 100 |
+
x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A')
|
| 101 |
+
N = CoordSys3D('N')
|
| 102 |
+
|
| 103 |
+
dy = N.i | N.i
|
| 104 |
+
test1 = (1 / x + 1 / y) * dy
|
| 105 |
+
assert (N.i & test1 & N.i) != (x + y) / (x * y)
|
| 106 |
+
test1 = test1.simplify()
|
| 107 |
+
assert test1.simplify() == simplify(test1)
|
| 108 |
+
assert (N.i & test1 & N.i) == (x + y) / (x * y)
|
| 109 |
+
|
| 110 |
+
test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * dy
|
| 111 |
+
test2 = test2.simplify()
|
| 112 |
+
assert (N.i & test2 & N.i) == (A**2 * s**4 / (4 * pi * k * m**3))
|
| 113 |
+
|
| 114 |
+
test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * dy
|
| 115 |
+
test3 = test3.simplify()
|
| 116 |
+
assert (N.i & test3 & N.i) == 0
|
| 117 |
+
|
| 118 |
+
test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * dy
|
| 119 |
+
test4 = test4.simplify()
|
| 120 |
+
assert (N.i & test4 & N.i) == -2 * y
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_dyadic_srepr():
|
| 124 |
+
from sympy.printing.repr import srepr
|
| 125 |
+
N = CoordSys3D('N')
|
| 126 |
+
|
| 127 |
+
dy = N.i | N.j
|
| 128 |
+
res = "BaseDyadic(CoordSys3D(Str('N'), Tuple(ImmutableDenseMatrix([["\
|
| 129 |
+
"Integer(1), Integer(0), Integer(0)], [Integer(0), Integer(1), "\
|
| 130 |
+
"Integer(0)], [Integer(0), Integer(0), Integer(1)]]), "\
|
| 131 |
+
"VectorZero())).i, CoordSys3D(Str('N'), Tuple(ImmutableDenseMatrix("\
|
| 132 |
+
"[[Integer(1), Integer(0), Integer(0)], [Integer(0), Integer(1), "\
|
| 133 |
+
"Integer(0)], [Integer(0), Integer(0), Integer(1)]]), VectorZero())).j)"
|
| 134 |
+
assert srepr(dy) == res
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_field_functions.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import Derivative
|
| 2 |
+
from sympy.vector.vector import Vector
|
| 3 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 4 |
+
from sympy.simplify import simplify
|
| 5 |
+
from sympy.core.symbol import symbols
|
| 6 |
+
from sympy.core import S
|
| 7 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 8 |
+
from sympy.vector.vector import Dot
|
| 9 |
+
from sympy.vector.operators import curl, divergence, gradient, Gradient, Divergence, Cross
|
| 10 |
+
from sympy.vector.deloperator import Del
|
| 11 |
+
from sympy.vector.functions import (is_conservative, is_solenoidal,
|
| 12 |
+
scalar_potential, directional_derivative,
|
| 13 |
+
laplacian, scalar_potential_difference)
|
| 14 |
+
from sympy.testing.pytest import raises
|
| 15 |
+
|
| 16 |
+
C = CoordSys3D('C')
|
| 17 |
+
i, j, k = C.base_vectors()
|
| 18 |
+
x, y, z = C.base_scalars()
|
| 19 |
+
delop = Del()
|
| 20 |
+
a, b, c, q = symbols('a b c q')
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_del_operator():
|
| 24 |
+
# Tests for curl
|
| 25 |
+
|
| 26 |
+
assert delop ^ Vector.zero == Vector.zero
|
| 27 |
+
assert ((delop ^ Vector.zero).doit() == Vector.zero ==
|
| 28 |
+
curl(Vector.zero))
|
| 29 |
+
assert delop.cross(Vector.zero) == delop ^ Vector.zero
|
| 30 |
+
assert (delop ^ i).doit() == Vector.zero
|
| 31 |
+
assert delop.cross(2*y**2*j, doit=True) == Vector.zero
|
| 32 |
+
assert delop.cross(2*y**2*j) == delop ^ 2*y**2*j
|
| 33 |
+
v = x*y*z * (i + j + k)
|
| 34 |
+
assert ((delop ^ v).doit() ==
|
| 35 |
+
(-x*y + x*z)*i + (x*y - y*z)*j + (-x*z + y*z)*k ==
|
| 36 |
+
curl(v))
|
| 37 |
+
assert delop ^ v == delop.cross(v)
|
| 38 |
+
assert (delop.cross(2*x**2*j) ==
|
| 39 |
+
(Derivative(0, C.y) - Derivative(2*C.x**2, C.z))*C.i +
|
| 40 |
+
(-Derivative(0, C.x) + Derivative(0, C.z))*C.j +
|
| 41 |
+
(-Derivative(0, C.y) + Derivative(2*C.x**2, C.x))*C.k)
|
| 42 |
+
assert (delop.cross(2*x**2*j, doit=True) == 4*x*k ==
|
| 43 |
+
curl(2*x**2*j))
|
| 44 |
+
|
| 45 |
+
#Tests for divergence
|
| 46 |
+
assert delop & Vector.zero is S.Zero == divergence(Vector.zero)
|
| 47 |
+
assert (delop & Vector.zero).doit() is S.Zero
|
| 48 |
+
assert delop.dot(Vector.zero) == delop & Vector.zero
|
| 49 |
+
assert (delop & i).doit() is S.Zero
|
| 50 |
+
assert (delop & x**2*i).doit() == 2*x == divergence(x**2*i)
|
| 51 |
+
assert (delop.dot(v, doit=True) == x*y + y*z + z*x ==
|
| 52 |
+
divergence(v))
|
| 53 |
+
assert delop & v == delop.dot(v)
|
| 54 |
+
assert delop.dot(1/(x*y*z) * (i + j + k), doit=True) == \
|
| 55 |
+
- 1 / (x*y*z**2) - 1 / (x*y**2*z) - 1 / (x**2*y*z)
|
| 56 |
+
v = x*i + y*j + z*k
|
| 57 |
+
assert (delop & v == Derivative(C.x, C.x) +
|
| 58 |
+
Derivative(C.y, C.y) + Derivative(C.z, C.z))
|
| 59 |
+
assert delop.dot(v, doit=True) == 3 == divergence(v)
|
| 60 |
+
assert delop & v == delop.dot(v)
|
| 61 |
+
assert simplify((delop & v).doit()) == 3
|
| 62 |
+
|
| 63 |
+
#Tests for gradient
|
| 64 |
+
assert (delop.gradient(0, doit=True) == Vector.zero ==
|
| 65 |
+
gradient(0))
|
| 66 |
+
assert delop.gradient(0) == delop(0)
|
| 67 |
+
assert (delop(S.Zero)).doit() == Vector.zero
|
| 68 |
+
assert (delop(x) == (Derivative(C.x, C.x))*C.i +
|
| 69 |
+
(Derivative(C.x, C.y))*C.j + (Derivative(C.x, C.z))*C.k)
|
| 70 |
+
assert (delop(x)).doit() == i == gradient(x)
|
| 71 |
+
assert (delop(x*y*z) ==
|
| 72 |
+
(Derivative(C.x*C.y*C.z, C.x))*C.i +
|
| 73 |
+
(Derivative(C.x*C.y*C.z, C.y))*C.j +
|
| 74 |
+
(Derivative(C.x*C.y*C.z, C.z))*C.k)
|
| 75 |
+
assert (delop.gradient(x*y*z, doit=True) ==
|
| 76 |
+
y*z*i + z*x*j + x*y*k ==
|
| 77 |
+
gradient(x*y*z))
|
| 78 |
+
assert delop(x*y*z) == delop.gradient(x*y*z)
|
| 79 |
+
assert (delop(2*x**2)).doit() == 4*x*i
|
| 80 |
+
assert ((delop(a*sin(y) / x)).doit() ==
|
| 81 |
+
-a*sin(y)/x**2 * i + a*cos(y)/x * j)
|
| 82 |
+
|
| 83 |
+
#Tests for directional derivative
|
| 84 |
+
assert (Vector.zero & delop)(a) is S.Zero
|
| 85 |
+
assert ((Vector.zero & delop)(a)).doit() is S.Zero
|
| 86 |
+
assert ((v & delop)(Vector.zero)).doit() == Vector.zero
|
| 87 |
+
assert ((v & delop)(S.Zero)).doit() is S.Zero
|
| 88 |
+
assert ((i & delop)(x)).doit() == 1
|
| 89 |
+
assert ((j & delop)(y)).doit() == 1
|
| 90 |
+
assert ((k & delop)(z)).doit() == 1
|
| 91 |
+
assert ((i & delop)(x*y*z)).doit() == y*z
|
| 92 |
+
assert ((v & delop)(x)).doit() == x
|
| 93 |
+
assert ((v & delop)(x*y*z)).doit() == 3*x*y*z
|
| 94 |
+
assert (v & delop)(x + y + z) == C.x + C.y + C.z
|
| 95 |
+
assert ((v & delop)(x + y + z)).doit() == x + y + z
|
| 96 |
+
assert ((v & delop)(v)).doit() == v
|
| 97 |
+
assert ((i & delop)(v)).doit() == i
|
| 98 |
+
assert ((j & delop)(v)).doit() == j
|
| 99 |
+
assert ((k & delop)(v)).doit() == k
|
| 100 |
+
assert ((v & delop)(Vector.zero)).doit() == Vector.zero
|
| 101 |
+
|
| 102 |
+
# Tests for laplacian on scalar fields
|
| 103 |
+
assert laplacian(x*y*z) is S.Zero
|
| 104 |
+
assert laplacian(x**2) == S(2)
|
| 105 |
+
assert laplacian(x**2*y**2*z**2) == \
|
| 106 |
+
2*y**2*z**2 + 2*x**2*z**2 + 2*x**2*y**2
|
| 107 |
+
A = CoordSys3D('A', transformation="spherical", variable_names=["r", "theta", "phi"])
|
| 108 |
+
B = CoordSys3D('B', transformation='cylindrical', variable_names=["r", "theta", "z"])
|
| 109 |
+
assert laplacian(A.r + A.theta + A.phi) == 2/A.r + cos(A.theta)/(A.r**2*sin(A.theta))
|
| 110 |
+
assert laplacian(B.r + B.theta + B.z) == 1/B.r
|
| 111 |
+
|
| 112 |
+
# Tests for laplacian on vector fields
|
| 113 |
+
assert laplacian(x*y*z*(i + j + k)) == Vector.zero
|
| 114 |
+
assert laplacian(x*y**2*z*(i + j + k)) == \
|
| 115 |
+
2*x*z*i + 2*x*z*j + 2*x*z*k
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def test_product_rules():
|
| 119 |
+
"""
|
| 120 |
+
Tests the six product rules defined with respect to the Del
|
| 121 |
+
operator
|
| 122 |
+
|
| 123 |
+
References
|
| 124 |
+
==========
|
| 125 |
+
|
| 126 |
+
.. [1] https://en.wikipedia.org/wiki/Del
|
| 127 |
+
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
#Define the scalar and vector functions
|
| 131 |
+
f = 2*x*y*z
|
| 132 |
+
g = x*y + y*z + z*x
|
| 133 |
+
u = x**2*i + 4*j - y**2*z*k
|
| 134 |
+
v = 4*i + x*y*z*k
|
| 135 |
+
|
| 136 |
+
# First product rule
|
| 137 |
+
lhs = delop(f * g, doit=True)
|
| 138 |
+
rhs = (f * delop(g) + g * delop(f)).doit()
|
| 139 |
+
assert simplify(lhs) == simplify(rhs)
|
| 140 |
+
|
| 141 |
+
# Second product rule
|
| 142 |
+
lhs = delop(u & v).doit()
|
| 143 |
+
rhs = ((u ^ (delop ^ v)) + (v ^ (delop ^ u)) + \
|
| 144 |
+
((u & delop)(v)) + ((v & delop)(u))).doit()
|
| 145 |
+
assert simplify(lhs) == simplify(rhs)
|
| 146 |
+
|
| 147 |
+
# Third product rule
|
| 148 |
+
lhs = (delop & (f*v)).doit()
|
| 149 |
+
rhs = ((f * (delop & v)) + (v & (delop(f)))).doit()
|
| 150 |
+
assert simplify(lhs) == simplify(rhs)
|
| 151 |
+
|
| 152 |
+
# Fourth product rule
|
| 153 |
+
lhs = (delop & (u ^ v)).doit()
|
| 154 |
+
rhs = ((v & (delop ^ u)) - (u & (delop ^ v))).doit()
|
| 155 |
+
assert simplify(lhs) == simplify(rhs)
|
| 156 |
+
|
| 157 |
+
# Fifth product rule
|
| 158 |
+
lhs = (delop ^ (f * v)).doit()
|
| 159 |
+
rhs = (((delop(f)) ^ v) + (f * (delop ^ v))).doit()
|
| 160 |
+
assert simplify(lhs) == simplify(rhs)
|
| 161 |
+
|
| 162 |
+
# Sixth product rule
|
| 163 |
+
lhs = (delop ^ (u ^ v)).doit()
|
| 164 |
+
rhs = (u * (delop & v) - v * (delop & u) +
|
| 165 |
+
(v & delop)(u) - (u & delop)(v)).doit()
|
| 166 |
+
assert simplify(lhs) == simplify(rhs)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
P = C.orient_new_axis('P', q, C.k) # type: ignore
|
| 170 |
+
scalar_field = 2*x**2*y*z
|
| 171 |
+
grad_field = gradient(scalar_field)
|
| 172 |
+
vector_field = y**2*i + 3*x*j + 5*y*z*k
|
| 173 |
+
curl_field = curl(vector_field)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def test_conservative():
|
| 177 |
+
assert is_conservative(Vector.zero) is True
|
| 178 |
+
assert is_conservative(i) is True
|
| 179 |
+
assert is_conservative(2 * i + 3 * j + 4 * k) is True
|
| 180 |
+
assert (is_conservative(y*z*i + x*z*j + x*y*k) is
|
| 181 |
+
True)
|
| 182 |
+
assert is_conservative(x * j) is False
|
| 183 |
+
assert is_conservative(grad_field) is True
|
| 184 |
+
assert is_conservative(curl_field) is False
|
| 185 |
+
assert (is_conservative(4*x*y*z*i + 2*x**2*z*j) is
|
| 186 |
+
False)
|
| 187 |
+
assert is_conservative(z*P.i + P.x*k) is True
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_solenoidal():
|
| 191 |
+
assert is_solenoidal(Vector.zero) is True
|
| 192 |
+
assert is_solenoidal(i) is True
|
| 193 |
+
assert is_solenoidal(2 * i + 3 * j + 4 * k) is True
|
| 194 |
+
assert (is_solenoidal(y*z*i + x*z*j + x*y*k) is
|
| 195 |
+
True)
|
| 196 |
+
assert is_solenoidal(y * j) is False
|
| 197 |
+
assert is_solenoidal(grad_field) is False
|
| 198 |
+
assert is_solenoidal(curl_field) is True
|
| 199 |
+
assert is_solenoidal((-2*y + 3)*k) is True
|
| 200 |
+
assert is_solenoidal(cos(q)*i + sin(q)*j + cos(q)*P.k) is True
|
| 201 |
+
assert is_solenoidal(z*P.i + P.x*k) is True
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def test_directional_derivative():
|
| 205 |
+
assert directional_derivative(C.x*C.y*C.z, 3*C.i + 4*C.j + C.k) == C.x*C.y + 4*C.x*C.z + 3*C.y*C.z
|
| 206 |
+
assert directional_derivative(5*C.x**2*C.z, 3*C.i + 4*C.j + C.k) == 5*C.x**2 + 30*C.x*C.z
|
| 207 |
+
assert directional_derivative(5*C.x**2*C.z, 4*C.j) is S.Zero
|
| 208 |
+
|
| 209 |
+
D = CoordSys3D("D", "spherical", variable_names=["r", "theta", "phi"],
|
| 210 |
+
vector_names=["e_r", "e_theta", "e_phi"])
|
| 211 |
+
r, theta, phi = D.base_scalars()
|
| 212 |
+
e_r, e_theta, e_phi = D.base_vectors()
|
| 213 |
+
assert directional_derivative(r**2*e_r, e_r) == 2*r*e_r
|
| 214 |
+
assert directional_derivative(5*r**2*phi, 3*e_r + 4*e_theta + e_phi) == 5*r**2 + 30*r*phi
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def test_scalar_potential():
|
| 218 |
+
assert scalar_potential(Vector.zero, C) == 0
|
| 219 |
+
assert scalar_potential(i, C) == x
|
| 220 |
+
assert scalar_potential(j, C) == y
|
| 221 |
+
assert scalar_potential(k, C) == z
|
| 222 |
+
assert scalar_potential(y*z*i + x*z*j + x*y*k, C) == x*y*z
|
| 223 |
+
assert scalar_potential(grad_field, C) == scalar_field
|
| 224 |
+
assert scalar_potential(z*P.i + P.x*k, C) == x*z*cos(q) + y*z*sin(q)
|
| 225 |
+
assert scalar_potential(z*P.i + P.x*k, P) == P.x*P.z
|
| 226 |
+
raises(ValueError, lambda: scalar_potential(x*j, C))
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def test_scalar_potential_difference():
|
| 230 |
+
point1 = C.origin.locate_new('P1', 1*i + 2*j + 3*k)
|
| 231 |
+
point2 = C.origin.locate_new('P2', 4*i + 5*j + 6*k)
|
| 232 |
+
genericpointC = C.origin.locate_new('RP', x*i + y*j + z*k)
|
| 233 |
+
genericpointP = P.origin.locate_new('PP', P.x*P.i + P.y*P.j + P.z*P.k)
|
| 234 |
+
assert scalar_potential_difference(S.Zero, C, point1, point2) == 0
|
| 235 |
+
assert (scalar_potential_difference(scalar_field, C, C.origin,
|
| 236 |
+
genericpointC) ==
|
| 237 |
+
scalar_field)
|
| 238 |
+
assert (scalar_potential_difference(grad_field, C, C.origin,
|
| 239 |
+
genericpointC) ==
|
| 240 |
+
scalar_field)
|
| 241 |
+
assert scalar_potential_difference(grad_field, C, point1, point2) == 948
|
| 242 |
+
assert (scalar_potential_difference(y*z*i + x*z*j +
|
| 243 |
+
x*y*k, C, point1,
|
| 244 |
+
genericpointC) ==
|
| 245 |
+
x*y*z - 6)
|
| 246 |
+
potential_diff_P = (2*P.z*(P.x*sin(q) + P.y*cos(q))*
|
| 247 |
+
(P.x*cos(q) - P.y*sin(q))**2)
|
| 248 |
+
assert (scalar_potential_difference(grad_field, P, P.origin,
|
| 249 |
+
genericpointP).simplify() ==
|
| 250 |
+
potential_diff_P.simplify())
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def test_differential_operators_curvilinear_system():
|
| 254 |
+
A = CoordSys3D('A', transformation="spherical", variable_names=["r", "theta", "phi"])
|
| 255 |
+
B = CoordSys3D('B', transformation='cylindrical', variable_names=["r", "theta", "z"])
|
| 256 |
+
# Test for spherical coordinate system and gradient
|
| 257 |
+
assert gradient(3*A.r + 4*A.theta) == 3*A.i + 4/A.r*A.j
|
| 258 |
+
assert gradient(3*A.r*A.phi + 4*A.theta) == 3*A.phi*A.i + 4/A.r*A.j + (3/sin(A.theta))*A.k
|
| 259 |
+
assert gradient(0*A.r + 0*A.theta+0*A.phi) == Vector.zero
|
| 260 |
+
assert gradient(A.r*A.theta*A.phi) == A.theta*A.phi*A.i + A.phi*A.j + (A.theta/sin(A.theta))*A.k
|
| 261 |
+
# Test for spherical coordinate system and divergence
|
| 262 |
+
assert divergence(A.r * A.i + A.theta * A.j + A.phi * A.k) == \
|
| 263 |
+
(sin(A.theta)*A.r + cos(A.theta)*A.r*A.theta)/(sin(A.theta)*A.r**2) + 3 + 1/(sin(A.theta)*A.r)
|
| 264 |
+
assert divergence(3*A.r*A.phi*A.i + A.theta*A.j + A.r*A.theta*A.phi*A.k) == \
|
| 265 |
+
(sin(A.theta)*A.r + cos(A.theta)*A.r*A.theta)/(sin(A.theta)*A.r**2) + 9*A.phi + A.theta/sin(A.theta)
|
| 266 |
+
assert divergence(Vector.zero) == 0
|
| 267 |
+
assert divergence(0*A.i + 0*A.j + 0*A.k) == 0
|
| 268 |
+
# Test for spherical coordinate system and curl
|
| 269 |
+
assert curl(A.r*A.i + A.theta*A.j + A.phi*A.k) == \
|
| 270 |
+
(cos(A.theta)*A.phi/(sin(A.theta)*A.r))*A.i + (-A.phi/A.r)*A.j + A.theta/A.r*A.k
|
| 271 |
+
assert curl(A.r*A.j + A.phi*A.k) == (cos(A.theta)*A.phi/(sin(A.theta)*A.r))*A.i + (-A.phi/A.r)*A.j + 2*A.k
|
| 272 |
+
|
| 273 |
+
# Test for cylindrical coordinate system and gradient
|
| 274 |
+
assert gradient(0*B.r + 0*B.theta+0*B.z) == Vector.zero
|
| 275 |
+
assert gradient(B.r*B.theta*B.z) == B.theta*B.z*B.i + B.z*B.j + B.r*B.theta*B.k
|
| 276 |
+
assert gradient(3*B.r) == 3*B.i
|
| 277 |
+
assert gradient(2*B.theta) == 2/B.r * B.j
|
| 278 |
+
assert gradient(4*B.z) == 4*B.k
|
| 279 |
+
# Test for cylindrical coordinate system and divergence
|
| 280 |
+
assert divergence(B.r*B.i + B.theta*B.j + B.z*B.k) == 3 + 1/B.r
|
| 281 |
+
assert divergence(B.r*B.j + B.z*B.k) == 1
|
| 282 |
+
# Test for cylindrical coordinate system and curl
|
| 283 |
+
assert curl(B.r*B.j + B.z*B.k) == 2*B.k
|
| 284 |
+
assert curl(3*B.i + 2/B.r*B.j + 4*B.k) == Vector.zero
|
| 285 |
+
|
| 286 |
+
def test_mixed_coordinates():
|
| 287 |
+
# gradient
|
| 288 |
+
a = CoordSys3D('a')
|
| 289 |
+
b = CoordSys3D('b')
|
| 290 |
+
c = CoordSys3D('c')
|
| 291 |
+
assert gradient(a.x*b.y) == b.y*a.i + a.x*b.j
|
| 292 |
+
assert gradient(3*cos(q)*a.x*b.x+a.y*(a.x+(cos(q)+b.x))) ==\
|
| 293 |
+
(a.y + 3*b.x*cos(q))*a.i + (a.x + b.x + cos(q))*a.j + (3*a.x*cos(q) + a.y)*b.i
|
| 294 |
+
# Some tests need further work:
|
| 295 |
+
# assert gradient(a.x*(cos(a.x+b.x))) == (cos(a.x + b.x))*a.i + a.x*Gradient(cos(a.x + b.x))
|
| 296 |
+
# assert gradient(cos(a.x + b.x)*cos(a.x + b.z)) == Gradient(cos(a.x + b.x)*cos(a.x + b.z))
|
| 297 |
+
assert gradient(a.x**b.y) == Gradient(a.x**b.y)
|
| 298 |
+
# assert gradient(cos(a.x+b.y)*a.z) == None
|
| 299 |
+
assert gradient(cos(a.x*b.y)) == Gradient(cos(a.x*b.y))
|
| 300 |
+
assert gradient(3*cos(q)*a.x*b.x*a.z*a.y+ b.y*b.z + cos(a.x+a.y)*b.z) == \
|
| 301 |
+
(3*a.y*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.i + \
|
| 302 |
+
(3*a.x*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.j + (3*a.x*a.y*b.x*cos(q))*a.k + \
|
| 303 |
+
(3*a.x*a.y*a.z*cos(q))*b.i + b.z*b.j + (b.y + cos(a.x + a.y))*b.k
|
| 304 |
+
# divergence
|
| 305 |
+
assert divergence(a.i*a.x+a.j*a.y+a.z*a.k + b.i*b.x+b.j*b.y+b.z*b.k + c.i*c.x+c.j*c.y+c.z*c.k) == S(9)
|
| 306 |
+
# assert divergence(3*a.i*a.x*cos(a.x+b.z) + a.j*b.x*c.z) == None
|
| 307 |
+
assert divergence(3*a.i*a.x*a.z + b.j*b.x*c.z + 3*a.j*a.z*a.y) == \
|
| 308 |
+
6*a.z + b.x*Dot(b.j, c.k)
|
| 309 |
+
assert divergence(3*cos(q)*a.x*b.x*b.i*c.x) == \
|
| 310 |
+
3*a.x*b.x*cos(q)*Dot(b.i, c.i) + 3*a.x*c.x*cos(q) + 3*b.x*c.x*cos(q)*Dot(b.i, a.i)
|
| 311 |
+
assert divergence(a.x*b.x*c.x*Cross(a.x*a.i, a.y*b.j)) ==\
|
| 312 |
+
a.x*b.x*c.x*Divergence(Cross(a.x*a.i, a.y*b.j)) + \
|
| 313 |
+
b.x*c.x*Dot(Cross(a.x*a.i, a.y*b.j), a.i) + \
|
| 314 |
+
a.x*c.x*Dot(Cross(a.x*a.i, a.y*b.j), b.i) + \
|
| 315 |
+
a.x*b.x*Dot(Cross(a.x*a.i, a.y*b.j), c.i)
|
| 316 |
+
assert divergence(a.x*b.x*c.x*(a.x*a.i + b.x*b.i)) == \
|
| 317 |
+
4*a.x*b.x*c.x +\
|
| 318 |
+
a.x**2*c.x*Dot(a.i, b.i) +\
|
| 319 |
+
a.x**2*b.x*Dot(a.i, c.i) +\
|
| 320 |
+
b.x**2*c.x*Dot(b.i, a.i) +\
|
| 321 |
+
a.x*b.x**2*Dot(b.i, c.i)
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_functions.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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rect import CoordSys3D
|
| 3 |
+
from sympy.vector.functions import express, matrix_to_vector, orthogonalize
|
| 4 |
+
from sympy.core.numbers import Rational
|
| 5 |
+
from sympy.core.singleton import S
|
| 6 |
+
from sympy.core.symbol import symbols
|
| 7 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 8 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 9 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 10 |
+
from sympy.testing.pytest import raises
|
| 11 |
+
|
| 12 |
+
N = CoordSys3D('N')
|
| 13 |
+
q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5')
|
| 14 |
+
A = N.orient_new_axis('A', q1, N.k) # type: ignore
|
| 15 |
+
B = A.orient_new_axis('B', q2, A.i)
|
| 16 |
+
C = B.orient_new_axis('C', q3, B.j)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_express():
|
| 20 |
+
assert express(Vector.zero, N) == Vector.zero
|
| 21 |
+
assert express(S.Zero, N) is S.Zero
|
| 22 |
+
assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k
|
| 23 |
+
assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
|
| 24 |
+
sin(q2)*cos(q3)*C.k
|
| 25 |
+
assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
|
| 26 |
+
cos(q2)*cos(q3)*C.k
|
| 27 |
+
assert express(A.i, N) == cos(q1)*N.i + sin(q1)*N.j
|
| 28 |
+
assert express(A.j, N) == -sin(q1)*N.i + cos(q1)*N.j
|
| 29 |
+
assert express(A.k, N) == N.k
|
| 30 |
+
assert express(A.i, A) == A.i
|
| 31 |
+
assert express(A.j, A) == A.j
|
| 32 |
+
assert express(A.k, A) == A.k
|
| 33 |
+
assert express(A.i, B) == B.i
|
| 34 |
+
assert express(A.j, B) == cos(q2)*B.j - sin(q2)*B.k
|
| 35 |
+
assert express(A.k, B) == sin(q2)*B.j + cos(q2)*B.k
|
| 36 |
+
assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k
|
| 37 |
+
assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
|
| 38 |
+
sin(q2)*cos(q3)*C.k
|
| 39 |
+
assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
|
| 40 |
+
cos(q2)*cos(q3)*C.k
|
| 41 |
+
# Check to make sure UnitVectors get converted properly
|
| 42 |
+
assert express(N.i, N) == N.i
|
| 43 |
+
assert express(N.j, N) == N.j
|
| 44 |
+
assert express(N.k, N) == N.k
|
| 45 |
+
assert express(N.i, A) == (cos(q1)*A.i - sin(q1)*A.j)
|
| 46 |
+
assert express(N.j, A) == (sin(q1)*A.i + cos(q1)*A.j)
|
| 47 |
+
assert express(N.k, A) == A.k
|
| 48 |
+
assert express(N.i, B) == (cos(q1)*B.i - sin(q1)*cos(q2)*B.j +
|
| 49 |
+
sin(q1)*sin(q2)*B.k)
|
| 50 |
+
assert express(N.j, B) == (sin(q1)*B.i + cos(q1)*cos(q2)*B.j -
|
| 51 |
+
sin(q2)*cos(q1)*B.k)
|
| 52 |
+
assert express(N.k, B) == (sin(q2)*B.j + cos(q2)*B.k)
|
| 53 |
+
assert express(N.i, C) == (
|
| 54 |
+
(cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*C.i -
|
| 55 |
+
sin(q1)*cos(q2)*C.j +
|
| 56 |
+
(sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*C.k)
|
| 57 |
+
assert express(N.j, C) == (
|
| 58 |
+
(sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.i +
|
| 59 |
+
cos(q1)*cos(q2)*C.j +
|
| 60 |
+
(sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.k)
|
| 61 |
+
assert express(N.k, C) == (-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
|
| 62 |
+
cos(q2)*cos(q3)*C.k)
|
| 63 |
+
|
| 64 |
+
assert express(A.i, N) == (cos(q1)*N.i + sin(q1)*N.j)
|
| 65 |
+
assert express(A.j, N) == (-sin(q1)*N.i + cos(q1)*N.j)
|
| 66 |
+
assert express(A.k, N) == N.k
|
| 67 |
+
assert express(A.i, A) == A.i
|
| 68 |
+
assert express(A.j, A) == A.j
|
| 69 |
+
assert express(A.k, A) == A.k
|
| 70 |
+
assert express(A.i, B) == B.i
|
| 71 |
+
assert express(A.j, B) == (cos(q2)*B.j - sin(q2)*B.k)
|
| 72 |
+
assert express(A.k, B) == (sin(q2)*B.j + cos(q2)*B.k)
|
| 73 |
+
assert express(A.i, C) == (cos(q3)*C.i + sin(q3)*C.k)
|
| 74 |
+
assert express(A.j, C) == (sin(q2)*sin(q3)*C.i + cos(q2)*C.j -
|
| 75 |
+
sin(q2)*cos(q3)*C.k)
|
| 76 |
+
assert express(A.k, C) == (-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
|
| 77 |
+
cos(q2)*cos(q3)*C.k)
|
| 78 |
+
|
| 79 |
+
assert express(B.i, N) == (cos(q1)*N.i + sin(q1)*N.j)
|
| 80 |
+
assert express(B.j, N) == (-sin(q1)*cos(q2)*N.i +
|
| 81 |
+
cos(q1)*cos(q2)*N.j + sin(q2)*N.k)
|
| 82 |
+
assert express(B.k, N) == (sin(q1)*sin(q2)*N.i -
|
| 83 |
+
sin(q2)*cos(q1)*N.j + cos(q2)*N.k)
|
| 84 |
+
assert express(B.i, A) == A.i
|
| 85 |
+
assert express(B.j, A) == (cos(q2)*A.j + sin(q2)*A.k)
|
| 86 |
+
assert express(B.k, A) == (-sin(q2)*A.j + cos(q2)*A.k)
|
| 87 |
+
assert express(B.i, B) == B.i
|
| 88 |
+
assert express(B.j, B) == B.j
|
| 89 |
+
assert express(B.k, B) == B.k
|
| 90 |
+
assert express(B.i, C) == (cos(q3)*C.i + sin(q3)*C.k)
|
| 91 |
+
assert express(B.j, C) == C.j
|
| 92 |
+
assert express(B.k, C) == (-sin(q3)*C.i + cos(q3)*C.k)
|
| 93 |
+
|
| 94 |
+
assert express(C.i, N) == (
|
| 95 |
+
(cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*N.i +
|
| 96 |
+
(sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*N.j -
|
| 97 |
+
sin(q3)*cos(q2)*N.k)
|
| 98 |
+
assert express(C.j, N) == (
|
| 99 |
+
-sin(q1)*cos(q2)*N.i + cos(q1)*cos(q2)*N.j + sin(q2)*N.k)
|
| 100 |
+
assert express(C.k, N) == (
|
| 101 |
+
(sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*N.i +
|
| 102 |
+
(sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*N.j +
|
| 103 |
+
cos(q2)*cos(q3)*N.k)
|
| 104 |
+
assert express(C.i, A) == (cos(q3)*A.i + sin(q2)*sin(q3)*A.j -
|
| 105 |
+
sin(q3)*cos(q2)*A.k)
|
| 106 |
+
assert express(C.j, A) == (cos(q2)*A.j + sin(q2)*A.k)
|
| 107 |
+
assert express(C.k, A) == (sin(q3)*A.i - sin(q2)*cos(q3)*A.j +
|
| 108 |
+
cos(q2)*cos(q3)*A.k)
|
| 109 |
+
assert express(C.i, B) == (cos(q3)*B.i - sin(q3)*B.k)
|
| 110 |
+
assert express(C.j, B) == B.j
|
| 111 |
+
assert express(C.k, B) == (sin(q3)*B.i + cos(q3)*B.k)
|
| 112 |
+
assert express(C.i, C) == C.i
|
| 113 |
+
assert express(C.j, C) == C.j
|
| 114 |
+
assert express(C.k, C) == C.k == (C.k)
|
| 115 |
+
|
| 116 |
+
# Check to make sure Vectors get converted back to UnitVectors
|
| 117 |
+
assert N.i == express((cos(q1)*A.i - sin(q1)*A.j), N).simplify()
|
| 118 |
+
assert N.j == express((sin(q1)*A.i + cos(q1)*A.j), N).simplify()
|
| 119 |
+
assert N.i == express((cos(q1)*B.i - sin(q1)*cos(q2)*B.j +
|
| 120 |
+
sin(q1)*sin(q2)*B.k), N).simplify()
|
| 121 |
+
assert N.j == express((sin(q1)*B.i + cos(q1)*cos(q2)*B.j -
|
| 122 |
+
sin(q2)*cos(q1)*B.k), N).simplify()
|
| 123 |
+
assert N.k == express((sin(q2)*B.j + cos(q2)*B.k), N).simplify()
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
assert A.i == express((cos(q1)*N.i + sin(q1)*N.j), A).simplify()
|
| 127 |
+
assert A.j == express((-sin(q1)*N.i + cos(q1)*N.j), A).simplify()
|
| 128 |
+
|
| 129 |
+
assert A.j == express((cos(q2)*B.j - sin(q2)*B.k), A).simplify()
|
| 130 |
+
assert A.k == express((sin(q2)*B.j + cos(q2)*B.k), A).simplify()
|
| 131 |
+
|
| 132 |
+
assert A.i == express((cos(q3)*C.i + sin(q3)*C.k), A).simplify()
|
| 133 |
+
assert A.j == express((sin(q2)*sin(q3)*C.i + cos(q2)*C.j -
|
| 134 |
+
sin(q2)*cos(q3)*C.k), A).simplify()
|
| 135 |
+
|
| 136 |
+
assert A.k == express((-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
|
| 137 |
+
cos(q2)*cos(q3)*C.k), A).simplify()
|
| 138 |
+
assert B.i == express((cos(q1)*N.i + sin(q1)*N.j), B).simplify()
|
| 139 |
+
assert B.j == express((-sin(q1)*cos(q2)*N.i +
|
| 140 |
+
cos(q1)*cos(q2)*N.j + sin(q2)*N.k), B).simplify()
|
| 141 |
+
|
| 142 |
+
assert B.k == express((sin(q1)*sin(q2)*N.i -
|
| 143 |
+
sin(q2)*cos(q1)*N.j + cos(q2)*N.k), B).simplify()
|
| 144 |
+
|
| 145 |
+
assert B.j == express((cos(q2)*A.j + sin(q2)*A.k), B).simplify()
|
| 146 |
+
assert B.k == express((-sin(q2)*A.j + cos(q2)*A.k), B).simplify()
|
| 147 |
+
assert B.i == express((cos(q3)*C.i + sin(q3)*C.k), B).simplify()
|
| 148 |
+
assert B.k == express((-sin(q3)*C.i + cos(q3)*C.k), B).simplify()
|
| 149 |
+
assert C.i == express((cos(q3)*A.i + sin(q2)*sin(q3)*A.j -
|
| 150 |
+
sin(q3)*cos(q2)*A.k), C).simplify()
|
| 151 |
+
assert C.j == express((cos(q2)*A.j + sin(q2)*A.k), C).simplify()
|
| 152 |
+
assert C.k == express((sin(q3)*A.i - sin(q2)*cos(q3)*A.j +
|
| 153 |
+
cos(q2)*cos(q3)*A.k), C).simplify()
|
| 154 |
+
assert C.i == express((cos(q3)*B.i - sin(q3)*B.k), C).simplify()
|
| 155 |
+
assert C.k == express((sin(q3)*B.i + cos(q3)*B.k), C).simplify()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def test_matrix_to_vector():
|
| 159 |
+
m = Matrix([[1], [2], [3]])
|
| 160 |
+
assert matrix_to_vector(m, C) == C.i + 2*C.j + 3*C.k
|
| 161 |
+
m = Matrix([[0], [0], [0]])
|
| 162 |
+
assert matrix_to_vector(m, N) == matrix_to_vector(m, C) == \
|
| 163 |
+
Vector.zero
|
| 164 |
+
m = Matrix([[q1], [q2], [q3]])
|
| 165 |
+
assert matrix_to_vector(m, N) == q1*N.i + q2*N.j + q3*N.k
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def test_orthogonalize():
|
| 169 |
+
C = CoordSys3D('C')
|
| 170 |
+
a, b = symbols('a b', integer=True)
|
| 171 |
+
i, j, k = C.base_vectors()
|
| 172 |
+
v1 = i + 2*j
|
| 173 |
+
v2 = 2*i + 3*j
|
| 174 |
+
v3 = 3*i + 5*j
|
| 175 |
+
v4 = 3*i + j
|
| 176 |
+
v5 = 2*i + 2*j
|
| 177 |
+
v6 = a*i + b*j
|
| 178 |
+
v7 = 4*a*i + 4*b*j
|
| 179 |
+
assert orthogonalize(v1, v2) == [C.i + 2*C.j, C.i*Rational(2, 5) + -C.j/5]
|
| 180 |
+
# from wikipedia
|
| 181 |
+
assert orthogonalize(v4, v5, orthonormal=True) == \
|
| 182 |
+
[(3*sqrt(10))*C.i/10 + (sqrt(10))*C.j/10, (-sqrt(10))*C.i/10 + (3*sqrt(10))*C.j/10]
|
| 183 |
+
raises(ValueError, lambda: orthogonalize(v1, v2, v3))
|
| 184 |
+
raises(ValueError, lambda: orthogonalize(v6, v7))
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_implicitregion.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.relational import Eq
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.abc import x, y, z, s, t
|
| 4 |
+
from sympy.sets import FiniteSet, EmptySet
|
| 5 |
+
from sympy.geometry import Point
|
| 6 |
+
from sympy.vector import ImplicitRegion
|
| 7 |
+
from sympy.testing.pytest import raises
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_ImplicitRegion():
|
| 11 |
+
ellipse = ImplicitRegion((x, y), (x**2/4 + y**2/16 - 1))
|
| 12 |
+
assert ellipse.equation == x**2/4 + y**2/16 - 1
|
| 13 |
+
assert ellipse.variables == (x, y)
|
| 14 |
+
assert ellipse.degree == 2
|
| 15 |
+
r = ImplicitRegion((x, y, z), Eq(x**4 + y**2 - x*y, 6))
|
| 16 |
+
assert r.equation == x**4 + y**2 - x*y - 6
|
| 17 |
+
assert r.variables == (x, y, z)
|
| 18 |
+
assert r.degree == 4
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_regular_point():
|
| 22 |
+
r1 = ImplicitRegion((x,), x**2 - 16)
|
| 23 |
+
assert r1.regular_point() == (-4,)
|
| 24 |
+
c1 = ImplicitRegion((x, y), x**2 + y**2 - 4)
|
| 25 |
+
assert c1.regular_point() == (0, -2)
|
| 26 |
+
c2 = ImplicitRegion((x, y), (x - S(5)/2)**2 + y**2 - (S(1)/4)**2)
|
| 27 |
+
assert c2.regular_point() == (S(5)/2, -S(1)/4)
|
| 28 |
+
c3 = ImplicitRegion((x, y), (y - 5)**2 - 16*(x - 5))
|
| 29 |
+
assert c3.regular_point() == (5, 5)
|
| 30 |
+
r2 = ImplicitRegion((x, y), x**2 - 4*x*y - 3*y**2 + 4*x + 8*y - 5)
|
| 31 |
+
assert r2.regular_point() == (S(4)/7, S(9)/7)
|
| 32 |
+
r3 = ImplicitRegion((x, y), x**2 - 2*x*y + 3*y**2 - 2*x - 5*y + 3/2)
|
| 33 |
+
raises(ValueError, lambda: r3.regular_point())
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_singular_points_and_multiplicty():
|
| 37 |
+
r1 = ImplicitRegion((x, y, z), Eq(x + y + z, 0))
|
| 38 |
+
assert r1.singular_points() == EmptySet
|
| 39 |
+
r2 = ImplicitRegion((x, y, z), x*y*z + y**4 -x**2*z**2)
|
| 40 |
+
assert r2.singular_points() == FiniteSet((0, 0, z), (x, 0, 0))
|
| 41 |
+
assert r2.multiplicity((0, 0, 0)) == 3
|
| 42 |
+
assert r2.multiplicity((0, 0, 6)) == 2
|
| 43 |
+
r3 = ImplicitRegion((x, y, z), z**2 - x**2 - y**2)
|
| 44 |
+
assert r3.singular_points() == FiniteSet((0, 0, 0))
|
| 45 |
+
assert r3.multiplicity((0, 0, 0)) == 2
|
| 46 |
+
r4 = ImplicitRegion((x, y), x**2 + y**2 - 2*x)
|
| 47 |
+
assert r4.singular_points() == EmptySet
|
| 48 |
+
assert r4.multiplicity(Point(1, 3)) == 0
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_rational_parametrization():
|
| 52 |
+
p = ImplicitRegion((x,), x - 2)
|
| 53 |
+
assert p.rational_parametrization() == (x - 2,)
|
| 54 |
+
|
| 55 |
+
line = ImplicitRegion((x, y), Eq(y, 3*x + 2))
|
| 56 |
+
assert line.rational_parametrization() == (x, 3*x + 2)
|
| 57 |
+
|
| 58 |
+
circle1 = ImplicitRegion((x, y), (x-2)**2 + (y+3)**2 - 4)
|
| 59 |
+
assert circle1.rational_parametrization(parameters=t) == (4*t/(t**2 + 1) + 2, 4*t**2/(t**2 + 1) - 5)
|
| 60 |
+
circle2 = ImplicitRegion((x, y), (x - S.Half)**2 + y**2 - (S(1)/2)**2)
|
| 61 |
+
|
| 62 |
+
assert circle2.rational_parametrization(parameters=t) == (t/(t**2 + 1) + S(1)/2, t**2/(t**2 + 1) - S(1)/2)
|
| 63 |
+
circle3 = ImplicitRegion((x, y), Eq(x**2 + y**2, 2*x))
|
| 64 |
+
assert circle3.rational_parametrization(parameters=(t,)) == (2*t/(t**2 + 1) + 1, 2*t**2/(t**2 + 1) - 1)
|
| 65 |
+
|
| 66 |
+
parabola = ImplicitRegion((x, y), (y - 3)**2 - 4*(x + 6))
|
| 67 |
+
assert parabola.rational_parametrization(t) == (-6 + 4/t**2, 3 + 4/t)
|
| 68 |
+
|
| 69 |
+
rect_hyperbola = ImplicitRegion((x, y), x*y - 1)
|
| 70 |
+
assert rect_hyperbola.rational_parametrization(t) == (-1 + (t + 1)/t, t)
|
| 71 |
+
|
| 72 |
+
cubic_curve = ImplicitRegion((x, y), x**3 + x**2 - y**2)
|
| 73 |
+
assert cubic_curve.rational_parametrization(parameters=(t)) == (t**2 - 1, t*(t**2 - 1))
|
| 74 |
+
cuspidal = ImplicitRegion((x, y), (x**3 - y**2))
|
| 75 |
+
assert cuspidal.rational_parametrization(t) == (t**2, t**3)
|
| 76 |
+
|
| 77 |
+
I = ImplicitRegion((x, y), x**3 + x**2 - y**2)
|
| 78 |
+
assert I.rational_parametrization(t) == (t**2 - 1, t*(t**2 - 1))
|
| 79 |
+
|
| 80 |
+
sphere = ImplicitRegion((x, y, z), Eq(x**2 + y**2 + z**2, 2*x))
|
| 81 |
+
assert sphere.rational_parametrization(parameters=(s, t)) == (2/(s**2 + t**2 + 1), 2*t/(s**2 + t**2 + 1), 2*s/(s**2 + t**2 + 1))
|
| 82 |
+
|
| 83 |
+
conic = ImplicitRegion((x, y), Eq(x**2 + 4*x*y + 3*y**2 + x - y + 10, 0))
|
| 84 |
+
assert conic.rational_parametrization(t) == (
|
| 85 |
+
S(17)/2 + 4/(3*t**2 + 4*t + 1), 4*t/(3*t**2 + 4*t + 1) - S(11)/2)
|
| 86 |
+
|
| 87 |
+
r1 = ImplicitRegion((x, y), y**2 - x**3 + x)
|
| 88 |
+
raises(NotImplementedError, lambda: r1.rational_parametrization())
|
| 89 |
+
r2 = ImplicitRegion((x, y), y**2 - x**3 - x**2 + 1)
|
| 90 |
+
raises(NotImplementedError, lambda: r2.rational_parametrization())
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_integrals.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import pi
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 4 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 5 |
+
from sympy.testing.pytest import raises
|
| 6 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 7 |
+
from sympy.vector.integrals import ParametricIntegral, vector_integrate
|
| 8 |
+
from sympy.vector.parametricregion import ParametricRegion
|
| 9 |
+
from sympy.vector.implicitregion import ImplicitRegion
|
| 10 |
+
from sympy.abc import x, y, z, u, v, r, t, theta, phi
|
| 11 |
+
from sympy.geometry import Point, Segment, Curve, Circle, Polygon, Plane
|
| 12 |
+
|
| 13 |
+
C = CoordSys3D('C')
|
| 14 |
+
|
| 15 |
+
def test_parametric_lineintegrals():
|
| 16 |
+
halfcircle = ParametricRegion((4*cos(theta), 4*sin(theta)), (theta, -pi/2, pi/2))
|
| 17 |
+
assert ParametricIntegral(C.x*C.y**4, halfcircle) == S(8192)/5
|
| 18 |
+
|
| 19 |
+
curve = ParametricRegion((t, t**2, t**3), (t, 0, 1))
|
| 20 |
+
field1 = 8*C.x**2*C.y*C.z*C.i + 5*C.z*C.j - 4*C.x*C.y*C.k
|
| 21 |
+
assert ParametricIntegral(field1, curve) == 1
|
| 22 |
+
line = ParametricRegion((4*t - 1, 2 - 2*t, t), (t, 0, 1))
|
| 23 |
+
assert ParametricIntegral(C.x*C.z*C.i - C.y*C.z*C.k, line) == 3
|
| 24 |
+
|
| 25 |
+
assert ParametricIntegral(4*C.x**3, ParametricRegion((1, t), (t, 0, 2))) == 8
|
| 26 |
+
|
| 27 |
+
helix = ParametricRegion((cos(t), sin(t), 3*t), (t, 0, 4*pi))
|
| 28 |
+
assert ParametricIntegral(C.x*C.y*C.z, helix) == -3*sqrt(10)*pi
|
| 29 |
+
|
| 30 |
+
field2 = C.y*C.i + C.z*C.j + C.z*C.k
|
| 31 |
+
assert ParametricIntegral(field2, ParametricRegion((cos(t), sin(t), t**2), (t, 0, pi))) == -5*pi/2 + pi**4/2
|
| 32 |
+
|
| 33 |
+
def test_parametric_surfaceintegrals():
|
| 34 |
+
|
| 35 |
+
semisphere = ParametricRegion((2*sin(phi)*cos(theta), 2*sin(phi)*sin(theta), 2*cos(phi)),\
|
| 36 |
+
(theta, 0, 2*pi), (phi, 0, pi/2))
|
| 37 |
+
assert ParametricIntegral(C.z, semisphere) == 8*pi
|
| 38 |
+
|
| 39 |
+
cylinder = ParametricRegion((sqrt(3)*cos(theta), sqrt(3)*sin(theta), z), (z, 0, 6), (theta, 0, 2*pi))
|
| 40 |
+
assert ParametricIntegral(C.y, cylinder) == 0
|
| 41 |
+
|
| 42 |
+
cone = ParametricRegion((v*cos(u), v*sin(u), v), (u, 0, 2*pi), (v, 0, 1))
|
| 43 |
+
assert ParametricIntegral(C.x*C.i + C.y*C.j + C.z**4*C.k, cone) == pi/3
|
| 44 |
+
|
| 45 |
+
triangle1 = ParametricRegion((x, y), (x, 0, 2), (y, 0, 10 - 5*x))
|
| 46 |
+
triangle2 = ParametricRegion((x, y), (y, 0, 10 - 5*x), (x, 0, 2))
|
| 47 |
+
assert ParametricIntegral(-15.6*C.y*C.k, triangle1) == ParametricIntegral(-15.6*C.y*C.k, triangle2)
|
| 48 |
+
assert ParametricIntegral(C.z, triangle1) == 10*C.z
|
| 49 |
+
|
| 50 |
+
def test_parametric_volumeintegrals():
|
| 51 |
+
|
| 52 |
+
cube = ParametricRegion((x, y, z), (x, 0, 1), (y, 0, 1), (z, 0, 1))
|
| 53 |
+
assert ParametricIntegral(1, cube) == 1
|
| 54 |
+
|
| 55 |
+
solidsphere1 = ParametricRegion((r*sin(phi)*cos(theta), r*sin(phi)*sin(theta), r*cos(phi)),\
|
| 56 |
+
(r, 0, 2), (theta, 0, 2*pi), (phi, 0, pi))
|
| 57 |
+
solidsphere2 = ParametricRegion((r*sin(phi)*cos(theta), r*sin(phi)*sin(theta), r*cos(phi)),\
|
| 58 |
+
(r, 0, 2), (phi, 0, pi), (theta, 0, 2*pi))
|
| 59 |
+
assert ParametricIntegral(C.x**2 + C.y**2, solidsphere1) == -256*pi/15
|
| 60 |
+
assert ParametricIntegral(C.x**2 + C.y**2, solidsphere2) == 256*pi/15
|
| 61 |
+
|
| 62 |
+
region_under_plane1 = ParametricRegion((x, y, z), (x, 0, 3), (y, 0, -2*x/3 + 2),\
|
| 63 |
+
(z, 0, 6 - 2*x - 3*y))
|
| 64 |
+
region_under_plane2 = ParametricRegion((x, y, z), (x, 0, 3), (z, 0, 6 - 2*x - 3*y),\
|
| 65 |
+
(y, 0, -2*x/3 + 2))
|
| 66 |
+
|
| 67 |
+
assert ParametricIntegral(C.x*C.i + C.j - 100*C.k, region_under_plane1) == \
|
| 68 |
+
ParametricIntegral(C.x*C.i + C.j - 100*C.k, region_under_plane2)
|
| 69 |
+
assert ParametricIntegral(2*C.x, region_under_plane2) == -9
|
| 70 |
+
|
| 71 |
+
def test_vector_integrate():
|
| 72 |
+
halfdisc = ParametricRegion((r*cos(theta), r* sin(theta)), (r, -2, 2), (theta, 0, pi))
|
| 73 |
+
assert vector_integrate(C.x**2, halfdisc) == 4*pi
|
| 74 |
+
assert vector_integrate(C.x, ParametricRegion((t, t**2), (t, 2, 3))) == -17*sqrt(17)/12 + 37*sqrt(37)/12
|
| 75 |
+
|
| 76 |
+
assert vector_integrate(C.y**3*C.z, (C.x, 0, 3), (C.y, -1, 4)) == 765*C.z/4
|
| 77 |
+
|
| 78 |
+
s1 = Segment(Point(0, 0), Point(0, 1))
|
| 79 |
+
assert vector_integrate(-15*C.y, s1) == S(-15)/2
|
| 80 |
+
s2 = Segment(Point(4, 3, 9), Point(1, 1, 7))
|
| 81 |
+
assert vector_integrate(C.y*C.i, s2) == -6
|
| 82 |
+
|
| 83 |
+
curve = Curve((sin(t), cos(t)), (t, 0, 2))
|
| 84 |
+
assert vector_integrate(5*C.z, curve) == 10*C.z
|
| 85 |
+
|
| 86 |
+
c1 = Circle(Point(2, 3), 6)
|
| 87 |
+
assert vector_integrate(C.x*C.y, c1) == 72*pi
|
| 88 |
+
c2 = Circle(Point(0, 0), Point(1, 1), Point(1, 0))
|
| 89 |
+
assert vector_integrate(1, c2) == c2.circumference
|
| 90 |
+
|
| 91 |
+
triangle = Polygon((0, 0), (1, 0), (1, 1))
|
| 92 |
+
assert vector_integrate(C.x*C.i - 14*C.y*C.j, triangle) == 0
|
| 93 |
+
p1, p2, p3, p4 = [(0, 0), (1, 0), (5, 1), (0, 1)]
|
| 94 |
+
poly = Polygon(p1, p2, p3, p4)
|
| 95 |
+
assert vector_integrate(-23*C.z, poly) == -161*C.z - 23*sqrt(17)*C.z
|
| 96 |
+
|
| 97 |
+
point = Point(2, 3)
|
| 98 |
+
assert vector_integrate(C.i*C.y - C.z, point) == ParametricIntegral(C.y*C.i, ParametricRegion((2, 3)))
|
| 99 |
+
|
| 100 |
+
c3 = ImplicitRegion((x, y), x**2 + y**2 - 4)
|
| 101 |
+
assert vector_integrate(45, c3) == 180*pi
|
| 102 |
+
c4 = ImplicitRegion((x, y), (x - 3)**2 + (y - 4)**2 - 9)
|
| 103 |
+
assert vector_integrate(1, c4) == 6*pi
|
| 104 |
+
|
| 105 |
+
pl = Plane(Point(1, 1, 1), Point(2, 3, 4), Point(2, 2, 2))
|
| 106 |
+
raises(ValueError, lambda: vector_integrate(C.x*C.z*C.i + C.k, pl))
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_operators.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.vector import CoordSys3D, Gradient, Divergence, Curl, VectorZero, Laplacian
|
| 2 |
+
from sympy.printing.repr import srepr
|
| 3 |
+
|
| 4 |
+
R = CoordSys3D('R')
|
| 5 |
+
s1 = R.x*R.y*R.z # type: ignore
|
| 6 |
+
s2 = R.x + 3*R.y**2 # type: ignore
|
| 7 |
+
s3 = R.x**2 + R.y**2 + R.z**2 # type: ignore
|
| 8 |
+
v1 = R.x*R.i + R.z*R.z*R.j # type: ignore
|
| 9 |
+
v2 = R.x*R.i + R.y*R.j + R.z*R.k # type: ignore
|
| 10 |
+
v3 = R.x**2*R.i + R.y**2*R.j + R.z**2*R.k # type: ignore
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_Gradient():
|
| 14 |
+
assert Gradient(s1) == Gradient(R.x*R.y*R.z)
|
| 15 |
+
assert Gradient(s2) == Gradient(R.x + 3*R.y**2)
|
| 16 |
+
assert Gradient(s1).doit() == R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
|
| 17 |
+
assert Gradient(s2).doit() == R.i + 6*R.y*R.j
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_Divergence():
|
| 21 |
+
assert Divergence(v1) == Divergence(R.x*R.i + R.z*R.z*R.j)
|
| 22 |
+
assert Divergence(v2) == Divergence(R.x*R.i + R.y*R.j + R.z*R.k)
|
| 23 |
+
assert Divergence(v1).doit() == 1
|
| 24 |
+
assert Divergence(v2).doit() == 3
|
| 25 |
+
# issue 22384
|
| 26 |
+
Rc = CoordSys3D('R', transformation='cylindrical')
|
| 27 |
+
assert Divergence(Rc.i).doit() == 1/Rc.r
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_Curl():
|
| 31 |
+
assert Curl(v1) == Curl(R.x*R.i + R.z*R.z*R.j)
|
| 32 |
+
assert Curl(v2) == Curl(R.x*R.i + R.y*R.j + R.z*R.k)
|
| 33 |
+
assert Curl(v1).doit() == (-2*R.z)*R.i
|
| 34 |
+
assert Curl(v2).doit() == VectorZero()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_Laplacian():
|
| 38 |
+
assert Laplacian(s3) == Laplacian(R.x**2 + R.y**2 + R.z**2)
|
| 39 |
+
assert Laplacian(v3) == Laplacian(R.x**2*R.i + R.y**2*R.j + R.z**2*R.k)
|
| 40 |
+
assert Laplacian(s3).doit() == 6
|
| 41 |
+
assert Laplacian(v3).doit() == 2*R.i + 2*R.j + 2*R.k
|
| 42 |
+
assert srepr(Laplacian(s3)) == \
|
| 43 |
+
'Laplacian(Add(Pow(R.x, Integer(2)), Pow(R.y, Integer(2)), Pow(R.z, Integer(2))))'
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_parametricregion.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import pi
|
| 2 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 3 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 4 |
+
from sympy.vector.parametricregion import ParametricRegion, parametric_region_list
|
| 5 |
+
from sympy.geometry import Point, Segment, Curve, Ellipse, Line, Parabola, Polygon
|
| 6 |
+
from sympy.testing.pytest import raises
|
| 7 |
+
from sympy.abc import a, b, r, t, x, y, z, theta, phi
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
C = CoordSys3D('C')
|
| 11 |
+
|
| 12 |
+
def test_ParametricRegion():
|
| 13 |
+
|
| 14 |
+
point = ParametricRegion((3, 4))
|
| 15 |
+
assert point.definition == (3, 4)
|
| 16 |
+
assert point.parameters == ()
|
| 17 |
+
assert point.limits == {}
|
| 18 |
+
assert point.dimensions == 0
|
| 19 |
+
|
| 20 |
+
# line x = y
|
| 21 |
+
line_xy = ParametricRegion((y, y), (y, 1, 5))
|
| 22 |
+
assert line_xy .definition == (y, y)
|
| 23 |
+
assert line_xy.parameters == (y,)
|
| 24 |
+
assert line_xy.dimensions == 1
|
| 25 |
+
|
| 26 |
+
# line y = z
|
| 27 |
+
line_yz = ParametricRegion((x,t,t), x, (t, 1, 2))
|
| 28 |
+
assert line_yz.definition == (x,t,t)
|
| 29 |
+
assert line_yz.parameters == (x, t)
|
| 30 |
+
assert line_yz.limits == {t: (1, 2)}
|
| 31 |
+
assert line_yz.dimensions == 1
|
| 32 |
+
|
| 33 |
+
p1 = ParametricRegion((9*a, -16*b), (a, 0, 2), (b, -1, 5))
|
| 34 |
+
assert p1.definition == (9*a, -16*b)
|
| 35 |
+
assert p1.parameters == (a, b)
|
| 36 |
+
assert p1.limits == {a: (0, 2), b: (-1, 5)}
|
| 37 |
+
assert p1.dimensions == 2
|
| 38 |
+
|
| 39 |
+
p2 = ParametricRegion((t, t**3), t)
|
| 40 |
+
assert p2.parameters == (t,)
|
| 41 |
+
assert p2.limits == {}
|
| 42 |
+
assert p2.dimensions == 0
|
| 43 |
+
|
| 44 |
+
circle = ParametricRegion((r*cos(theta), r*sin(theta)), r, (theta, 0, 2*pi))
|
| 45 |
+
assert circle.definition == (r*cos(theta), r*sin(theta))
|
| 46 |
+
assert circle.dimensions == 1
|
| 47 |
+
|
| 48 |
+
halfdisc = ParametricRegion((r*cos(theta), r*sin(theta)), (r, -2, 2), (theta, 0, pi))
|
| 49 |
+
assert halfdisc.definition == (r*cos(theta), r*sin(theta))
|
| 50 |
+
assert halfdisc.parameters == (r, theta)
|
| 51 |
+
assert halfdisc.limits == {r: (-2, 2), theta: (0, pi)}
|
| 52 |
+
assert halfdisc.dimensions == 2
|
| 53 |
+
|
| 54 |
+
ellipse = ParametricRegion((a*cos(t), b*sin(t)), (t, 0, 8))
|
| 55 |
+
assert ellipse.parameters == (t,)
|
| 56 |
+
assert ellipse.limits == {t: (0, 8)}
|
| 57 |
+
assert ellipse.dimensions == 1
|
| 58 |
+
|
| 59 |
+
cylinder = ParametricRegion((r*cos(theta), r*sin(theta), z), (r, 0, 1), (theta, 0, 2*pi), (z, 0, 4))
|
| 60 |
+
assert cylinder.parameters == (r, theta, z)
|
| 61 |
+
assert cylinder.dimensions == 3
|
| 62 |
+
|
| 63 |
+
sphere = ParametricRegion((r*sin(phi)*cos(theta),r*sin(phi)*sin(theta), r*cos(phi)),
|
| 64 |
+
r, (theta, 0, 2*pi), (phi, 0, pi))
|
| 65 |
+
assert sphere.definition == (r*sin(phi)*cos(theta),r*sin(phi)*sin(theta), r*cos(phi))
|
| 66 |
+
assert sphere.parameters == (r, theta, phi)
|
| 67 |
+
assert sphere.dimensions == 2
|
| 68 |
+
|
| 69 |
+
raises(ValueError, lambda: ParametricRegion((a*t**2, 2*a*t), (a, -2)))
|
| 70 |
+
raises(ValueError, lambda: ParametricRegion((a, b), (a**2, sin(b)), (a, 2, 4, 6)))
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_parametric_region_list():
|
| 74 |
+
|
| 75 |
+
point = Point(-5, 12)
|
| 76 |
+
assert parametric_region_list(point) == [ParametricRegion((-5, 12))]
|
| 77 |
+
|
| 78 |
+
e = Ellipse(Point(2, 8), 2, 6)
|
| 79 |
+
assert parametric_region_list(e, t) == [ParametricRegion((2*cos(t) + 2, 6*sin(t) + 8), (t, 0, 2*pi))]
|
| 80 |
+
|
| 81 |
+
c = Curve((t, t**3), (t, 5, 3))
|
| 82 |
+
assert parametric_region_list(c) == [ParametricRegion((t, t**3), (t, 5, 3))]
|
| 83 |
+
|
| 84 |
+
s = Segment(Point(2, 11, -6), Point(0, 2, 5))
|
| 85 |
+
assert parametric_region_list(s, t) == [ParametricRegion((2 - 2*t, 11 - 9*t, 11*t - 6), (t, 0, 1))]
|
| 86 |
+
s1 = Segment(Point(0, 0), (1, 0))
|
| 87 |
+
assert parametric_region_list(s1, t) == [ParametricRegion((t, 0), (t, 0, 1))]
|
| 88 |
+
s2 = Segment(Point(1, 2, 3), Point(1, 2, 5))
|
| 89 |
+
assert parametric_region_list(s2, t) == [ParametricRegion((1, 2, 2*t + 3), (t, 0, 1))]
|
| 90 |
+
s3 = Segment(Point(12, 56), Point(12, 56))
|
| 91 |
+
assert parametric_region_list(s3) == [ParametricRegion((12, 56))]
|
| 92 |
+
|
| 93 |
+
poly = Polygon((1,3), (-3, 8), (2, 4))
|
| 94 |
+
assert parametric_region_list(poly, t) == [ParametricRegion((1 - 4*t, 5*t + 3), (t, 0, 1)), ParametricRegion((5*t - 3, 8 - 4*t), (t, 0, 1)), ParametricRegion((2 - t, 4 - t), (t, 0, 1))]
|
| 95 |
+
|
| 96 |
+
p1 = Parabola(Point(0, 0), Line(Point(5, 8), Point(7,8)))
|
| 97 |
+
raises(ValueError, lambda: parametric_region_list(p1))
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_printing.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
from sympy.core.function import Function
|
| 3 |
+
from sympy.integrals.integrals import Integral
|
| 4 |
+
from sympy.printing.latex import latex
|
| 5 |
+
from sympy.printing.pretty import pretty as xpretty
|
| 6 |
+
from sympy.vector import CoordSys3D, Del, Vector, express
|
| 7 |
+
from sympy.abc import a, b, c
|
| 8 |
+
from sympy.testing.pytest import XFAIL
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def pretty(expr):
|
| 12 |
+
"""ASCII pretty-printing"""
|
| 13 |
+
return xpretty(expr, use_unicode=False, wrap_line=False)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def upretty(expr):
|
| 17 |
+
"""Unicode pretty-printing"""
|
| 18 |
+
return xpretty(expr, use_unicode=True, wrap_line=False)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Initialize the basic and tedious vector/dyadic expressions
|
| 22 |
+
# needed for testing.
|
| 23 |
+
# Some of the pretty forms shown denote how the expressions just
|
| 24 |
+
# above them should look with pretty printing.
|
| 25 |
+
N = CoordSys3D('N')
|
| 26 |
+
C = N.orient_new_axis('C', a, N.k) # type: ignore
|
| 27 |
+
v = []
|
| 28 |
+
d = []
|
| 29 |
+
v.append(Vector.zero)
|
| 30 |
+
v.append(N.i) # type: ignore
|
| 31 |
+
v.append(-N.i) # type: ignore
|
| 32 |
+
v.append(N.i + N.j) # type: ignore
|
| 33 |
+
v.append(a*N.i) # type: ignore
|
| 34 |
+
v.append(a*N.i - b*N.j) # type: ignore
|
| 35 |
+
v.append((a**2 + N.x)*N.i + N.k) # type: ignore
|
| 36 |
+
v.append((a**2 + b)*N.i + 3*(C.y - c)*N.k) # type: ignore
|
| 37 |
+
f = Function('f')
|
| 38 |
+
v.append(N.j - (Integral(f(b)) - C.x**2)*N.k) # type: ignore
|
| 39 |
+
upretty_v_8 = """\
|
| 40 |
+
⎛ 2 ⌠ ⎞ \n\
|
| 41 |
+
j_N + ⎜x_C - ⎮ f(b) db⎟ k_N\n\
|
| 42 |
+
⎝ ⌡ ⎠ \
|
| 43 |
+
"""
|
| 44 |
+
pretty_v_8 = """\
|
| 45 |
+
j_N + / / \\\n\
|
| 46 |
+
| 2 | |\n\
|
| 47 |
+
|x_C - | f(b) db|\n\
|
| 48 |
+
| | |\n\
|
| 49 |
+
\\ / / \
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
v.append(N.i + C.k) # type: ignore
|
| 53 |
+
v.append(express(N.i, C)) # type: ignore
|
| 54 |
+
v.append((a**2 + b)*N.i + (Integral(f(b)))*N.k) # type: ignore
|
| 55 |
+
upretty_v_11 = """\
|
| 56 |
+
⎛ 2 ⎞ ⎛⌠ ⎞ \n\
|
| 57 |
+
⎝a + b⎠ i_N + ⎜⎮ f(b) db⎟ k_N\n\
|
| 58 |
+
⎝⌡ ⎠ \
|
| 59 |
+
"""
|
| 60 |
+
pretty_v_11 = """\
|
| 61 |
+
/ 2 \\ + / / \\\n\
|
| 62 |
+
\\a + b/ i_N| | |\n\
|
| 63 |
+
| | f(b) db|\n\
|
| 64 |
+
| | |\n\
|
| 65 |
+
\\/ / \
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
for x in v:
|
| 69 |
+
d.append(x | N.k) # type: ignore
|
| 70 |
+
s = 3*N.x**2*C.y # type: ignore
|
| 71 |
+
upretty_s = """\
|
| 72 |
+
2\n\
|
| 73 |
+
3⋅y_C⋅x_N \
|
| 74 |
+
"""
|
| 75 |
+
pretty_s = """\
|
| 76 |
+
2\n\
|
| 77 |
+
3*y_C*x_N \
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
# This is the pretty form for ((a**2 + b)*N.i + 3*(C.y - c)*N.k) | N.k
|
| 81 |
+
upretty_d_7 = """\
|
| 82 |
+
⎛ 2 ⎞ \n\
|
| 83 |
+
⎝a + b⎠ (i_N|k_N) + (3⋅y_C - 3⋅c) (k_N|k_N)\
|
| 84 |
+
"""
|
| 85 |
+
pretty_d_7 = """\
|
| 86 |
+
/ 2 \\ (i_N|k_N) + (3*y_C - 3*c) (k_N|k_N)\n\
|
| 87 |
+
\\a + b/ \
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_str_printing():
|
| 92 |
+
assert str(v[0]) == '0'
|
| 93 |
+
assert str(v[1]) == 'N.i'
|
| 94 |
+
assert str(v[2]) == '(-1)*N.i'
|
| 95 |
+
assert str(v[3]) == 'N.i + N.j'
|
| 96 |
+
assert str(v[8]) == 'N.j + (C.x**2 - Integral(f(b), b))*N.k'
|
| 97 |
+
assert str(v[9]) == 'C.k + N.i'
|
| 98 |
+
assert str(s) == '3*C.y*N.x**2'
|
| 99 |
+
assert str(d[0]) == '0'
|
| 100 |
+
assert str(d[1]) == '(N.i|N.k)'
|
| 101 |
+
assert str(d[4]) == 'a*(N.i|N.k)'
|
| 102 |
+
assert str(d[5]) == 'a*(N.i|N.k) + (-b)*(N.j|N.k)'
|
| 103 |
+
assert str(d[8]) == ('(N.j|N.k) + (C.x**2 - ' +
|
| 104 |
+
'Integral(f(b), b))*(N.k|N.k)')
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@XFAIL
|
| 108 |
+
def test_pretty_printing_ascii():
|
| 109 |
+
assert pretty(v[0]) == '0'
|
| 110 |
+
assert pretty(v[1]) == 'i_N'
|
| 111 |
+
assert pretty(v[5]) == '(a) i_N + (-b) j_N'
|
| 112 |
+
assert pretty(v[8]) == pretty_v_8
|
| 113 |
+
assert pretty(v[2]) == '(-1) i_N'
|
| 114 |
+
assert pretty(v[11]) == pretty_v_11
|
| 115 |
+
assert pretty(s) == pretty_s
|
| 116 |
+
assert pretty(d[0]) == '(0|0)'
|
| 117 |
+
assert pretty(d[5]) == '(a) (i_N|k_N) + (-b) (j_N|k_N)'
|
| 118 |
+
assert pretty(d[7]) == pretty_d_7
|
| 119 |
+
assert pretty(d[10]) == '(cos(a)) (i_C|k_N) + (-sin(a)) (j_C|k_N)'
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_pretty_print_unicode_v():
|
| 123 |
+
assert upretty(v[0]) == '0'
|
| 124 |
+
assert upretty(v[1]) == 'i_N'
|
| 125 |
+
assert upretty(v[5]) == '(a) i_N + (-b) j_N'
|
| 126 |
+
# Make sure the printing works in other objects
|
| 127 |
+
assert upretty(v[5].args) == '((a) i_N, (-b) j_N)'
|
| 128 |
+
assert upretty(v[8]) == upretty_v_8
|
| 129 |
+
assert upretty(v[2]) == '(-1) i_N'
|
| 130 |
+
assert upretty(v[11]) == upretty_v_11
|
| 131 |
+
assert upretty(s) == upretty_s
|
| 132 |
+
assert upretty(d[0]) == '(0|0)'
|
| 133 |
+
assert upretty(d[5]) == '(a) (i_N|k_N) + (-b) (j_N|k_N)'
|
| 134 |
+
assert upretty(d[7]) == upretty_d_7
|
| 135 |
+
assert upretty(d[10]) == '(cos(a)) (i_C|k_N) + (-sin(a)) (j_C|k_N)'
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def test_latex_printing():
|
| 139 |
+
assert latex(v[0]) == '\\mathbf{\\hat{0}}'
|
| 140 |
+
assert latex(v[1]) == '\\mathbf{\\hat{i}_{N}}'
|
| 141 |
+
assert latex(v[2]) == '- \\mathbf{\\hat{i}_{N}}'
|
| 142 |
+
assert latex(v[5]) == ('\\left(a\\right)\\mathbf{\\hat{i}_{N}} + ' +
|
| 143 |
+
'\\left(- b\\right)\\mathbf{\\hat{j}_{N}}')
|
| 144 |
+
assert latex(v[6]) == ('\\left(\\mathbf{{x}_{N}} + a^{2}\\right)\\mathbf{\\hat{i}_' +
|
| 145 |
+
'{N}} + \\mathbf{\\hat{k}_{N}}')
|
| 146 |
+
assert latex(v[8]) == ('\\mathbf{\\hat{j}_{N}} + \\left(\\mathbf{{x}_' +
|
| 147 |
+
'{C}}^{2} - \\int f{\\left(b \\right)}\\,' +
|
| 148 |
+
' db\\right)\\mathbf{\\hat{k}_{N}}')
|
| 149 |
+
assert latex(s) == '3 \\mathbf{{y}_{C}} \\mathbf{{x}_{N}}^{2}'
|
| 150 |
+
assert latex(d[0]) == '(\\mathbf{\\hat{0}}|\\mathbf{\\hat{0}})'
|
| 151 |
+
assert latex(d[4]) == ('\\left(a\\right)\\left(\\mathbf{\\hat{i}_{N}}{\\middle|}' +
|
| 152 |
+
'\\mathbf{\\hat{k}_{N}}\\right)')
|
| 153 |
+
assert latex(d[9]) == ('\\left(\\mathbf{\\hat{k}_{C}}{\\middle|}' +
|
| 154 |
+
'\\mathbf{\\hat{k}_{N}}\\right) + \\left(' +
|
| 155 |
+
'\\mathbf{\\hat{i}_{N}}{\\middle|}\\mathbf{' +
|
| 156 |
+
'\\hat{k}_{N}}\\right)')
|
| 157 |
+
assert latex(d[11]) == ('\\left(a^{2} + b\\right)\\left(\\mathbf{\\hat{i}_{N}}' +
|
| 158 |
+
'{\\middle|}\\mathbf{\\hat{k}_{N}}\\right) + ' +
|
| 159 |
+
'\\left(\\int f{\\left(b \\right)}\\, db\\right)\\left(' +
|
| 160 |
+
'\\mathbf{\\hat{k}_{N}}{\\middle|}\\mathbf{' +
|
| 161 |
+
'\\hat{k}_{N}}\\right)')
|
| 162 |
+
|
| 163 |
+
def test_issue_23058():
|
| 164 |
+
from sympy import symbols, sin, cos, pi, UnevaluatedExpr
|
| 165 |
+
|
| 166 |
+
delop = Del()
|
| 167 |
+
CC_ = CoordSys3D("C")
|
| 168 |
+
y = CC_.y
|
| 169 |
+
xhat = CC_.i
|
| 170 |
+
|
| 171 |
+
t = symbols("t")
|
| 172 |
+
ten = symbols("10", positive=True)
|
| 173 |
+
eps, mu = 4*pi*ten**(-11), ten**(-5)
|
| 174 |
+
|
| 175 |
+
Bx = 2 * ten**(-4) * cos(ten**5 * t) * sin(ten**(-3) * y)
|
| 176 |
+
vecB = Bx * xhat
|
| 177 |
+
vecE = (1/eps) * Integral(delop.cross(vecB/mu).doit(), t)
|
| 178 |
+
vecE = vecE.doit()
|
| 179 |
+
|
| 180 |
+
vecB_str = """\
|
| 181 |
+
⎛ ⎛y_C⎞ ⎛ 5 ⎞⎞ \n\
|
| 182 |
+
⎜2⋅sin⎜───⎟⋅cos⎝10 ⋅t⎠⎟ i_C\n\
|
| 183 |
+
⎜ ⎜ 3⎟ ⎟ \n\
|
| 184 |
+
⎜ ⎝10 ⎠ ⎟ \n\
|
| 185 |
+
⎜─────────────────────⎟ \n\
|
| 186 |
+
⎜ 4 ⎟ \n\
|
| 187 |
+
⎝ 10 ⎠ \
|
| 188 |
+
"""
|
| 189 |
+
vecE_str = """\
|
| 190 |
+
⎛ 4 ⎛ 5 ⎞ ⎛y_C⎞ ⎞ \n\
|
| 191 |
+
⎜-10 ⋅sin⎝10 ⋅t⎠⋅cos⎜───⎟ ⎟ k_C\n\
|
| 192 |
+
⎜ ⎜ 3⎟ ⎟ \n\
|
| 193 |
+
⎜ ⎝10 ⎠ ⎟ \n\
|
| 194 |
+
⎜─────────────────────────⎟ \n\
|
| 195 |
+
⎝ 2⋅π ⎠ \
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
assert upretty(vecB) == vecB_str
|
| 199 |
+
assert upretty(vecE) == vecE_str
|
| 200 |
+
|
| 201 |
+
ten = UnevaluatedExpr(10)
|
| 202 |
+
eps, mu = 4*pi*ten**(-11), ten**(-5)
|
| 203 |
+
|
| 204 |
+
Bx = 2 * ten**(-4) * cos(ten**5 * t) * sin(ten**(-3) * y)
|
| 205 |
+
vecB = Bx * xhat
|
| 206 |
+
|
| 207 |
+
vecB_str = """\
|
| 208 |
+
⎛ -4 ⎛ 5⎞ ⎛ -3⎞⎞ \n\
|
| 209 |
+
⎝2⋅10 ⋅cos⎝t⋅10 ⎠⋅sin⎝y_C⋅10 ⎠⎠ i_C \
|
| 210 |
+
"""
|
| 211 |
+
assert upretty(vecB) == vecB_str
|
| 212 |
+
|
| 213 |
+
def test_custom_names():
|
| 214 |
+
A = CoordSys3D('A', vector_names=['x', 'y', 'z'],
|
| 215 |
+
variable_names=['i', 'j', 'k'])
|
| 216 |
+
assert A.i.__str__() == 'A.i'
|
| 217 |
+
assert A.x.__str__() == 'A.x'
|
| 218 |
+
assert A.i._pretty_form == 'i_A'
|
| 219 |
+
assert A.x._pretty_form == 'x_A'
|
| 220 |
+
assert A.i._latex_form == r'\mathbf{{i}_{A}}'
|
| 221 |
+
assert A.x._latex_form == r"\mathbf{\hat{x}_{A}}"
|
mgm/lib/python3.10/site-packages/sympy/vector/tests/test_vector.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Rational, S
|
| 2 |
+
from sympy.simplify import simplify, trigsimp
|
| 3 |
+
from sympy.core.function import (Derivative, Function, diff)
|
| 4 |
+
from sympy.core.numbers import pi
|
| 5 |
+
from sympy.core.symbol import symbols
|
| 6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 7 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 8 |
+
from sympy.integrals.integrals import Integral
|
| 9 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 10 |
+
from sympy.vector.vector import Vector, BaseVector, VectorAdd, \
|
| 11 |
+
VectorMul, VectorZero
|
| 12 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 13 |
+
from sympy.vector.vector import Cross, Dot, cross
|
| 14 |
+
from sympy.testing.pytest import raises
|
| 15 |
+
|
| 16 |
+
C = CoordSys3D('C')
|
| 17 |
+
|
| 18 |
+
i, j, k = C.base_vectors()
|
| 19 |
+
a, b, c = symbols('a b c')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_cross():
|
| 23 |
+
v1 = C.x * i + C.z * C.z * j
|
| 24 |
+
v2 = C.x * i + C.y * j + C.z * k
|
| 25 |
+
assert Cross(v1, v2) == Cross(C.x*C.i + C.z**2*C.j, C.x*C.i + C.y*C.j + C.z*C.k)
|
| 26 |
+
assert Cross(v1, v2).doit() == C.z**3*C.i + (-C.x*C.z)*C.j + (C.x*C.y - C.x*C.z**2)*C.k
|
| 27 |
+
assert cross(v1, v2) == C.z**3*C.i + (-C.x*C.z)*C.j + (C.x*C.y - C.x*C.z**2)*C.k
|
| 28 |
+
assert Cross(v1, v2) == -Cross(v2, v1)
|
| 29 |
+
assert Cross(v1, v2) + Cross(v2, v1) == Vector.zero
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_dot():
|
| 33 |
+
v1 = C.x * i + C.z * C.z * j
|
| 34 |
+
v2 = C.x * i + C.y * j + C.z * k
|
| 35 |
+
assert Dot(v1, v2) == Dot(C.x*C.i + C.z**2*C.j, C.x*C.i + C.y*C.j + C.z*C.k)
|
| 36 |
+
assert Dot(v1, v2).doit() == C.x**2 + C.y*C.z**2
|
| 37 |
+
assert Dot(v1, v2).doit() == C.x**2 + C.y*C.z**2
|
| 38 |
+
assert Dot(v1, v2) == Dot(v2, v1)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_vector_sympy():
|
| 42 |
+
"""
|
| 43 |
+
Test whether the Vector framework confirms to the hashing
|
| 44 |
+
and equality testing properties of SymPy.
|
| 45 |
+
"""
|
| 46 |
+
v1 = 3*j
|
| 47 |
+
assert v1 == j*3
|
| 48 |
+
assert v1.components == {j: 3}
|
| 49 |
+
v2 = 3*i + 4*j + 5*k
|
| 50 |
+
v3 = 2*i + 4*j + i + 4*k + k
|
| 51 |
+
assert v3 == v2
|
| 52 |
+
assert v3.__hash__() == v2.__hash__()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_vector():
|
| 56 |
+
assert isinstance(i, BaseVector)
|
| 57 |
+
assert i != j
|
| 58 |
+
assert j != k
|
| 59 |
+
assert k != i
|
| 60 |
+
assert i - i == Vector.zero
|
| 61 |
+
assert i + Vector.zero == i
|
| 62 |
+
assert i - Vector.zero == i
|
| 63 |
+
assert Vector.zero != 0
|
| 64 |
+
assert -Vector.zero == Vector.zero
|
| 65 |
+
|
| 66 |
+
v1 = a*i + b*j + c*k
|
| 67 |
+
v2 = a**2*i + b**2*j + c**2*k
|
| 68 |
+
v3 = v1 + v2
|
| 69 |
+
v4 = 2 * v1
|
| 70 |
+
v5 = a * i
|
| 71 |
+
|
| 72 |
+
assert isinstance(v1, VectorAdd)
|
| 73 |
+
assert v1 - v1 == Vector.zero
|
| 74 |
+
assert v1 + Vector.zero == v1
|
| 75 |
+
assert v1.dot(i) == a
|
| 76 |
+
assert v1.dot(j) == b
|
| 77 |
+
assert v1.dot(k) == c
|
| 78 |
+
assert i.dot(v2) == a**2
|
| 79 |
+
assert j.dot(v2) == b**2
|
| 80 |
+
assert k.dot(v2) == c**2
|
| 81 |
+
assert v3.dot(i) == a**2 + a
|
| 82 |
+
assert v3.dot(j) == b**2 + b
|
| 83 |
+
assert v3.dot(k) == c**2 + c
|
| 84 |
+
|
| 85 |
+
assert v1 + v2 == v2 + v1
|
| 86 |
+
assert v1 - v2 == -1 * (v2 - v1)
|
| 87 |
+
assert a * v1 == v1 * a
|
| 88 |
+
|
| 89 |
+
assert isinstance(v5, VectorMul)
|
| 90 |
+
assert v5.base_vector == i
|
| 91 |
+
assert v5.measure_number == a
|
| 92 |
+
assert isinstance(v4, Vector)
|
| 93 |
+
assert isinstance(v4, VectorAdd)
|
| 94 |
+
assert isinstance(v4, Vector)
|
| 95 |
+
assert isinstance(Vector.zero, VectorZero)
|
| 96 |
+
assert isinstance(Vector.zero, Vector)
|
| 97 |
+
assert isinstance(v1 * 0, VectorZero)
|
| 98 |
+
|
| 99 |
+
assert v1.to_matrix(C) == Matrix([[a], [b], [c]])
|
| 100 |
+
|
| 101 |
+
assert i.components == {i: 1}
|
| 102 |
+
assert v5.components == {i: a}
|
| 103 |
+
assert v1.components == {i: a, j: b, k: c}
|
| 104 |
+
|
| 105 |
+
assert VectorAdd(v1, Vector.zero) == v1
|
| 106 |
+
assert VectorMul(a, v1) == v1*a
|
| 107 |
+
assert VectorMul(1, i) == i
|
| 108 |
+
assert VectorAdd(v1, Vector.zero) == v1
|
| 109 |
+
assert VectorMul(0, Vector.zero) == Vector.zero
|
| 110 |
+
raises(TypeError, lambda: v1.outer(1))
|
| 111 |
+
raises(TypeError, lambda: v1.dot(1))
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_vector_magnitude_normalize():
|
| 115 |
+
assert Vector.zero.magnitude() == 0
|
| 116 |
+
assert Vector.zero.normalize() == Vector.zero
|
| 117 |
+
|
| 118 |
+
assert i.magnitude() == 1
|
| 119 |
+
assert j.magnitude() == 1
|
| 120 |
+
assert k.magnitude() == 1
|
| 121 |
+
assert i.normalize() == i
|
| 122 |
+
assert j.normalize() == j
|
| 123 |
+
assert k.normalize() == k
|
| 124 |
+
|
| 125 |
+
v1 = a * i
|
| 126 |
+
assert v1.normalize() == (a/sqrt(a**2))*i
|
| 127 |
+
assert v1.magnitude() == sqrt(a**2)
|
| 128 |
+
|
| 129 |
+
v2 = a*i + b*j + c*k
|
| 130 |
+
assert v2.magnitude() == sqrt(a**2 + b**2 + c**2)
|
| 131 |
+
assert v2.normalize() == v2 / v2.magnitude()
|
| 132 |
+
|
| 133 |
+
v3 = i + j
|
| 134 |
+
assert v3.normalize() == (sqrt(2)/2)*C.i + (sqrt(2)/2)*C.j
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def test_vector_simplify():
|
| 138 |
+
A, s, k, m = symbols('A, s, k, m')
|
| 139 |
+
|
| 140 |
+
test1 = (1 / a + 1 / b) * i
|
| 141 |
+
assert (test1 & i) != (a + b) / (a * b)
|
| 142 |
+
test1 = simplify(test1)
|
| 143 |
+
assert (test1 & i) == (a + b) / (a * b)
|
| 144 |
+
assert test1.simplify() == simplify(test1)
|
| 145 |
+
|
| 146 |
+
test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * i
|
| 147 |
+
test2 = simplify(test2)
|
| 148 |
+
assert (test2 & i) == (A**2 * s**4 / (4 * pi * k * m**3))
|
| 149 |
+
|
| 150 |
+
test3 = ((4 + 4 * a - 2 * (2 + 2 * a)) / (2 + 2 * a)) * i
|
| 151 |
+
test3 = simplify(test3)
|
| 152 |
+
assert (test3 & i) == 0
|
| 153 |
+
|
| 154 |
+
test4 = ((-4 * a * b**2 - 2 * b**3 - 2 * a**2 * b) / (a + b)**2) * i
|
| 155 |
+
test4 = simplify(test4)
|
| 156 |
+
assert (test4 & i) == -2 * b
|
| 157 |
+
|
| 158 |
+
v = (sin(a)+cos(a))**2*i - j
|
| 159 |
+
assert trigsimp(v) == (2*sin(a + pi/4)**2)*i + (-1)*j
|
| 160 |
+
assert trigsimp(v) == v.trigsimp()
|
| 161 |
+
|
| 162 |
+
assert simplify(Vector.zero) == Vector.zero
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def test_vector_dot():
|
| 166 |
+
assert i.dot(Vector.zero) == 0
|
| 167 |
+
assert Vector.zero.dot(i) == 0
|
| 168 |
+
assert i & Vector.zero == 0
|
| 169 |
+
|
| 170 |
+
assert i.dot(i) == 1
|
| 171 |
+
assert i.dot(j) == 0
|
| 172 |
+
assert i.dot(k) == 0
|
| 173 |
+
assert i & i == 1
|
| 174 |
+
assert i & j == 0
|
| 175 |
+
assert i & k == 0
|
| 176 |
+
|
| 177 |
+
assert j.dot(i) == 0
|
| 178 |
+
assert j.dot(j) == 1
|
| 179 |
+
assert j.dot(k) == 0
|
| 180 |
+
assert j & i == 0
|
| 181 |
+
assert j & j == 1
|
| 182 |
+
assert j & k == 0
|
| 183 |
+
|
| 184 |
+
assert k.dot(i) == 0
|
| 185 |
+
assert k.dot(j) == 0
|
| 186 |
+
assert k.dot(k) == 1
|
| 187 |
+
assert k & i == 0
|
| 188 |
+
assert k & j == 0
|
| 189 |
+
assert k & k == 1
|
| 190 |
+
|
| 191 |
+
raises(TypeError, lambda: k.dot(1))
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def test_vector_cross():
|
| 195 |
+
assert i.cross(Vector.zero) == Vector.zero
|
| 196 |
+
assert Vector.zero.cross(i) == Vector.zero
|
| 197 |
+
|
| 198 |
+
assert i.cross(i) == Vector.zero
|
| 199 |
+
assert i.cross(j) == k
|
| 200 |
+
assert i.cross(k) == -j
|
| 201 |
+
assert i ^ i == Vector.zero
|
| 202 |
+
assert i ^ j == k
|
| 203 |
+
assert i ^ k == -j
|
| 204 |
+
|
| 205 |
+
assert j.cross(i) == -k
|
| 206 |
+
assert j.cross(j) == Vector.zero
|
| 207 |
+
assert j.cross(k) == i
|
| 208 |
+
assert j ^ i == -k
|
| 209 |
+
assert j ^ j == Vector.zero
|
| 210 |
+
assert j ^ k == i
|
| 211 |
+
|
| 212 |
+
assert k.cross(i) == j
|
| 213 |
+
assert k.cross(j) == -i
|
| 214 |
+
assert k.cross(k) == Vector.zero
|
| 215 |
+
assert k ^ i == j
|
| 216 |
+
assert k ^ j == -i
|
| 217 |
+
assert k ^ k == Vector.zero
|
| 218 |
+
|
| 219 |
+
assert k.cross(1) == Cross(k, 1)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def test_projection():
|
| 223 |
+
v1 = i + j + k
|
| 224 |
+
v2 = 3*i + 4*j
|
| 225 |
+
v3 = 0*i + 0*j
|
| 226 |
+
assert v1.projection(v1) == i + j + k
|
| 227 |
+
assert v1.projection(v2) == Rational(7, 3)*C.i + Rational(7, 3)*C.j + Rational(7, 3)*C.k
|
| 228 |
+
assert v1.projection(v1, scalar=True) == S.One
|
| 229 |
+
assert v1.projection(v2, scalar=True) == Rational(7, 3)
|
| 230 |
+
assert v3.projection(v1) == Vector.zero
|
| 231 |
+
assert v3.projection(v1, scalar=True) == S.Zero
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def test_vector_diff_integrate():
|
| 235 |
+
f = Function('f')
|
| 236 |
+
v = f(a)*C.i + a**2*C.j - C.k
|
| 237 |
+
assert Derivative(v, a) == Derivative((f(a))*C.i +
|
| 238 |
+
a**2*C.j + (-1)*C.k, a)
|
| 239 |
+
assert (diff(v, a) == v.diff(a) == Derivative(v, a).doit() ==
|
| 240 |
+
(Derivative(f(a), a))*C.i + 2*a*C.j)
|
| 241 |
+
assert (Integral(v, a) == (Integral(f(a), a))*C.i +
|
| 242 |
+
(Integral(a**2, a))*C.j + (Integral(-1, a))*C.k)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def test_vector_args():
|
| 246 |
+
raises(ValueError, lambda: BaseVector(3, C))
|
| 247 |
+
raises(TypeError, lambda: BaseVector(0, Vector.zero))
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def test_srepr():
|
| 251 |
+
from sympy.printing.repr import srepr
|
| 252 |
+
res = "CoordSys3D(Str('C'), Tuple(ImmutableDenseMatrix([[Integer(1), "\
|
| 253 |
+
"Integer(0), Integer(0)], [Integer(0), Integer(1), Integer(0)], "\
|
| 254 |
+
"[Integer(0), Integer(0), Integer(1)]]), VectorZero())).i"
|
| 255 |
+
assert srepr(C.i) == res
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def test_scalar():
|
| 259 |
+
from sympy.vector import CoordSys3D
|
| 260 |
+
C = CoordSys3D('C')
|
| 261 |
+
v1 = 3*C.i + 4*C.j + 5*C.k
|
| 262 |
+
v2 = 3*C.i - 4*C.j + 5*C.k
|
| 263 |
+
assert v1.is_Vector is True
|
| 264 |
+
assert v1.is_scalar is False
|
| 265 |
+
assert (v1.dot(v2)).is_scalar is True
|
| 266 |
+
assert (v1.cross(v2)).is_scalar is False
|
mgm/lib/python3.10/site-packages/sympy/vector/vector.py
ADDED
|
@@ -0,0 +1,623 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from itertools import product
|
| 3 |
+
|
| 4 |
+
from sympy.core.add import Add
|
| 5 |
+
from sympy.core.assumptions import StdFactKB
|
| 6 |
+
from sympy.core.expr import AtomicExpr, Expr
|
| 7 |
+
from sympy.core.power import Pow
|
| 8 |
+
from sympy.core.singleton import S
|
| 9 |
+
from sympy.core.sorting import default_sort_key
|
| 10 |
+
from sympy.core.sympify import sympify
|
| 11 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 12 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 13 |
+
from sympy.vector.basisdependent import (BasisDependentZero,
|
| 14 |
+
BasisDependent, BasisDependentMul, BasisDependentAdd)
|
| 15 |
+
from sympy.vector.coordsysrect import CoordSys3D
|
| 16 |
+
from sympy.vector.dyadic import Dyadic, BaseDyadic, DyadicAdd
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Vector(BasisDependent):
|
| 20 |
+
"""
|
| 21 |
+
Super class for all Vector classes.
|
| 22 |
+
Ideally, neither this class nor any of its subclasses should be
|
| 23 |
+
instantiated by the user.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
is_scalar = False
|
| 27 |
+
is_Vector = True
|
| 28 |
+
_op_priority = 12.0
|
| 29 |
+
|
| 30 |
+
_expr_type: type[Vector]
|
| 31 |
+
_mul_func: type[Vector]
|
| 32 |
+
_add_func: type[Vector]
|
| 33 |
+
_zero_func: type[Vector]
|
| 34 |
+
_base_func: type[Vector]
|
| 35 |
+
zero: VectorZero
|
| 36 |
+
|
| 37 |
+
@property
|
| 38 |
+
def components(self):
|
| 39 |
+
"""
|
| 40 |
+
Returns the components of this vector in the form of a
|
| 41 |
+
Python dictionary mapping BaseVector instances to the
|
| 42 |
+
corresponding measure numbers.
|
| 43 |
+
|
| 44 |
+
Examples
|
| 45 |
+
========
|
| 46 |
+
|
| 47 |
+
>>> from sympy.vector import CoordSys3D
|
| 48 |
+
>>> C = CoordSys3D('C')
|
| 49 |
+
>>> v = 3*C.i + 4*C.j + 5*C.k
|
| 50 |
+
>>> v.components
|
| 51 |
+
{C.i: 3, C.j: 4, C.k: 5}
|
| 52 |
+
|
| 53 |
+
"""
|
| 54 |
+
# The '_components' attribute is defined according to the
|
| 55 |
+
# subclass of Vector the instance belongs to.
|
| 56 |
+
return self._components
|
| 57 |
+
|
| 58 |
+
def magnitude(self):
|
| 59 |
+
"""
|
| 60 |
+
Returns the magnitude of this vector.
|
| 61 |
+
"""
|
| 62 |
+
return sqrt(self & self)
|
| 63 |
+
|
| 64 |
+
def normalize(self):
|
| 65 |
+
"""
|
| 66 |
+
Returns the normalized version of this vector.
|
| 67 |
+
"""
|
| 68 |
+
return self / self.magnitude()
|
| 69 |
+
|
| 70 |
+
def dot(self, other):
|
| 71 |
+
"""
|
| 72 |
+
Returns the dot product of this Vector, either with another
|
| 73 |
+
Vector, or a Dyadic, or a Del operator.
|
| 74 |
+
If 'other' is a Vector, returns the dot product scalar (SymPy
|
| 75 |
+
expression).
|
| 76 |
+
If 'other' is a Dyadic, the dot product is returned as a Vector.
|
| 77 |
+
If 'other' is an instance of Del, returns the directional
|
| 78 |
+
derivative operator as a Python function. If this function is
|
| 79 |
+
applied to a scalar expression, it returns the directional
|
| 80 |
+
derivative of the scalar field wrt this Vector.
|
| 81 |
+
|
| 82 |
+
Parameters
|
| 83 |
+
==========
|
| 84 |
+
|
| 85 |
+
other: Vector/Dyadic/Del
|
| 86 |
+
The Vector or Dyadic we are dotting with, or a Del operator .
|
| 87 |
+
|
| 88 |
+
Examples
|
| 89 |
+
========
|
| 90 |
+
|
| 91 |
+
>>> from sympy.vector import CoordSys3D, Del
|
| 92 |
+
>>> C = CoordSys3D('C')
|
| 93 |
+
>>> delop = Del()
|
| 94 |
+
>>> C.i.dot(C.j)
|
| 95 |
+
0
|
| 96 |
+
>>> C.i & C.i
|
| 97 |
+
1
|
| 98 |
+
>>> v = 3*C.i + 4*C.j + 5*C.k
|
| 99 |
+
>>> v.dot(C.k)
|
| 100 |
+
5
|
| 101 |
+
>>> (C.i & delop)(C.x*C.y*C.z)
|
| 102 |
+
C.y*C.z
|
| 103 |
+
>>> d = C.i.outer(C.i)
|
| 104 |
+
>>> C.i.dot(d)
|
| 105 |
+
C.i
|
| 106 |
+
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
# Check special cases
|
| 110 |
+
if isinstance(other, Dyadic):
|
| 111 |
+
if isinstance(self, VectorZero):
|
| 112 |
+
return Vector.zero
|
| 113 |
+
outvec = Vector.zero
|
| 114 |
+
for k, v in other.components.items():
|
| 115 |
+
vect_dot = k.args[0].dot(self)
|
| 116 |
+
outvec += vect_dot * v * k.args[1]
|
| 117 |
+
return outvec
|
| 118 |
+
from sympy.vector.deloperator import Del
|
| 119 |
+
if not isinstance(other, (Del, Vector)):
|
| 120 |
+
raise TypeError(str(other) + " is not a vector, dyadic or " +
|
| 121 |
+
"del operator")
|
| 122 |
+
|
| 123 |
+
# Check if the other is a del operator
|
| 124 |
+
if isinstance(other, Del):
|
| 125 |
+
def directional_derivative(field):
|
| 126 |
+
from sympy.vector.functions import directional_derivative
|
| 127 |
+
return directional_derivative(field, self)
|
| 128 |
+
return directional_derivative
|
| 129 |
+
|
| 130 |
+
return dot(self, other)
|
| 131 |
+
|
| 132 |
+
def __and__(self, other):
|
| 133 |
+
return self.dot(other)
|
| 134 |
+
|
| 135 |
+
__and__.__doc__ = dot.__doc__
|
| 136 |
+
|
| 137 |
+
def cross(self, other):
|
| 138 |
+
"""
|
| 139 |
+
Returns the cross product of this Vector with another Vector or
|
| 140 |
+
Dyadic instance.
|
| 141 |
+
The cross product is a Vector, if 'other' is a Vector. If 'other'
|
| 142 |
+
is a Dyadic, this returns a Dyadic instance.
|
| 143 |
+
|
| 144 |
+
Parameters
|
| 145 |
+
==========
|
| 146 |
+
|
| 147 |
+
other: Vector/Dyadic
|
| 148 |
+
The Vector or Dyadic we are crossing with.
|
| 149 |
+
|
| 150 |
+
Examples
|
| 151 |
+
========
|
| 152 |
+
|
| 153 |
+
>>> from sympy.vector import CoordSys3D
|
| 154 |
+
>>> C = CoordSys3D('C')
|
| 155 |
+
>>> C.i.cross(C.j)
|
| 156 |
+
C.k
|
| 157 |
+
>>> C.i ^ C.i
|
| 158 |
+
0
|
| 159 |
+
>>> v = 3*C.i + 4*C.j + 5*C.k
|
| 160 |
+
>>> v ^ C.i
|
| 161 |
+
5*C.j + (-4)*C.k
|
| 162 |
+
>>> d = C.i.outer(C.i)
|
| 163 |
+
>>> C.j.cross(d)
|
| 164 |
+
(-1)*(C.k|C.i)
|
| 165 |
+
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
# Check special cases
|
| 169 |
+
if isinstance(other, Dyadic):
|
| 170 |
+
if isinstance(self, VectorZero):
|
| 171 |
+
return Dyadic.zero
|
| 172 |
+
outdyad = Dyadic.zero
|
| 173 |
+
for k, v in other.components.items():
|
| 174 |
+
cross_product = self.cross(k.args[0])
|
| 175 |
+
outer = cross_product.outer(k.args[1])
|
| 176 |
+
outdyad += v * outer
|
| 177 |
+
return outdyad
|
| 178 |
+
|
| 179 |
+
return cross(self, other)
|
| 180 |
+
|
| 181 |
+
def __xor__(self, other):
|
| 182 |
+
return self.cross(other)
|
| 183 |
+
|
| 184 |
+
__xor__.__doc__ = cross.__doc__
|
| 185 |
+
|
| 186 |
+
def outer(self, other):
|
| 187 |
+
"""
|
| 188 |
+
Returns the outer product of this vector with another, in the
|
| 189 |
+
form of a Dyadic instance.
|
| 190 |
+
|
| 191 |
+
Parameters
|
| 192 |
+
==========
|
| 193 |
+
|
| 194 |
+
other : Vector
|
| 195 |
+
The Vector with respect to which the outer product is to
|
| 196 |
+
be computed.
|
| 197 |
+
|
| 198 |
+
Examples
|
| 199 |
+
========
|
| 200 |
+
|
| 201 |
+
>>> from sympy.vector import CoordSys3D
|
| 202 |
+
>>> N = CoordSys3D('N')
|
| 203 |
+
>>> N.i.outer(N.j)
|
| 204 |
+
(N.i|N.j)
|
| 205 |
+
|
| 206 |
+
"""
|
| 207 |
+
|
| 208 |
+
# Handle the special cases
|
| 209 |
+
if not isinstance(other, Vector):
|
| 210 |
+
raise TypeError("Invalid operand for outer product")
|
| 211 |
+
elif (isinstance(self, VectorZero) or
|
| 212 |
+
isinstance(other, VectorZero)):
|
| 213 |
+
return Dyadic.zero
|
| 214 |
+
|
| 215 |
+
# Iterate over components of both the vectors to generate
|
| 216 |
+
# the required Dyadic instance
|
| 217 |
+
args = [(v1 * v2) * BaseDyadic(k1, k2) for (k1, v1), (k2, v2)
|
| 218 |
+
in product(self.components.items(), other.components.items())]
|
| 219 |
+
|
| 220 |
+
return DyadicAdd(*args)
|
| 221 |
+
|
| 222 |
+
def projection(self, other, scalar=False):
|
| 223 |
+
"""
|
| 224 |
+
Returns the vector or scalar projection of the 'other' on 'self'.
|
| 225 |
+
|
| 226 |
+
Examples
|
| 227 |
+
========
|
| 228 |
+
|
| 229 |
+
>>> from sympy.vector.coordsysrect import CoordSys3D
|
| 230 |
+
>>> C = CoordSys3D('C')
|
| 231 |
+
>>> i, j, k = C.base_vectors()
|
| 232 |
+
>>> v1 = i + j + k
|
| 233 |
+
>>> v2 = 3*i + 4*j
|
| 234 |
+
>>> v1.projection(v2)
|
| 235 |
+
7/3*C.i + 7/3*C.j + 7/3*C.k
|
| 236 |
+
>>> v1.projection(v2, scalar=True)
|
| 237 |
+
7/3
|
| 238 |
+
|
| 239 |
+
"""
|
| 240 |
+
if self.equals(Vector.zero):
|
| 241 |
+
return S.Zero if scalar else Vector.zero
|
| 242 |
+
|
| 243 |
+
if scalar:
|
| 244 |
+
return self.dot(other) / self.dot(self)
|
| 245 |
+
else:
|
| 246 |
+
return self.dot(other) / self.dot(self) * self
|
| 247 |
+
|
| 248 |
+
@property
|
| 249 |
+
def _projections(self):
|
| 250 |
+
"""
|
| 251 |
+
Returns the components of this vector but the output includes
|
| 252 |
+
also zero values components.
|
| 253 |
+
|
| 254 |
+
Examples
|
| 255 |
+
========
|
| 256 |
+
|
| 257 |
+
>>> from sympy.vector import CoordSys3D, Vector
|
| 258 |
+
>>> C = CoordSys3D('C')
|
| 259 |
+
>>> v1 = 3*C.i + 4*C.j + 5*C.k
|
| 260 |
+
>>> v1._projections
|
| 261 |
+
(3, 4, 5)
|
| 262 |
+
>>> v2 = C.x*C.y*C.z*C.i
|
| 263 |
+
>>> v2._projections
|
| 264 |
+
(C.x*C.y*C.z, 0, 0)
|
| 265 |
+
>>> v3 = Vector.zero
|
| 266 |
+
>>> v3._projections
|
| 267 |
+
(0, 0, 0)
|
| 268 |
+
"""
|
| 269 |
+
|
| 270 |
+
from sympy.vector.operators import _get_coord_systems
|
| 271 |
+
if isinstance(self, VectorZero):
|
| 272 |
+
return (S.Zero, S.Zero, S.Zero)
|
| 273 |
+
base_vec = next(iter(_get_coord_systems(self))).base_vectors()
|
| 274 |
+
return tuple([self.dot(i) for i in base_vec])
|
| 275 |
+
|
| 276 |
+
def __or__(self, other):
|
| 277 |
+
return self.outer(other)
|
| 278 |
+
|
| 279 |
+
__or__.__doc__ = outer.__doc__
|
| 280 |
+
|
| 281 |
+
def to_matrix(self, system):
|
| 282 |
+
"""
|
| 283 |
+
Returns the matrix form of this vector with respect to the
|
| 284 |
+
specified coordinate system.
|
| 285 |
+
|
| 286 |
+
Parameters
|
| 287 |
+
==========
|
| 288 |
+
|
| 289 |
+
system : CoordSys3D
|
| 290 |
+
The system wrt which the matrix form is to be computed
|
| 291 |
+
|
| 292 |
+
Examples
|
| 293 |
+
========
|
| 294 |
+
|
| 295 |
+
>>> from sympy.vector import CoordSys3D
|
| 296 |
+
>>> C = CoordSys3D('C')
|
| 297 |
+
>>> from sympy.abc import a, b, c
|
| 298 |
+
>>> v = a*C.i + b*C.j + c*C.k
|
| 299 |
+
>>> v.to_matrix(C)
|
| 300 |
+
Matrix([
|
| 301 |
+
[a],
|
| 302 |
+
[b],
|
| 303 |
+
[c]])
|
| 304 |
+
|
| 305 |
+
"""
|
| 306 |
+
|
| 307 |
+
return Matrix([self.dot(unit_vec) for unit_vec in
|
| 308 |
+
system.base_vectors()])
|
| 309 |
+
|
| 310 |
+
def separate(self):
|
| 311 |
+
"""
|
| 312 |
+
The constituents of this vector in different coordinate systems,
|
| 313 |
+
as per its definition.
|
| 314 |
+
|
| 315 |
+
Returns a dict mapping each CoordSys3D to the corresponding
|
| 316 |
+
constituent Vector.
|
| 317 |
+
|
| 318 |
+
Examples
|
| 319 |
+
========
|
| 320 |
+
|
| 321 |
+
>>> from sympy.vector import CoordSys3D
|
| 322 |
+
>>> R1 = CoordSys3D('R1')
|
| 323 |
+
>>> R2 = CoordSys3D('R2')
|
| 324 |
+
>>> v = R1.i + R2.i
|
| 325 |
+
>>> v.separate() == {R1: R1.i, R2: R2.i}
|
| 326 |
+
True
|
| 327 |
+
|
| 328 |
+
"""
|
| 329 |
+
|
| 330 |
+
parts = {}
|
| 331 |
+
for vect, measure in self.components.items():
|
| 332 |
+
parts[vect.system] = (parts.get(vect.system, Vector.zero) +
|
| 333 |
+
vect * measure)
|
| 334 |
+
return parts
|
| 335 |
+
|
| 336 |
+
def _div_helper(one, other):
|
| 337 |
+
""" Helper for division involving vectors. """
|
| 338 |
+
if isinstance(one, Vector) and isinstance(other, Vector):
|
| 339 |
+
raise TypeError("Cannot divide two vectors")
|
| 340 |
+
elif isinstance(one, Vector):
|
| 341 |
+
if other == S.Zero:
|
| 342 |
+
raise ValueError("Cannot divide a vector by zero")
|
| 343 |
+
return VectorMul(one, Pow(other, S.NegativeOne))
|
| 344 |
+
else:
|
| 345 |
+
raise TypeError("Invalid division involving a vector")
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
class BaseVector(Vector, AtomicExpr):
|
| 349 |
+
"""
|
| 350 |
+
Class to denote a base vector.
|
| 351 |
+
|
| 352 |
+
"""
|
| 353 |
+
|
| 354 |
+
def __new__(cls, index, system, pretty_str=None, latex_str=None):
|
| 355 |
+
if pretty_str is None:
|
| 356 |
+
pretty_str = "x{}".format(index)
|
| 357 |
+
if latex_str is None:
|
| 358 |
+
latex_str = "x_{}".format(index)
|
| 359 |
+
pretty_str = str(pretty_str)
|
| 360 |
+
latex_str = str(latex_str)
|
| 361 |
+
# Verify arguments
|
| 362 |
+
if index not in range(0, 3):
|
| 363 |
+
raise ValueError("index must be 0, 1 or 2")
|
| 364 |
+
if not isinstance(system, CoordSys3D):
|
| 365 |
+
raise TypeError("system should be a CoordSys3D")
|
| 366 |
+
name = system._vector_names[index]
|
| 367 |
+
# Initialize an object
|
| 368 |
+
obj = super().__new__(cls, S(index), system)
|
| 369 |
+
# Assign important attributes
|
| 370 |
+
obj._base_instance = obj
|
| 371 |
+
obj._components = {obj: S.One}
|
| 372 |
+
obj._measure_number = S.One
|
| 373 |
+
obj._name = system._name + '.' + name
|
| 374 |
+
obj._pretty_form = '' + pretty_str
|
| 375 |
+
obj._latex_form = latex_str
|
| 376 |
+
obj._system = system
|
| 377 |
+
# The _id is used for printing purposes
|
| 378 |
+
obj._id = (index, system)
|
| 379 |
+
assumptions = {'commutative': True}
|
| 380 |
+
obj._assumptions = StdFactKB(assumptions)
|
| 381 |
+
|
| 382 |
+
# This attr is used for re-expression to one of the systems
|
| 383 |
+
# involved in the definition of the Vector. Applies to
|
| 384 |
+
# VectorMul and VectorAdd too.
|
| 385 |
+
obj._sys = system
|
| 386 |
+
|
| 387 |
+
return obj
|
| 388 |
+
|
| 389 |
+
@property
|
| 390 |
+
def system(self):
|
| 391 |
+
return self._system
|
| 392 |
+
|
| 393 |
+
def _sympystr(self, printer):
|
| 394 |
+
return self._name
|
| 395 |
+
|
| 396 |
+
def _sympyrepr(self, printer):
|
| 397 |
+
index, system = self._id
|
| 398 |
+
return printer._print(system) + '.' + system._vector_names[index]
|
| 399 |
+
|
| 400 |
+
@property
|
| 401 |
+
def free_symbols(self):
|
| 402 |
+
return {self}
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
class VectorAdd(BasisDependentAdd, Vector):
|
| 406 |
+
"""
|
| 407 |
+
Class to denote sum of Vector instances.
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
def __new__(cls, *args, **options):
|
| 411 |
+
obj = BasisDependentAdd.__new__(cls, *args, **options)
|
| 412 |
+
return obj
|
| 413 |
+
|
| 414 |
+
def _sympystr(self, printer):
|
| 415 |
+
ret_str = ''
|
| 416 |
+
items = list(self.separate().items())
|
| 417 |
+
items.sort(key=lambda x: x[0].__str__())
|
| 418 |
+
for system, vect in items:
|
| 419 |
+
base_vects = system.base_vectors()
|
| 420 |
+
for x in base_vects:
|
| 421 |
+
if x in vect.components:
|
| 422 |
+
temp_vect = self.components[x] * x
|
| 423 |
+
ret_str += printer._print(temp_vect) + " + "
|
| 424 |
+
return ret_str[:-3]
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class VectorMul(BasisDependentMul, Vector):
|
| 428 |
+
"""
|
| 429 |
+
Class to denote products of scalars and BaseVectors.
|
| 430 |
+
"""
|
| 431 |
+
|
| 432 |
+
def __new__(cls, *args, **options):
|
| 433 |
+
obj = BasisDependentMul.__new__(cls, *args, **options)
|
| 434 |
+
return obj
|
| 435 |
+
|
| 436 |
+
@property
|
| 437 |
+
def base_vector(self):
|
| 438 |
+
""" The BaseVector involved in the product. """
|
| 439 |
+
return self._base_instance
|
| 440 |
+
|
| 441 |
+
@property
|
| 442 |
+
def measure_number(self):
|
| 443 |
+
""" The scalar expression involved in the definition of
|
| 444 |
+
this VectorMul.
|
| 445 |
+
"""
|
| 446 |
+
return self._measure_number
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
class VectorZero(BasisDependentZero, Vector):
|
| 450 |
+
"""
|
| 451 |
+
Class to denote a zero vector
|
| 452 |
+
"""
|
| 453 |
+
|
| 454 |
+
_op_priority = 12.1
|
| 455 |
+
_pretty_form = '0'
|
| 456 |
+
_latex_form = r'\mathbf{\hat{0}}'
|
| 457 |
+
|
| 458 |
+
def __new__(cls):
|
| 459 |
+
obj = BasisDependentZero.__new__(cls)
|
| 460 |
+
return obj
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
class Cross(Vector):
|
| 464 |
+
"""
|
| 465 |
+
Represents unevaluated Cross product.
|
| 466 |
+
|
| 467 |
+
Examples
|
| 468 |
+
========
|
| 469 |
+
|
| 470 |
+
>>> from sympy.vector import CoordSys3D, Cross
|
| 471 |
+
>>> R = CoordSys3D('R')
|
| 472 |
+
>>> v1 = R.i + R.j + R.k
|
| 473 |
+
>>> v2 = R.x * R.i + R.y * R.j + R.z * R.k
|
| 474 |
+
>>> Cross(v1, v2)
|
| 475 |
+
Cross(R.i + R.j + R.k, R.x*R.i + R.y*R.j + R.z*R.k)
|
| 476 |
+
>>> Cross(v1, v2).doit()
|
| 477 |
+
(-R.y + R.z)*R.i + (R.x - R.z)*R.j + (-R.x + R.y)*R.k
|
| 478 |
+
|
| 479 |
+
"""
|
| 480 |
+
|
| 481 |
+
def __new__(cls, expr1, expr2):
|
| 482 |
+
expr1 = sympify(expr1)
|
| 483 |
+
expr2 = sympify(expr2)
|
| 484 |
+
if default_sort_key(expr1) > default_sort_key(expr2):
|
| 485 |
+
return -Cross(expr2, expr1)
|
| 486 |
+
obj = Expr.__new__(cls, expr1, expr2)
|
| 487 |
+
obj._expr1 = expr1
|
| 488 |
+
obj._expr2 = expr2
|
| 489 |
+
return obj
|
| 490 |
+
|
| 491 |
+
def doit(self, **hints):
|
| 492 |
+
return cross(self._expr1, self._expr2)
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
class Dot(Expr):
|
| 496 |
+
"""
|
| 497 |
+
Represents unevaluated Dot product.
|
| 498 |
+
|
| 499 |
+
Examples
|
| 500 |
+
========
|
| 501 |
+
|
| 502 |
+
>>> from sympy.vector import CoordSys3D, Dot
|
| 503 |
+
>>> from sympy import symbols
|
| 504 |
+
>>> R = CoordSys3D('R')
|
| 505 |
+
>>> a, b, c = symbols('a b c')
|
| 506 |
+
>>> v1 = R.i + R.j + R.k
|
| 507 |
+
>>> v2 = a * R.i + b * R.j + c * R.k
|
| 508 |
+
>>> Dot(v1, v2)
|
| 509 |
+
Dot(R.i + R.j + R.k, a*R.i + b*R.j + c*R.k)
|
| 510 |
+
>>> Dot(v1, v2).doit()
|
| 511 |
+
a + b + c
|
| 512 |
+
|
| 513 |
+
"""
|
| 514 |
+
|
| 515 |
+
def __new__(cls, expr1, expr2):
|
| 516 |
+
expr1 = sympify(expr1)
|
| 517 |
+
expr2 = sympify(expr2)
|
| 518 |
+
expr1, expr2 = sorted([expr1, expr2], key=default_sort_key)
|
| 519 |
+
obj = Expr.__new__(cls, expr1, expr2)
|
| 520 |
+
obj._expr1 = expr1
|
| 521 |
+
obj._expr2 = expr2
|
| 522 |
+
return obj
|
| 523 |
+
|
| 524 |
+
def doit(self, **hints):
|
| 525 |
+
return dot(self._expr1, self._expr2)
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
def cross(vect1, vect2):
|
| 529 |
+
"""
|
| 530 |
+
Returns cross product of two vectors.
|
| 531 |
+
|
| 532 |
+
Examples
|
| 533 |
+
========
|
| 534 |
+
|
| 535 |
+
>>> from sympy.vector import CoordSys3D
|
| 536 |
+
>>> from sympy.vector.vector import cross
|
| 537 |
+
>>> R = CoordSys3D('R')
|
| 538 |
+
>>> v1 = R.i + R.j + R.k
|
| 539 |
+
>>> v2 = R.x * R.i + R.y * R.j + R.z * R.k
|
| 540 |
+
>>> cross(v1, v2)
|
| 541 |
+
(-R.y + R.z)*R.i + (R.x - R.z)*R.j + (-R.x + R.y)*R.k
|
| 542 |
+
|
| 543 |
+
"""
|
| 544 |
+
if isinstance(vect1, Add):
|
| 545 |
+
return VectorAdd.fromiter(cross(i, vect2) for i in vect1.args)
|
| 546 |
+
if isinstance(vect2, Add):
|
| 547 |
+
return VectorAdd.fromiter(cross(vect1, i) for i in vect2.args)
|
| 548 |
+
if isinstance(vect1, BaseVector) and isinstance(vect2, BaseVector):
|
| 549 |
+
if vect1._sys == vect2._sys:
|
| 550 |
+
n1 = vect1.args[0]
|
| 551 |
+
n2 = vect2.args[0]
|
| 552 |
+
if n1 == n2:
|
| 553 |
+
return Vector.zero
|
| 554 |
+
n3 = ({0,1,2}.difference({n1, n2})).pop()
|
| 555 |
+
sign = 1 if ((n1 + 1) % 3 == n2) else -1
|
| 556 |
+
return sign*vect1._sys.base_vectors()[n3]
|
| 557 |
+
from .functions import express
|
| 558 |
+
try:
|
| 559 |
+
v = express(vect1, vect2._sys)
|
| 560 |
+
except ValueError:
|
| 561 |
+
return Cross(vect1, vect2)
|
| 562 |
+
else:
|
| 563 |
+
return cross(v, vect2)
|
| 564 |
+
if isinstance(vect1, VectorZero) or isinstance(vect2, VectorZero):
|
| 565 |
+
return Vector.zero
|
| 566 |
+
if isinstance(vect1, VectorMul):
|
| 567 |
+
v1, m1 = next(iter(vect1.components.items()))
|
| 568 |
+
return m1*cross(v1, vect2)
|
| 569 |
+
if isinstance(vect2, VectorMul):
|
| 570 |
+
v2, m2 = next(iter(vect2.components.items()))
|
| 571 |
+
return m2*cross(vect1, v2)
|
| 572 |
+
|
| 573 |
+
return Cross(vect1, vect2)
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
def dot(vect1, vect2):
|
| 577 |
+
"""
|
| 578 |
+
Returns dot product of two vectors.
|
| 579 |
+
|
| 580 |
+
Examples
|
| 581 |
+
========
|
| 582 |
+
|
| 583 |
+
>>> from sympy.vector import CoordSys3D
|
| 584 |
+
>>> from sympy.vector.vector import dot
|
| 585 |
+
>>> R = CoordSys3D('R')
|
| 586 |
+
>>> v1 = R.i + R.j + R.k
|
| 587 |
+
>>> v2 = R.x * R.i + R.y * R.j + R.z * R.k
|
| 588 |
+
>>> dot(v1, v2)
|
| 589 |
+
R.x + R.y + R.z
|
| 590 |
+
|
| 591 |
+
"""
|
| 592 |
+
if isinstance(vect1, Add):
|
| 593 |
+
return Add.fromiter(dot(i, vect2) for i in vect1.args)
|
| 594 |
+
if isinstance(vect2, Add):
|
| 595 |
+
return Add.fromiter(dot(vect1, i) for i in vect2.args)
|
| 596 |
+
if isinstance(vect1, BaseVector) and isinstance(vect2, BaseVector):
|
| 597 |
+
if vect1._sys == vect2._sys:
|
| 598 |
+
return S.One if vect1 == vect2 else S.Zero
|
| 599 |
+
from .functions import express
|
| 600 |
+
try:
|
| 601 |
+
v = express(vect2, vect1._sys)
|
| 602 |
+
except ValueError:
|
| 603 |
+
return Dot(vect1, vect2)
|
| 604 |
+
else:
|
| 605 |
+
return dot(vect1, v)
|
| 606 |
+
if isinstance(vect1, VectorZero) or isinstance(vect2, VectorZero):
|
| 607 |
+
return S.Zero
|
| 608 |
+
if isinstance(vect1, VectorMul):
|
| 609 |
+
v1, m1 = next(iter(vect1.components.items()))
|
| 610 |
+
return m1*dot(v1, vect2)
|
| 611 |
+
if isinstance(vect2, VectorMul):
|
| 612 |
+
v2, m2 = next(iter(vect2.components.items()))
|
| 613 |
+
return m2*dot(vect1, v2)
|
| 614 |
+
|
| 615 |
+
return Dot(vect1, vect2)
|
| 616 |
+
|
| 617 |
+
|
| 618 |
+
Vector._expr_type = Vector
|
| 619 |
+
Vector._mul_func = VectorMul
|
| 620 |
+
Vector._add_func = VectorAdd
|
| 621 |
+
Vector._zero_func = VectorZero
|
| 622 |
+
Vector._base_func = BaseVector
|
| 623 |
+
Vector.zero = VectorZero()
|
wemm/lib/python3.10/site-packages/torch/include/torch/csrc/jit/jit_opt_limit.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#include <torch/csrc/Export.h>
|
| 3 |
+
#include <string>
|
| 4 |
+
#include <unordered_map>
|
| 5 |
+
|
| 6 |
+
// `TorchScript` offers a simple optimization limit checker
|
| 7 |
+
// that can be configured through environment variable `PYTORCH_JIT_OPT_LIMIT`.
|
| 8 |
+
// The purpose is to limit how many optimization you can make per pass.
|
| 9 |
+
// This is useful for debugging any passes.
|
| 10 |
+
|
| 11 |
+
// Opt limit checker is enabled on a per file basis (hence per pass). For
|
| 12 |
+
// example, in `constant_propagation.cpp`, `PYTORCH_JIT_OPT_LIMIT` should be set
|
| 13 |
+
// to `constant_propagation=<opt_limt>` or, simply, to
|
| 14 |
+
// `constant_propagation=<opt_limit>` where <opt_limit> is the number of
|
| 15 |
+
// optimizations you want to make for the pass. (i.e.
|
| 16 |
+
// `PYTORCH_JIT_OPT_LIMIT="constant_propagation=<opt_limit>"`).
|
| 17 |
+
|
| 18 |
+
// Multiple files can be configured by separating each file name with a colon
|
| 19 |
+
// `:` as in the following example,
|
| 20 |
+
// `PYTORCH_JIT_OPT_LIMIT="constant_propagation=<opt_limit>:dead_code_elimination=<opt_limit>"`
|
| 21 |
+
|
| 22 |
+
// You can call opt limiter by calling JIT_OPT_ALLOWED. It will return true if
|
| 23 |
+
// we haven't reached the optimization limit yet. Otherwise, it will return
|
| 24 |
+
// false. Typical usage:
|
| 25 |
+
|
| 26 |
+
// if (!JIT_OPT_ALLOWED) {
|
| 27 |
+
// GRAPH_DUMP(...); //supplied from jit_log
|
| 28 |
+
// return;
|
| 29 |
+
// }
|
| 30 |
+
|
| 31 |
+
namespace torch {
|
| 32 |
+
namespace jit {
|
| 33 |
+
|
| 34 |
+
TORCH_API bool opt_limit(const char* pass_name);
|
| 35 |
+
|
| 36 |
+
#define JIT_OPT_ALLOWED opt_limit(__FILE__)
|
| 37 |
+
|
| 38 |
+
} // namespace jit
|
| 39 |
+
} // namespace torch
|
wemm/lib/python3.10/site-packages/torch/include/torch/csrc/jit/passes/common_subexpression_elimination.h
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/jit/ir/ir.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace jit {
|
| 7 |
+
|
| 8 |
+
TORCH_API bool EliminateCommonSubexpression(
|
| 9 |
+
const std::shared_ptr<Graph>& graph);
|
| 10 |
+
}
|
| 11 |
+
} // namespace torch
|