def regular_completion(matrix): m,n = matrix.shape r = matrix.rank() #~ IPS() assert m!=n, "There is no regular completion of a square matrix." if m<n: assert r==m, "Matrix does not have full row rank." A, B, V_pi = reshape_matrix_columns(matrix) zeros = sp.zeros(n-m,m) ones = sp.eye(n-m) S = st.col_stack(zeros,ones) completion = S*V_pi.T regular_matrix = st.row_stack(matrix,completion) assert st.rnd_number_rank(regular_matrix)==n, "Regular completion seems to be wrong." elif n<m: assert r==n, "Matrix does not have full column rank." A, B, V_pi = reshape_matrix_columns(matrix.T) zeros = sp.zeros(m-n,n) ones = sp.eye(m-n) S = st.col_stack(zeros,ones) completion = V_pi*S.T regular_matrix = st.col_stack(completion,matrix) assert st.rnd_number_rank(regular_matrix)==m, "Regular completion seems to be wrong." return completion
def regular_completion(matrix): m, n = matrix.shape r = matrix.rank() #~ IPS() assert m != n, "There is no regular completion of a square matrix." if m < n: assert r == m, "Matrix does not have full row rank." A, B, V_pi = reshape_matrix_columns(matrix) zeros = sp.zeros(n - m, m) ones = sp.eye(n - m) S = st.col_stack(zeros, ones) completion = S * V_pi.T regular_matrix = st.row_stack(matrix, completion) assert st.rnd_number_rank( regular_matrix) == n, "Regular completion seems to be wrong." elif n < m: assert r == n, "Matrix does not have full column rank." A, B, V_pi = reshape_matrix_columns(matrix.T) zeros = sp.zeros(m - n, n) ones = sp.eye(m - n) S = st.col_stack(zeros, ones) completion = V_pi * S.T regular_matrix = st.col_stack(completion, matrix) assert st.rnd_number_rank( regular_matrix) == m, "Regular completion seems to be wrong." return completion
def is_linearly_independent(matrix, column_vector): m, n = matrix.shape rank1 = st.rnd_number_rank(matrix) tmp = st.concat_cols(matrix, column_vector) rank2 = st.rnd_number_rank(tmp) assert rank2 >= rank1 return rank2 > rank1
def left_pseudo_inverse(matrix): m, n = matrix.shape r = st.rnd_number_rank(matrix) #assert r==n, "Matrix does not have full column rank!" assert not is_zero_matrix(matrix), "Matrix is zero matrix!" transposed_rpinv = right_pseudo_inverse(matrix.T) matrix_lpinv = transposed_rpinv.T assert is_unit_matrix(matrix_lpinv*matrix), "Leftpseudoinverse is not correct." return matrix_lpinv
def left_pseudo_inverse(matrix): m, n = matrix.shape r = st.rnd_number_rank(matrix) #assert r==n, "Matrix does not have full column rank!" assert not is_zero_matrix(matrix), "Matrix is zero matrix!" transposed_rpinv = right_pseudo_inverse(matrix.T) matrix_lpinv = transposed_rpinv.T assert is_unit_matrix(matrix_lpinv * matrix), "Leftpseudoinverse is not correct." return matrix_lpinv
def has_left_ortho_complement(matrix): r = st.rnd_number_rank(matrix) m, n = matrix.shape if(n>=m): return True if (r<m) else False return True
def is_regular_matrix(matrix): assert is_square_matrix(matrix), "Matrix is not a square matrix." m, n = matrix.shape r = st.rnd_number_rank(matrix) return True if (r == m) else False
def has_full_row_rank(matrix): m, n = matrix.shape r = st.rnd_number_rank(matrix) return True if (m==r) else False
def reshape_matrix_columns(P): m0, n0 = P.shape # pick m0 of the simplest lin. independent columns of P: P_new = remove_zero_columns(P) m1, n1 = P_new.shape list_of_cols = matrix_to_vectorlist(P_new) # sort by "complexity" if pinv_optimization == "free_symbols": cols_sorted_by_atoms = sorted(list_of_cols, key=lambda x: x.free_symbols, reverse=False) elif pinv_optimization == "count_ops": cols_sorted_by_atoms = sorted(list_of_cols, key=lambda x: nr_of_ops(x), reverse=False) elif pinv_optimization == "none": cols_sorted_by_atoms = list_of_cols # sort by number of zero entries colvecs_sorted = sorted(cols_sorted_by_atoms, key=lambda x: count_zero_entries(x), reverse=True) # pick m suitable column vectors and add to new matrix A: ---------- A = colvecs_sorted[0] for j in range(len(colvecs_sorted) - 1): column = colvecs_sorted[j + 1] if is_linearly_independent(A, column): A = st.concat_cols(A, column) if st.rnd_number_rank(A) == m1: break assert A.is_square # calculate transformation matrix R: ------------------------------- #R_tilde = sp.Matrix([]) used_cols = [] R = sp.Matrix([]) for k in range(m1): new_column_index = k old_column_index = get_column_index_of_matrix(P, A.col(k)) used_cols.append(old_column_index) tmp = sp.zeros(n0, 1) tmp[old_column_index] = 1 #R_tilde = st.concat_cols(R_tilde,tmp) R = st.concat_cols(R, tmp) #R=R_tilde # remainder columns of R matrix #m2,n2 = R_tilde.shape m2, n2 = R.shape for l in range(n0): if l not in used_cols: R_col = sp.zeros(n0, 1) R_col[l] = 1 R = st.concat_cols(R, R_col) m3, n3 = A.shape r2 = st.rnd_number_rank(A) assert m3 == r2, "A problem occured in reshaping the matrix." # calculate B matrix: ---------------------------------------------- B = sp.Matrix([]) tmp = P * R for i in range(n0 - m0): B = st.concat_cols(B, tmp.col(m0 + i)) assert is_zero_matrix( (P * R) - st.concat_cols(A, B)), "A problem occured in reshaping the matrix." return A, B, R
def reshape_matrix_columns(P): m0, n0 = P.shape # pick m0 of the simplest lin. independent columns of P: P_new = remove_zero_columns(P) m1, n1 = P_new.shape list_of_cols = matrix_to_vectorlist(P_new) # sort by "complexity" if pinv_optimization=="free_symbols": cols_sorted_by_atoms = sorted( list_of_cols, key=lambda x: x.free_symbols, reverse=False) elif pinv_optimization=="count_ops": cols_sorted_by_atoms = sorted( list_of_cols, key=lambda x: nr_of_ops(x), reverse=False) elif pinv_optimization=="none": cols_sorted_by_atoms = list_of_cols # sort by number of zero entries colvecs_sorted = sorted( cols_sorted_by_atoms, key=lambda x: count_zero_entries(x), reverse=True) # pick m suitable column vectors and add to new matrix A: ---------- A = colvecs_sorted[0] for j in xrange(len(colvecs_sorted)-1): column = colvecs_sorted[j+1] if is_linearly_independent(A,column): A = st.concat_cols(A,column) if st.rnd_number_rank(A)==m1: break assert A.is_square # calculate transformation matrix R: ------------------------------- #R_tilde = sp.Matrix([]) used_cols = [] R = sp.Matrix([]) for k in xrange(m1): new_column_index = k old_column_index = get_column_index_of_matrix(P,A.col(k)) used_cols.append(old_column_index) tmp = sp.zeros(n0,1) tmp[old_column_index] = 1 #R_tilde = st.concat_cols(R_tilde,tmp) R = st.concat_cols(R,tmp) #R=R_tilde # remainder columns of R matrix #m2,n2 = R_tilde.shape m2,n2 = R.shape for l in xrange(n0): if l not in used_cols: R_col = sp.zeros(n0,1) R_col[l] = 1 R = st.concat_cols(R,R_col) m3, n3 = A.shape r2 = st.rnd_number_rank(A) assert m3==r2, "A problem occured in reshaping the matrix." # calculate B matrix: ---------------------------------------------- B = sp.Matrix([]) tmp = P*R for i in xrange(n0-m0): B = st.concat_cols(B,tmp.col(m0+i)) assert is_zero_matrix( (P*R) - st.concat_cols(A,B)), "A problem occured in reshaping the matrix." return A, B, R
def is_special_case(Bi): """ Checks for special case """ n, p = Bi.shape return True if (st.rnd_number_rank(Bi) < p) else False
def has_right_ortho_complement(matrix): r = st.rnd_number_rank(matrix) m, n = matrix.shape if(m>=n): return True if (r<n) else False return True
def has_right_ortho_complement(matrix): r = st.rnd_number_rank(matrix) m, n = matrix.shape if (m >= n): return True if (r < n) else False return True
def has_left_ortho_complement(matrix): r = st.rnd_number_rank(matrix) m, n = matrix.shape if (n >= m): return True if (r < m) else False return True
def has_full_row_rank(matrix): m, n = matrix.shape r = st.rnd_number_rank(matrix) return True if (m == r) else False
def end_condition(Bi): """ The algorithm ends if Bi has full row rank. """ n, p = Bi.shape return True if (n == st.rnd_number_rank(Bi)) else False