def buildMatrices(filename): global Y, E, J, A #assumes proper formatting (no error detection/correction) f=open(filename) lines=f.readlines() f.close() branches=int(lines[0]) nodes=int(lines[1]) Y=Matrix(i=branches,j=branches) E=Matrix(i=branches,j=1) J=Matrix(i=branches,j=1) A=Matrix(i=nodes,j=branches) #NB: lines list uses array indexing (starts at 0), but Matrix class uses indexing starting at 1. for lineNum in range(2,(branches+1)+1): #skip first two lines already read (adjusted for array indexing, i.e. starts at 0) branch=lineNum-1 for i,v in enumerate(lines[lineNum].split(',')): if (i==0): J.set(branch,1,float(v)) elif (i==1): Y.set(branch,branch,1/float(v)) else: E.set(branch,1,float(v)) for lineNum in range((branches+1)+1,(branches+1+nodes)+1): node=lineNum-(branches+1) for j,v in enumerate(lines[lineNum].split(',')): A.set(node,j+1,float(v))
upperNodes=currentNodes numBranches=(3*n**2+3*n)/2 numNodes=(n**2+3*n+2)/2 populateBranches() numBranches+=1 #add 1 to the number of branches (for source branch) #build incidence matrix from branches list A=Matrix(i=numNodes,j=numBranches) for j,branch in enumerate(branches): A.set(branch[0],j+1,1) A.set(branch[1],j+1,-1) #set source branch A.set(1,numBranches,-1) A.set(numNodes,numBranches,1) f=open(output,'w') f.write(str(numBranches)+'\n') #number of nodes (subtract one taken as ground) f.write(str(numNodes-1)+'\n') #print J,R,E for each branch (0,1,0)