Coverage for /usr/lib/python3/dist-packages/sympy/matrices/subspaces.py: 14%

35 statements  

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

1from .utilities import _iszero 

2 

3 

4def _columnspace(M, simplify=False): 

5 """Returns a list of vectors (Matrix objects) that span columnspace of ``M`` 

6 

7 Examples 

8 ======== 

9 

10 >>> from sympy import Matrix 

11 >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) 

12 >>> M 

13 Matrix([ 

14 [ 1, 3, 0], 

15 [-2, -6, 0], 

16 [ 3, 9, 6]]) 

17 >>> M.columnspace() 

18 [Matrix([ 

19 [ 1], 

20 [-2], 

21 [ 3]]), Matrix([ 

22 [0], 

23 [0], 

24 [6]])] 

25 

26 See Also 

27 ======== 

28 

29 nullspace 

30 rowspace 

31 """ 

32 

33 reduced, pivots = M.echelon_form(simplify=simplify, with_pivots=True) 

34 

35 return [M.col(i) for i in pivots] 

36 

37 

38def _nullspace(M, simplify=False, iszerofunc=_iszero): 

39 """Returns list of vectors (Matrix objects) that span nullspace of ``M`` 

40 

41 Examples 

42 ======== 

43 

44 >>> from sympy import Matrix 

45 >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) 

46 >>> M 

47 Matrix([ 

48 [ 1, 3, 0], 

49 [-2, -6, 0], 

50 [ 3, 9, 6]]) 

51 >>> M.nullspace() 

52 [Matrix([ 

53 [-3], 

54 [ 1], 

55 [ 0]])] 

56 

57 See Also 

58 ======== 

59 

60 columnspace 

61 rowspace 

62 """ 

63 

64 reduced, pivots = M.rref(iszerofunc=iszerofunc, simplify=simplify) 

65 

66 free_vars = [i for i in range(M.cols) if i not in pivots] 

67 basis = [] 

68 

69 for free_var in free_vars: 

70 # for each free variable, we will set it to 1 and all others 

71 # to 0. Then, we will use back substitution to solve the system 

72 vec = [M.zero] * M.cols 

73 vec[free_var] = M.one 

74 

75 for piv_row, piv_col in enumerate(pivots): 

76 vec[piv_col] -= reduced[piv_row, free_var] 

77 

78 basis.append(vec) 

79 

80 return [M._new(M.cols, 1, b) for b in basis] 

81 

82 

83def _rowspace(M, simplify=False): 

84 """Returns a list of vectors that span the row space of ``M``. 

85 

86 Examples 

87 ======== 

88 

89 >>> from sympy import Matrix 

90 >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) 

91 >>> M 

92 Matrix([ 

93 [ 1, 3, 0], 

94 [-2, -6, 0], 

95 [ 3, 9, 6]]) 

96 >>> M.rowspace() 

97 [Matrix([[1, 3, 0]]), Matrix([[0, 0, 6]])] 

98 """ 

99 

100 reduced, pivots = M.echelon_form(simplify=simplify, with_pivots=True) 

101 

102 return [reduced.row(i) for i in range(len(pivots))] 

103 

104 

105def _orthogonalize(cls, *vecs, normalize=False, rankcheck=False): 

106 """Apply the Gram-Schmidt orthogonalization procedure 

107 to vectors supplied in ``vecs``. 

108 

109 Parameters 

110 ========== 

111 

112 vecs 

113 vectors to be made orthogonal 

114 

115 normalize : bool 

116 If ``True``, return an orthonormal basis. 

117 

118 rankcheck : bool 

119 If ``True``, the computation does not stop when encountering 

120 linearly dependent vectors. 

121 

122 If ``False``, it will raise ``ValueError`` when any zero 

123 or linearly dependent vectors are found. 

124 

125 Returns 

126 ======= 

127 

128 list 

129 List of orthogonal (or orthonormal) basis vectors. 

130 

131 Examples 

132 ======== 

133 

134 >>> from sympy import I, Matrix 

135 >>> v = [Matrix([1, I]), Matrix([1, -I])] 

136 >>> Matrix.orthogonalize(*v) 

137 [Matrix([ 

138 [1], 

139 [I]]), Matrix([ 

140 [ 1], 

141 [-I]])] 

142 

143 See Also 

144 ======== 

145 

146 MatrixBase.QRdecomposition 

147 

148 References 

149 ========== 

150 

151 .. [1] https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process 

152 """ 

153 from .decompositions import _QRdecomposition_optional 

154 

155 if not vecs: 

156 return [] 

157 

158 all_row_vecs = (vecs[0].rows == 1) 

159 

160 vecs = [x.vec() for x in vecs] 

161 M = cls.hstack(*vecs) 

162 Q, R = _QRdecomposition_optional(M, normalize=normalize) 

163 

164 if rankcheck and Q.cols < len(vecs): 

165 raise ValueError("GramSchmidt: vector set not linearly independent") 

166 

167 ret = [] 

168 for i in range(Q.cols): 

169 if all_row_vecs: 

170 col = cls(Q[:, i].T) 

171 else: 

172 col = cls(Q[:, i]) 

173 ret.append(col) 

174 return ret