-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_methods.py
260 lines (240 loc) · 8.97 KB
/
matrix_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
r"""
This file contains the methods used in the Bijective Matrix Algebra package.
It creates all the foundational methods around matrices, including multiplication,
determinant, adjoint and printing the matrix.
AUTHORS:
- Steven Tartakovsky (2012): initial version
"""
#*****************************************************************************
# Copyright (C) 2012 Steven Tartakovsky <startakovsky@gmail.com>,
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.rings.polynomial.polynomial_ring import PolynomialRing_general
from sage.matrix.all import matrix
from sage.matrix.all import MatrixSpace
from sage.combinat.permutation import *
from sage.combinat.cartesian_product import CartesianProduct
from sage.bijectivematrixalgebra.combinatorial_objects import CombinatorialObject
from sage.bijectivematrixalgebra.combinatorial_scalar_rings_and_elements import CombinatorialScalarWrapper
from sage.bijectivematrixalgebra.combinatorial_scalar_rings_and_elements import CombinatorialScalarRing
from sage.sets.finite_set_maps import FiniteSetMaps
from copy import copy
from copy import deepcopy
def _product_row(mat1, mat2, row):
dim = mat1.nrows()
r = list()
for j in range(dim):
C = CombinatorialScalarWrapper(set())
for k in range(dim):
C = C + (mat1[row,k]*mat2[k,j])
for elm in C:
elm.set_row(row)
elm.set_col(j)
r.append(C)
return r
def identity_matrix(dim):
r"""
Returns standard combinatorial identity matrix
"""
mat_space = MatrixSpace(CombinatorialScalarRing(),dim)
prnt = mat_space.base_ring()
L = list()
for i in range(dim):
L.append(list())
for j in range(dim):
if i==j:
L[i].append(prnt._one())
else:
L[i].append(prnt._zero())
return mat_space(L)
def matrix_multiply(mat1,mat2):
r"""
Only works for square matrices.
"""
mat_space = mat1.matrix_space()
dim = mat_space.nrows()
l = list()
for row in range(dim):
l.append(_product_row(mat1,mat2,row))
return mat_space(l)
def matrix_generating_function(m):
r"""
returns the generating function of each scalar as a matrix
"""
num_var = 10
dimx = m.nrows()
dimy = m.ncols()
d = dict()
for x in range(dimx):
for y in range(dimy):
d[(x,y)]=m[x,y].get_generating_function()
return matrix(PolynomialRing(ZZ,['x'+str(i) for i in range(num_var)]),d)
def matrix_remove_row_col(mat,row,col):
r"""
Return matrix with row, col removed
"""
L = list()
newrows = range(mat.nrows())
newcols= range(mat.ncols())
newrows.pop(row)
newcols.pop(col)
for x in newrows:
L.append(list())
for y in newcols:
L[len(L)-1].append(mat[x,y])
return matrix(mat.parent().base_ring(),len(newrows),len(newcols),L)
def matrix_determinant(mat):
r"""
Return determinant scalar, the form of which is:
(\sigma,a_1,...,a_n) where sign \sigma is sgn(\sigma)
and weight \sigma is 1.
"""
dim = mat.nrows()
P = Permutations(dim)
S = set()
for p in P:
l = list()
p_comb = CombinatorialScalarWrapper([CombinatorialObject(p,p.signature())])
for i in range(1,dim+1):
l.append(mat[i-1,p(i)-1])
cp = CartesianProduct(p_comb,*l)
for i in cp:
weight = 1
sign = 1
for elm in i:
sign = sign*elm.get_sign()
weight = weight*elm.get_weight()
S.add(CombinatorialObject(tuple(i),sign,weight))
return CombinatorialScalarWrapper(S)
def matrix_combinatorial_adjoint(mat):
r"""
Return Combinatorial Adjoint.
"""
dim = mat.nrows()
M = list()
mat_space = MatrixSpace(CombinatorialScalarRing(),dim)
prnt = mat_space.base_ring()
#create list L of lists of sets, dimension is increased by one to mitigate index confusion
L = list()
for i in range(dim+1):
L.append(list())
for j in range(dim+1):
L[i].append(set())
P = Permutations(dim)
for p in P:
p_comb = CombinatorialScalarWrapper([CombinatorialObject(p,p.signature())])
l = list()
for i in range(1,dim+1):
l.append(mat[p(i)-1,i-1])
#This list will have empty sets, which will yield to an empty cartesian product
#especially when the matrix input is triangular (except for the identity permutation).
#We will now iterate through the selected entries in each column
#and create a set of a singleton of an empty string that corresponds
#to the "missing" element of the tuple described in definition 39.
for i in range(1,dim+1):
copy_l = copy(l)
copy_l[i-1]=CombinatorialScalarWrapper([CombinatorialObject('_',1)])
cp = CartesianProduct(p_comb,*copy_l)
for tupel in cp:
tupel = tuple(tupel)
tupel_weight = 1
tupel_sign = 1
for elm in tupel:
tupel_sign = tupel_sign*elm.get_sign()
tupel_weight = tupel_weight*elm.get_weight()
L[i][p(i)].add(CombinatorialObject(tupel,tupel_sign,tupel_weight))
#turn these sets into CombinatorialScalars
for i in range(1,dim+1):
l = list()
for j in range(1,dim+1):
l.append(CombinatorialScalarWrapper(L[i][j]))
M.append(l)
return mat_space(M)
def matrix_clean_up(mat):
r"""
Apply get_cleaned_up_version to each object within
each entry and return a cleaned up matrix
"""
dim = mat.nrows()
L = list()
mat_space = MatrixSpace(CombinatorialScalarRing(),dim)
prnt = mat_space.base_ring()
for i in range(dim):
L.append(list())
for j in range(dim):
L[i].append(CombinatorialScalarWrapper(mat[i,j].get_cleaned_up_version()))
return mat_space(L)
def matrix_print(mat):
print "Printing..."
for i in range(mat.nrows()):
for j in range(mat.ncols()):
print "row " + str(i) + ", column " + str(j) + "; " + str(mat[i,j].get_size()) + " elements"
mat[i,j].print_list()
print "------------------------------"
def matrix_comparison(matA,matB):
r"""
Returns True if matrices are equal.
"""
nrows = matA.nrows()
ncols = matB.ncols()
if matA.ncols()!=matB.ncols() or matA.nrows()!=matB.nrows():
raise ValueError, "Check dimensions of input"
else:
for i in range(nrows):
for j in range(ncols):
if matA[i,j]!=matB[i,j]:
return False
return True
def matrix_identity_multiply_scalar(scal,nrows,ncols):
r"""
Currently this method only returns for
the same number of rows and columns.
"""
if nrows!=ncols:
raise ValueError, "check dimensions"
else:
L = list()
for i in range(nrows):
L.append(list())
for j in range(ncols):
if i == j:
L[i].append(scal)
else:
L[i].append(CombinatorialScalarWrapper(set()))
return matrix(CombinatorialScalarRing(),nrows,ncols,L)
def matrix_adjoint_lemma_40(mat):
r"""
Returns a matrix which is the target in the
reduction of adjoint(A) times A to det(A) times I.
"""
if mat.nrows()!=mat.ncols():
raise ValueError, "Make sure that this is indeed a Combinatorial Adjoint matrix"
else:
dim = mat.nrows()
L = list()
mat_space = MatrixSpace(CombinatorialScalarRing(),dim)
for i in range(dim):
L.append(list())
for j in range(dim):
if i==j:
copyset = deepcopy(mat[i,j].get_set())
for elm in copyset:
tmp = list(elm.get_object()[0].get_object())
#object is tuple, elements come from the actual tuple, hence double get_object()
index = tmp.index(CombinatorialObject('_',1))
tmp[index]=elm.get_object()[1]
elm.set_object(tuple(tmp))
else:
copyset = CombinatorialScalarWrapper(set())
L[i].append(CombinatorialScalarWrapper(copyset))
return mat_space(L)