def create_network_branch_matrices_mesh(num_branches, branch_resistance, test_current): """ Create the Y, J, E network branch matrices of the resistive mesh given by the provided number of branches, branch resistance and test current. :param num_branches: the number of branches in the mesh :param branch_resistance: the resistance of each branch in the mesh :param test_current: the test current to apply to the mesh :return: the Y, J, E network branch matrices """ Y = Matrix.diagonal([1 / branch_resistance if branch < num_branches - 1 else 0 for branch in range(num_branches)]) # Negative test current here because we assume current is coming OUT of the test current node. J = Matrix.column_vector([0 if branch < num_branches - 1 else -test_current for branch in range(num_branches)]) E = Matrix.column_vector([0 for _ in range(num_branches)]) return Y, J, E
def csv_to_network_branch_matrices(filename): """ Converts a CSV file to Y, J, E network matrices. :param filename: the name of the CSV file :return: the Y, J, E network matrices """ with open(filename, 'r') as csv_file: reader = csv.reader(csv_file) J = [] Y = [] E = [] for row in reader: J_k = float(row[0]) R_k = float(row[1]) E_k = float(row[2]) J.append(J_k) Y.append(1 / R_k) E.append(E_k) Y = Matrix.diagonal(Y) J = Matrix.column_vector(J) E = Matrix.column_vector(E) return Y, J, E
from csv_saver import save_rows_to_csv from linear_networks import solve_linear_network, csv_to_network_branch_matrices from choleski import choleski_solve from matrices import Matrix NETWORK_DIRECTORY = 'network_data' L_2 = Matrix([[5, 0], [1, 3]]) L_3 = Matrix([[3, 0, 0], [1, 2, 0], [8, 5, 1]]) L_4 = Matrix([[1, 0, 0, 0], [2, 8, 0, 0], [5, 5, 4, 0], [7, 2, 8, 7]]) matrix_2 = L_2 * L_2.transpose() matrix_3 = L_3 * L_3.transpose() matrix_4 = L_4 * L_4.transpose() positive_definite_matrices = [matrix_2, matrix_3, matrix_4] x_2 = Matrix.column_vector([8, 3]) x_3 = Matrix.column_vector([9, 4, 3]) x_4 = Matrix.column_vector([5, 4, 1, 9]) xs = [x_2, x_3, x_4] def q1(): """ Question 1 """ q1b() q1c() q1d() def q1b():