Coverage for /usr/lib/python3/dist-packages/sympy/matrices/utilities.py: 47%
32 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1from contextlib import contextmanager
2from threading import local
4from sympy.core.function import expand_mul
7class DotProdSimpState(local):
8 def __init__(self):
9 self.state = None
11_dotprodsimp_state = DotProdSimpState()
13@contextmanager
14def dotprodsimp(x):
15 old = _dotprodsimp_state.state
17 try:
18 _dotprodsimp_state.state = x
19 yield
20 finally:
21 _dotprodsimp_state.state = old
24def _dotprodsimp(expr, withsimp=False):
25 """Wrapper for simplify.dotprodsimp to avoid circular imports."""
26 from sympy.simplify.simplify import dotprodsimp as dps
27 return dps(expr, withsimp=withsimp)
30def _get_intermediate_simp(deffunc=lambda x: x, offfunc=lambda x: x,
31 onfunc=_dotprodsimp, dotprodsimp=None):
32 """Support function for controlling intermediate simplification. Returns a
33 simplification function according to the global setting of dotprodsimp
34 operation.
36 ``deffunc`` - Function to be used by default.
37 ``offfunc`` - Function to be used if dotprodsimp has been turned off.
38 ``onfunc`` - Function to be used if dotprodsimp has been turned on.
39 ``dotprodsimp`` - True, False or None. Will be overridden by global
40 _dotprodsimp_state.state if that is not None.
41 """
43 if dotprodsimp is False or _dotprodsimp_state.state is False:
44 return offfunc
45 if dotprodsimp is True or _dotprodsimp_state.state is True:
46 return onfunc
48 return deffunc # None, None
51def _get_intermediate_simp_bool(default=False, dotprodsimp=None):
52 """Same as ``_get_intermediate_simp`` but returns bools instead of functions
53 by default."""
55 return _get_intermediate_simp(default, False, True, dotprodsimp)
58def _iszero(x):
59 """Returns True if x is zero."""
60 return getattr(x, 'is_zero', None)
63def _is_zero_after_expand_mul(x):
64 """Tests by expand_mul only, suitable for polynomials and rational
65 functions."""
66 return expand_mul(x) == 0
69def _simplify(expr):
70 """ Wrapper to avoid circular imports. """
71 from sympy.simplify.simplify import simplify
72 return simplify(expr)