o
    Ei                     @   s   d dl Z d dlmZ ddlmZ d dlZd dlmZ d dl	mZ
 e jdd ddd
dZe jdddZe jje jdd dd	dddedee jef dedede
jde jfddZee_dS )    N)partial   )util)Unionc                    s   d fdd	S )Nint32c                    s    | ||dS )Ndtype )exprs_inexpr_outbackendr   cr	   K/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/einx/op/arange.py<lambda>
   s    <lambda>.<locals>.<lambda>)Nr   r	   tr   r	   r   r   r   
       r   )tracer   c                    sd  t  trtj  |  D ]}t |tjjjrt	dq| |fD ]}| D ]}t |tjjj
r6t	dq(q"dd | D }t|dkrPt	dt| t|dkr[|d jndt| gd } t|gd }fdd	}tjj||}	tjj|	}	tjd
d | D  fdd| D |	g d\\}
}tj|g|
g|g d\}
|
tjj|fS )Nz&Marker in input expression not allowedzConcatenation not allowedc                 S   s.   g | ]}t |tjjjrtjj|r|qS r	   )
isinstanceeinxexprstage3Axis	is_marked.0r   r	   r	   r   
<listcomp>   s    z!arange_stage3.<locals>.<listcomp>r   &Expected at most one marked axis, got r   c                    sT   t | tjjjr&tjj| r(tjjdd t D } tjj| } | S d S d S )Nc                 S   s   g | ]
}t jjd dqS )Nr   )r   r   r   r   )r   _r	   r	   r   r   '   s    z2arange_stage3.<locals>.replace.<locals>.<listcomp>)	r   r   r   r   r   r   ConcatenationrangeComposition)r   )ndimr	   r   replace%   s   zarange_stage3.<locals>.replacec                 S   s   g | ]}|  qS r	   )__deepcopy__r   axisr	   r	   r   r   1   s    c                    s   g | ]
} j |jd qS )r   )arangevaluer'   r   r   r	   r   r   2   s    )r   )r   strr   r   getallr   r   Marker
ValueErrorr!   lenr*   r   flattenr%   demarkrearrange_stage3	unflatten)expr_inr   r   r   r   rootmarked_axesexpr_out_flatr%   expr_out_flat_withconcattensorr    r	   )r   r   r$   r   arange_stage3	   sJ   
	r<   Tc                    s:  t jj| |\} }t jj| }t|dkr1t jjt jj	t jj
|d d g|d g}t|d dkrDtdt|d  t|d dkrWtdt|d  t jjt jj|d d   fdd}t jjt j|d d gt j|d d g dd | D  ||d	d d
 \}}||fS )Nr   r   z%Expected 1 input expression, but got z&Expected 1 output expression, but got c                    sn   | d }dd |  D }dd |D }t|dkr#tdt| t|t| }tj t|ggS )Nr   c                 S   s*   g | ]}t |tjjjtjjjfr|qS r	   )r   r   r   stage2	NamedAxisUnnamedAxisr   r	   r	   r   r   ^   s    z/parse.<locals>.after_stage2.<locals>.<listcomp>c                 S   s   g | ]}t jj|r|qS r	   )r   r   r=   r   r   r	   r	   r   r   c   s    r   )r.   r1   r0   r   r   Equationnpasarray)exprs1exprs2r   out_axesmarked_out_axesr$   marked_expr_outr	   r   after_stage2\   s   zparse.<locals>.after_stage2c                 S   s4   g | ]\}}t jj|t|d tjf dddqS ).N)depth1depth2)r   r   r@   rA   rB   newaxis)r   kvr	   r	   r   r   l   s    "zparse.<locals>.<listcomp>)cserI      )r   opr   !_clean_description_and_parametersr   stage1parse_opr1   OpArgsget_unmarkedr0   r#   
get_markedsolver@   items)descriptionrO   
parametersrQ   rI   r6   r   r	   rG   r   parseD   s<     	r]   c                    s   d fdd	S )Nc                    s    | fi |S Nr	   )r[   r   kwargsr   r	   r   r   x   s    r   r^   r	   r   r	   r   r   r   x   r   )r   rO   r[   r   r   rO   r\   returnc                K   s0   t | fd|i|\}}t||||d\}}|S )a  n-dimensional ``arange`` operation.

    *This function might be removed in a future version.*

    Runs ``arange`` for every axis in ``input``, and stacks the results along the single
    marked axis in ``output``. Always uses ``start=0`` and ``step=1``.

    The `description` argument must meet one of the following formats:

    1. ``input -> output``
        Runs ``backend.arange`` for every axis in ``input``, and stacks the results along the
        marked axis in ``output``. The values are stacked in the order that the axes appear
        in ``input``.

    2. ``output``
        Implicitly determines the input expression by removing the marked axis from ``output``.

        Example: ``a b [2]`` resolves to ``a b -> a b [2]``

    Args:
        description: Description string in Einstein notation (see above).
        backend: Backend to use for all operations.
        cse: Whether to apply common subexpression elimination to the expressions. Defaults
            to True.
        graph: Whether to return the graph representation of the operation instead of computing
            the result. Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        The result of the n-dimensional arange operation if `graph=False`, otherwise the graph
        representation of the operation.

    Examples:
        Arange two-dimensional coordinates:

        >>> tensor = einx.arange("a b [2]", a=5, b=6, backend="numpy")
        >>> tensor.shape
        (5, 6, 2)
        >>> tensor[2, 3]
        array([2, 3], dtype=int32)

        Arange two-dimensional coordinates with inverted coordinates (`Cartesian ordering
        <https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html>`_:
        First axis of tensor corresponds to second coordinate along stacked axis and vice versa.):

        >>> tensor = einx.arange("a b -> b a [2]", a=5, b=6, backend="numpy")
        >>> tensor.shape
        (6, 5, 2)
        >>> tensor[2, 3]
        array([3, 2], dtype=int32)

        Arange one-dimensional coordinates:

        >>> einx.arange("a", a=5, backend="numpy").shape
        (5,)
    rO   r+   )r]   r<   )r[   r   r   rO   r\   r6   r   r;   r	   r	   r   r)   w   s   Br)   )r   )T)r   	functoolsr    r   numpyrA   typingr   numpy.typingnptjitr<   	lru_cacher]   traceback_utilfilterr,   Backendbool	ArrayLikeTensorr)   r	   r	   r	   r   <module>   s<    62
E