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
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1from .utilities import _iszero
4def _columnspace(M, simplify=False):
5 """Returns a list of vectors (Matrix objects) that span columnspace of ``M``
7 Examples
8 ========
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]])]
26 See Also
27 ========
29 nullspace
30 rowspace
31 """
33 reduced, pivots = M.echelon_form(simplify=simplify, with_pivots=True)
35 return [M.col(i) for i in pivots]
38def _nullspace(M, simplify=False, iszerofunc=_iszero):
39 """Returns list of vectors (Matrix objects) that span nullspace of ``M``
41 Examples
42 ========
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]])]
57 See Also
58 ========
60 columnspace
61 rowspace
62 """
64 reduced, pivots = M.rref(iszerofunc=iszerofunc, simplify=simplify)
66 free_vars = [i for i in range(M.cols) if i not in pivots]
67 basis = []
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
75 for piv_row, piv_col in enumerate(pivots):
76 vec[piv_col] -= reduced[piv_row, free_var]
78 basis.append(vec)
80 return [M._new(M.cols, 1, b) for b in basis]
83def _rowspace(M, simplify=False):
84 """Returns a list of vectors that span the row space of ``M``.
86 Examples
87 ========
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 """
100 reduced, pivots = M.echelon_form(simplify=simplify, with_pivots=True)
102 return [reduced.row(i) for i in range(len(pivots))]
105def _orthogonalize(cls, *vecs, normalize=False, rankcheck=False):
106 """Apply the Gram-Schmidt orthogonalization procedure
107 to vectors supplied in ``vecs``.
109 Parameters
110 ==========
112 vecs
113 vectors to be made orthogonal
115 normalize : bool
116 If ``True``, return an orthonormal basis.
118 rankcheck : bool
119 If ``True``, the computation does not stop when encountering
120 linearly dependent vectors.
122 If ``False``, it will raise ``ValueError`` when any zero
123 or linearly dependent vectors are found.
125 Returns
126 =======
128 list
129 List of orthogonal (or orthonormal) basis vectors.
131 Examples
132 ========
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]])]
143 See Also
144 ========
146 MatrixBase.QRdecomposition
148 References
149 ==========
151 .. [1] https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
152 """
153 from .decompositions import _QRdecomposition_optional
155 if not vecs:
156 return []
158 all_row_vecs = (vecs[0].rows == 1)
160 vecs = [x.vec() for x in vecs]
161 M = cls.hstack(*vecs)
162 Q, R = _QRdecomposition_optional(M, normalize=normalize)
164 if rankcheck and Q.cols < len(vecs):
165 raise ValueError("GramSchmidt: vector set not linearly independent")
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