# control symmetry property when exporting matrix as Matrix Market format .mtx

For example, I have

h = SparseArray@{{0, I}, {-I, 0}}


If I export h as matrix market format(.mtx) like

ExportString[ h, "MTX"]


I got

%%MatrixMarket matrix coordinate complex skew-symmetric

%Created with the Wolfram Language : www.wolfram.com

2 2 1

2 1    0.0000000000000000E+00   -1.0000000000000000E+00


It automatically recognize the symmetry property of h, that is fantastic!! However, I don't want this. I want a general coordinate format, like this

%%MatrixMarket matrix coordinate complex general

%Created with the Wolfram Language : www.wolfram.com

2 2 2

1 2    0.0000000000000000E+00    1.0000000000000000E+00

2 1    0.0000000000000000E+00   -1.0000000000000000E+00


On the page of mtx doc, there is very limited documentation, though it says

Export["file.mtx",expr,elem] creates a Matrix Market file by treating expr as specifying element elem.

I don't know what does it mean by "specifying element elem". And it also says

Export["file.mtx",expr,Subscript[opt, 1]->Subscript[val, 1],[Ellipsis]] exports expr with the specified option elements taken to have the specified values.

What kind of option is supported?

One the page of matrix market format, it says

Several instances of each of these basic formats are defined. These are obtained by specifying an arithmetic field for the matrix entries (i.e., real, complex, integer, pattern) and a symmetry structure which may reduce the size of the data file (i.e. general, symmetric, skew-symmetric, Hermitian) by storing nonzero entries only on or below the main diagonal.

So is it possible control symmetry property when exporting matrix as Matrix Market format .mtx in Mathematica? Or more specifically, just force .mtx output general coordinate format without any symmetry?

Just set the option "MatrixStructure" to "General":

ExportString[h, "MTX", "MatrixStructure" -> "General"]


%%MatrixMarket matrix coordinate complex general
%Created with the Wolfram Language : www.wolfram.com 2 2 2
1 2 0.0000000000000000E+00 1.0000000000000000E+00
2 1 0.0000000000000000E+00 -1.0000000000000000E+00

Other possible values are "Hermitian", "SkewSymmetric", and "Symmetric".

• Admittedly, Mathematica's file format documentations are written in a rather confusing way, being quite different to the rest of the documentation. In the case of the MTX documentation, just replace Meta-information elements by Option. (I did not know this before; I just gave it a try.) – Henrik Schumacher Apr 6 '18 at 7:32
• For converting a matrix A into a pattern array, you can use SparseArray @@ {Automatic, Dimensions[A], A["Background"], {1, {A["RowPointers"], A["ColumnIndices"]}, ConstantArray[Pattern, A["RowPointers"][[-1]]]}}. I am not sure what you mean by "representation". If you mean converting between the matrix market "Coordinate Format" and the "Array Format": Just apply Normal and SparseArray before export. – Henrik Schumacher Apr 6 '18 at 7:42
• Oh, my god. I found that though I read the pages for 10 times, but I didn't notice there is an "Elements" cell in the doc!! Maybe it is too similar to "Example", what a shame for me : ( – matheorem Apr 6 '18 at 7:43
• Well, for pattern array, actually you could use A["PatternArray"] – matheorem Apr 6 '18 at 7:46
• Oh, my god. I found that though I read the A["Properties"] for 10 times, but I didn't notice there is an "PatternArray"!! What a shame for me ;) – Henrik Schumacher Apr 6 '18 at 7:47