ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 11 Jul 2018 17:03:18 -0500How can I generate a graph from an .mtx file?http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/The .mtx file contains the information of an sparse matrix in matrix market format. How can I generate the underlying graph of this matrix in SAGE?
Alternatively, since I now how to generate the .mat file from .mtx file in MATLAB, how can I generate the graph from .mat file in SAGE?
The matrix in .mtx in format is the adjacency matrix of the graph. The problem is I cannot read the matrix and generate the underlying graph in SAGE 8.2 (Jupyter).Sun, 08 Jul 2018 23:13:44 -0500http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/Comment by slelievre for <p>The .mtx file contains the information of an sparse matrix in matrix market format. How can I generate the underlying graph of this matrix in SAGE?
Alternatively, since I now how to generate the .mat file from .mtx file in MATLAB, how can I generate the graph from .mat file in SAGE?
The matrix in .mtx in format is the adjacency matrix of the graph. The problem is I cannot read the matrix and generate the underlying graph in SAGE 8.2 (Jupyter).</p>
http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?comment=42936#post-id-42936Also, please write Sage rather than SAGE. See this wiki page:
- [https://wiki.sagemath.org/FromSageToSageMath](https://wiki.sagemath.org/FromSageToSageMath)Wed, 11 Jul 2018 17:03:18 -0500http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?comment=42936#post-id-42936Comment by ASH for <p>The .mtx file contains the information of an sparse matrix in matrix market format. How can I generate the underlying graph of this matrix in SAGE?
Alternatively, since I now how to generate the .mat file from .mtx file in MATLAB, how can I generate the graph from .mat file in SAGE?
The matrix in .mtx in format is the adjacency matrix of the graph. The problem is I cannot read the matrix and generate the underlying graph in SAGE 8.2 (Jupyter).</p>
http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?comment=42875#post-id-42875I forgot to mention that the matrix in .mtx in format is the adjacency matrix of the graph. The problem is I cannot read the matrix and generate the underlying graph in SAGE 8.2 (Jupyter).Mon, 09 Jul 2018 20:23:59 -0500http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?comment=42875#post-id-42875Answer by slelievre for <p>The .mtx file contains the information of an sparse matrix in matrix market format. How can I generate the underlying graph of this matrix in SAGE?
Alternatively, since I now how to generate the .mat file from .mtx file in MATLAB, how can I generate the graph from .mat file in SAGE?
The matrix in .mtx in format is the adjacency matrix of the graph. The problem is I cannot read the matrix and generate the underlying graph in SAGE 8.2 (Jupyter).</p>
http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?answer=42887#post-id-42887The [Matrix Market](https://math.nist.gov/MatrixMarket/) is
> a visual repository of test data for use in comparative studies of algorithms
> for numerical linear algebra, featuring nearly 500 sparse matrices
> from a variety of applications, as well as matrix generation tools and services
proposed by the National Institute of Standards and Technology (NIST),
a branch of the US Department of Commerce. As the [Matrix Market info page](https://math.nist.gov/MatrixMarket/info.html) puts it,
> The Matrix Market is a component of the NIST project on
> [Tools for Evaluation of Mathematical and Statistical Software](https://math.nist.gov/temss/)
> which has focus areas in linear algebra, special functions and statistics.
The `.mtx` Matrix Market Exchange format is documented on the
[NIST page on Matrix Market Exchange formats](https://math.nist.gov/MatrixMarket/formats.html).
Having saved the following into a file called `adjacency.mtx`:
%%MatrixMarket matrix coordinate real symmetric
%
% adjacency matrix for a graph
%
5 5 6
1 2 1.000e+00
1 3 1.000e+00
1 5 1.000e+00
2 4 1.000e+00
3 4 1.000e+00
4 5 1.000e+00
one can use input-output functionality in SciPy to read that file
into a SciPy sparse matrix:
sage: from scipy.io import mmread
sage: a = mmread('adjacency.mtx')
which is represented as follows:
sage: a
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 12 stored elements in COOrdinate format>
and, if one want the list of its entries, prints out as follows:
sage: print(a)
(0, 1) 1.0
(0, 2) 1.0
(0, 4) 1.0
(1, 3) 1.0
(2, 3) 1.0
(3, 4) 1.0
(1, 0) 1.0
(2, 0) 1.0
(4, 0) 1.0
(3, 1) 1.0
(3, 2) 1.0
(4, 3) 1.0
One can convert it to a SciPy dense matrix:
sage: b = a.todense()
sage: b
matrix([[ 0., 1., 1., 0., 1.],
[ 1., 0., 0., 1., 0.],
[ 1., 0., 0., 1., 0.],
[ 0., 1., 1., 0., 1.],
[ 1., 0., 0., 1., 0.]])
and then convert that to a Sage matrix:
sage: c = matrix(b)
sage: c
[0.0 1.0 1.0 0.0 1.0]
[1.0 0.0 0.0 1.0 0.0]
[1.0 0.0 0.0 1.0 0.0]
[0.0 1.0 1.0 0.0 1.0]
[1.0 0.0 0.0 1.0 0.0]
from which one can create a graph:
sage: g = Graph(c)
sage: g
Graph on 5 vertices
sage: g.plot()
Launched png viewer for Graphics object consisting of 12 graphics primitives
Alternatively, to call Octave or MATLAB from SageMath, one can use the
corresponding [interfaces](https://wiki.sagemath.org/Interfaces):
- [SageMath interface to Octave](http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/octave.html)
- [SageMath interface to MATLAB](http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/matlab.html)
but for the needs of this question, that should not be necessary.
In summary:
sage: from scipy.io import mmread
sage: g = Graph(matrix(mmread('adjacency.mtx').todense()))
Tue, 10 Jul 2018 08:18:34 -0500http://ask.sagemath.org/question/42866/how-can-i-generate-a-graph-from-an-mtx-file/?answer=42887#post-id-42887