%PDF- %PDF-
| Direktori : /lib64/python3.9/site-packages/numpy/core/__pycache__/ |
| Current File : //lib64/python3.9/site-packages/numpy/core/__pycache__/einsumfunc.cpython-39.pyc |
a
z[yc�� � @ s� d Z ddlZddlZddlmZ ddlmZmZ ddlm Z ddgZ
dZee�Z
d d
� Zdd� Zd
d� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zddd�dd�Ze edd�d d!d�d"d��Zddd#�d$d%�Ze edd�dd!d#�d&d��ZdS )'z&
Implementation of optimized einsum.
� N)�c_einsum)�
asanyarray� tensordot)�array_function_dispatch�einsum�einsum_pathZ4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc C s, t | |�}td|d �}|r$|d7 }|| S )a�
Computes the number of FLOPS in the contraction.
Parameters
----------
idx_contraction : iterable
The indices involved in the contraction
inner : bool
Does this contraction require an inner product?
num_terms : int
The number of terms in a contraction
size_dictionary : dict
The size of each of the indices in idx_contraction
Returns
-------
flop_count : int
The total number of FLOPS required for the contraction.
Examples
--------
>>> _flop_count('abc', False, 1, {'a': 2, 'b':3, 'c':5})
30
>>> _flop_count('abc', True, 2, {'a': 2, 'b':3, 'c':5})
60
� )�_compute_size_by_dict�max)Zidx_contraction�innerZ num_termsZsize_dictionaryZoverall_sizeZ op_factor� r �;/usr/lib64/python3.9/site-packages/numpy/core/einsumfunc.py�_flop_count s
r c C s d}| D ]}||| 9 }q|S )a�
Computes the product of the elements in indices based on the dictionary
idx_dict.
Parameters
----------
indices : iterable
Indices to base the product on.
idx_dict : dictionary
Dictionary of index sizes
Returns
-------
ret : int
The resulting product.
Examples
--------
>>> _compute_size_by_dict('abbc', {'a': 2, 'b':3, 'c':5})
90
r r )�indices�idx_dict�ret�ir r r
r 8 s r c
C sn t � }|�� }g }t|�D ],\}}|| v r4||O }q|�|� ||O }q||@ }|| } |�|� ||| |fS )a
Finds the contraction for a given set of input and output sets.
Parameters
----------
positions : iterable
Integer positions of terms used in the contraction.
input_sets : list
List of sets that represent the lhs side of the einsum subscript
output_set : set
Set that represents the rhs side of the overall einsum subscript
Returns
-------
new_result : set
The indices of the resulting contraction
remaining : list
List of sets that have not been contracted, the new set is appended to
the end of this list
idx_removed : set
Indices removed from the entire contraction
idx_contraction : set
The indices used in the current contraction
Examples
--------
# A simple dot product test case
>>> pos = (0, 1)
>>> isets = [set('ab'), set('bc')]
>>> oset = set('ac')
>>> _find_contraction(pos, isets, oset)
({'a', 'c'}, [{'a', 'c'}], {'b'}, {'a', 'b', 'c'})
# A more complex case with additional terms in the contraction
>>> pos = (0, 2)
>>> isets = [set('abd'), set('ac'), set('bdc')]
>>> oset = set('ac')
>>> _find_contraction(pos, isets, oset)
({'a', 'c'}, [{'a', 'c'}, {'a', 'c'}], {'b', 'd'}, {'a', 'b', 'c', 'd'})
)�set�copy� enumerate�append)
� positions�
input_sets�
output_set�idx_contractZ
idx_remain� remaining�ind�value�
new_result�idx_removedr r r
�_find_contractionU s +
r c C s, dg | fg}t t| �d �D ]�}g }|D ]�}|\}} }
t�t t| �| �d�D ]`}t||
|�}|\}
}}}t|
|�}||kr~qN|t||t|�|� }| |g }|�|||f� qNq(|r�|}qt|dd� d�d }|t t t| �| ��g7 }| S qt|�dk�rt t t| ���gS t|dd� d�d }|S )a�
Computes all possible pair contractions, sieves the results based
on ``memory_limit`` and returns the lowest cost path. This algorithm
scales factorial with respect to the elements in the list ``input_sets``.
Parameters
----------
input_sets : list
List of sets that represent the lhs side of the einsum subscript
output_set : set
Set that represents the rhs side of the overall einsum subscript
idx_dict : dictionary
Dictionary of index sizes
memory_limit : int
The maximum number of elements in a temporary array
Returns
-------
path : list
The optimal contraction order within the memory limit constraint.
Examples
--------
>>> isets = [set('abd'), set('ac'), set('bdc')]
>>> oset = set()
>>> idx_sizes = {'a': 1, 'b':2, 'c':3, 'd':4}
>>> _optimal_path(isets, oset, idx_sizes, 5000)
[(0, 2), (0, 1)]
r r � c S s | d S �Nr r ��xr r r
�<lambda>� � z_optimal_path.<locals>.<lambda>��keyc S s | d S r"