a
    yn„f~!  ã                   @  sª   d dl mZ d dlmZ d dlmZ d dl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 d	Ze ¡ Ze ¡ Zeƒ d
dddddœddddddœdd„ƒZdS )é    )Úannotations)Úbackend_of_obj)ÚNumpyBackend)Úbehavior_ofÚget_array_classÚget_record_class)Úhigh_level_function)ÚNumpyMetadata)Úparameters_are_equal)Ú	to_layout)Úalmost_equalgñhãˆµøä>g:Œ0âŽyE>T)ÚrtolÚatolÚdtype_exactÚcheck_parametersÚcheck_regularÚfloatÚboolc                #  sº   | |fV  t | ƒ‰t |ƒ‰t| td�}t|td�}||ur>dS |‰t| dd� ¡ }	t|dd� ¡ }
ˆjjsrtdƒ‚ddœ‡fdd„‰d	d
„ ‰ddœ‡ ‡‡‡‡‡‡‡‡	‡
f
dd„‰
ˆ
|	|
ƒS )aŸ  
    Args:
        left: Array-like data (anything #ak.to_layout recognizes).
        right: Array-like data (anything #ak.to_layout recognizes).
        rtol: the relative tolerance parameter (see below).
        atol: the absolute tolerance parameter (see below).
        dtype_exact: whether the dtypes must be exactly the same, or just the
            same family.
        check_parameters: whether to compare parameters.
        check_regular: whether to consider ragged and regular dimensions as
            unequal.

    Return True if the two array-like arguments are considered equal for the
    given options. Otherwise, return False.

    The relative difference (`rtol * abs(b)`) and the absolute difference `atol`
    are added together to compare against the absolute difference between `left`
    and `right`.

    TypeTracer arrays are not supported, as there is very little information to
    be compared.
    )ÚdefaultF)Zallow_recordzVAwkward Arrays with typetracer backends cannot yet be compared with `ak.almost_equal`.r   )Úreturnc                   s:   ˆ s2t jt jfD ] }t  | |¡rt  ||¡  S q| |kS ©N)ÚnpÚintegerZfloatingÚ
issubdtype)ÚleftÚrightÚfamily)r   © úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_almost_equal.pyÚis_approx_dtypeI   s
    z%almost_equal.<locals>.is_approx_dtypec                 S  s$   |   d¡} | j| jd | jd … S )NFr   éÿÿÿÿ)Zto_ListOffsetArray64ÚcontentÚoffsets)Zlayoutr   r   r   Úpacked_list_contentP   s    
z)almost_equal.<locals>.packed_list_contentc           	   	     sœ  ˆ j rˆ jsˆ  ¡ ‰ ˆj r(ˆjs(ˆ ¡ ‰ˆ jr6ˆ  ¡ ‰ ˆjrDˆ ¡ ‰ˆ jr\ˆ jdkr\ˆ  ¡ ‰ ˆjrtˆjdkrtˆ ¡ ‰ˆ jˆjkr„dS ˆrštˆ j	ˆj	ƒsšdS t
ˆ ˆƒt
ˆˆ	ƒu s¶ˆr¶dS ˆ jrÜˆjrÜˆ jˆjkoÚˆˆ jˆjƒS ˆ j�rRˆj�rRˆ j�rˆj�sˆ �oˆˆ jˆˆƒƒS ˆj�r<ˆ j�s<ˆ �o:ˆˆˆ ƒˆjƒS ˆˆˆ ƒˆˆƒƒS �nFˆ j�rˆj�rt ˆ jtj¡�sªt ˆjtj¡�sªt ˆ jtj¡�sªt ˆjtj¡�rÚˆ jˆjk�oØˆj ˆ jˆjk¡�oØˆ jˆjkS ˆˆ jˆjƒ�oˆj ˆjjˆ jˆjˆ
ˆdd�¡�oˆ jˆjkS �nzˆ j�r\ˆj�r\ˆj ˆ  d¡ˆ d¡¡�oZˆˆ  ¡ ˆ ¡ ƒS ˆ j�r
ˆj�r
‡fdd„}|ˆ jjƒ}|ˆjjƒ}ˆjj|jtjd�}|||< |ˆ jj }ˆj |ˆjjk¡�sÒdS t ||ƒD ](\}}ˆˆ  |¡ˆ |¡ƒ�sÜ dS �qÜdS ˆ j!�r€ˆj!�r€t"ˆ ˆƒt"ˆˆ	ƒu �s8ˆ �o~ˆ j#ˆj#k�o~ˆ j#�sdt$ˆ j%ƒt$ˆj%ƒk�o~t‡ ‡‡fdd	„ˆ j%D ƒƒS ˆ j&�r”ˆj&�r”dS dS d S )
Né   F)r   r   Z	equal_nanTc                   s"   ˆ j  | ¡^}}}| ˆ j  |¡ S r   )Úindex_nplikeZ
unique_allÚsort)ÚvaluesÚuniqueZunique_indexÚ_)Úbackendr   r   Úordered_unique_values¬   s    
üz<almost_equal.<locals>.visitor.<locals>.ordered_unique_values)Údtypec                 3  s$   | ]}ˆˆ   |¡ˆ  |¡ƒV  qd S r   )r!   )Ú.0Úf)r   r   Úvisitorr   r   Ú	<genexpr>Ö   ó    z0almost_equal.<locals>.visitor.<locals>.<genexpr>)'Z
is_indexedZ	is_optionÚprojectZto_IndexedOptionArray64Zis_numpyZpurelist_depthZto_RegularArrayÚlengthr
   Ú
parametersr   Z
is_regularÚsizer!   Zis_listr   r   r,   Z
datetime64Ztimedelta64ÚnplikeÚallÚdataÚshapeÚiscloser%   Zarray_equalZmask_as_boolZis_unionÚtagsÚemptyZint64ÚzipZ	is_recordr   Úis_tupleÚlenÚfieldsZ
is_unknown)	r   r   r+   Zleft_tag_orderZright_tag_orderZleft_tag_to_right_tagZnew_left_tagÚiÚj)
r   r*   r   r   r   Úleft_behaviorr#   Úright_behaviorr   r/   )r   r   r   r/   T   sÊ    ÿÿÿý
þ
þþÿþýüÿ
ýÿÿÿ
ù	ÿþÿ
ÿý
ûúø
zalmost_equal.<locals>.visitor)r   r   Úcpur   Z	to_packedr6   Z
known_dataÚNotImplementedError)r   r   r   r   r   r   r   Zleft_backendZright_backendZleft_layoutZright_layoutr   )r   r*   r   r   r   r   rC   r#   rD   r   r/   r   r      s&    "
ÿ$ r   N)Ú
__future__r   Zawkward._backends.dispatchr   Zawkward._backends.numpyr   Zawkward._behaviorr   r   r   Zawkward._dispatchr   Zawkward._nplikes.numpy_liker	   Zawkward._parametersr
   Zawkward.operations.ak_to_layoutr   Ú__all__Úinstancer   rE   r   r   r   r   r   Ú<module>   s"   ø