
    2Vh|                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d d	lmZ d
 Zd Zd Zd Zd Zd Z G d de      Z ed      d        Zy)    N)keras_export)KerasTensor)any_symbolic_tensors)shape)prod)reshape)	transpose)	Operationc           	         i }t        | |      D ]  \  }}t        j                  d|      }|r|j                  d      j	                         }|D cg c]	  }||v s| }	}|D cg c]	  }||vs| }
}|
r)|
d   }t        |	D cg c]  }||   	 c}      }||z  ||<   |j                  |D ci c]  }|||   
 c}       |||<    |S c c}w c c}w c c}w c c}w )N\(([\w\s]+)\)   r   )ziprematchgroupsplitr   update)axesinput_shapeaxes_lengthsaxes_mapaxisdimgrouped_axes
inner_axesa
known_axesinferred_axesinferred_axisknown_products                D/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/ops/einops.py_create_axes_mapr"      s    H{+ !	cxx 0$7%++A.446J%/E13D!EJE(2L1a|6KQLML -a 0 $z%J!l1o%J K.1].B]+OODAQQ/DE HTN!!$ O FL &K Es$   	CC$	C.CC
.C
c                     g }| D ]]  }t        j                  d|      }|r1|j                  d      j                         }|j	                  |       L|j	                  |g       _ |S )Nr   r   )r   r   r   r   append)r   grouped_output_axesr   r   r   s        r!   _create_grouped_axesr&   %   sj     /xx 0$7%++A.446J&&z2&&v./     c                 @    | D cg c]  }|D ]  }|  c}}S c c}}w N )r   xsxs      r!   _flatten_groupr-   3   s"    )"b)A)A)))s   c                 r    t        t        |             }|D cg c]  }|j                  |       c}S c c}w r)   )r-   r&   index)
from_shapeto_shapeflattened_from_shaper   s       r!   _get_transpose_orderr3   7   s3    )*>z*JK7?@ &&s+@@@s   4c                 n    g }|D ]$  }d}|D ]
  }|| |   z  } |j                  |       & t        |      S )Nr   )r$   tuple)r   r   output_shaper   sizer   s         r!   _compute_output_shaper8   =   sS    L " 	#DHTN"D	#D!	" r'   c                    g }g }| D ]v  }d|v rKt        j                  d|      }|D cg c]  }||   	 }}|j                  |       |j                  |       R|j                  |       |j                  ||          x |S c c}w )N(z\w+)r   findallextendr$   )	
input_axesr   r   reshaped_input_axesreshaped_sizesr   r   r   sizess	            r!   _compute_decomposed_shaperA   H   s    N 2$;FD1J.89\!_9E9&&z2!!%(&&t,!!(4.12  :s   Bc                       e Zd Zd Zd Zy)	Rearrangec                     t        ||fi |S r)   )	rearrange)selftensorpatternr   s       r!   callzRearrange.callZ   s    9L99r'   c                    t        j                  d|      \  }}t        j                  d|      }t        j                  d|      }t        |      }t	        |||      }	t        |      }
t        |	|
      }t        ||j                        S )N\s*->\s*\w+|\(.*?\))r   dtype)	r   r   r;   r   r"   r&   r8   r   rM   )rF   rG   rH   r   input_patternoutput_patternr=   output_axesr   r   r%   r6   s               r!   compute_output_speczRearrange.compute_output_spec]   sw    (*g(F%~ZZ>
jj@Fm#J\J2;?,X7JKV\\BBr'   N)__name__
__module____qualname__rI   rQ   r*   r'   r!   rC   rC   Y   s    :
Cr'   rC   zkeras.ops.rearrangec                    t        | f      r t               j                  | |fi |S t        j                  d|      \  }}t        j
                  d|      }t        j
                  d|      }t        |       }t        |||      }t        |      }	t        |	      }
t        |||      }|| j                  k7  rt        | |      } t        ||
      }t        | |      } t        ||	      }t        | |      } | S )az  Rearranges the axes of a Keras tensor according to a specified pattern,
    einops-style.

    Args:
        tensor: Input Keras tensor.
        pattern: String describing the rearrangement in einops notation.
        **axes_lengths: Keyword arguments specifying lengths of axes
            when axes decomposition is used.

    Returns:
        Tensor: A Keras tensor with rearranged axes.

    Follows the logic of:

    1. If decomposition is needed, reshape to match decomposed dimensions.
    2. Permute known and inferred axes to match the form of the output.
    3. Reshape to match the desired output shape.


    Example Usage:

    ```
    >>> import numpy as np
    >>> from keras.ops import rearrange
    >>> images = np.random.rand(32, 30, 40, 3) # BHWC format

    # Reordering to BCHW
    >>> rearrange(images, 'b h w c -> b c h w').shape
    TensorShape([32, 3, 30, 40])

    # "Merge" along first axis - concat images from a batch
    >>> rearrange(images, 'b h w c -> (b h) w c').shape
    TensorShape([960, 40, 3])

    # "Merge" along second axis - concat images horizontally
    >>> rearrange(images, 'b h w c -> h (b w) c').shape
    TensorShape([30, 1280, 3])

    # Flatten images into a CHW vector
    >>> rearrange(images, 'b h w c -> b (c h w)').shape
    TensorShape([32, 3600])

    # Decompose H and W axes into 4 smaller patches
    >>> rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', h1=2, w1=2).shape
    TensorShape([128, 15, 20, 3])

    # Space-to-depth decomposition of input axes
    >>> rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape
    TensorShape([32, 15, 20, 12])
    ```
    rK   rL   )r   rC   symbolic_callr   r   r;   r   r"   r&   r-   rA   r   r3   r	   r8   )rG   rH   r   rN   rO   r=   rP   r   r   r%   flattened_output_axesdecomposed_shapespermute_orderr6   s                 r!   rE   rE   j   s    l VI&(y{((ILII %'HH['$B!M>NM:J**^^<K-K  
KFH.{;*+>? 2L( FLL(!23 )5JKMv}-F )3FGLV\*FMr'   )r   keras.src.api_exportr   keras.src.backendr   r   keras.src.ops.corer   keras.src.ops.numpyr   r   r	   keras.src.ops.operationr
   r"   r&   r-   r3   r8   rA   rC   rE   r*   r'   r!   <module>r_      si    	 - ) 2 $ $ ' ) -0*A"C	 C" #$R %Rr'   