B
    [                 @   s   d dl mZmZ d dlmZmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ dd	 Zeee< G d
d deeZeje_eZG dd deeZdS )    )print_functiondivision)BasicIntegerTupleDictSsympify)	converter)
MatrixBase)DenseMatrix)SparseMatrixMutableSparseMatrix)
MatrixExprc             C   s   |   S )N)Zas_immutable)arg r   7lib/python3.7/site-packages/sympy/matrices/immutable.pysympify_matrix   s    r   c               @   s   e Zd ZdZdZdZdZdd Zej	Z	e
dd Zed	d
 Zdd Zdd Zdd Zdd Zedd Zedd Zedd ZdS )ImmutableDenseMatrixar  Create an immutable version of a matrix.

    Examples
    ========

    >>> from sympy import eye
    >>> from sympy.matrices import ImmutableMatrix
    >>> ImmutableMatrix(eye(3))
    Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]])
    >>> _[0, 0] = 42
    Traceback (most recent call last):
    ...
    TypeError: Cannot set values of ImmutableDenseMatrix
    T   gn $@c             O   s   | j ||S )N)_new)clsargskwargsr   r   r   __new__)   s    zImmutableDenseMatrix.__new__c             O   s   t |dkr"t|d tr"|d S |dddkrRt |dkrFtd|\}}}n| j||\}}}t|}t|}t|}t|tst| }t	
| |||S )N   r   copyTF   zA'copy=False' requires a matrix be initialized as rows,cols,[list])len
isinstancer   get	TypeErrorZ_handle_creation_inputslistr   r   r   r   )r   r   r   rowscolsZ	flat_listr   r   r   r   .   s    
zImmutableDenseMatrix._newc             C   s   | j d j S )N   )r   )selfr   r   r   _mat@   s    zImmutableDenseMatrix._matc             C   s   t | ||fS )N)r   __getitem__)r&   ijr   r   r   _entryG   s    zImmutableDenseMatrix._entryc             G   s   t d| jd S )NzCannot set values of {})r!   format	__class__)r&   r   r   r   r   __setitem__J   s    z ImmutableDenseMatrix.__setitem__c             C   sF   t |dr| j|jkrtjS t|tr4t|ts4dS | | }t|jS )a  Helper method for Equality with matrices.

        Relational automatically converts matrices to ImmutableDenseMatrix
        instances, so this method only applies here.  Returns True if the
        matrices are definitively the same, False if they are definitively
        different, and None if undetermined (e.g. if they contain Symbols).
        Returning None triggers default handling of Equalities.

        shapeN)	hasattrr/   r   Zfalser   r   r   r	   is_zero)r&   otherZdiffr   r   r   _eval_EqM   s    
zImmutableDenseMatrix._eval_Eqc                sR   | j | j  fdd|D }| jt|ttfdd|D ddiddS )Nc             3   s$   | ]}D ]}|  | V  q
qd S )Nr   ).0r)   r*   )r$   colsListr   r   	<genexpr>d   s    z5ImmutableDenseMatrix._eval_extract.<locals>.<genexpr>c             3   s   | ]} | V  qd S )Nr   )r4   r)   )matr   r   r6   f   s    r	   F)r   )r'   r$   r   r   r   )r&   ZrowsListr5   indicesr   )r$   r5   r7   r   _eval_extract_   s
    z"ImmutableDenseMatrix._eval_extractc             C   s   t | jd S )Nr   )intr   )r&   r   r   r   r$   h   s    zImmutableDenseMatrix.colsc             C   s   t | jd S )Nr   )r:   r   )r&   r   r   r   r#   l   s    zImmutableDenseMatrix.rowsc             C   s   t dd | jd d D S )Nc             s   s   | ]}t |V  qd S )N)r:   )r4   r)   r   r   r   r6   r   s    z-ImmutableDenseMatrix.shape.<locals>.<genexpr>r%   )tupler   )r&   r   r   r   r/   p   s    zImmutableDenseMatrix.shapeN)__name__
__module____qualname____doc__Z	_iterable_class_priorityZ_op_priorityr   r   __hash__classmethodr   propertyr'   r+   r.   r3   r9   r$   r#   r/   r   r   r   r   r      s   	r   c               @   sB   e Zd ZdZdZdZedd Zdd Zdd	 Z	d
d Z
ejZdS )ImmutableSparseMatrixa  Create an immutable version of a sparse matrix.

    Examples
    ========

    >>> from sympy import eye
    >>> from sympy.matrices.immutable import ImmutableSparseMatrix
    >>> ImmutableSparseMatrix(1, 1, {})
    Matrix([[0]])
    >>> ImmutableSparseMatrix(eye(3))
    Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]])
    >>> _[0, 0] = 42
    Traceback (most recent call last):
    ...
    TypeError: Cannot set values of ImmutableSparseMatrix
    >>> _.shape
    (3, 3)
    T	   c             O   sR   t | }t|j}t|j}t|j}t| |||}|j|_|j|_|j|_|S )N)r   r   r#   r$   r   _smatr   r   )r   r   r   sr#   r$   r7   objr   r   r   r      s    


zImmutableSparseMatrix._newc             O   s   | j ||S )N)r   )r   r   r   r   r   r   r      s    zImmutableSparseMatrix.__new__c             G   s   t dd S )Nz*Cannot set values of ImmutableSparseMatrix)r!   )r&   r   r   r   r   r.      s    z!ImmutableSparseMatrix.__setitem__c             C   s    t t| jf| jt| jf S )N)hashtyper<   r/   r;   rF   )r&   r   r   r   rA      s    zImmutableSparseMatrix.__hash__N)r<   r=   r>   r?   Z	is_Matrixr@   rB   r   r   r.   rA   r   r3   r   r   r   r   rD   ~   s   rD   N)Z
__future__r   r   Z
sympy.corer   r   r   r   r   r	   Zsympy.core.sympifyr
   Zsympify_converterZsympy.matrices.matricesr   Zsympy.matrices.denser   Zsympy.matrices.sparser   r   Zsympy.matrices.expressionsr   r   r   r1   ZImmutableMatrixrD   r   r   r   r   <module>   s    h