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