Statistiques
| Révision :

root / pobysoPythonSage / src / sageSLZ / sageMatrixOperations.sage @ 97

Historique | Voir | Annoter | Télécharger (3,66 ko)

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