Quantcast
Channel: ASKSAGE: Sage Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 4

Answer by slelievre for 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).

$
0
0
The [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 '' 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()))

Viewing all articles
Browse latest Browse all 4

Trending Articles