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