root / pobysoPythonSage / src / sageSLZ / sageMatrixOperations.sage @ 230
Historique | Voir | Annoter | Télécharger (4,23 ko)
1 | 87 | storres | print "sageMatrixOperations loading..." |
---|---|---|---|
2 | 172 | storres | #2345678901234567890123456789012345678901234567890123456789012345678901234567890 |
3 | 75 | storres | def smo_is_diagonal_complete_matrix(mat): |
4 | 74 | storres | """ |
5 | 75 | storres | Check that all the element on the diagonal are not 0. |
6 | 74 | storres | """ |
7 | 74 | storres | dimensions = mat.dimensions() |
8 | 74 | storres | # Must be a bidimensional matrix. |
9 | 74 | storres | if len(dimensions) != 2: |
10 | 74 | storres | return False |
11 | 74 | storres | # Must be square. |
12 | 74 | storres | if dimensions[0] != dimensions[1]: |
13 | 74 | storres | return False |
14 | 75 | storres | # A 1x1 matrix is diagonal complete if it's single element is not 0. |
15 | 74 | storres | if dimensions[0] == 1: |
16 | 75 | storres | if mat[0, 0] != 0: |
17 | 75 | storres | return True |
18 | 75 | storres | else: |
19 | 75 | storres | return False |
20 | 75 | storres | # End if |
21 | 74 | storres | for rowIndex in xrange(0, dimensions[0]): |
22 | 75 | storres | if mat[rowIndex, rowIndex] == 0: |
23 | 75 | storres | print mat.rows()[rowIndex], rowIndex |
24 | 75 | storres | return False |
25 | 74 | storres | return True |
26 | 75 | storres | # End smo_is_diagonal_complete_matrix |
27 | 74 | storres | |
28 | 75 | storres | def smo_is_lower_triangular_matrix(mat): |
29 | 74 | storres | """ |
30 | 75 | storres | Check that the matrix is lower triangular. |
31 | 74 | storres | """ |
32 | 74 | storres | dimensions = mat.dimensions() |
33 | 74 | storres | # Must be a bidimensional matrix. |
34 | 74 | storres | if len(dimensions) != 2: |
35 | 74 | storres | return False |
36 | 74 | storres | # Must be square. |
37 | 74 | storres | if dimensions[0] != dimensions[1]: |
38 | 74 | storres | return False |
39 | 75 | storres | # A 1x1 matrix is lower triangular. |
40 | 74 | storres | if dimensions[0] == 1: |
41 | 75 | storres | return True |
42 | 74 | storres | for rowIndex in xrange(0, dimensions[0]): |
43 | 75 | storres | for colIndex in xrange(rowIndex + 1, dimensions[1]): |
44 | 75 | storres | if mat[rowIndex, colIndex] != 0: |
45 | 75 | storres | print mat.rows()[rowIndex] |
46 | 75 | storres | return False |
47 | 74 | storres | return True |
48 | 75 | storres | # End smo_is_lower_triangular_matrix |
49 | 75 | storres | |
50 | 83 | storres | def smo_is_upper_triangular_matrix(mat): |
51 | 83 | storres | """ |
52 | 83 | storres | Check that the matrix is upper triangular. |
53 | 83 | storres | """ |
54 | 83 | storres | dimensions = mat.dimensions() |
55 | 83 | storres | # Must be a bidimensional matrix. |
56 | 83 | storres | if len(dimensions) != 2: |
57 | 83 | storres | return False |
58 | 83 | storres | # Must be square. |
59 | 83 | storres | if dimensions[0] != dimensions[1]: |
60 | 83 | storres | return False |
61 | 83 | storres | # A 1x1 matrix is lower triangular. |
62 | 83 | storres | if dimensions[0] == 1: |
63 | 83 | storres | return True |
64 | 83 | storres | for rowIndex in xrange(1, dimensions[0]): |
65 | 83 | storres | for colIndex in xrange(0, rowIndex): |
66 | 83 | storres | if mat[rowIndex, colIndex] != 0: |
67 | 83 | storres | print mat.rows()[rowIndex] |
68 | 83 | storres | return False |
69 | 83 | storres | return True |
70 | 83 | storres | # End smo_is_upper_triangular_matrix |
71 | 83 | storres | |
72 | 94 | storres | def smo_max_non_null_abs(mat): |
73 | 94 | storres | """ |
74 | 94 | storres | Compute the maximum absolute value of the matrix elements. |
75 | 94 | storres | """ |
76 | 94 | storres | maxNonNull = -Infinity |
77 | 94 | storres | mlist = mat._list() |
78 | 94 | storres | for i in mlist: |
79 | 94 | storres | if i != 0 and i.abs() > maxNonNull : |
80 | 94 | storres | maxNonNull = i.abs() |
81 | 94 | storres | return maxNonNull |
82 | 94 | storres | # End smo_min_non_null_abs |
83 | 94 | storres | |
84 | 94 | storres | # |
85 | 94 | storres | |
86 | 94 | storres | def smo_min_non_null_abs(mat): |
87 | 94 | storres | """ |
88 | 94 | storres | Compute the minimum absolute value of the matrix elements. |
89 | 94 | storres | """ |
90 | 94 | storres | minNonNull = Infinity |
91 | 94 | storres | mlist = mat._list() |
92 | 94 | storres | for i in mlist: |
93 | 94 | storres | if i != 0 and i.abs() < minNonNull : |
94 | 94 | storres | minNonNull = i.abs() |
95 | 94 | storres | return minNonNull |
96 | 94 | storres | # End smo_min_non_null_abs |
97 | 95 | storres | |
98 | 103 | storres | def smo_transformation_row_matrix_strings(varPrefixString, matrix): |
99 | 104 | storres | """ |
100 | 104 | storres | Create a string that will be evaluated to create one variable per |
101 | 104 | storres | matrix row. |
102 | 104 | storres | The first application is the computation of transformation matrices. |
103 | 104 | storres | """ |
104 | 95 | storres | m = matrix.nrows() |
105 | 95 | storres | if m == 0 : |
106 | 95 | storres | return None |
107 | 95 | storres | varDeclarationString = "var('" |
108 | 95 | storres | varListArrayDeclaration ="[" |
109 | 95 | storres | varListString = "" |
110 | 95 | storres | for k in xrange(1,m+1): |
111 | 95 | storres | if k != m: |
112 | 95 | storres | varListString += varPrefixString + str(k) + "," |
113 | 95 | storres | else: |
114 | 95 | storres | varListString += varPrefixString + str(k) |
115 | 95 | storres | varDeclarationString += varListString + "')" |
116 | 95 | storres | varListArrayDeclaration += "]" |
117 | 95 | storres | return(varDeclarationString, varListArrayDeclaration) |
118 | 95 | storres | # End smo_transformation_row_matrix_strings. |
119 | 96 | storres | |
120 | 111 | storres | def smo_triangular_matrix_determinant(matrix): |
121 | 111 | storres | if matrix == None: |
122 | 111 | storres | return None |
123 | 111 | storres | numRows = matrix.nrows() |
124 | 111 | storres | if numRows < 1: |
125 | 111 | storres | return 0 |
126 | 111 | storres | determinant = 1 |
127 | 111 | storres | for rowIndex in xrange(numRows): |
128 | 111 | storres | determinant *= matrix[rowIndex, rowIndex] |
129 | 111 | storres | return determinant |
130 | 111 | storres | # End smo_triangular_matrix_determinant |
131 | 111 | storres | # |
132 | 96 | storres | def smo_zero_rows_index_list(matrix): |
133 | 96 | storres | """ |
134 | 96 | storres | Compute the list of indices of the rows that have a 0 norm. |
135 | 96 | storres | """ |
136 | 97 | storres | if 0 == matrix.nrows(): |
137 | 96 | storres | return [] |
138 | 96 | storres | zeroRowsList = [] |
139 | 96 | storres | rowIndex = 0 |
140 | 96 | storres | for row in matrix.rows(): |
141 | 96 | storres | if row.norm(p=1) == 0 : |
142 | 96 | storres | zeroRowsList.append(rowIndex) |
143 | 97 | storres | rowIndex += 1 |
144 | 96 | storres | return zeroRowsList |
145 | 96 | storres | |
146 | 96 | storres | # End smo_zero_rows_index_list |
147 | 95 | storres | |
148 | 87 | storres | print "\t...sageMatrixOperations loaded" |