ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 28 Aug 2019 06:40:55 -0500Given a matrix $M$ how to form the following matrix $N$ from $M$.http://ask.sagemath.org/question/47642/given-a-matrix-m-how-to-form-the-following-matrix-n-from-m/> Given a matrix $M$ how to form the following matrix $N$ from $M$.
Suppose $M=$\begin{bmatrix} 0 &3 \\ 2 &0 \end{bmatrix}
Here $M$ is a $2\times 2$ matrix with 1st row $[0,3]$ and 2nd row $[2,0]$.
We need to form $N$ such that
$N=$ \begin{bmatrix} 3& 3\\ 2&2\end{bmatrix}
Here $N$ is a $2\times 2$ matrix with 1st row $[3,3]$ and 2nd row $[2,2]$.
Thus $N$ is formed from $M$ by just adding all the off the diagonal elements of $M$ in a given row to the diagonal element
So the diagonal element of $N$ is the sum of all the remaining entries in a given row of $M$ whereas the rest of the elements of $N$ are the same as $M$.
So the elements of $N$ are obtained from $M$ in the following way :
$a_{11}=0+3=3, a_{12}=3,a_{21}=2,a_{22}=2+0=2$
How to code it?
Please help.CaptchaWed, 28 Aug 2019 06:40:55 -0500http://ask.sagemath.org/question/47642/Why can't I find the spectral radius of a tree?http://ask.sagemath.org/question/43496/why-cant-i-find-the-spectral-radius-of-a-tree/ If I create some connected graphs, and ask SageMath for their spectral radius, the command never returns (well, I have only let it run for a few minutes) if the target is a tree.
Here is a simple example:
g = graphs.CompleteBipartiteGraph(1,3)
print g.spectrum()
print g.spectral_radius()
The only advertised requirement for the target is that it be strongly connected, but as it is a connected graph, this should automatically be satisfied.
Yes, I can work around by computing the spectrum and taking the biggest, but I'd still like to know why this does not work.GordonMon, 27 Aug 2018 22:23:37 -0500http://ask.sagemath.org/question/43496/Traceback errorhttp://ask.sagemath.org/question/33456/traceback-error/ Hello,
I am new to Sage Math and new to this forum. Basically, I have written code to generate a specific graph on 10 vertices from its adjacency matrix. I am receiving an error (pasted below), but when my advisor runs the same exact code on his machine, he does not receive an error. I do not know how to resolve this situation. I've pasted my code below, and the error that I receive follows. Any insight would be much appreciated, and let me know if you need more details (e.g., information about the machine I'm using, operating system, etc.).
sage: M = Matrix([(0,0,1,0,1,1,1,0,1,1), (0,0,0,1,1,1,0,1,1,1), \
(1,0,0,1,1,1,1,0,0,0), (0,1,1,0,1,1,0,1,0,0), (1,1,1,1,0,1,0,0,1,0), \
(1,1,1,1,1,0,0,0,0,1), (1,0,1,0,0,0,0,1,1,1), (0,1,0,1,0,0,1,0,1,1), \
(1,1,0,0,1,0,1,1,0,1), (1,1,0,0,0,1,1,1,1,0)])
sage: M
[0 0 1 0 1 1 1 0 1 1]
[0 0 0 1 1 1 0 1 1 1]
[1 0 0 1 1 1 1 0 0 0]
[0 1 1 0 1 1 0 1 0 0]
[1 1 1 1 0 1 0 0 1 0]
[1 1 1 1 1 0 0 0 0 1]
[1 0 1 0 0 0 0 1 1 1]
[0 1 0 1 0 0 1 0 1 1]
[1 1 0 0 1 0 1 1 0 1]
[1 1 0 0 0 1 1 1 1 0]
sage: G = Graph(M); G
Graph on 10 vertices
sage: G.plot().show()
Here is the error.
Traceback (most recent call last): [0 0 0 1 1 1 0 1 1 1]
File "", line 1, in <module>
File "/private/var/folders/sw/0pqlf1452k58z_jbk3_yv5m97h2tyd/T/tmpabbyzH/___code___.py", line 5
G = Graph(M); G
^
SyntaxError: invalid syntax
JEAThu, 19 May 2016 14:56:50 -0500http://ask.sagemath.org/question/33456/How to make a graph from an latin square matrix ?http://ask.sagemath.org/question/31564/how-to-make-a-graph-from-an-latin-square-matrix/As the question says I want to write a program that will make a graph from the given matrix ?
How can I do that in Sage?vicariousMon, 14 Dec 2015 13:20:10 -0600http://ask.sagemath.org/question/31564/is it a sparse matrix or dense matrix?http://ask.sagemath.org/question/10554/is-it-a-sparse-matrix-or-dense-matrix/if the number of non zero elements in a matrix is equal to the number of zeros than what should we call this matrix -- a sparse matrix or a dense matrix?radhika agrawalFri, 20 Sep 2013 03:52:26 -0500http://ask.sagemath.org/question/10554/NetworkX - create a Graphhttp://ask.sagemath.org/question/10514/networkx-create-a-graph/Hi experts!
I wanna use
networkx.has_path(G)
where G is the graph associated to de adjacency matrix M (a N x N numpy array) of a undirected graph.
How can I create a networkx graph (necesary for use networkx.has_path()) from M?
Thanks a lot!mresimulatorThu, 05 Sep 2013 13:11:59 -0500http://ask.sagemath.org/question/10514/nilpotent adjacency matrixhttp://ask.sagemath.org/question/9870/nilpotent-adjacency-matrix/i wish to define a nilpotent adjacency matrix.
example vertex adjacency matrix of a graph ($K_4$ minus an edge) is
A=
[0 1 1 0]
[1 0 1 1]
[1 1 0 1]
[0 1 1 0]
where N=4 vertices
so for all entries $A_{ij}$ i wish to define a function to replace the 1's by $b_j$
so for the example above i get a nilpotent matrix
B=
[0 $b_2$ $b_3$ 0]
[$b_1$ 0 $b_3$ $b_4$]
[$b_1$ $b_2$ 0 1]
[0 $b_2$ $b_3$ 0]
where the $b_j$ where $i,j\in${1,2,3,4} obey the following rules of multiplication:
$b_jb_i=b_ib_j$ and $b_j^2=0$ (so i also need to define a function for these rules)
so for the nilpotent adjacency matrix i can define matrix multiplication using the above rules for its entries i.e. $B^N$
i'd like the function to be something like nil(B,k)
and for it to print the trace of $B^k$
e.g. $nil(B,2)=2b_1b_2+2b_1b_3+2b_2b_3+2b_2b_4+2b_3b_4$
i'll try to work on this myself too in the meantime, but this is probably the most complicated function i've had to do.. mainly due to redefining the adjacency matrixjtaaSun, 03 Mar 2013 01:59:48 -0600http://ask.sagemath.org/question/9870/plot digraph use adjacency matrixhttp://ask.sagemath.org/question/9775/plot-digraph-use-adjacency-matrix/Hi everyone, I got a question here, and I have been trying to debug it for past week, still can't get it sorted out, can anyone please help me? Thanks heaps.
I have a file as my data (will paste at the bottom), basically the vertices are all binary strings with directed edges named "a" or "b", or "c". For example, if in my data, there is line says (0000, a, 1000), that means there is an edge from vertex 0000 to 1000 named a. Similarly, if I have (0000, b, 0000), means there is a self-loop from 0000 to it self named b. So there can be more than one self-loops sometimes.
Now I have written the function digraph as below:
def digraph(n, filename):
f = open(filename, 'r')
W = Words('01', length=n)
S = [str(w) for w in W]
M = zero_matrix(ZZ, 2^n)
f.readline()
for c in f:
c = c[1:-2]
s1, g, s2 = c.split(', ')
i = S.index('word: ' + s1)
j = S.index('word: ' + s2)
M[i,j] = 1
f.close()
return DiGraph(M, vertex_labels=S, loops = True, implementation="c_graph")
So the idea should be pretty simple, create list of words with all binary strings length 4, then search where my data the words are allocated in the list, then assign value to 1 to the entries of my original zero matrix.
But I keep getting error message says change a copy of the matrix instead, I tried to make a copy(M), but still doesn't work, then it says something else like "need more than 1 value unpack" etc.
I really need a hand on this please, can anybody help me please? Also, I can't understand why it is exactly same code, but used to run very well on someone else's linux computer two months ago, and I didn't change anything but it just wouldn't run here and the error messages seem like syntax error.
Here are my data as below
(0000, a, 1000)
(0000, b, 0000)
(0000, c, 0000)
(0001, a, 1001)
(0001, b, 0001)
(0001, c, 0001)
(0010, a, 1010)
(0010, b, 0010)
(0010, c, 0010)
(0011, a, 1011)
(0011, b, 0011)
(0011, c, 0011)
(0100, a, 1110)
(0100, b, 0100)
(0100, c, 0100)
(0101, a, 1111)
(0101, b, 0101)
(0101, c, 0101)
(0110, a, 1100)
(0110, b, 0111)
(0110, c, 0111)
(0111, a, 1101)
(0111, b, 0110)
(0111, c, 0110)
(1000, a, 0000)
(1000, b, 1000)
(1000, c, 1100)
(1001, a, 0001)
(1001, b, 1001)
(1001, c, 1101)
(1010, a, 0010)
(1010, b, 1010)
(1010, c, 1111)
(1011, a, 0011)
(1011, b, 1011)
(1011, c, 1110)
(1100, a, 0110)
(1100, b, 1110)
(1100, c, 1000)
(1101, a, 0111)
(1101, b, 1111)
(1101, c, 1001)
(1110, a, 0100)
(1110, b, 1100)
(1110, c, 1011)
(1111, a, 0101)
(1111, b, 1101)
(1111, c, 1010)
tsangTue, 05 Feb 2013 17:47:34 -0600http://ask.sagemath.org/question/9775/adjacency matrix importhttp://ask.sagemath.org/question/9473/adjacency-matrix-import/I am working on a social network analysis of sustainability staff at a large research university in the mid-west. I would like to keep track of my networks using a spreadsheet which will include information about connections (undirected or directed graphs) and information about the nodes (e.g. labels and groups).
Has anyone built scripts that will import generic csv files (or even better .xls or .numbers files)?
if I am being incredibly naive, please point me to the appropriate enlightenment.
thanks,
Lewis E GilbertLewisEGilbertFri, 26 Oct 2012 16:16:58 -0500http://ask.sagemath.org/question/9473/Editing entries of matrixhttp://ask.sagemath.org/question/8366/editing-entries-of-matrix/I'm looking at a matrix, say adjacency matrix of a graph, g.am(). I want to change some of the entries now. I am told it's a vector and it's immutable. What can I do?
One thought I had is adding some other matrix to it with only one nonblank entry. But, is there some easy way to do that?
Another thought I had was turning it into a list, list(g.am()). Maybe that's acceptable. I'm not 100% sure yet for what I'm working on. If I go that way, is there a way to print it in a matrix form again, i.e., entries print in a square shape.
Thanks for any helpG-SageSun, 09 Oct 2011 09:15:34 -0500http://ask.sagemath.org/question/8366/How to recognise DiGraph equivalencehttp://ask.sagemath.org/question/8348/how-to-recognise-digraph-equivalence/Hi -
I am trying to check that a "manual" calculation I did on posets is correct.
my starting point is a Matlab-generated upper-triangular 60x60 adjacency matrix M representing a DiGraph G or P=Poset(G); I have generated a minimal (ie covering relations only) 60x60 adjacency matrix from it in two ways. First by letting SAGE reduce it using PP=P.cover_relations(), and the other by reducing M "manually" in Matlab, then exporting that matrix Q to SAGE and using the same function on it to get it into the same format. I would like to show PP=Q in some sense.
I tried to check equality (==) between the graph objects, and the posets, and the graphs/posets obtained by re-setting the output of the cover_relations function to be a DiGraph all over again, and even setting those things to be adjacency matrices ... but even though I have laboriously checked that the 2 objects ARE indeed the same (by comparing the edges one-by-one), I nevertheless cannot get SAGE to agree!!
The output of the cover_relations function seems to be the sticking point - it is a sort-of matrix but not with any consistent ordering of the edges - hence even though the sets of edges are clearly the same, the different ordering seems to throw the comparison function off.
What am I missing please?
ThanksGaryMakTue, 27 Sep 2011 07:35:00 -0500http://ask.sagemath.org/question/8348/