Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168741
Image: ubuntu2004
def sns(a): """ Determines the sns of a sign pattern matrix. INPUT: A square matrix, where each element should only be from the set {-1,0,1}. 1 signifying a positive sign and -1 signifying a negative sign, while 0 will simply signify 0. OUTPUT: Outputs the sns number of the matrix. EXAMPLES:: sage:a = matrix([[1,-1],[-1,1]]) sage:a [ 1 -1] [-1 1] sage:sns(a) 1 """ length = a.ncols(); new_matrix=matrix(length,[var('x%s%s'%(i,j)) for i in range(length) for j in range(length)]) b = matrix(length,[(new_matrix[i,j] * a[i,j]) for i in range(length) for j in range(length)]) for i in range(length,0,-1): for v in CartesianProduct(Subsets(range(length),i),Subsets(range(length),i)): for det in [b[list(v[0]),list(v[1])].det().expand()]: w=[[-1 in term.operands() for term in det.operands()]] if any([not(True in k and False in k) for k in w]): return i
# Make a random -1,0,1 matrix a=random_matrix(ZZ,5,x=-1,y=2) a
[ 0 0 0 0 0] [ 0 1 1 -1 0] [-1 -1 -1 0 -1] [ 1 1 -1 -1 0] [-1 -1 0 -1 0]
sage_input(a)
matrix(ZZ, [[0, 0, 0, 0, 0], [0, 1, 1, -1, 0], [-1, -1, -1, 0, -1], [1, 1, -1, -1, 0], [-1, -1, 0, -1, 0]])
sns(a)
5
g=DiGraph(a)
g.plot(edge_labels=True, save_pos=True)
g.plot(color_by_label=True)
g.weighted_adjacency_matrix()
[ 0 0 0 0 0] [ 0 1 1 -1 0] [-1 -1 -1 0 -1] [ 1 1 -1 -1 0] [-1 -1 0 -1 0]