passagemath-symbolics 10.8.1a1__cp314-cp314t-musllinux_1_2_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. passagemath_symbolics/__init__.py +3 -0
  2. passagemath_symbolics-10.8.1a1.dist-info/METADATA +186 -0
  3. passagemath_symbolics-10.8.1a1.dist-info/RECORD +181 -0
  4. passagemath_symbolics-10.8.1a1.dist-info/WHEEL +5 -0
  5. passagemath_symbolics-10.8.1a1.dist-info/top_level.txt +3 -0
  6. sage/all__sagemath_symbolics.py +17 -0
  7. sage/calculus/all.py +14 -0
  8. sage/calculus/calculus.py +2838 -0
  9. sage/calculus/desolvers.py +1864 -0
  10. sage/calculus/predefined.py +51 -0
  11. sage/calculus/tests.py +225 -0
  12. sage/calculus/var.cpython-314t-aarch64-linux-musl.so +0 -0
  13. sage/calculus/var.pyx +401 -0
  14. sage/dynamics/all__sagemath_symbolics.py +6 -0
  15. sage/dynamics/complex_dynamics/all.py +5 -0
  16. sage/dynamics/complex_dynamics/mandel_julia.py +765 -0
  17. sage/dynamics/complex_dynamics/mandel_julia_helper.cpython-314t-aarch64-linux-musl.so +0 -0
  18. sage/dynamics/complex_dynamics/mandel_julia_helper.pyx +1034 -0
  19. sage/ext/all__sagemath_symbolics.py +1 -0
  20. sage/ext_data/kenzo/CP2.txt +45 -0
  21. sage/ext_data/kenzo/CP3.txt +349 -0
  22. sage/ext_data/kenzo/CP4.txt +4774 -0
  23. sage/ext_data/kenzo/README.txt +49 -0
  24. sage/ext_data/kenzo/S4.txt +20 -0
  25. sage/ext_data/magma/latex/latex.m +1021 -0
  26. sage/ext_data/magma/latex/latex.spec +1 -0
  27. sage/ext_data/magma/sage/basic.m +356 -0
  28. sage/ext_data/magma/sage/sage.spec +1 -0
  29. sage/ext_data/magma/spec +9 -0
  30. sage/geometry/all__sagemath_symbolics.py +8 -0
  31. sage/geometry/hyperbolic_space/all.py +5 -0
  32. sage/geometry/hyperbolic_space/hyperbolic_coercion.py +755 -0
  33. sage/geometry/hyperbolic_space/hyperbolic_constants.py +5 -0
  34. sage/geometry/hyperbolic_space/hyperbolic_geodesic.py +2419 -0
  35. sage/geometry/hyperbolic_space/hyperbolic_interface.py +206 -0
  36. sage/geometry/hyperbolic_space/hyperbolic_isometry.py +1083 -0
  37. sage/geometry/hyperbolic_space/hyperbolic_model.py +1502 -0
  38. sage/geometry/hyperbolic_space/hyperbolic_point.py +621 -0
  39. sage/geometry/riemannian_manifolds/all.py +7 -0
  40. sage/geometry/riemannian_manifolds/parametrized_surface3d.py +1632 -0
  41. sage/geometry/riemannian_manifolds/surface3d_generators.py +461 -0
  42. sage/interfaces/all__sagemath_symbolics.py +1 -0
  43. sage/interfaces/magma.py +2991 -0
  44. sage/interfaces/magma_free.py +90 -0
  45. sage/interfaces/maple.py +1402 -0
  46. sage/interfaces/mathematica.py +1345 -0
  47. sage/interfaces/mathics.py +1312 -0
  48. sage/interfaces/sympy.py +1398 -0
  49. sage/interfaces/sympy_wrapper.py +197 -0
  50. sage/interfaces/tides.py +938 -0
  51. sage/libs/all__sagemath_symbolics.py +6 -0
  52. sage/manifolds/all.py +7 -0
  53. sage/manifolds/calculus_method.py +553 -0
  54. sage/manifolds/catalog.py +437 -0
  55. sage/manifolds/chart.py +4010 -0
  56. sage/manifolds/chart_func.py +3416 -0
  57. sage/manifolds/continuous_map.py +2183 -0
  58. sage/manifolds/continuous_map_image.py +155 -0
  59. sage/manifolds/differentiable/affine_connection.py +2475 -0
  60. sage/manifolds/differentiable/all.py +1 -0
  61. sage/manifolds/differentiable/automorphismfield.py +1383 -0
  62. sage/manifolds/differentiable/automorphismfield_group.py +604 -0
  63. sage/manifolds/differentiable/bundle_connection.py +1445 -0
  64. sage/manifolds/differentiable/characteristic_cohomology_class.py +1840 -0
  65. sage/manifolds/differentiable/chart.py +1241 -0
  66. sage/manifolds/differentiable/curve.py +1028 -0
  67. sage/manifolds/differentiable/de_rham_cohomology.py +541 -0
  68. sage/manifolds/differentiable/degenerate.py +559 -0
  69. sage/manifolds/differentiable/degenerate_submanifold.py +1668 -0
  70. sage/manifolds/differentiable/diff_form.py +1660 -0
  71. sage/manifolds/differentiable/diff_form_module.py +1062 -0
  72. sage/manifolds/differentiable/diff_map.py +1315 -0
  73. sage/manifolds/differentiable/differentiable_submanifold.py +291 -0
  74. sage/manifolds/differentiable/examples/all.py +1 -0
  75. sage/manifolds/differentiable/examples/euclidean.py +2517 -0
  76. sage/manifolds/differentiable/examples/real_line.py +897 -0
  77. sage/manifolds/differentiable/examples/sphere.py +1186 -0
  78. sage/manifolds/differentiable/examples/symplectic_space.py +187 -0
  79. sage/manifolds/differentiable/examples/symplectic_space_test.py +40 -0
  80. sage/manifolds/differentiable/integrated_curve.py +4035 -0
  81. sage/manifolds/differentiable/levi_civita_connection.py +841 -0
  82. sage/manifolds/differentiable/manifold.py +4254 -0
  83. sage/manifolds/differentiable/manifold_homset.py +1826 -0
  84. sage/manifolds/differentiable/metric.py +3032 -0
  85. sage/manifolds/differentiable/mixed_form.py +1507 -0
  86. sage/manifolds/differentiable/mixed_form_algebra.py +559 -0
  87. sage/manifolds/differentiable/multivector_module.py +800 -0
  88. sage/manifolds/differentiable/multivectorfield.py +1522 -0
  89. sage/manifolds/differentiable/poisson_tensor.py +268 -0
  90. sage/manifolds/differentiable/pseudo_riemannian.py +755 -0
  91. sage/manifolds/differentiable/pseudo_riemannian_submanifold.py +1839 -0
  92. sage/manifolds/differentiable/scalarfield.py +1343 -0
  93. sage/manifolds/differentiable/scalarfield_algebra.py +472 -0
  94. sage/manifolds/differentiable/symplectic_form.py +912 -0
  95. sage/manifolds/differentiable/symplectic_form_test.py +220 -0
  96. sage/manifolds/differentiable/tangent_space.py +412 -0
  97. sage/manifolds/differentiable/tangent_vector.py +616 -0
  98. sage/manifolds/differentiable/tensorfield.py +4665 -0
  99. sage/manifolds/differentiable/tensorfield_module.py +963 -0
  100. sage/manifolds/differentiable/tensorfield_paral.py +2450 -0
  101. sage/manifolds/differentiable/tensorfield_paral_test.py +16 -0
  102. sage/manifolds/differentiable/vector_bundle.py +1725 -0
  103. sage/manifolds/differentiable/vectorfield.py +1717 -0
  104. sage/manifolds/differentiable/vectorfield_module.py +2445 -0
  105. sage/manifolds/differentiable/vectorframe.py +1832 -0
  106. sage/manifolds/family.py +270 -0
  107. sage/manifolds/local_frame.py +1490 -0
  108. sage/manifolds/manifold.py +3090 -0
  109. sage/manifolds/manifold_homset.py +452 -0
  110. sage/manifolds/operators.py +359 -0
  111. sage/manifolds/point.py +994 -0
  112. sage/manifolds/scalarfield.py +3718 -0
  113. sage/manifolds/scalarfield_algebra.py +629 -0
  114. sage/manifolds/section.py +3111 -0
  115. sage/manifolds/section_module.py +831 -0
  116. sage/manifolds/structure.py +229 -0
  117. sage/manifolds/subset.py +2721 -0
  118. sage/manifolds/subsets/all.py +1 -0
  119. sage/manifolds/subsets/closure.py +131 -0
  120. sage/manifolds/subsets/pullback.py +883 -0
  121. sage/manifolds/topological_submanifold.py +891 -0
  122. sage/manifolds/trivialization.py +733 -0
  123. sage/manifolds/utilities.py +1348 -0
  124. sage/manifolds/vector_bundle.py +1347 -0
  125. sage/manifolds/vector_bundle_fiber.py +332 -0
  126. sage/manifolds/vector_bundle_fiber_element.py +111 -0
  127. sage/matrix/all__sagemath_symbolics.py +1 -0
  128. sage/matrix/matrix_symbolic_dense.cpython-314t-aarch64-linux-musl.so +0 -0
  129. sage/matrix/matrix_symbolic_dense.pxd +6 -0
  130. sage/matrix/matrix_symbolic_dense.pyx +1030 -0
  131. sage/matrix/matrix_symbolic_sparse.cpython-314t-aarch64-linux-musl.so +0 -0
  132. sage/matrix/matrix_symbolic_sparse.pxd +6 -0
  133. sage/matrix/matrix_symbolic_sparse.pyx +1038 -0
  134. sage/modules/all__sagemath_symbolics.py +1 -0
  135. sage/modules/vector_callable_symbolic_dense.py +105 -0
  136. sage/modules/vector_symbolic_dense.py +116 -0
  137. sage/modules/vector_symbolic_sparse.py +118 -0
  138. sage/rings/all__sagemath_symbolics.py +4 -0
  139. sage/rings/asymptotic/all.py +6 -0
  140. sage/rings/asymptotic/asymptotic_expansion_generators.py +1485 -0
  141. sage/rings/asymptotic/asymptotic_ring.py +4858 -0
  142. sage/rings/asymptotic/asymptotics_multivariate_generating_functions.py +4106 -0
  143. sage/rings/asymptotic/growth_group.py +5373 -0
  144. sage/rings/asymptotic/growth_group_cartesian.py +1400 -0
  145. sage/rings/asymptotic/term_monoid.py +5205 -0
  146. sage/rings/function_field/all__sagemath_symbolics.py +2 -0
  147. sage/rings/polynomial/all__sagemath_symbolics.py +1 -0
  148. sage/symbolic/all.py +15 -0
  149. sage/symbolic/assumptions.py +987 -0
  150. sage/symbolic/benchmark.py +93 -0
  151. sage/symbolic/callable.py +456 -0
  152. sage/symbolic/callable.pyi +66 -0
  153. sage/symbolic/comparison_impl.pyi +38 -0
  154. sage/symbolic/complexity_measures.py +35 -0
  155. sage/symbolic/constants.py +1286 -0
  156. sage/symbolic/constants_c_impl.pyi +10 -0
  157. sage/symbolic/expression_conversion_algebraic.py +310 -0
  158. sage/symbolic/expression_conversion_sympy.py +317 -0
  159. sage/symbolic/expression_conversions.py +1727 -0
  160. sage/symbolic/function_factory.py +355 -0
  161. sage/symbolic/function_factory.pyi +41 -0
  162. sage/symbolic/getitem_impl.pyi +24 -0
  163. sage/symbolic/integration/all.py +1 -0
  164. sage/symbolic/integration/external.py +271 -0
  165. sage/symbolic/integration/integral.py +1075 -0
  166. sage/symbolic/maxima_wrapper.py +162 -0
  167. sage/symbolic/operators.py +267 -0
  168. sage/symbolic/operators.pyi +61 -0
  169. sage/symbolic/pynac_constant_impl.pyi +13 -0
  170. sage/symbolic/pynac_function_impl.pyi +8 -0
  171. sage/symbolic/random_tests.py +461 -0
  172. sage/symbolic/relation.py +2062 -0
  173. sage/symbolic/ring.cpython-314t-aarch64-linux-musl.so +0 -0
  174. sage/symbolic/ring.pxd +5 -0
  175. sage/symbolic/ring.pyi +110 -0
  176. sage/symbolic/ring.pyx +1393 -0
  177. sage/symbolic/series_impl.pyi +10 -0
  178. sage/symbolic/subring.py +1025 -0
  179. sage/symbolic/symengine.py +19 -0
  180. sage/symbolic/tests.py +40 -0
  181. sage/symbolic/units.py +1468 -0
@@ -0,0 +1,1038 @@
1
+ # sage_setup: distribution = sagemath-symbolics
2
+ """
3
+ Symbolic sparse matrices
4
+
5
+ EXAMPLES::
6
+
7
+ sage: matrix(SR, 2, 2, range(4), sparse=True)
8
+ [0 1]
9
+ [2 3]
10
+ sage: matrix(SR, 2, 2, var('t'), sparse=True)
11
+ [t 0]
12
+ [0 t]
13
+
14
+ Arithmetic::
15
+
16
+ sage: -matrix(SR, 2, range(4), sparse=True)
17
+ [ 0 -1]
18
+ [-2 -3]
19
+ sage: m = matrix(SR, 2, [1..4], sparse=True); sqrt(2)*m
20
+ [ sqrt(2) 2*sqrt(2)]
21
+ [3*sqrt(2) 4*sqrt(2)]
22
+ sage: m = matrix(SR, 4, [1..4^2], sparse=True)
23
+ sage: m * m
24
+ [ 90 100 110 120]
25
+ [202 228 254 280]
26
+ [314 356 398 440]
27
+ [426 484 542 600]
28
+
29
+ sage: m = matrix(SR, 3, [1, 2, 3], sparse=True); m
30
+ [1]
31
+ [2]
32
+ [3]
33
+ sage: m.transpose() * m
34
+ [14]
35
+
36
+ Computing inverses::
37
+
38
+ sage: M = matrix(SR, 2, var('a,b,c,d'), sparse=True)
39
+ sage: ~M
40
+ [1/a - b*c/(a^2*(b*c/a - d)) b/(a*(b*c/a - d))]
41
+ [ c/(a*(b*c/a - d)) -1/(b*c/a - d)]
42
+ sage: (~M*M).simplify_rational()
43
+ [1 0]
44
+ [0 1]
45
+ sage: M = matrix(SR, 3, 3, range(9), sparse=True) - var('t')
46
+ sage: (~M * M).simplify_rational()
47
+ [1 0 0]
48
+ [0 1 0]
49
+ [0 0 1]
50
+
51
+ sage: matrix(SR, 1, 1, 1, sparse=True).inverse()
52
+ [1]
53
+ sage: matrix(SR, 0, 0, sparse=True).inverse()
54
+ []
55
+ sage: matrix(SR, 3, 0, sparse=True).inverse()
56
+ Traceback (most recent call last):
57
+ ...
58
+ ArithmeticError: self must be a square matrix
59
+
60
+ Transposition::
61
+
62
+ sage: m = matrix(SR, 2, [sqrt(2), -1, pi, e^2], sparse=True)
63
+ sage: m.transpose()
64
+ [sqrt(2) pi]
65
+ [ -1 e^2]
66
+
67
+ ``.T`` is a convenient shortcut for the transpose::
68
+
69
+ sage: m.T
70
+ [sqrt(2) pi]
71
+ [ -1 e^2]
72
+
73
+ Test pickling::
74
+
75
+ sage: m = matrix(SR, 2, [sqrt(2), 3, pi, e], sparse=True); m
76
+ [sqrt(2) 3]
77
+ [ pi e]
78
+ sage: TestSuite(m).run()
79
+
80
+ Comparison::
81
+
82
+ sage: m = matrix(SR, 2, [sqrt(2), 3, pi, e], sparse=True)
83
+ sage: m == m
84
+ True
85
+ sage: m != 3
86
+ True
87
+ sage: m = matrix(SR,2,[1..4], sparse=True); n = m^2
88
+ sage: (exp(m+n) - exp(m)*exp(n)).simplify_rational() == 0 # indirect test
89
+ True
90
+
91
+
92
+ Determinant::
93
+
94
+ sage: M = matrix(SR, 2, 2, [x,2,3,4], sparse=True)
95
+ sage: M.determinant()
96
+ 4*x - 6
97
+ sage: M = matrix(SR, 3,3,range(9), sparse=True)
98
+ sage: M.det()
99
+ 0
100
+ sage: t = var('t')
101
+ sage: M = matrix(SR, 2, 2, [cos(t), sin(t), -sin(t), cos(t)], sparse=True)
102
+ sage: M.det()
103
+ cos(t)^2 + sin(t)^2
104
+ sage: M = matrix([[sqrt(x),0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]], sparse=True)
105
+ sage: det(M)
106
+ sqrt(x)
107
+
108
+ Permanents::
109
+
110
+ sage: M = matrix(SR, 2, 2, [x,2,3,4], sparse=True)
111
+ sage: M.permanent()
112
+ 4*x + 6
113
+
114
+ Rank::
115
+
116
+ sage: M = matrix(SR, 5, 5, range(25), sparse=True)
117
+ sage: M.rank()
118
+ 2
119
+ sage: M = matrix(SR, 5, 5, range(25), sparse=True) - var('t')
120
+ sage: M.rank()
121
+ 5
122
+
123
+ .. warning::
124
+
125
+ :meth:`rank` may return the wrong answer if it cannot determine that a
126
+ matrix element that is equivalent to zero is indeed so.
127
+
128
+ Copying symbolic matrices::
129
+
130
+ sage: m = matrix(SR, 2, [sqrt(2), 3, pi, e], sparse=True)
131
+ sage: n = copy(m)
132
+ sage: n[0,0] = sin(1)
133
+ sage: m
134
+ [sqrt(2) 3]
135
+ [ pi e]
136
+ sage: n
137
+ [sin(1) 3]
138
+ [ pi e]
139
+
140
+ Conversion to Maxima::
141
+
142
+ sage: m = matrix(SR, 2, [sqrt(2), 3, pi, e], sparse=True)
143
+ sage: m._maxima_()
144
+ matrix([sqrt(2),3],[%pi,%e])
145
+
146
+ TESTS:
147
+
148
+ Check that :issue:`12778` is fixed::
149
+
150
+ sage: M = Matrix([[1, 0.9, 1/5, x^2], [2, 1.9, 2/5, x^3], [3, 2.9, 3/5, x^4]], sparse=True); M
151
+ [ 1 0.900000000000000 1/5 x^2]
152
+ [ 2 1.90000000000000 2/5 x^3]
153
+ [ 3 2.90000000000000 3/5 x^4]
154
+ sage: parent(M)
155
+ Full MatrixSpace of 3 by 4 sparse matrices over Symbolic Ring
156
+
157
+ Check that :issue:`35653` is fixed::
158
+
159
+ sage: diagonal_matrix([x]).inverse()
160
+ [1/x]
161
+ sage: M = MatrixSpace(SR,2,2,sparse=True)
162
+ sage: M([[x,0],[0,x]]).inverse()
163
+ [1/x 0]
164
+ [ 0 1/x]
165
+ """
166
+ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
167
+ from sage.structure.factorization import Factorization
168
+
169
+ from sage.matrix.matrix_generic_sparse cimport Matrix_generic_sparse
170
+ from sage.matrix.constructor import matrix
171
+ from sage.misc.flatten import flatten
172
+
173
+ cdef maxima
174
+
175
+ from sage.calculus.calculus import maxima
176
+
177
+ cdef class Matrix_symbolic_sparse(Matrix_generic_sparse):
178
+ def echelonize(self, **kwds):
179
+ """
180
+ Echelonize using the classical algorithm.
181
+
182
+ TESTS::
183
+
184
+ sage: m = matrix([[cos(pi/5), sin(pi/5)], [-sin(pi/5), cos(pi/5)]], sparse=True)
185
+ sage: m.echelonize(); m
186
+ [1 0]
187
+ [0 1]
188
+ """
189
+ return super().echelonize(algorithm='classical', **kwds)
190
+
191
+ def eigenvalues(self, extend=True):
192
+ """
193
+ Compute the eigenvalues by solving the characteristic
194
+ polynomial in maxima.
195
+
196
+ The argument ``extend`` is ignored but kept for compatibility with
197
+ other matrix classes.
198
+
199
+ EXAMPLES::
200
+
201
+ sage: a=matrix(SR,[[1,2],[3,4]], sparse=True)
202
+ sage: a.eigenvalues()
203
+ [-1/2*sqrt(33) + 5/2, 1/2*sqrt(33) + 5/2]
204
+
205
+ TESTS:
206
+
207
+ Check for :issue:`31700`::
208
+
209
+ sage: m = matrix([[cos(pi/5), sin(pi/5)], [-sin(pi/5), cos(pi/5)]], sparse=True)
210
+ sage: t = linear_transformation(m)
211
+ sage: t.eigenvalues()
212
+ [1/4*sqrt(5) - 1/4*sqrt(2*sqrt(5) - 10) + 1/4,
213
+ 1/4*sqrt(5) + 1/4*sqrt(2*sqrt(5) - 10) + 1/4]
214
+ """
215
+ maxima_evals = self._maxima_(maxima).eigenvalues()._sage_()
216
+ if not len(maxima_evals):
217
+ raise ArithmeticError("could not determine eigenvalues exactly using symbolic matrices; try using a different type of matrix via self.change_ring(), if possible")
218
+ return sum([[ev] * int(mult) for ev, mult in zip(*maxima_evals)], [])
219
+
220
+ def eigenvectors_left(self, other=None):
221
+ r"""
222
+ Compute the left eigenvectors of a matrix.
223
+
224
+ INPUT:
225
+
226
+ - ``other`` -- a square matrix `B` (default: ``None``) in a generalized
227
+ eigenvalue problem; if ``None``, an ordinary eigenvalue problem is
228
+ solved (currently supported only if the base ring of ``self`` is
229
+ ``RDF`` or ``CDF``)
230
+
231
+ OUTPUT:
232
+
233
+ For each distinct eigenvalue, returns a list of the form (e,V,n)
234
+ where e is the eigenvalue, V is a list of eigenvectors forming a
235
+ basis for the corresponding left eigenspace, and n is the
236
+ algebraic multiplicity of the eigenvalue.
237
+
238
+ EXAMPLES::
239
+
240
+ sage: A = matrix(SR,3,3,range(9), sparse=True); A
241
+ [0 1 2]
242
+ [3 4 5]
243
+ [6 7 8]
244
+ sage: es = A.eigenvectors_left(); es
245
+ [(-3*sqrt(6) + 6, [(1, -1/5*sqrt(6) + 4/5, -2/5*sqrt(6) + 3/5)], 1),
246
+ (3*sqrt(6) + 6, [(1, 1/5*sqrt(6) + 4/5, 2/5*sqrt(6) + 3/5)], 1),
247
+ (0, [(1, -2, 1)], 1)]
248
+ sage: eval, [evec], mult = es[0]
249
+ sage: delta = eval*evec - evec*A
250
+ sage: abs(abs(delta)) < 1e-10
251
+ 3/5*sqrt(((2*sqrt(6) - 3)*(sqrt(6) - 2) + 7*sqrt(6) - 18)^2 + ((sqrt(6) - 2)*(sqrt(6) - 4) + 6*sqrt(6) - 14)^2) < (1.00000000000000e-10)
252
+ sage: abs(abs(delta)).n() < 1e-10
253
+ True
254
+
255
+ ::
256
+
257
+ sage: A = matrix(SR, 2, 2, var('a,b,c,d'), sparse=True)
258
+ sage: A.eigenvectors_left()
259
+ [(1/2*a + 1/2*d - 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2), [(1, -1/2*(a - d + sqrt(a^2 + 4*b*c - 2*a*d + d^2))/c)], 1), (1/2*a + 1/2*d + 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2), [(1, -1/2*(a - d - sqrt(a^2 + 4*b*c - 2*a*d + d^2))/c)], 1)]
260
+ sage: es = A.eigenvectors_left(); es
261
+ [(1/2*a + 1/2*d - 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2), [(1, -1/2*(a - d + sqrt(a^2 + 4*b*c - 2*a*d + d^2))/c)], 1), (1/2*a + 1/2*d + 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2), [(1, -1/2*(a - d - sqrt(a^2 + 4*b*c - 2*a*d + d^2))/c)], 1)]
262
+ sage: eval, [evec], mult = es[0]
263
+ sage: delta = eval*evec - evec*A
264
+ sage: delta.apply_map(lambda x: x.full_simplify())
265
+ (0, 0)
266
+
267
+ This routine calls Maxima and can struggle with even small matrices
268
+ with a few variables, such as a `3\times 3` matrix with three variables.
269
+ However, if the entries are integers or rationals it can produce exact
270
+ values in a reasonable time. These examples create 0-1 matrices from
271
+ the adjacency matrices of graphs and illustrate how the format and type
272
+ of the results differ when the base ring changes. First for matrices
273
+ over the rational numbers, then the same matrix but viewed as a symbolic
274
+ matrix. ::
275
+
276
+ sage: # needs sage.graphs
277
+ sage: G = graphs.CycleGraph(5)
278
+ sage: am = G.adjacency_matrix(sparse=True)
279
+ sage: spectrum = am.eigenvectors_left()
280
+ sage: qqbar_evalue = spectrum[2][0]
281
+ sage: type(qqbar_evalue)
282
+ <class 'sage.rings.qqbar.AlgebraicNumber'>
283
+ sage: qqbar_evalue
284
+ 0.618033988749895?
285
+ sage: am = G.adjacency_matrix(sparse=True).change_ring(SR)
286
+ sage: spectrum = am.eigenvectors_left()
287
+ sage: symbolic_evalue = spectrum[2][0]
288
+ sage: type(symbolic_evalue)
289
+ <class 'sage.symbolic.expression.Expression'>
290
+ sage: symbolic_evalue
291
+ 1/2*sqrt(5) - 1/2
292
+ sage: bool(qqbar_evalue == symbolic_evalue)
293
+ True
294
+
295
+ A slightly larger matrix with a "nice" spectrum. ::
296
+
297
+ sage: # needs sage.graphs
298
+ sage: G = graphs.CycleGraph(6)
299
+ sage: am = G.adjacency_matrix(sparse=True).change_ring(SR)
300
+ sage: am.eigenvectors_left()
301
+ [(-1, [(1, 0, -1, 1, 0, -1), (0, 1, -1, 0, 1, -1)], 2), (1, [(1, 0, -1, -1, 0, 1), (0, 1, 1, 0, -1, -1)], 2), (-2, [(1, -1, 1, -1, 1, -1)], 1), (2, [(1, 1, 1, 1, 1, 1)], 1)]
302
+
303
+ TESTS::
304
+
305
+ sage: A = matrix(SR, [[1, 2], [3, 4]], sparse=True)
306
+ sage: B = matrix(SR, [[1, 1], [0, 1]], sparse=True)
307
+ sage: A.eigenvectors_left(B)
308
+ Traceback (most recent call last):
309
+ ...
310
+ NotImplementedError: generalized eigenvector decomposition is
311
+ implemented for RDF and CDF, but not for Symbolic Ring
312
+
313
+ Check that :issue:`23332` is fixed::
314
+
315
+ sage: matrix([[x, x^2], [1, 0]], sparse=True).eigenvectors_left()
316
+ [(-1/2*x*(sqrt(5) - 1), [(1, -1/2*x*(sqrt(5) + 1))], 1),
317
+ (1/2*x*(sqrt(5) + 1), [(1, 1/2*x*(sqrt(5) - 1))], 1)]
318
+ """
319
+ if other is not None:
320
+ raise NotImplementedError('generalized eigenvector decomposition '
321
+ 'is implemented for RDF and CDF, but '
322
+ 'not for %s' % self.base_ring())
323
+
324
+ from sage.modules.free_module_element import vector
325
+ from sage.rings.integer_ring import ZZ
326
+
327
+ [evals, mults], evecs = self.transpose()._maxima_(maxima).eigenvectors()._sage_()
328
+ result = []
329
+ for e, evec, m in zip(evals, evecs, mults):
330
+ result.append((e, [vector(v) for v in evec], ZZ(m)))
331
+
332
+ return result
333
+
334
+ def eigenvectors_right(self, other=None):
335
+ r"""
336
+ Compute the right eigenvectors of a matrix.
337
+
338
+ INPUT:
339
+
340
+ - ``other`` -- a square matrix `B` (default: ``None``) in a generalized
341
+ eigenvalue problem; if ``None``, an ordinary eigenvalue problem is
342
+ solved (currently supported only if the base ring of ``self`` is
343
+ ``RDF`` or ``CDF``)
344
+
345
+ OUTPUT:
346
+
347
+ For each distinct eigenvalue, returns a list of the form (e,V,n)
348
+ where e is the eigenvalue, V is a list of eigenvectors forming a
349
+ basis for the corresponding right eigenspace, and n is the
350
+ algebraic multiplicity of the eigenvalue.
351
+
352
+ EXAMPLES::
353
+
354
+ sage: A = matrix(SR,2,2,range(4), sparse=True); A
355
+ [0 1]
356
+ [2 3]
357
+ sage: right = A.eigenvectors_right(); right
358
+ [(-1/2*sqrt(17) + 3/2, [(1, -1/2*sqrt(17) + 3/2)], 1), (1/2*sqrt(17) + 3/2, [(1, 1/2*sqrt(17) + 3/2)], 1)]
359
+
360
+ The right eigenvectors are nothing but the left eigenvectors of the
361
+ transpose matrix::
362
+
363
+ sage: left = A.transpose().eigenvectors_left(); left
364
+ [(-1/2*sqrt(17) + 3/2, [(1, -1/2*sqrt(17) + 3/2)], 1), (1/2*sqrt(17) + 3/2, [(1, 1/2*sqrt(17) + 3/2)], 1)]
365
+ sage: right[0][1] == left[0][1]
366
+ True
367
+
368
+ TESTS::
369
+
370
+ sage: A = matrix(SR, [[1, 2], [3, 4]], sparse=True)
371
+ sage: B = matrix(SR, [[1, 1], [0, 1]], sparse=True)
372
+ sage: A.eigenvectors_right(B)
373
+ Traceback (most recent call last):
374
+ ...
375
+ NotImplementedError: generalized eigenvector decomposition is
376
+ implemented for RDF and CDF, but not for Symbolic Ring
377
+
378
+ Check that :issue:`23332` is fixed::
379
+
380
+ sage: matrix([[x, x^2], [1, 0]], sparse=True).eigenvectors_right()
381
+ [(-1/2*x*(sqrt(5) - 1), [(1, -1/2*(sqrt(5) + 1)/x)], 1),
382
+ (1/2*x*(sqrt(5) + 1), [(1, 1/2*(sqrt(5) - 1)/x)], 1)]
383
+ """
384
+ return self.transpose().eigenvectors_left(other=other)
385
+
386
+ def exp(self):
387
+ r"""
388
+ Return the matrix exponential of this matrix `X`, which is the matrix
389
+
390
+ .. MATH::
391
+
392
+ e^X = \sum_{k=0}^{\infty} \frac{X^k}{k!}.
393
+
394
+ This function depends on maxima's matrix exponentiation
395
+ function, which does not deal well with floating point
396
+ numbers. If the matrix has floating point numbers, they will
397
+ be rounded automatically to rational numbers during the
398
+ computation.
399
+
400
+ EXAMPLES::
401
+
402
+ sage: m = matrix(SR,2, [0,x,x,0], sparse=True); m
403
+ [0 x]
404
+ [x 0]
405
+ sage: m.exp()
406
+ [1/2*(e^(2*x) + 1)*e^(-x) 1/2*(e^(2*x) - 1)*e^(-x)]
407
+ [1/2*(e^(2*x) - 1)*e^(-x) 1/2*(e^(2*x) + 1)*e^(-x)]
408
+ sage: exp(m)
409
+ [1/2*(e^(2*x) + 1)*e^(-x) 1/2*(e^(2*x) - 1)*e^(-x)]
410
+ [1/2*(e^(2*x) - 1)*e^(-x) 1/2*(e^(2*x) + 1)*e^(-x)]
411
+
412
+ Exponentiation works on 0x0 and 1x1 matrices, but the 1x1 example
413
+ requires a patched version of maxima (:issue:`32898`) for now::
414
+
415
+ sage: m = matrix(SR,0,[], sparse=True); m
416
+ []
417
+ sage: m.exp()
418
+ []
419
+ sage: m = matrix(SR,1,[2], sparse=True); m
420
+ [2]
421
+ sage: m.exp() # not tested, requires patched maxima
422
+ [e^2]
423
+
424
+ Commuting matrices `m, n` have the property that
425
+ `e^{m+n} = e^m e^n` (but non-commuting matrices need not)::
426
+
427
+ sage: m = matrix(SR,2,[1..4], sparse=True); n = m^2
428
+ sage: m*n
429
+ [ 37 54]
430
+ [ 81 118]
431
+ sage: n*m
432
+ [ 37 54]
433
+ [ 81 118]
434
+
435
+ sage: a = exp(m+n) - exp(m)*exp(n)
436
+ sage: a.simplify_rational() == 0
437
+ True
438
+
439
+ The input matrix must be square::
440
+
441
+ sage: m = matrix(SR,2,3,[1..6], sparse=True); exp(m)
442
+ Traceback (most recent call last):
443
+ ...
444
+ ValueError: exp only defined on square matrices
445
+
446
+ In this example we take the symbolic answer and make it
447
+ numerical at the end::
448
+
449
+ sage: exp(matrix(SR, [[1.2, 5.6], [3,4]], sparse=True)).change_ring(RDF) # rel tol 1e-15
450
+ [ 346.5574872980695 661.7345909344504]
451
+ [354.50067371488416 677.4247827652946]
452
+
453
+ Another example involving the reversed identity matrix, which
454
+ we clumsily create::
455
+
456
+ sage: m = identity_matrix(SR,4, sparse=True)
457
+ sage: m = matrix(list(reversed(m.rows())), sparse=True) * x
458
+ sage: exp(m)
459
+ [1/2*(e^(2*x) + 1)*e^(-x) 0 0 1/2*(e^(2*x) - 1)*e^(-x)]
460
+ [ 0 1/2*(e^(2*x) + 1)*e^(-x) 1/2*(e^(2*x) - 1)*e^(-x) 0]
461
+ [ 0 1/2*(e^(2*x) - 1)*e^(-x) 1/2*(e^(2*x) + 1)*e^(-x) 0]
462
+ [1/2*(e^(2*x) - 1)*e^(-x) 0 0 1/2*(e^(2*x) + 1)*e^(-x)]
463
+ """
464
+ if not self.is_square():
465
+ raise ValueError("exp only defined on square matrices")
466
+ if self.nrows() == 0:
467
+ return self
468
+ # Maxima's matrixexp function chokes on floating point numbers
469
+ # so we automatically convert floats to rationals by passing
470
+ # keepfloat: false
471
+ m = self._maxima_(maxima)
472
+ z = maxima('matrixexp(%s), keepfloat: false' % m.name())
473
+ if self.nrows() == 1:
474
+ # We do the following, because Maxima stupidly exp's 1x1
475
+ # matrices into non-matrices!
476
+ z = maxima('matrix([%s])' % z.name())
477
+
478
+ return z._sage_()
479
+
480
+ def charpoly(self, var='x', algorithm=None):
481
+ r"""
482
+ Compute the characteristic polynomial of ``self``, using maxima.
483
+
484
+ .. NOTE::
485
+
486
+ The characteristic polynomial is defined as `\det(xI-A)`.
487
+
488
+ INPUT:
489
+
490
+ - ``var`` -- (default: ``'x'``) name of variable of charpoly
491
+
492
+ EXAMPLES::
493
+
494
+ sage: M = matrix(SR, 2, 2, var('a,b,c,d'), sparse=True)
495
+ sage: M.charpoly('t')
496
+ t^2 + (-a - d)*t - b*c + a*d
497
+ sage: matrix(SR, 5, [1..5^2], sparse=True).charpoly()
498
+ x^5 - 65*x^4 - 250*x^3
499
+
500
+ TESTS:
501
+
502
+ The cached polynomial should be independent of the ``var``
503
+ argument (:issue:`12292`). We check (indirectly) that the
504
+ second call uses the cached value by noting that its result is
505
+ not cached::
506
+
507
+ sage: M = MatrixSpace(SR, 2, sparse=True)
508
+ sage: A = M(range(0, 2^2))
509
+ sage: type(A)
510
+ <class 'sage.matrix.matrix_symbolic_sparse.Matrix_symbolic_sparse'>
511
+ sage: A.charpoly('x')
512
+ x^2 - 3*x - 2
513
+ sage: A.charpoly('y')
514
+ y^2 - 3*y - 2
515
+ sage: A._cache['charpoly']
516
+ x^2 - 3*x - 2
517
+
518
+ Ensure the variable name of the polynomial does not conflict
519
+ with variables used within the matrix (:issue:`14403`)::
520
+
521
+ sage: Matrix(SR, [[sqrt(x), x],[1,x]], sparse=True).charpoly().list()
522
+ [x^(3/2) - x, -x - sqrt(x), 1]
523
+
524
+ Test that :issue:`13711` is fixed::
525
+
526
+ sage: matrix([[sqrt(2), -1], [pi, e^2]], sparse=True).charpoly()
527
+ x^2 + (-sqrt(2) - e^2)*x + pi + sqrt(2)*e^2
528
+
529
+ Test that :issue:`26427` is fixed::
530
+
531
+ sage: M = matrix(SR, 7, 7, SR.var('a', 49), sparse=True)
532
+ sage: M.charpoly().degree() # long time
533
+ 7
534
+ """
535
+ cache_key = 'charpoly'
536
+ cp = self.fetch(cache_key)
537
+ if cp is not None:
538
+ return cp.change_variable_name(var)
539
+ from sage.symbolic.ring import SR
540
+
541
+ # We must not use a variable name already present in the matrix
542
+ vname = 'do_not_use_this_name_in_a_matrix_youll_compute_a_charpoly_of'
543
+ vsym = SR(vname)
544
+
545
+ cp = self._maxima_(maxima).charpoly(vname)._sage_().expand()
546
+ cp = [cp.coefficient(vsym, i) for i in range(self.nrows() + 1)]
547
+ cp = SR[var](cp)
548
+
549
+ # Maxima has the definition det(matrix-xI) instead of
550
+ # det(xI-matrix), which is what Sage uses elsewhere. We
551
+ # correct for the discrepancy.
552
+ if self.nrows() % 2 == 1:
553
+ cp = -cp
554
+
555
+ self.cache(cache_key, cp)
556
+ return cp
557
+
558
+ def minpoly(self, var='x'):
559
+ """
560
+ Return the minimal polynomial of ``self``.
561
+
562
+ EXAMPLES::
563
+
564
+ sage: M = Matrix.identity(SR, 2, sparse=True)
565
+ sage: M.minpoly()
566
+ x - 1
567
+
568
+ sage: t = var('t')
569
+ sage: m = matrix(2, [1, 2, 4, t], sparse=True)
570
+ sage: m.minimal_polynomial()
571
+ x^2 + (-t - 1)*x + t - 8
572
+
573
+ TESTS:
574
+
575
+ Check that the variable `x` can occur in the matrix::
576
+
577
+ sage: m = matrix([[x]], sparse=True)
578
+ sage: m.minimal_polynomial('y')
579
+ y - x
580
+ """
581
+ mp = self.fetch('minpoly')
582
+ if mp is None:
583
+ mp = self._maxima_lib_().jordan().minimalPoly().expand()
584
+ d = mp.hipow('x')
585
+ mp = [mp.coeff('x', i) for i in range(int(d) + 1)]
586
+ mp = PolynomialRing(self.base_ring(), 'x')(mp)
587
+ self.cache('minpoly', mp)
588
+ return mp.change_variable_name(var)
589
+
590
+ def fcp(self, var='x'):
591
+ """
592
+ Return the factorization of the characteristic polynomial of ``self``.
593
+
594
+ INPUT:
595
+
596
+ - ``var`` -- (default: ``'x'``) name of variable of charpoly
597
+
598
+ EXAMPLES::
599
+
600
+ sage: a = matrix(SR,[[1,2],[3,4]], sparse=True)
601
+ sage: a.fcp()
602
+ x^2 - 5*x - 2
603
+ sage: [i for i in a.fcp()]
604
+ [(x^2 - 5*x - 2, 1)]
605
+ sage: a = matrix(SR,[[1,0],[0,2]], sparse=True)
606
+ sage: a.fcp()
607
+ (x - 2) * (x - 1)
608
+ sage: [i for i in a.fcp()]
609
+ [(x - 2, 1), (x - 1, 1)]
610
+ sage: a = matrix(SR, 5, [1..5^2], sparse=True)
611
+ sage: a.fcp()
612
+ (x^2 - 65*x - 250) * x^3
613
+ sage: list(a.fcp())
614
+ [(x^2 - 65*x - 250, 1), (x, 3)]
615
+ """
616
+ from sage.symbolic.ring import SR
617
+ sub_dict = {var: SR.var(var)}
618
+ return Factorization(self.charpoly(var).subs(**sub_dict).factor_list())
619
+
620
+ def jordan_form(self, subdivide=True, transformation=False):
621
+ """
622
+ Return a Jordan normal form of ``self``.
623
+
624
+ INPUT:
625
+
626
+ - ``self`` -- a square matrix
627
+
628
+ - ``subdivide`` -- boolean (default: ``True``)
629
+
630
+ - ``transformation`` -- boolean (default: ``False``)
631
+
632
+ OUTPUT:
633
+
634
+ If ``transformation`` is ``False``, only a Jordan normal form
635
+ (unique up to the ordering of the Jordan blocks) is returned.
636
+ Otherwise, a pair ``(J, P)`` is returned, where ``J`` is a
637
+ Jordan normal form and ``P`` is an invertible matrix such that
638
+ ``self`` equals ``P * J * P^(-1)``.
639
+
640
+ If ``subdivide`` is ``True``, the Jordan blocks in the
641
+ returned matrix ``J`` are indicated by a subdivision in
642
+ the sense of :meth:`~sage.matrix.matrix2.subdivide`.
643
+
644
+ EXAMPLES:
645
+
646
+ We start with some examples of diagonalisable matrices::
647
+
648
+ sage: a,b,c,d = var('a,b,c,d')
649
+ sage: matrix([a], sparse=True).jordan_form()
650
+ [a]
651
+ sage: matrix([[a, 0], [1, d]], sparse=True).jordan_form(subdivide=True)
652
+ [d|0]
653
+ [-+-]
654
+ [0|a]
655
+ sage: matrix([[a, 0], [1, d]], sparse=True).jordan_form(subdivide=False)
656
+ [d 0]
657
+ [0 a]
658
+ sage: matrix([[a, x, x], [0, b, x], [0, 0, c]], sparse=True).jordan_form()
659
+ [c|0|0]
660
+ [-+-+-]
661
+ [0|b|0]
662
+ [-+-+-]
663
+ [0|0|a]
664
+
665
+ In the following examples, we compute Jordan forms of some
666
+ non-diagonalisable matrices::
667
+
668
+ sage: matrix([[a, a], [0, a]], sparse=True).jordan_form()
669
+ [a 1]
670
+ [0 a]
671
+ sage: matrix([[a, 0, b], [0, c, 0], [0, 0, a]], sparse=True).jordan_form()
672
+ [c|0 0]
673
+ [-+---]
674
+ [0|a 1]
675
+ [0|0 a]
676
+
677
+ The following examples illustrate the ``transformation`` flag.
678
+ Note that symbolic expressions may need to be simplified to
679
+ make consistency checks succeed::
680
+
681
+ sage: A = matrix([[x - a*c, a^2], [-c^2, x + a*c]], sparse=True)
682
+ sage: J, P = A.jordan_form(transformation=True)
683
+ sage: J, P
684
+ (
685
+ [x 1] [-a*c 1]
686
+ [0 x], [-c^2 0]
687
+ )
688
+ sage: A1 = P * J * ~P; A1
689
+ [ -a*c + x (a*c - x)*a/c + a*x/c]
690
+ [ -c^2 a*c + x]
691
+ sage: A1.simplify_rational() == A
692
+ True
693
+
694
+ sage: B = matrix([[a, b, c], [0, a, d], [0, 0, a]], sparse=True)
695
+ sage: J, T = B.jordan_form(transformation=True)
696
+ sage: J, T
697
+ (
698
+ [a 1 0] [b*d c 0]
699
+ [0 a 1] [ 0 d 0]
700
+ [0 0 a], [ 0 0 1]
701
+ )
702
+ sage: (B * T).simplify_rational() == T * J
703
+ True
704
+
705
+ Finally, some examples involving square roots::
706
+
707
+ sage: matrix([[a, -b], [b, a]], sparse=True).jordan_form()
708
+ [a - I*b| 0]
709
+ [-------+-------]
710
+ [ 0|a + I*b]
711
+ sage: matrix([[a, b], [c, d]], sparse=True).jordan_form(subdivide=False)
712
+ [1/2*a + 1/2*d - 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2) 0]
713
+ [ 0 1/2*a + 1/2*d + 1/2*sqrt(a^2 + 4*b*c - 2*a*d + d^2)]
714
+
715
+ Check that :issue:`40803` is fixed::
716
+
717
+ sage: matrix([[a, 0], [0, a]], sparse=True).jordan_form()
718
+ [a|0]
719
+ [-+-]
720
+ [0|a]
721
+ """
722
+ A = self._maxima_lib_()
723
+ jordan_info = A.jordan()
724
+ J = matrix(jordan_info.dispJordan()._sage_(), sparse=True)
725
+ if subdivide:
726
+ # Repeated eigen values indices are part of the same list
727
+ v = flatten([x[1:] for x in jordan_info])
728
+ w = [sum(v[0:i]) for i in range(1, len(v))]
729
+ J.subdivide(w, w)
730
+ if transformation:
731
+ P = A.diag_mode_matrix(jordan_info)._sage_()
732
+ return J, matrix(P, sparse=True)
733
+ else:
734
+ return J
735
+
736
+ def simplify(self):
737
+ """
738
+ Simplify ``self``.
739
+
740
+ EXAMPLES::
741
+
742
+ sage: var('x,y,z')
743
+ (x, y, z)
744
+ sage: m = matrix([[z, (x+y)/(x+y)], [x^2, y^2+2]], sparse=True); m
745
+ [ z 1]
746
+ [ x^2 y^2 + 2]
747
+ sage: m.simplify()
748
+ [ z 1]
749
+ [ x^2 y^2 + 2]
750
+ """
751
+ return self.parent()([x.simplify() for x in self.list()])
752
+
753
+ def simplify_trig(self):
754
+ """
755
+ EXAMPLES::
756
+
757
+ sage: theta = var('theta')
758
+ sage: M = matrix(SR, 2, 2, [cos(theta), sin(theta), -sin(theta), cos(theta)], sparse=True)
759
+ sage: ~M
760
+ [1/cos(theta) - sin(theta)^2/((sin(theta)^2/cos(theta) + cos(theta))*cos(theta)^2) -sin(theta)/((sin(theta)^2/cos(theta) + cos(theta))*cos(theta))]
761
+ [ sin(theta)/((sin(theta)^2/cos(theta) + cos(theta))*cos(theta)) 1/(sin(theta)^2/cos(theta) + cos(theta))]
762
+ sage: (~M).simplify_trig()
763
+ [ cos(theta) -sin(theta)]
764
+ [ sin(theta) cos(theta)]
765
+ """
766
+ return self._maxima_(maxima).trigexpand().trigsimp()._sage_()
767
+
768
+ def simplify_rational(self):
769
+ """
770
+ EXAMPLES::
771
+
772
+ sage: M = matrix(SR, 3, 3, range(9), sparse=True) - var('t')
773
+ sage: (~M*M)[0,0]
774
+ t*(3*(2/t + (6/t + 7)/((t - 3/t - 4)*t))*(2/t + (6/t + 5)/((t - 3/t
775
+ - 4)*t))/(t - (6/t + 7)*(6/t + 5)/(t - 3/t - 4) - 12/t - 8) + 1/t +
776
+ 3/((t - 3/t - 4)*t^2)) - 6*(2/t + (6/t + 5)/((t - 3/t - 4)*t))/(t -
777
+ (6/t + 7)*(6/t + 5)/(t - 3/t - 4) - 12/t - 8) - 3*(6/t + 7)*(2/t +
778
+ (6/t + 5)/((t - 3/t - 4)*t))/((t - (6/t + 7)*(6/t + 5)/(t - 3/t -
779
+ 4) - 12/t - 8)*(t - 3/t - 4)) - 3/((t - 3/t - 4)*t)
780
+ sage: expand((~M*M)[0,0])
781
+ 1
782
+ sage: (~M * M).simplify_rational()
783
+ [1 0 0]
784
+ [0 1 0]
785
+ [0 0 1]
786
+ """
787
+ return self._maxima_(maxima).fullratsimp()._sage_()
788
+
789
+ def simplify_full(self):
790
+ """
791
+ Simplify a symbolic matrix by calling
792
+ :meth:`Expression.simplify_full()` componentwise.
793
+
794
+ INPUT:
795
+
796
+ - ``self`` -- the matrix whose entries we should simplify
797
+
798
+ OUTPUT: a copy of ``self`` with all of its entries simplified
799
+
800
+ EXAMPLES:
801
+
802
+ Symbolic matrices will have their entries simplified::
803
+
804
+ sage: a,n,k = SR.var('a,n,k')
805
+ sage: f1 = sin(x)^2 + cos(x)^2
806
+ sage: f2 = sin(x/(x^2 + x))
807
+ sage: f3 = binomial(n,k)*factorial(k)*factorial(n-k)
808
+ sage: f4 = x*sin(2)/(x^a)
809
+ sage: A = matrix(SR, [[f1,f2],[f3,f4]], sparse=True)
810
+ sage: A.simplify_full()
811
+ [ 1 sin(1/(x + 1))]
812
+ [ factorial(n) x^(-a + 1)*sin(2)]
813
+ """
814
+ M = self.parent()
815
+ return M([expr.simplify_full() for expr in self])
816
+
817
+ def canonicalize_radical(self):
818
+ r"""
819
+ Choose a canonical branch of each entry of ``self`` by calling
820
+ :meth:`Expression.canonicalize_radical()` componentwise.
821
+
822
+ EXAMPLES::
823
+
824
+ sage: var('x','y')
825
+ (x, y)
826
+ sage: l1 = [sqrt(2)*sqrt(3)*sqrt(6) , log(x*y)]
827
+ sage: l2 = [sin(x/(x^2 + x)) , 1]
828
+ sage: m = matrix([l1, l2], sparse=True)
829
+ sage: m
830
+ [sqrt(6)*sqrt(3)*sqrt(2) log(x*y)]
831
+ [ sin(x/(x^2 + x)) 1]
832
+ sage: m.canonicalize_radical()
833
+ [ 6 log(x) + log(y)]
834
+ [ sin(1/(x + 1)) 1]
835
+ """
836
+ M = self.parent()
837
+ return M([expr.canonicalize_radical() for expr in self])
838
+
839
+ def factor(self):
840
+ """
841
+ Operate point-wise on each element.
842
+
843
+ EXAMPLES::
844
+
845
+ sage: M = matrix(SR, 2, 2, x^2 - 2*x + 1, sparse=True); M
846
+ [x^2 - 2*x + 1 0]
847
+ [ 0 x^2 - 2*x + 1]
848
+ sage: M.factor()
849
+ [(x - 1)^2 0]
850
+ [ 0 (x - 1)^2]
851
+ """
852
+ return matrix(self._maxima_(maxima).factor()._sage_(), sparse=True)
853
+
854
+ def expand(self):
855
+ """
856
+ Operate point-wise on each element.
857
+
858
+ EXAMPLES::
859
+
860
+ sage: M = matrix(2, 2, range(4)) - var('x')
861
+ sage: M*M
862
+ [ x^2 + 2 -2*x + 3]
863
+ [ -4*x + 6 (x - 3)^2 + 2]
864
+ sage: (M*M).expand()
865
+ [ x^2 + 2 -2*x + 3]
866
+ [ -4*x + 6 x^2 - 6*x + 11]
867
+ """
868
+ from sage.misc.call import attrcall
869
+ return self.apply_map(attrcall('expand'))
870
+
871
+ def variables(self):
872
+ """
873
+ Return the variables of ``self``.
874
+
875
+ EXAMPLES::
876
+
877
+ sage: var('a,b,c,x,y')
878
+ (a, b, c, x, y)
879
+ sage: m = matrix([[x, x+2], [x^2, x^2+2]], sparse=True); m
880
+ [ x x + 2]
881
+ [ x^2 x^2 + 2]
882
+ sage: m.variables()
883
+ (x,)
884
+ sage: m = matrix([[a, b+c], [x^2, y^2+2]], sparse=True); m
885
+ [ a b + c]
886
+ [ x^2 y^2 + 2]
887
+ sage: m.variables()
888
+ (a, b, c, x, y)
889
+ """
890
+ vars = set(sum([op.variables() for op in self.list()], ()))
891
+ return tuple(sorted(vars, key=repr))
892
+
893
+ def arguments(self):
894
+ """
895
+ Return a tuple of the arguments that ``self`` can take.
896
+
897
+ EXAMPLES::
898
+
899
+ sage: var('x,y,z')
900
+ (x, y, z)
901
+ sage: M = MatrixSpace(SR,2,2, sparse=True)
902
+ sage: M(x).arguments()
903
+ (x,)
904
+ sage: M(x+sin(x)).arguments()
905
+ (x,)
906
+ """
907
+ return self.variables()
908
+
909
+ def number_of_arguments(self):
910
+ """
911
+ Return the number of arguments that ``self`` can take.
912
+
913
+ EXAMPLES::
914
+
915
+ sage: var('a,b,c,x,y')
916
+ (a, b, c, x, y)
917
+ sage: m = matrix([[a, (x+y)/(x+y)], [x^2, y^2+2]], sparse=True); m
918
+ [ a 1]
919
+ [ x^2 y^2 + 2]
920
+ sage: m.number_of_arguments()
921
+ 3
922
+ """
923
+ return len(self.variables())
924
+
925
+ def __call__(self, *args, **kwargs):
926
+ """
927
+ EXAMPLES::
928
+
929
+ sage: var('x,y,z')
930
+ (x, y, z)
931
+ sage: M = MatrixSpace(SR,2,2, sparse=True)
932
+ sage: h = M(sin(x)+cos(x))
933
+ sage: h
934
+ [cos(x) + sin(x) 0]
935
+ [ 0 cos(x) + sin(x)]
936
+ sage: h(x=1)
937
+ [cos(1) + sin(1) 0]
938
+ [ 0 cos(1) + sin(1)]
939
+ sage: h(x=x)
940
+ [cos(x) + sin(x) 0]
941
+ [ 0 cos(x) + sin(x)]
942
+
943
+ sage: h = M((sin(x)+cos(x)).function(x))
944
+ sage: h
945
+ [cos(x) + sin(x) 0]
946
+ [ 0 cos(x) + sin(x)]
947
+
948
+ sage: f = M([0,x,y,z]); f
949
+ [0 x]
950
+ [y z]
951
+ sage: f.arguments()
952
+ (x, y, z)
953
+ sage: f()
954
+ [0 x]
955
+ [y z]
956
+ sage: f(x=1)
957
+ [0 1]
958
+ [y z]
959
+ sage: f(x=1,y=2)
960
+ [0 1]
961
+ [2 z]
962
+ sage: f(x=1,y=2,z=3)
963
+ [0 1]
964
+ [2 3]
965
+ sage: f({x:1,y:2,z:3})
966
+ [0 1]
967
+ [2 3]
968
+
969
+ TESTS::
970
+
971
+ sage: f(1, x=2)
972
+ Traceback (most recent call last):
973
+ ...
974
+ ValueError: args and kwargs cannot both be specified
975
+ sage: f(x=1,y=2,z=3,t=4)
976
+ [0 1]
977
+ [2 3]
978
+
979
+ sage: h(1)
980
+ Traceback (most recent call last):
981
+ ...
982
+ ValueError: use named arguments, like EXPR(x=..., y=...)
983
+ """
984
+ if kwargs and args:
985
+ raise ValueError("args and kwargs cannot both be specified")
986
+
987
+ if args:
988
+ if len(args) == 1 and isinstance(args[0], dict):
989
+ kwargs = {repr(x): vx for x, vx in args[0].items()}
990
+ else:
991
+ raise ValueError('use named arguments, like EXPR(x=..., y=...)')
992
+
993
+ new_entries = []
994
+ for entry in self.list():
995
+ try:
996
+ new_entries.append(entry(**kwargs))
997
+ except ValueError:
998
+ new_entries.append(entry)
999
+
1000
+ return self.parent(new_entries)
1001
+
1002
+ cdef bint get_is_zero_unsafe(self, Py_ssize_t i, Py_ssize_t j) except -1:
1003
+ r"""
1004
+ Return 1 if the entry ``(i, j)`` is zero, otherwise 0.
1005
+
1006
+ EXAMPLES::
1007
+
1008
+ sage: M = matrix(SR, [[0,1,0],[0,0,0]], sparse=True)
1009
+ sage: M.zero_pattern_matrix() # indirect doctest
1010
+ [1 0 1]
1011
+ [1 1 1]
1012
+ """
1013
+ entry = self.get_unsafe(i, j)
1014
+ # See if we can avoid the full proof machinery that the entry is 0
1015
+ if entry.is_trivial_zero():
1016
+ return 1
1017
+ if entry:
1018
+ return 0
1019
+ else:
1020
+ return 1
1021
+
1022
+ def function(self, *args):
1023
+ """
1024
+ Return a matrix over a callable symbolic expression ring.
1025
+
1026
+ EXAMPLES::
1027
+
1028
+ sage: x, y = var('x,y')
1029
+ sage: v = matrix([[x,y],[x*sin(y), 0]], sparse=True)
1030
+ sage: w = v.function([x,y]); w
1031
+ [ (x, y) |--> x (x, y) |--> y]
1032
+ [(x, y) |--> x*sin(y) (x, y) |--> 0]
1033
+ sage: w.parent()
1034
+ Full MatrixSpace of 2 by 2 sparse matrices over Callable function ring with arguments (x, y)
1035
+ """
1036
+ from sage.symbolic.callable import CallableSymbolicExpressionRing
1037
+ return matrix(CallableSymbolicExpressionRing(args),
1038
+ self.nrows(), self.ncols(), self.list(), sparse=True)