Coverage for /usr/lib/python3/dist-packages/sympy/matrices/expressions/sets.py: 47%

38 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1from sympy.core.assumptions import check_assumptions 

2from sympy.core.logic import fuzzy_and 

3from sympy.core.sympify import _sympify 

4from sympy.matrices.common import MatrixKind 

5from sympy.sets.sets import Set, SetKind 

6from sympy.core.kind import NumberKind 

7from .matexpr import MatrixExpr 

8 

9 

10class MatrixSet(Set): 

11 """ 

12 MatrixSet represents the set of matrices with ``shape = (n, m)`` over the 

13 given set. 

14 

15 Examples 

16 ======== 

17 

18 >>> from sympy.matrices import MatrixSet 

19 >>> from sympy import S, I, Matrix 

20 >>> M = MatrixSet(2, 2, set=S.Reals) 

21 >>> X = Matrix([[1, 2], [3, 4]]) 

22 >>> X in M 

23 True 

24 >>> X = Matrix([[1, 2], [I, 4]]) 

25 >>> X in M 

26 False 

27 

28 """ 

29 is_empty = False 

30 

31 def __new__(cls, n, m, set): 

32 n, m, set = _sympify(n), _sympify(m), _sympify(set) 

33 cls._check_dim(n) 

34 cls._check_dim(m) 

35 if not isinstance(set, Set): 

36 raise TypeError("{} should be an instance of Set.".format(set)) 

37 return Set.__new__(cls, n, m, set) 

38 

39 @property 

40 def shape(self): 

41 return self.args[:2] 

42 

43 @property 

44 def set(self): 

45 return self.args[2] 

46 

47 def _contains(self, other): 

48 if not isinstance(other, MatrixExpr): 

49 raise TypeError("{} should be an instance of MatrixExpr.".format(other)) 

50 if other.shape != self.shape: 

51 are_symbolic = any(_sympify(x).is_Symbol for x in other.shape + self.shape) 

52 if are_symbolic: 

53 return None 

54 return False 

55 return fuzzy_and(self.set.contains(x) for x in other) 

56 

57 @classmethod 

58 def _check_dim(cls, dim): 

59 """Helper function to check invalid matrix dimensions""" 

60 ok = check_assumptions(dim, integer=True, nonnegative=True) 

61 if ok is False: 

62 raise ValueError( 

63 "The dimension specification {} should be " 

64 "a nonnegative integer.".format(dim)) 

65 

66 def _kind(self): 

67 return SetKind(MatrixKind(NumberKind))