← Back
Editing: _mmio.cpython-311.pyc
� d�c�� � � � d Z ddlZddlZddlmZmZmZmZmZm Z m Z mZmZ ddl mZmZ g d�Zd� Zd� Zd� Zdd �Z G d� d� � Zd � ZdS )z� Matrix Market I/O in Python. See http://math.nist.gov/MatrixMarket/formats.html for information about the Matrix Market format. � N) �asarray�real�imag�conj�zeros�ndarray�concatenate�ones�can_cast)� coo_matrix� isspmatrix)�mminfo�mmread�mmwrite�MMFilec �t � t | t � � r| � d� � S t | � � S )N�latin1)� isinstance�bytes�decode�str)�ss �0/usr/lib/python3/dist-packages/scipy/io/_mmio.py�asstrr s1 � ��!�U��� "��x�x��!�!�!��q�6�6�M� c �6 � t � | � � S )a� Return size and storage parameters from Matrix Market file-like 'source'. Parameters ---------- source : str or file-like Matrix Market filename (extension .mtx) or open file-like object Returns ------- rows : int Number of matrix rows. cols : int Number of matrix columns. entries : int Number of non-zero entries of a sparse matrix or rows*cols for a dense matrix. format : str Either 'coordinate' or 'array'. field : str Either 'real', 'complex', 'pattern', or 'integer'. symmetry : str Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. Examples -------- >>> from io import StringIO >>> from scipy.io import mminfo >>> text = '''%%MatrixMarket matrix coordinate real general ... 5 5 7 ... 2 3 1.0 ... 3 4 2.0 ... 3 5 3.0 ... 4 1 4.0 ... 4 2 5.0 ... 4 3 6.0 ... 4 4 7.0 ... ''' ``mminfo(source)`` returns the number of rows, number of columns, format, field type and symmetry attribute of the source file. >>> mminfo(StringIO(text)) (5, 5, 7, 'coordinate', 'real', 'general') )r �info��sources r r r s � �` �;�;�v���r c �D � t � � � | � � S )aG Reads the contents of a Matrix Market file-like 'source' into a matrix. Parameters ---------- source : str or file-like Matrix Market filename (extensions .mtx, .mtz.gz) or open file-like object. Returns ------- a : ndarray or coo_matrix Dense or sparse matrix depending on the matrix format in the Matrix Market file. Examples -------- >>> from io import StringIO >>> from scipy.io import mmread >>> text = '''%%MatrixMarket matrix coordinate real general ... 5 5 7 ... 2 3 1.0 ... 3 4 2.0 ... 3 5 3.0 ... 4 1 4.0 ... 4 2 5.0 ... 4 3 6.0 ... 4 4 7.0 ... ''' ``mmread(source)`` returns the data as sparse matrix in COO format. >>> m = mmread(StringIO(text)) >>> m <5x5 sparse matrix of type '<class 'numpy.float64'>' with 7 stored elements in COOrdinate format> >>> m.A array([[0., 0., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 2., 3.], [4., 5., 6., 7., 0.], [0., 0., 0., 0., 0.]]) )r �readr s r r r T s � �Z �8�8�=�=�� � � r � c �R � t � � � | |||||� � dS )a� Writes the sparse or dense array `a` to Matrix Market file-like `target`. Parameters ---------- target : str or file-like Matrix Market filename (extension .mtx) or open file-like object. a : array like Sparse or dense 2-D array. comment : str, optional Comments to be prepended to the Matrix Market file. field : None or str, optional Either 'real', 'complex', 'pattern', or 'integer'. precision : None or int, optional Number of digits to display for real or complex values. symmetry : None or str, optional Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'. If symmetry is None the symmetry type of 'a' is determined by its values. Returns ------- None Examples -------- >>> from io import BytesIO >>> import numpy as np >>> from scipy.sparse import coo_matrix >>> from scipy.io import mmwrite Write a small NumPy array to a matrix market file. The file will be written in the ``'array'`` format. >>> 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.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 2.5000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 6.2500000000000000e+00 Add 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.000e+00 0.000e+00 0.000e+00 2.500e+00 0.000e+00 0.000e+00 0.000e+00 6.250e+00 Convert to a sparse matrix before calling ``mmwrite``. This will result in the output format being ``'coordinate'`` rather than ``'array'``. >>> target = BytesIO() >>> mmwrite(target, coo_matrix(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+00 Write 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.00e+00 0.00e+00 1.00e+00 -2.00e+00 4.00e+00 3.00e+00 1.00e+00 0.00e+00 0.00e+00 5.00e+00 2.50e+00 0.00e+00 N)r �write)�target�a�comment�field� precision�symmetrys r r r � s+ � �X �H�H�N�N�6�1�g�u�i��B�B�B�B�Br c � � e Zd ZdZed� � � Zed� � � Zed� � � Zed� � � Zed� � � Z ed� � � Z ed� � � Zd Zd Z ee fZed� � � ZdZd ZdZdZdZeeeeefZed� � � ZdZdZdZdZeeeefZed� � � ZededededediZed� � � Z ed� � � Z!ed� � � Z"ed+d�� � Z#ed � � � Z$ed!� � � Z%d"� Z&d#� Z' d,d&�Z(d'� Z)d(� Z*d)� Z+ d,d*�Z,d%S )-r )�_rows�_cols�_entries�_format�_field� _symmetryc � � | j S �N)r, ��selfs r �rowszMMFile.rows� � � ��z�r c � � | j S r3 )r- r4 s r �colszMMFile.cols r7 r c � � | j S r3 )r. r4 s r �entrieszMMFile.entries s � ��}�r c � � | j S r3 )r/ r4 s r �formatz MMFile.format s � ��|�r c � � | j S r3 )r0 r4 s r r( zMMFile.field s � ��{�r c � � | j S r3 )r1 r4 s r r* zMMFile.symmetry s � ��~�r c �8 � | j | j | j | j fv S r3 )r1 �SYMMETRY_SYMMETRIC�SYMMETRY_SKEW_SYMMETRIC�SYMMETRY_HERMITIANr4 s r �has_symmetryzMMFile.has_symmetry s'