1 from __future__
import division
9 Matrix is a class for matrices. 12 def __init__(self, row, column, compo=0, coeff_ring=0):
14 Matrix(row, column [,components, coeff_ring]) 23 if (isinstance(row, (int, long))
24 and isinstance(column, (int, long))
37 zero = coeff_ring.zero
41 if isinstance(compo[0], list):
42 if len(compo) != self.
row:
43 raise ValueError(
"len(compo) " +
44 "is not match the matrix size")
46 if len(compo[i]) != self.
column:
47 raise ValueError(
"len(compo[%d]) " % i +
48 "is not match the matrix size")
49 self.
compo.append(compo[i][:])
50 elif isinstance(compo[0], tuple):
51 if len(compo) != self.
column:
52 raise ValueError(
"len(compo) " +
53 "is not match the matrix size")
56 if len(compo[i]) != self.
row:
57 raise ValueError(
"len(compo[%d]) " % i +
58 "is not match the matrix size")
61 self.
compo[j].append(ele)
64 if len(compo) != self.
column:
65 raise ValueError(
"len(compo) " +
66 "is not match the matrix size")
69 if len(compo[i]) != self.
row:
70 raise ValueError(
"len(compo[%d]) " % i +
71 "is not match the matrix size")
73 for ele
in compo[i].compo:
74 self.
compo[j].append(ele)
77 if (len(compo) != self.
row * self.
column):
78 raise ValueError(
"len(compo) " +
79 "is not match the matrix size")
87 if not(isinstance(ring.getRing(self.
compo[0][0]), coeff_ring.__class__)):
90 compos.append(
map(coeff_ring.createElement, self.
compo[i]))
93 raise ValueError(
"invalid value for matrix size")
112 M[i,j] : Return (i,j)-component of M. 113 M[i] <==> M.getColumn(i) 115 if isinstance(index, tuple):
116 return self.
compo[index[0] - 1][index[1] - 1]
117 elif isinstance(index, (int, long)):
120 raise IndexError(
"Matrix invalid index: %s" % index)
124 M[i,j] = a : Substitute a for (i,j)-component of M. 125 M[i] = V <==> M.setColumn(i, V) 127 if isinstance(key, tuple):
128 self.
compo[key[0] - 1][key[1] - 1] = value
129 elif isinstance(key, (int, long)):
137 self == 0 means whether self == zeromatrix or not. 139 if isinstance(other, Matrix):
140 if (self.
row != other.row)
or (self.
column != other.column):
144 if self.
compo[i][j] != other.compo[i][j]:
147 elif isinstance(other, int)
and other == 0:
148 return not bool(self)
156 val += hash(self.
compo[i][j])
162 self != 0 means whether self != zeromatrix or not. 164 return not (self == other)
168 Check self != zeromatrix 178 Check whether item == one of self's element. 181 if item
in self.
compo[i]:
188 return_str += str(self.
compo[i])
189 if i + 1 != self.
row:
198 if len(str(self.
compo[i][j])) > width[j]:
199 width[j] = len(str(self.
compo[i][j]))
202 return_str +=
"%*s " % (width[j], str(self.
compo[i][j]))
204 return return_str[:-1]
208 Return matrix applied __call__ to each elements. 213 ele = self.
compo[i][j]
223 Return matrix applied function to all self elements. 227 compos = compos + (
map(function, self.
compo[i]))
230 def reduce(self, function, initializer=None):
232 Return value applied binomial function to all self elements. 244 Create a copy of the instance. 249 compos.append(self.
compo[i][j])
255 set(compo) : Substitute list for components 257 if (len(compo) != self.
row * self.
column):
258 raise ValueError(
"len(compo) is not match the matrix size")
264 setRow(m, new_row) : new_row should be a list/Vector 266 if isinstance(arg, list):
267 if (len(arg) != self.
column):
269 "len(compo) is not match the row size")
270 self.
compo[m - 1] = arg[:]
272 if (len(arg) != self.
column):
274 "len(compo) is not match the row size")
275 self.
compo[m - 1] = arg.compo[:]
277 raise TypeError(self.
setRow.__doc__)
281 setColumn(n, new_column) : new_column should be a list/Vector 283 if isinstance(arg, list):
284 if (len(arg) != self.
row):
285 raise ValueError(
"len(compo) is not match the column size")
287 self.
compo[i][n - 1] = arg[i]
289 if (len(arg) != self.
row):
290 raise ValueError(
"len(compo) is not match the column size")
292 self.
compo[i][n - 1] = arg.compo[i]
298 getRow(i) : Return i-th row in form of Matrix 304 getColumn(j) : Return j-th column in form of Matrix 308 column.append(self.
compo[k][j - 1])
313 swapRow(m1, m2) : Swap self's m1-th row and m2-th row. 315 tmp = self.
compo[m1 - 1][:]
317 self.
compo[m2 - 1] = tmp[:]
321 swapColumn(n1, n2) : Swap self's n1-th column and n2-th column. 324 tmp = self.
compo[k][n1 - 1]
326 self.
compo[k][n2 - 1] = tmp
330 insertRow(i, new_row) : added new_row 331 new_row can be a list or a Matrix 333 if isinstance(arg, list):
334 if self.
column != len(arg):
336 self.
compo.insert(i - 1, arg)
339 if self.
column != len(arg):
341 self.
compo.insert(i - 1, arg.compo)
343 elif isinstance(arg, Matrix):
344 if self.
column != arg.column:
346 self.
compo += arg.compo
354 insertColumn(j, new_column) : added new_column 355 new_column can be a list or a Matrix 357 if isinstance(arg, list):
358 if self.
row != len(arg):
361 self.
compo[k].insert(j-1, arg[k])
364 if self.
row != len(arg):
367 self.
compo[k].insert(j-1, arg.compo[k])
369 elif isinstance(arg, Matrix):
370 if self.
row != arg.row:
372 for k
in range(arg.row):
374 self.
compo[k][:j - 1] + arg.compo[k] + self.
compo[k][j - 1:]
382 extendRow(new_row) : join new_row in vertical way. 388 extendColumn(new_column) : join new_column in horizontal way. 394 deleteRow(i) : deleted i-th row 397 del self.
compo[i - 1]
402 deleteColumn(j) : deleted j-th column 406 del self.
compo[k][j - 1]
412 Return transposed matrix of self. 417 trans.append(self[i, j])
422 Return a block whose size is row*column, (1,1)-element is self[i,j]. 426 if i + row - 1 > self.
row or j + column - 1 > self.
column:
429 for k
in range(i - 1, i + row - 1):
430 mat.extend(self.
compo[k][j - 1:j + column - 1])
435 Return submatrix whose element is self[i, j] for i in I and j in J. 436 If I, J is not index(list or tuple) but integer, 437 return submatrix which deleted I-th row and J-th column from self. 441 if isinstance(I, (int, long))
and isinstance(J, (int, long)):
450 mat.append(self[i, j])
455 return the matrix (self) 456 (this method is for compatibility to vector) 463 SquareMatrix is a class for square matrices. 466 def __init__(self, row, column=0, compo=0, coeff_ring=0):
468 SquareMatrix(row [, column ,components, coeff_ring]) 469 SquareMatrix must be row == column . 481 if isinstance(column, list):
490 raise ValueError(
"not square matrix")
491 Matrix._initialize(self, row, column, compo, coeff_ring)
495 Check whether self is upper triangular matrix or not. 498 for i
in range(j + 1, self.
row + 1):
505 Check whether self is lower triangular matrix or not. 515 Check whether self is diagonal matrix or not. 521 Check whether self is scalar matrix or not. 522 Scalar matrix is matrix which is unit matrix times some scalar. 528 if self[i, i] != chk:
534 Check whether self is symmetric matrix or not. 538 if self[i, j] != self[j, i]:
545 RingMatrix is a class for matrices whose elements are in ring. 548 def __init__(self, row, column, compo=0, coeff_ring=0):
550 RingMatrix(row, column [,components, coeff_ring]) 566 Return matrix addition. 568 if (self.
row != other.row)
or (self.
column != other.column):
573 sums.append(self[i, j] + other[i, j])
575 self.
coeff_ring.getCommonSuperring(other.coeff_ring))
579 Return matrix subtraction. 581 if (self.
row != other.row)
or (self.
column != other.column):
586 diff.append(self[i, j] - other[i, j])
588 self.
coeff_ring.getCommonSuperring(other.coeff_ring))
592 multiplication with a Matrix or a scalar 594 if isinstance(other, Matrix):
595 if self.
column != other.row:
599 for j
in range(1, other.column + 1):
600 part_product = self[i, 1] * other[1, j]
602 part_product = part_product + self[i, k] * other[k, j]
603 product.append(part_product)
605 self.
coeff_ring.getCommonSuperring(other.coeff_ring))
607 if self.
column != len(other):
611 part_product = self[i, 1] * other[1]
613 part_product = part_product + self[i, j] * other[j]
614 product.append(part_product)
618 rings = self.
coeff_ring.getCommonSuperring(ring.getRing(other))
620 return NotImplemented
624 product.append(self[i, j] * other)
628 if isinstance(other, Matrix):
629 if other.column != self.
row:
632 for i
in range(1, other.row + 1):
634 part_product = other[i, 1] * self[1, j]
636 part_product = part_product + other[i, k] * self[k, j]
637 product.append(part_product)
639 self.
coeff_ring.getCommonSuperring(other.coeff_ring))
641 if self.
row != len(other):
645 part_product = other[1] * self[1, j]
647 part_product = part_product + other[i] * self[i, j]
648 product.append(part_product)
652 rings = self.
coeff_ring.getCommonSuperring(ring.getRing(other))
654 return NotImplemented
658 product.append(self[i, j] * other)
663 return self modulo other. 666 raise ZeroDivisionError()
670 mod.append(self[i, j] % other)
688 return self.
map(
lambda ele: minus_one * ele)
692 Set and return coefficient ring. 694 if not hasattr(self,
"_coeff_ring"):
698 cring = ring.getRing(self[i, j])
699 if scalars
is None or \
700 scalars != cring
and scalars.issubring(cring):
702 elif not scalars.issuperring(cring):
703 scalars = scalars.getCommonSuperring(cring)
706 elif not scalars.issuperring(self.
coeff_ring):
707 scalars = scalars.getCommonSuperring(self.
coeff_ring)
712 """RingMatrix -> FieldMatrix""" 717 """RingMatrix -> Subspace""" 724 Find the Hermite Normal Form for integer matrix. 725 If non_zero is True, return non-zero columns for self's HNF 731 for i
in range(1, self.
row + 1)[::-1]:
734 for j
in range(1, j0)[::-1]:
740 u, v, d = rings.extgcd(A[i, k], A[i, j])
741 A_ik = ring.exact_division(A[i, k], d)
742 A_ij = ring.exact_division(A[i, j], d)
743 B = u * A[k] + v * A[j]
744 A[j] = A_ik * A[j] - A_ij * A[k]
756 A[j] = A[j] - q * A[k]
773 HNF = hermiteNormalForm
777 This method is a common process used in 778 extHNF() and kernelAsModule() 785 for i
in range(1, self.
row + 1)[::-1]:
788 for j
in range(1, j0)[::-1]:
794 u, v, d = rings.extgcd(A[i, k], A[i, j])
795 A_ik = ring.exact_division(A[i, k], d)
796 A_ij = ring.exact_division(A[i, j], d)
797 B = u * A[k] + v * A[j]
798 A[j] = A_ik * A[j] - A_ij * A[k]
800 B = u * U[k] + v * U[j]
801 U[j] = A_ik * U[j] - A_ij * U[k]
814 A[j] = A[j] - q * A[k]
815 U[j] = U[j] - q * U[k]
827 Find the Hermite Normal Form M for integer matrix. 828 Computing U which satisfied M=self*U. 829 Return matrices tuple,(U, M). 841 return (new_U, new_A)
845 extHNF = exthermiteNormalForm
849 Compute kernel as Z-module. 862 RingSquareMatrix is a class for square matrices whose elements are in ring. 865 def __init__(self, row, column=0, compo=0, coeff_ring=0):
867 RingSquareMatrix(row [, column ,components, coeff_ring]) 868 RingSquareMatrix must be row == column . 874 powering self to integer. 877 if not isinstance(n, (int, long)):
878 raise TypeError(
"index must be an integer")
886 if hasattr(self,
"inverse"):
900 """RingSquareMatrix -> FieldSquareMatrix""" 906 Return matrix ring of self. 912 Check whether self is orthogonal matrix or not. 913 Orthogonal matrix satisfies M*M^T equals unit matrix. 919 Check whether self is alternating matrix or not. 920 Alternating (skew symmetric, or antisymmetric) matrix satisfies M=-M^T. 924 if self[i, j] != -self[j, i]:
928 isAntisymmetricMatrix = isAlternatingMatrix
929 isSkewsymmetricMatrix = isAlternatingMatrix
933 Check determinant == 0 or not. 939 Return trace of self. 943 trace = trace + self[i, i]
948 Return determinant of self. 954 for k
in range(1, n):
958 while not bool(M[i, k]):
963 for j
in range(k, n + 1):
969 for i
in range(k + 1, n + 1):
970 for j
in range(k + 1, n + 1):
971 t = p * M[i, j] - M[i, k] * M[k, j]
972 M[i, j] = ring.exact_division(t, c)
981 Return (i, j)-cofactor of self. 990 Return commutator defined as follows: 991 [self, other] = self * other - other * self . 993 return self * other - other * self
997 Return the characteristic matrix (i.e. xI-A) of self. 1000 x = uniutil.polynomial({1:1}, self.
coeff_ring)
1005 for characteristicPolynomial, adjugateMatrix 1007 Assume self.row >= 2. 1011 C = self + coeff[-1] * unit
1015 coeff.append(ring.exact_division(-C.trace(), i))
1016 C = C + coeff[-1] * unit
1018 coeff.append(ring.exact_division(-(self * C).
trace(), i))
1024 characteristicPolynomial() -> Polynomial 1027 genPoly = nzmath.poly.uniutil.polynomial
1030 return genPoly({0:-self.
trace(), 1:rings.one}, rings)
1032 return genPoly(dict(enumerate(coeff)), self.
coeff_ring)
1036 Return adjugate(classical adjoint) matrix. 1048 Return cofactor matrix. 1052 cofactors = cofactorMatrix
1056 Find the Smith Normal Form for square non-singular integral matrix. 1057 Return the list of diagonal elements. 1064 raise ValueError(
"Don't input singular matrix")
1074 u, v, d = rings.extgcd(M[n, j], M[n, n])
1075 B = v * M.getColumn(n) + u * M.getColumn(j)
1076 M_nn = ring.exact_division(M[n, n], d)
1077 M_nj = ring.exact_division(M[n, j], d)
1078 M.setColumn(j, ((M_nn * M.getColumn(j)
1079 - M_nj * M.getColumn(n)) % R))
1080 M.setColumn(n, (B % R))
1085 u, v, d = rings.extgcd(M[j, n], M[n, n])
1086 B = v * M.getRow(n) + u * M.getRow(j)
1087 M_nn = ring.exact_division(M[n, n], d)
1088 M_jn = ring.exact_division(M[j, n], d)
1089 M.setRow(j, ((M_nn * M.getRow(j)
1090 - M_jn * M.getRow(n)) % R))
1091 M.setRow(n, (B % R))
1098 for k
in range(1, n):
1099 for l
in range(1, n):
1101 M.setRow(n, M.getRow(n) + M.getRow(k))
1104 dd = rings.gcd(M[n, n], R)
1106 R = ring.exact_division(R, dd)
1108 dd = rings.gcd(M[1, 1], R)
1113 SNF = smithNormalForm
1114 elementary_divisor = smithNormalForm
1118 Find the Smith Normal Form M for square matrix, 1119 Computing U,V which satisfied M=U*self*V. 1120 Return matrices tuple,(U,V,M). 1133 u, v, d = rings.extgcd(M[n, j], M[n, n])
1134 M_nn = ring.exact_division(M[n, n], d)
1135 M_nj = ring.exact_division(M[n, j], d)
1136 B = v * M.getColumn(n) + u * M.getColumn(j)
1137 M.setColumn(j, (M_nn * M.getColumn(j)
1138 - M_nj * M.getColumn(n)))
1140 B = v * V.getColumn(n) + u * V.getColumn(j)
1141 V.setColumn(j, (M_nn * V.getColumn(j)
1142 - M_nj * V.getColumn(n)))
1148 u, v, d = rings.extgcd(M[j, n], M[n, n])
1149 M_nn = ring.exact_division(M[n, n], d)
1150 M_jn = ring.exact_division(M[j, n], d)
1151 B = v * M.getRow(n) + u * M.getRow(j)
1152 M.setRow(j, (M_nn * M.getRow(j) - M_jn * M.getRow(n)))
1154 B = v * U.getRow(n) + u * U.getRow(j)
1155 U.setRow(j, (M_nn * U.getRow(j) - M_jn * U.getRow(n)))
1161 for k
in range(1, n):
1162 for l
in range(1, n):
1164 M.setRow(n, M.getRow(n) + M.getRow(k))
1165 U.setRow(n, U.getRow(n) + U.getRow(k))
1169 for j
in range(1, M.column + 1):
1175 extSNF = extsmithNormalForm
1180 FieldMatrix is a class for matrices whose elements are in field. 1183 def __init__(self, row, column, compo=0, coeff_ring=0):
1185 FieldMatrix(row, column [,components, coeff_ring]) 1191 """initialize matrix""" 1192 RingMatrix._initialize(self, row, column, compo, coeff_ring)
1198 Select Matrix class. 1208 division by a scalar. 1210 return ring.inverse(other) * self
1212 __div__ = __truediv__
1215 """FieldMatrix -> Subspace""" 1221 _cohensSimplify is a common process used in image() and kernel() 1223 Return a tuple of modified matrix M, image data c and kernel data d. 1226 c = [0] * (M.row + 1)
1227 d = [-1] * (M.column + 1)
1228 for k
in range(1, M.column + 1):
1229 for j
in range(1, M.row + 1):
1230 if not c[j]
and M[j, k]:
1235 top = -ring.inverse(M[j, k])
1237 for s
in range(k + 1, M.column + 1):
1238 M[j, s] = top * M[j, s]
1239 for i
in range(1, M.row + 1):
1244 for s
in range(k + 1, M.column + 1):
1245 M[i, s] = M[i, s] + top * M[j, s]
1252 Return a Matrix whose column vectors are one basis of self's kernel, 1253 or return None if self's kernel is 0. 1256 M, d = tmp[0], tmp[2]
1258 for k
in range(1, M.column + 1):
1262 for i
in range(1, M.column + 1):
1264 vect.append(M[d[i], k])
1270 dimension = len(basis)
1274 for j
in range(1, dimension + 1):
1275 output.setColumn(j, basis[j - 1])
1280 Return a Matrix which column vectors are one basis of self's image, 1281 or return None if self's image is 0. 1284 M, c = tmp[0], tmp[1]
1286 for j
in range(1, M.row + 1):
1288 basis.append(self[c[j]])
1289 dimension = len(basis)
1293 for j
in range(1, dimension + 1):
1294 output.setColumn(j, basis[j - 1])
1295 output._selectMatrix()
1300 Return rank of self. 1310 inverseImage(V) -> X 1312 such that self * X == V 1315 if self.
row != len(V):
1319 if self.
row != V.row:
1330 for j
in range(1, n + 1):
1332 for k
in range(i, m + 1):
1339 for l
in range(j, n + 1):
1345 d = ring.inverse(M[i, j])
1346 for k
in range(i + 1, m + 1):
1348 for l
in range(j + 1, n + 1):
1349 M[k, l] = M[k, l] - ck * M[i, l]
1351 B[k, l] = B[k, l] - ck * B[i, l]
1352 non_zero.insert(0, j)
1360 d = ring.inverse(M[i, j])
1363 for l
in range(j + 1, n + 1):
1364 sums = sums + M[i, l] * X[l, k]
1365 X[j, k] = (B[i, k] - sums) * d
1368 i = len(non_zero) + 1
1369 for j
in range(1, r + 1):
1370 for k
in range(i, m + 1):
1377 Return solution X for self * X = B (B is a vector). 1378 This function returns tuple (V, M) below. 1379 V: one solution as vector 1380 M: kernel of self as list of basis vectors. 1381 If you want only one solution, use 'inverseImage'. 1383 Warning: B should not be a matrix instead of a vector 1386 M_1.insertColumn(self.
column + 1, B.compo)
1393 for j
in range(1, V.column + 1):
1394 if not bool(V[n, j]):
1397 d = -ring.inverse(V[n, j])
1406 Return a Matrix in column echelon form whose image is equal to 1411 for i
in range(M.row, 0, -1):
1412 for j
in range(k, 0, -1):
1417 d = ring.inverse(M[i, j])
1418 for l
in range(1, i + 1):
1422 for j
in range(1, M.column + 1):
1425 for l
in range(1, i + 1):
1426 M[l, j] = M[l, j] - M[l, k] * M[i, j]
1433 FieldSquareMatrix is a class for square matrices in field. 1436 def __init__(self, row, column=0, compo=0, coeff_ring=0):
1438 FieldSquareMatrix(row [, column, components, coeff_ring]) 1439 FieldSquareMatrix must be row == column . 1445 Return triangulated matrix of self. 1447 triangle = self.
copy()
1449 for i
in range(1, triangle.row + 1):
1450 if not triangle[i, i]:
1451 for k
in range(i + 1, triangle.row + 1):
1453 triangle.swapRow(i + 1, k + 1)
1459 for k
in range(i + 1, triangle.row + 1):
1460 inv_i_i = ring.inverse(triangle[i, i])
1461 ratio = triangle[k, i] * inv_i_i
1462 for l
in range(i, triangle.column + 1):
1463 triangle[k, l] = triangle[k, l] - triangle[i, l] * ratio
1465 for j
in range(triangle.row, triangle.column + 1):
1466 triangle[triangle.row, j] = -triangle[triangle.row, j]
1471 Return determinant of self. 1476 det = det * triangle[i, i]
1481 Return inverse matrix of self or self.inverse() * V. 1482 If inverse does not exist, raise NoInverse error. 1485 if self.
row != len(V):
1488 elif isinstance(V, Matrix):
1489 if self.
row != V.row:
1499 for j
in range(1, n + 1):
1501 for i
in range(j, n + 1):
1508 for l
in range(j, n + 1):
1514 d = ring.inverse(M[j, j])
1515 for k
in range(j + 1, n + 1):
1517 for l
in range(j + 1, n + 1):
1518 M[k, l] = M[k, l] - ck * M[j, l]
1519 for l
in range(1, r + 1):
1520 B[k, l] = B[k, l] - ck * B[j, l]
1522 for i
in range(n, 0, -1):
1523 d = ring.inverse(M[i, i])
1524 for k
in range(1, r + 1):
1526 for j
in range(i + 1, n + 1):
1527 sums = sums + M[i, j] * X[j, k]
1528 X[i, k] = (B[i, k] - sums) * d
1535 """Return a Matrix in Hessenberg Form.""" 1540 for m
in range(2, H.row):
1542 for i
in range(m + 1, n + 1):
1547 tinv = ring.inverse(H[i, m - 1])
1549 for j
in range(m - 1, n + 1):
1555 for i
in range(m + 1, n + 1):
1557 u = H[i, m - 1] * tinv
1558 for j
in range(m, n + 1):
1559 H[i, j] = H[i, j] - u * H[m, j]
1561 H.setColumn(m, H[m] + u * H[i])
1566 LUDecomposition() -> (L, U) 1568 L and U are matrices such that 1570 L : lower triangular matrix 1571 U : upper triangular matrix 1578 for i
in range(1, n + 1):
1579 for j
in range(i + 1, n + 1):
1580 L[j, i] = U[j, i] * ring.inverse(U[i, i])
1581 for k
in range(i, n + 1):
1582 U[j, k] = U[j, k] - U[i, k] * L[j, i]
1588 MatrixRing is a class for matrix rings. 1595 MatrixRing(size, scalars) 1597 size: size of matrices (positive integer) 1598 scalars: ring of scalars 1600 ring.Ring.__init__(self)
1609 self.
size == other.size
and 1610 self.
scalars == other.scalars)
1616 return "MatrixRing(%d, %s)" % (self.
size, self.
scalars)
1619 return "M_%d(%s)" % (self.
size, str(self.
scalars))
1624 Return the cached instance of the specified matrix ring. If 1625 the specified ring is not cached, it is created, cached and 1628 The method is a class method. 1637 Return the unit matrix. 1639 return self.
one.copy()
1643 getter for one (unit matrix) 1645 if self.
_one is None:
1649 one = property(_getOne,
None,
None,
"multiplicative unit")
1653 Return the zero matrix. 1655 return self.
zero.copy()
1661 if self.
_zero is None:
1665 zero = property(_getZero,
None,
None,
"additive unit")
1669 Return a newly created matrix from 'compo'. 1671 'compo' must be a list of n*n components in the scalar ring, 1672 where n = self.size. 1678 Return the characteristic of the ring. 1684 Report whether another ring contains the ring as a subring. 1688 if not isinstance(other, MatrixRing):
1694 Report whether the ring is a superring of another ring. 1698 if not isinstance(other, MatrixRing):
1700 return self.
size == other.size
and \
1705 Return common super ring of self and another ring. 1707 if not isinstance(other, MatrixRing)
or self.
size != other.size:
1708 raise TypeError(
"no common super ring")
1709 return MatrixRing.getInstance(self.
size,
1715 Subspace is a class for subspaces. 1718 def __init__(self, row, column, compo=0, coeff_ring=0, isbasis=None):
1720 Subspace(row, column [,components, coeff_ring, isbasis]) 1722 if isinstance(compo, bool):
1725 elif isinstance(coeff_ring, bool):
1726 isbasis = coeff_ring
1734 A constructor class method, which creates Subspace from a 1738 for row
in mat.compo:
1740 return cls(mat.row, mat.column, compo, mat.coeff_ring, isbasis)
1744 Subspace -> Field(Square)Matrix 1753 Check self is in other as subspace 1756 other.inverseImage(self)
1763 Change matrix to basis. 1766 basis = self.
image()
1775 Return a basis of full space, which including self's column vectors. 1784 for s
in range(1, k + 1):
1785 for t
in range(s, n + 1):
1790 d = ring.inverse(M[t, s])
1793 pnt[t - 1] = pnt[s - 1]
1794 for j
in range(s + 1, k + 1):
1800 for i
in range(1, n + 1):
1801 if i != s
and i != t:
1802 M[i, j] = M[i, j] - M[i, s] * M[s, j]
1810 return Subspace.fromMatrix(B,
True)
1814 Return space which is sum of self and other. 1816 if self.
row != other.row:
1819 N.extendColumn(other)
1820 return Subspace.fromMatrix(N.image(),
True)
1824 Return space which is intersection of self and other. 1826 if self.
row != other.row:
1829 M1.extendColumn(other)
1833 N1 = N.getBlock(1, 1, self.
column, N.column)
1834 return Subspace.fromMatrix((self * N1).
image(),
True)
1843 generate new Matrix or SquareMatrix class. 1848 if isinstance(column, list):
1854 if coeff_ring != 0
and isinstance(coeff_ring, int):
1859 if isinstance(compo[0], (list, tuple)):
1860 coeff_ring = ring.getRing(compo[0][0])
1862 coeff_ring = ring.getRing(compo[0][1])
1864 coeff_ring = ring.getRing(compo[0])
1865 if coeff_ring.isfield():
1869 return FieldMatrix(row, column, compo, coeff_ring)
1874 return RingMatrix(row, column, compo, coeff_ring)
1878 return unit matrix of size. 1879 coeff is subclass for ring.Ring or ring.Ring.one. 1886 coeff = ring.getRing(one)
1889 units = [zero] * size + [one]
1890 for i
in range(size - 1):
1891 unit_matrix = unit_matrix + units
1894 identityMatrix = unitMatrix
1899 coeff is subclass for ring.Ring or ring.Ring.zero. 1904 if not(isinstance(column, (int, long))):
1914 coeff = ring.getRing(coeff)
1915 zero_matrix = [zero] * (row * column)
1924 """Invalid input error for matrix size.""" 1927 class VectorsNotIndependent(Exception):
1928 """Invalid input error because column vectors are linear dependent.""" 1932 """Invalid input error because self do not have inverse image for input.""" 1936 """Invalid input error because matrix is not invertible.""" 1942 If 'coeff_ring' represents characteristic, return F_p or Z_n instance. 1944 if isinstance(coeff_ring, int):