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

1from contextlib import contextmanager 

2from threading import local 

3 

4from sympy.core.function import expand_mul 

5 

6 

7class DotProdSimpState(local): 

8 def __init__(self): 

9 self.state = None 

10 

11_dotprodsimp_state = DotProdSimpState() 

12 

13@contextmanager 

14def dotprodsimp(x): 

15 old = _dotprodsimp_state.state 

16 

17 try: 

18 _dotprodsimp_state.state = x 

19 yield 

20 finally: 

21 _dotprodsimp_state.state = old 

22 

23 

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) 

28 

29 

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. 

35 

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 """ 

42 

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 

47 

48 return deffunc # None, None 

49 

50 

51def _get_intermediate_simp_bool(default=False, dotprodsimp=None): 

52 """Same as ``_get_intermediate_simp`` but returns bools instead of functions 

53 by default.""" 

54 

55 return _get_intermediate_simp(default, False, True, dotprodsimp) 

56 

57 

58def _iszero(x): 

59 """Returns True if x is zero.""" 

60 return getattr(x, 'is_zero', None) 

61 

62 

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 

67 

68 

69def _simplify(expr): 

70 """ Wrapper to avoid circular imports. """ 

71 from sympy.simplify.simplify import simplify 

72 return simplify(expr)