## Graph Theory : example 7.7 : (pg 7.18 & 7.19)
import numpy
##Complete incidence matrix Aa
print("\nAa=");
print numpy.matrix([[1, 0 ,0 ,-1 ,1 ,0 ,0 ,0],[-1, 1, 0 ,0 ,0 ,1 ,0 ,0],[0 ,-1 ,1 ,0 ,0 ,0 ,1 ,0],[0, 0 ,-1, 1 ,0 ,0 ,0, 1],[0 ,0 ,0 ,0 ,-1, -1 ,-1, -1]]);
##eliminating last row from Aa
print("\nA=");
print numpy.matrix([[1, 0 ,0 ,-1, 1 ,0 ,0 ,0],[-1, 1 ,0 ,0 ,0 ,1 ,0 ,0],[0 ,-1, 1, 0, 0 ,0 ,1 ,0],[0, 0, -1, 1 ,0 ,0 ,0 ,1]]);
##Tieset matrix B
print("\ntwigs={1,3,5,7} \nlinks={2,4,6,8} \ntieset 2={2,7,5,1} \ntieset 4={4,5,7,3} \ntieset 6={6,5,1} \ntieset 8={8,7,3}");
## forward direction = 1, reverse direction = -1
print("\nB=");
print numpy.matrix([[1, 1 ,0 ,0 ,-1, 0, 1, 1],[0, 0, 1 ,1 ,1 ,0, -1, 0],[1, 0 ,0 ,0 ,-1, 1, 0, 0],[0, 0 ,1 ,0 ,0 ,0 ,-1, 1]]);
## f-cutset matrix Q
print("\nf-cutset 1={1,6,2} \nf-cutset 3={3,4,8} \nf-cutset 5={5,4,6,2} \nf-cutset 7={7,2,8,4}");
print("\nQ=");
print numpy.matrix([[1 ,-1, 0, 0, 0 ,-1, 0, 0],[0 ,0, 1, -1, 0, 0 ,0 ,-1],[0, 1, 0 ,-1, 1, 1, 0, 0],[0 ,-1, 0, 1, 0 ,0, 1, 1]]);
## Graph Theory : example 7.8 :(pg 7.19 & 7.20)
##Complete Incidence Matrix Aa
import numpy
from numpy import linalg
print("\nAa=");
import numpy
print numpy.matrix([[1 ,0 ,-1 ,1],[-1, 1 ,0 ,0],[0 ,-1 ,1 ,-1]]);
## Reduced Incidence matrix A (by eliminating last row from Aa)
A=numpy.matrix([[1 ,0, -1, 1],[-1, 1 ,0 ,0]]);
print("\nA=");
print numpy.matrix([[1 ,0 ,-1 ,1],[-1, 1 ,0 ,0]]);
A1=numpy.matrix([[1 ],[0], [-1], [1],[-1], [1] ,[0] ,[0]]);
print("A1")
print("\nNumber of possible trees=");##A^T=A'= transpose of A
x=(A*numpy.matrix.transpose(A));
print (x);
alp = numpy.linalg.det(x);
print(alp)
# Graph Theory : example 7.11 :(pg 7.21 & 7.22)
import numpy
from numpy import linalg
print ("Aa=[0 -1 1 0 0;0 0 -1 -1 -1;-1 0 0 0 1;1 1 0 1 0]");#Complete incidence matrix
A=numpy.matrix([[0, -1, 1, 0, 0],[0, 0, -1, -1, -1],[-1, 0, 0, 0, 1]]);#Reduced incidence matrix
print("\nNumber of possible trees = |A*A^T|");#A^T=A'=transpose of A
x=(A*numpy.matrix.transpose(A));
print (x);
alp = numpy.linalg.det(x);
print("\n|A*A^T|= ",round(alp));#No. of possible trees
#Tieset Matrix B
print("\ntwigs={3,4,5} \nlinks={1,2} \ntieset 1={1,4,5} \ntieset 2={2,3,4}");
print ("B=[1 0 0 -1 1;0 1 1 -1 0]");
#f-cutset Matrix Q
print("\nf-cutset 3={3,2} \nf-cutset 4={4,2,1} \nf-cutset 5={5,1}");
print ("Q=[0 -1 1 0 0;1 1 0 1 0;-1 0 0 0 1]");
##Graph Theory : example 7.14 :(pg 7.37 & 7.38)
##Tieset Matrix B
import numpy
import numpy
from numpy import linalg
print("\ntieset1={1,4,5} \ntieset2={2,4,6} \ntieset={3,5,6} \nB=");
B=numpy.matrix([[1, 0 ,0 ,1 ,1 ,0],[0 ,1 ,0 ,-1, 0, -1],[0, 0 ,1 ,0 ,-1, 1]]);
print(B);
print("\nThe KVL equation in matrix form \nB.Zb.(B^T).Il = B.Vs-B.Zb.Is");
print("\nB.Zb.(B^T).Il = B.Vs \nZb=");##Is=0
import numpy as np
Zb = np.array([1,1,1,2,2,2])
d = np.diag(Zb)
print(d)
print("\n(B^T)=");
Z= numpy.matrix.transpose(B)
print(Z)
Vs=numpy.matrix([[2],[0],[0],[0],[0],[0]]);
print("\nVs=");
print(Vs);
print("\nB.Zb =");
x=numpy.dot(B,Zb);
print(x);
print("\nB.Vs=");
z=numpy.dot(B,Vs);
print(z);
print("\nLoad currents:");
M=numpy.matrix([[5, -2 ,-2],[-2 ,5 ,-2],[-2, -2, 5]]);
H=numpy.linalg.inv(M);
N=([[2],[0],[0]]);
X=numpy.dot(H,N);
print(X);
print("\nIl1=0.857 A \nIl2=0.571 A \nIl3=0.571 A");
print("\nBranch currents:");
P=(Z,X);
print(P);##Currents in amperes
##Graph Theory : example 7.15 :(pg 7.38 & 7.39)
##Tieset Matrix B
import numpy
import numpy
from numpy import linalg
print("\ntieset1={1,4,6} \ntieset2={2,5,6} \ntieset={3,5,4} \nB=");
B=numpy.matrix([[1, 0 ,0 ,1 ,0 ,1],[0, 1, 0, 0, 1 ,-1],[0 ,0 ,1 ,-1 ,-1 ,0]]);
print(B);
print("\nThe KVL equation in matrix form \nB.Zb.(B^T).Il = B.Vs-B.Zb.Is");
print("\nB.Zb.(B^T).Il = B.Vs \nZb=");##Is=0
Zb = numpy.array([6,4,3,4,6,2])
d = numpy.diag(Zb)
print(d)
XY=numpy.matrix.transpose(B)
print("\n(B^T)=");
print(XY);
Vs=([[12],[-6],[-8],[0],[0],[0]]);
print("\nVs=");
print(Vs);
print("\nB.Zb=");
x=numpy.dot(B,Zb);
print(x);
print("\nB.Zb.(B^T)=");
#y=numpy.dot(XY,x);
#print(y);
print("\nB.Vs=");
z=numpy.dot(B,Vs);
print(z);
print("\nLoad currents:");
M=numpy.matrix([[12 ,-2 ,-4],[-2, 12, -6],[-4, -6, 12]]);
H=numpy.linalg.inv(M);
N=([[12],[-6],[-8]]);
X=numpy.dot(H,N);
print(X);
print("\nIl1=0.55 A \nIl2=-0.866 A \nIl3=-0.916 A");
##Graph Theory : example 7.19
import math
import numpy
from numpy import linalg
Q=numpy.matrix([[1, -1, 0, 0],[0 ,-1 ,1 ,1]]);
print("\nQ=");
print(Q);
print("\nThe KCL equation in matrix form is given by");
print("\nQ.Yb.(Q^T).Vl=Q.Is-Q.Yb.Vs");
print("\nQ.Yb.(Q^T).Vl=Q.Is");##Vs=0
Yb=numpy.array([5,5,5,10]);
d=numpy.diag(Yb)
Is=numpy.matrix([[-10],[0],[0],[0]]);
print("\nYb=");
print(d);
print("\n(Q^T)=");
ZZ=numpy.matrix.transpose(Q)
print(ZZ);
print("\nIs=");
print(Is);##current entering into nodes is taken as negative
x=numpy.dot(Q,Yb);
print("\nQ.Yb=");
print(x);
#y=numpy.dot(x[0],ZZ);
print("\nQ.Yb.(Q^T)=");
#print(y);
z=numpy.dot(Q,Is);
print("\nQ.Is=");
print(z);
print("\nLoad voltages:");
M=numpy.matrix([[10, 5],[5, 20]]);
P=numpy.linalg.inv(M);
N=numpy.matrix([[-10],[0]]);
X=numpy.dot(P,N);
print(X);
print("\nvl1=-1.14 V \nvl2=0.28 V");
##Graph Theory : example 7.20 :(pg 7.35 & 7.36)
import numpy
from numpy import linalg
print("\nf-cutset1={1,4,5,6} \nf-cutset2={2,4,5} \nf-cutset3={3,4,6}");
Q=numpy.matrix([[1, 0 ,0 ,-1, -1, 1],[0 ,1 ,0 ,-1, -1 ,0],[0 ,0, 1, -1 ,0 ,1]]);
print("\nQ=");
print(Q);
print("\nThe KCL equation in matrix form is given by");
print("\nQ.Yb.(Q^T).Vl=Q.Is-Q.Yb.Vs");
print("\nQ.Yb.(Q^T).Vl=Q.Is");##Is=0
Yb=numpy.array([0.2,0.2,0.2,0.1,0.5,0.1]);
yz=numpy.diag(Yb)
Vs=numpy.matrix([[910],[0],[0],[0],[0],[0]]);
Is=numpy.matrix([[0],[0],[0],[0],[0],[0]]);
print("\nyz=");
print(yz);
print("\nVs=");
print(Vs);
print("\nIs=");
print(Is);
x=numpy.dot(Q,yz);
print("\nQ.Yb=");
print(x);
z=numpy.dot(x,Vs);
print("\nQ.Yb.Vs=");
print(z);
print("\nQ.Is=");
u=numpy.dot(Q,Is);
print(u);
v=(u-z);
print("\nQ.Is-Q.Yb.Vs=");
print(v);
print("\nLoad voltages:");
M=([[0.9, 0.6, 0.2],[0.6, 0.8, 0.1],[0.2 ,0.1 ,0.3]]);
P=numpy.linalg.inv(M);
N=([[-182],[0],[0]]);
X=numpy.dot(P,N);
print(X);
print("\nvl1=-460 V \nvl2=320 V \nvl3=200 V");
##Graph Theory : example 7.21 :(pg 7.38 & 7.39)
import numpy
from numpy import linalg
print("\ntwigs={1,2} \nf-cutset1={1,4} \nf-cutset2={2,3}");
Q=numpy.matrix([[1, 0, 0 ,-1],[0 ,1 ,-1, 0]]);
print("\nQ=");
print(Q);
print("\nThe KCL equation in matrix form is given by");
print("\nQ.Yb.(Q^T).Vl=Q.Is-Q.Yb.Vs");
ZZ=numpy.matrix.transpose(Q)
yxx=numpy.array([0.25,0.5,0.25,0.5]);
Yb=numpy.diag(yxx)
Vs=numpy.matrix([[1],[1],[0],[0]]);
Is=numpy.matrix([[0],[0],[0.5],[-0.5]]);
print("\nYb=");
print(Yb);
print("\n(Q^T)=");
print(ZZ);
print("\nVs=");
print(Vs);
print("\nIs=");
print(Is);
x=numpy.dot(Q,Yb);
print("\nQ.Yb=");
print(x);
y=numpy.dot(x,ZZ);
print("\nQ.Yb.(Q^T)=");
print(y);
print("\nQ.Is=");
u=numpy.dot(Q,Is);
print(Q*Is);
z=numpy.dot(x,Vs);
print("\nQ.Yb.Vs=");
print(z);
v=(u-z);
print("\nQ.Is-Q.Yb.Vs=");
print(v);
print("\nLoad voltages:");
M=([[0.75, 0],[0 ,0.75]]);
P=numpy.linalg.inv(M);
N=([[0.25],[-1]]);
X=numpy.dot(P,N);
print(X);
print("\nvl1=0.33 V \nvl2=-1.33 V");
vl2=-1.33;
v=1.+vl2;
print"%s %.2f %s"%("\nV=",v,"");