bundles / scipy 1.18.0 / scipy / io / _fast_matrix_market / mmwrite
function
scipy.io._fast_matrix_market:mmwrite
Signature
def mmwrite(target, a, comment = None, field = None, precision = None, symmetry = AUTO)Summary
Writes the sparse or dense array a to Matrix Market file-like target.
Parameters
target: str or file-likeMatrix Market filename (extension .mtx) or open file-like object.
a: array likeSparse or dense 2-D array.
comment: str, optionalComments to be prepended to the Matrix Market file.
field: None or str, optionalEither 'real', 'complex', 'pattern', or 'integer'.
precision: None or int, optionalNumber of digits to display for real or complex values.
symmetry: None or str, optionalEither 'AUTO', 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. If symmetry is None the symmetry type of 'a' is determined by its values. If symmetry is 'AUTO' the symmetry type of 'a' is either determined or set to 'general', at mmwrite's discretion.
Notes
Examples
from io import BytesIO import numpy as np from scipy.sparse import coo_array from scipy.io import mmwrite✓
a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]]) target = BytesIO() mmwrite(target, a)✓
print(target.getvalue().decode('latin1'))
✗%%MatrixMarket matrix array real general % 2 4 1 0 0 2.5 0 0 0 6.25Add a comment to the output file, and set the precision to 3.
target = BytesIO() mmwrite(target, a, comment='\n Some test data.\n', precision=3)✓
print(target.getvalue().decode('latin1'))
✗%%MatrixMarket matrix array real general % % Some test data. % 2 4 1.00e+00 0.00e+00 0.00e+00 2.50e+00 0.00e+00 0.00e+00 0.00e+00 6.25e+00Convert to a sparse matrix before calling ``mmwrite``. This will result in the output format being ``'coordinate'`` rather than ``'array'``.
target = BytesIO() mmwrite(target, coo_array(a), precision=3)✓
print(target.getvalue().decode('latin1'))
✗%%MatrixMarket matrix coordinate real general % 2 4 3 1 1 1.00e+00 2 2 2.50e+00 2 4 6.25e+00Write a complex Hermitian array to a matrix market file. Note that only six values are actually written to the file; the other values are implied by the symmetry.
z = np.array([[3, 1+2j, 4-3j], [1-2j, 1, -5j], [4+3j, 5j, 2.5]]) z✓
array([[ 3. +0.j, 1. +2.j, 4. -3.j],
[ 1. -2.j, 1. +0.j, -0. -5.j],
[ 4. +3.j, 0. +5.j, 2.5+0.j]])
target = BytesIO() mmwrite(target, z, precision=2)✓
print(target.getvalue().decode('latin1'))
✗%%MatrixMarket matrix array complex hermitian % 3 3 3.0e+00 0.0e+00 1.0e+00 -2.0e+00 4.0e+00 3.0e+00 1.0e+00 0.0e+00 0.0e+00 5.0e+00 2.5e+00 0.0e+00This method is threaded. The default number of threads is equal to the number of CPUs in the system. Use `threadpoolctl <https://github.com/joblib/threadpoolctl>`_ to override:
import threadpoolctl
⚠Traceback (most recent call last):
File "<doctest scipy.io._fast_matrix_market:mmwrite[0]>", line 1, in <module>
import threadpoolctl
ModuleNotFoundError: No module named 'threadpoolctl'
target = BytesIO()
✓with threadpoolctl.threadpool_limits(limits=2): mmwrite(target, a)⚠
Traceback (most recent call last):
File "<doctest scipy.io._fast_matrix_market:mmwrite[0]>", line 1, in <module>
with threadpoolctl.threadpool_limits(limits=2):
^^^^^^^^^^^^^
NameError: name 'threadpoolctl' is not defined
Aliases
scipy.io.mmwrite