Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/matrix/matrix_domain_dense.pyx
4079 views
1
"""nodoctest
2
Matrices over a domain
3
"""
4
5
########################################################################
6
# Copyright (C) 2005 William Stein <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
########################################################################
19
20
21
cimport matrix
22
import matrix
23
24
import sage.structure.sequence
25
26
27
cdef class Matrix_domain_dense(matrix.Matrix):
28
29
30
def charpoly(self, *args, **kwds):
31
r"""
32
Return the characteristic polynomial of self, as a polynomial
33
over the base ring.
34
35
ALGORITHM: Use \code{self.matrix_over_field()} to obtain the
36
corresponding matrix over a field, then call the
37
\code{charpoly} function on it.
38
39
EXAMPLES:
40
First a matrix over $\Z$:
41
sage: A = MatrixSpace(IntegerRing(),2)( [[1,2], [3,4]] )
42
sage: f = A.charpoly('x')
43
sage: f
44
x^2 - 5*x - 2
45
sage: f.parent()
46
Univariate Polynomial Ring in x over Integer Ring
47
48
We compute the characteristic polynomial of a matrix over
49
the polynomial ring $\Z[a]$:
50
sage: R = PolynomialRing(IntegerRing(),'a'); a = R.gen()
51
sage: M = MatrixSpace(R,2)([[a,1], [a,a+1]])
52
sage: M
53
[ a 1]
54
[ a a + 1]
55
sage: f = M.charpoly('x')
56
sage: f
57
x^2 + (-2*a - 1)*x + a^2
58
sage: f.parent()
59
Univariate Polynomial Ring in x over Univariate Polynomial Ring in a over Integer Ring
60
sage: M.trace()
61
2*a + 1
62
sage: M.determinant()
63
a^2
64
65
We compute the characteristic polynomial of a matrix over the
66
multi-variate polynomial ring $\Z[x,y]$:
67
sage: R = PolynomialRing(IntegerRing(),2); x,y = R.gens()
68
sage: A = MatrixSpace(R,2)([x, y, x^2, y^2])
69
sage: f = A.charpoly('x')
70
sage: f
71
x^2 + (-1*x1^2 - x0)*x + x0*x1^2 - x0^2*x1
72
73
It's a little difficult to distinguish the variables. To fix this,
74
we rename the indeterminate $Z$:
75
sage: f.parent()._assign_names("Z")
76
sage: f
77
Z^2 + (-1*x1^2 - x0)*Z + x0*x1^2 - x0^2*x1
78
79
We can pass parameters in, which are passed on to the charpoly
80
function for matrices over a field.
81
sage: A = 1000*MatrixSpace(ZZ,10)(range(100))
82
sage: A.charpoly(bound=2)
83
x^10 + 14707*x^9 - 21509*x^8
84
sage: A = 1000*MatrixSpace(ZZ,10)(range(100))
85
sage: A.charpoly('x')
86
x^10 - 495000*x^9 - 8250000000*x^8
87
"""
88
f = self.matrix_over_field(copy=True).charpoly(*args, **kwds)
89
return f.base_extend(self.base_ring())
90
91
def determinant(self):
92
"""
93
Return the determinant of this matrix.
94
95
INPUT:
96
-- a square matrix
97
98
ALGORITHM: Find the characteristic polynomial and take its
99
constant term (up to sign).
100
101
EXAMPLES:
102
We create a matrix over $\Z[x,y]$ and compute its determinant.
103
sage: R = PolynomialRing(IntegerRing(),2); x,y = R.gens()
104
sage: A = MatrixSpace(R,2)([x, y, x**2, y**2])
105
sage: A.determinant()
106
x0*x1^2 - x0^2*x1
107
"""
108
if self.nrows() != self.ncols():
109
raise ArithmeticError("Matrix must be square, but is %sx%s"%(
110
self.nrows(), self.ncols()))
111
# Use stupid slow but completely general method.
112
d = (-1)**self.nrows() * self.charpoly('x')[0]
113
return self.base_ring()(d)
114
115
def is_invertible(self):
116
r"""
117
Return True if this matrix is invertible.
118
119
EXAMPLES:
120
The following matrix is invertible over $\Q$ but not over $\Z$.
121
sage: A = MatrixSpace(IntegerRing(), 2)(range(4))
122
sage: A.is_invertible()
123
False
124
sage: A.matrix_over_field().is_invertible()
125
True
126
127
The inverse function is a constructor for matrices over the
128
fraction field, so it can work even if A is not invertible.
129
sage: ~A # inverse of A
130
[-3/2 1/2]
131
[ 1 0]
132
133
The next matrix is invertible over $\Z$.
134
sage: A = MatrixSpace(IntegerRing(),2)([1,10,0,-1])
135
sage: A.is_invertible()
136
True
137
sage: ~A # compute the inverse
138
[ 1 10]
139
[ 0 -1]
140
141
The following nontrivial matrix is invertible over $\Z[x]$.
142
sage: R = PolynomialRing(IntegerRing())
143
sage: x = R.gen()
144
sage: A = MatrixSpace(R,2)([1,x,0,-1])
145
sage: A.is_invertible()
146
True
147
sage: ~A
148
[ 1 x]
149
[ 0 -1]
150
"""
151
return self.is_square() and self.determinant().is_unit()
152
153
def __invert__(self):
154
r"""
155
Return this inverse of this matrix, as a matrix over the fraction field.
156
157
Raises a \code{ZeroDivisionError} if the matrix has zero
158
determinant, and raises an \code{ArithmeticError}, if the
159
inverse doesn't exist because the matrix is nonsquare.
160
161
EXAMPLES:
162
sage: A = MatrixSpace(IntegerRing(), 2)([1,1,3,5])
163
sage: ~A
164
[ 5/2 -1/2]
165
[-3/2 1/2]
166
167
Even if the inverse lies in the base field, the result is still a matrix
168
over the fraction field.
169
sage: I = MatrixSpace(IntegerRing(),2)( 1 ) # identity matrix
170
sage: ~I
171
[1 0]
172
[0 1]
173
sage: (~I).parent()
174
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
175
176
TESTS:
177
sage: MatrixSpace(IntegerRing(), 0)().inverse()
178
[]
179
"""
180
return ~self.matrix_over_field()
181
182
183
def matrix_over_field(self, copy=False):
184
"""
185
Return this matrix, but with entries viewed as elements
186
of the fraction field of the base ring.
187
188
EXAMPLES:
189
sage: A = MatrixSpace(IntegerRing(),2)([1,2,3,4])
190
sage: B = A.matrix_over_field()
191
sage: B
192
[1 2]
193
[3 4]
194
sage: B.parent()
195
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
196
"""
197
return self.change_ring(self.base_ring().fraction_field(), copy=copy)
198
199
def _singular_(self, singular=None):
200
"""
201
Tries to coerce this matrix to a singular matrix.
202
"""
203
if singular is None:
204
from sage.interfaces.all import singular as singular_default
205
singular = singular_default
206
try:
207
self.base_ring()._singular_(singular)
208
except (NotImplementedError, AttributeError):
209
raise TypeError("Cannot coerce to Singular")
210
211
return singular.matrix(self.nrows(),self.ncols(),singular(self.list()))
212
213
214
215