
    `i52                         d dl Zd dlZd dlZ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  e	ej        dddd	
          ZddZd ZddZd ZddZddZd Zd Zd ZdS )    N)_core)internal)_GUFunc)_solve)_utilz(n?,k),(k,m?)->(n?,m?)Ta  matmul(x1, x2, /, out=None, \*\*kwargs)

    Matrix product of two arrays.

    Returns the matrix product of two arrays and is the implementation of
    the `@` operator introduced in Python 3.5 following PEP465.

    The main difference against cupy.dot are the handling of arrays with more
    than 2 dimensions. For more information see :func:`numpy.matmul`.

    Args:
        x1 (cupy.ndarray): The left argument.
        x2 (cupy.ndarray): The right argument.
        out (cupy.ndarray, optional): Output array.
        \*\*kwargs: ufunc keyword arguments.

    Returns:
        cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.matmul`
    )supports_batchedsupports_outdocc                 .    |                      ||          S )aC  Returns a dot product of two arrays.

    For arrays with more than one axis, it computes the dot product along the
    last axis of ``a`` and the second-to-last axis of ``b``. This is just a
    matrix product if the both arrays are 2-D. For 1-D arrays, it uses their
    unique axis as an axis to take dot product over.

    Args:
        a (cupy.ndarray): The left argument.
        b (cupy.ndarray): The right argument.
        out (cupy.ndarray): Output array.

    Returns:
        cupy.ndarray: The dot product of ``a`` and ``b``.

    .. seealso:: :func:`numpy.dot`

    )dotabouts      h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/linalg/_product.pyr   r   +   s    ( 55C==    c           	          | j         |j         k    rt          d          | j        j        dk    r|                                 } t          j        | |ddd| j         d          S )a~  Returns the dot product of two vectors.

    The input arrays are flattened into 1-D vectors and then it performs inner
    product of these vectors.

    Args:
        a (cupy.ndarray): The first argument.
        b (cupy.ndarray): The second argument.

    Returns:
        cupy.ndarray: Zero-dimensional array of the dot product result.

    .. seealso:: :func:`numpy.vdot`

    Axis dimension mismatchcN    )size
ValueErrordtypekindconjr   tensordot_core)r   r   s     r   vdotr   B   s]      	v2333w|sFFHH1dAq!&"===r   c                    |
|fdz  \  }}}t          j        |           } t          j        |          }t          j        || j                  }t          j        ||j                  }t          j        | |d          } t          j        ||d          }| j        d         dvs|j        d         dvrd}t          |          t          j        | d         |d                   j        }| j        d         dk    s|j        d         dk    r'|dz  }t          j        |t          |                    }t          j
        | j        |j                  }t          j        ||          }	| d         }
| d         }| j        d         dk    r| d	         }|d         }|d         }|j        d         dk    r|d	         }|	j        d
k    r)|	j        d         dk    r|	d         }|	d         }|	d	         }| j        d         dk    r|j        d         dk    r!t          j        |
||	           |	||z  z  }	|	S |j        d         dk    sJ t          j        |||           t          j        |
||           t          j        ||           t          j        |
||           |||z  z  }n"| j        d         dk    sJ |j        d         dk    rt          j        |||           ||z  }||z  }t          j        |||           t          j        |
||           ||z  }t          j        |
||           t          j        |||           ||z  }nv|j        d         dk    sJ t          j        |||           t          j        ||           t          j        |||           t          j        |
||           |||z  z  }t          j        |	d|          S )a  Returns the cross product of two vectors.

    The cross product of ``a`` and ``b`` in :math:`R^3` is a vector
    perpendicular to both ``a`` and ``b``.  If ``a`` and ``b`` are arrays
    of vectors, the vectors are defined by the last axis of ``a`` and ``b``
    by default, and these axes can have dimensions 2 or 3.  Where the
    dimension of either ``a`` or ``b`` is 2, the third component of the input
    vector is assumed to be zero and the cross product calculated accordingly.
    In cases where both input vectors have dimension 2, the z-component of
    the cross product is returned.

    Args:
        a (cupy.ndarray): Components of the first vector(s).
        b (cupy.ndarray): Components of the second vector(s).
        axisa (int, optional):
            Axis of ``a`` that defines the vector(s).
            By default, the last axis.
        axisb (int, optional):
            Axis of ``b`` that defines the vector(s).
            By default, the last axis.
        axisc (int, optional):
            Axis of ``c`` containing the cross product vector(s).  Ignored if
            both input vectors have dimension 2, as the return is scalar.
            By default, the last axis.
        axis (int, optional):
            If defined, the axis of ``a``, ``b`` and ``c``
            that defines the vector(s) and cross product(s).
            Overrides ``axisa``, ``axisb`` and ``axisc``.

    Returns:
        cupy.ndarray :
            Vector cross product(s).

    .. seealso:: :func:`numpy.cross`

    N   r   )   r!   zDincompatible dimensions for cross product
(dimension must be 2 or 3)).r   )r!   ).r   ).r"   r   r"   r   )cupyasarrayr   _normalize_axis_indexndimmoveaxisshaper   	broadcastlenpromote_typesr   emptymultiplynegative)r   r   axisaaxisbaxiscaxismsgr)   r   cpa0a1a2b0b1b2cp0cp1cp2tmps                       r   crossr@   Z   s   L #gkueQAQA*5!&99E*5!&99E 	a##Aa##Awr{&  AGBKv$=$=,oo N1V9ai006Ewr{a172;!++.uc%jjAAqw00E	E5	!	!B 
6B	
6Bwr{avY	
6B	
6Bwr{avY	w!||))jjjwr{a72;!M"bb))))"r'MBI72;!#### M"bc****M"bc****M#3''''M"bc****27NCCwr{a72;! M"bc****r'C3JCM"bc****M"bc****3JCM"bc****M"bc****3JCC72;!#### M"bc****M#3''''M"bc****M"bc****27NC=R'''r   c           	         | j         }|j         }|dk    s|dk    rt          j        | |          S |dz
  }|dz
  }| j        d         |j        d         k    rt	          d          |rt          j        | |d          } |rt          j        ||d          }| j        dd         |j        dd         z   }| j        d         }| j        |z  }|j        |z  }	t          j        | |d||	||          S )aG  Returns the inner product of two arrays.

    It uses the last axis of each argument to take sum product.

    Args:
        a (cupy.ndarray): The first argument.
        b (cupy.ndarray): The second argument.

    Returns:
        cupy.ndarray: The inner product of ``a`` and ``b``.

    .. seealso:: :func:`numpy.inner`

    r   r   r   r   N)	r'   r$   r.   r)   r   rollaxisr   r   r   )
r   r   a_ndimb_ndima_axisb_axis	ret_shapeknms
             r   innerrK      s     VFVF{{fkk}Q"""aZFaZFwr{agbk!!2333 (M!VQ'' (M!VQ''agabbk)I	
A	!A	!A1dAq!Y???r   c                     t          j        |                                 dddf         |                                dddf         |          S )a  Returns the outer product of two vectors.

    The input arrays are flattened into 1-D vectors and then it performs outer
    product of these vectors.

    Args:
        a (cupy.ndarray): The first argument.
        b (cupy.ndarray): The second argument.
        out (cupy.ndarray): Output array.

    Returns:
        cupy.ndarray: 2-D array of the outer product of ``a`` and ``b``.

    .. seealso:: :func:`numpy.outer`

    Nr#   )r$   r.   ravelr   s      r   outerrN      sE    " =111d7+QWWYYtQQQw-?SIIIIr   r"   c           	      L   | j         |j         dk    sdk    r0|dk    r|dk    rt          d          t          j        | |          S t	          |t
          j        j                  rVt          |          dk    rt          d          |\  }}t          j
        |          r|f}t          j
        |          r|f}n<t          t          |z
                      }t          t          |                    }t          |          }|t          |          k    rt          d          t          ||          D ]0\  }}| j        |         |j        |         k    rt          d          1t          | fd|D                       } t          |fd	|D                       }| j        |d
         |j        |d
         z   }t!          j        | j        d
|                   }	|	dk    r
| j        |	z  nd}
|	dk    r
|j        |	z  nd}t'          j        | |d
|
||	|          S )a$  Returns the tensor dot product of two arrays along specified axes.

    This is equivalent to compute dot product along the specified axes which
    are treated as one axis by reshaping.

    Args:
        a (cupy.ndarray): The first argument.
        b (cupy.ndarray): The second argument.
        axes:
            - If it is an integer, then ``axes`` axes at the last of ``a`` and
              the first of ``b`` are used.
            - If it is a pair of sequences of integers, then these two
              sequences specify the list of axes for ``a`` and ``b``. The
              corresponding axes are paired for sum-product.

    Returns:
        cupy.ndarray: The tensor dot product of ``a`` and ``b`` along the
        axes specified by ``axes``.

    .. seealso:: :func:`numpy.tensordot`

    r   )r   r   z.An input is zero-dim while axes has dimensionsr"   z Axes must consist of two arrays.zAxes length mismatchr   c                     g | ]}|z  S r   r   ).0r3   rC   s     r   
<listcomp>ztensordot.<locals>.<listcomp>C      @@@tf}@@@r   c                     g | ]}|z  S r   r   )rQ   r3   rD   s     r   rR   ztensordot.<locals>.<listcomp>D  rS   r   N)r'   r   r$   r.   
isinstancecollectionsabcSequencer+   numpyisscalartuplerangezipr)   _move_axes_to_headr   prodr   r   r   )r   r   axesa_axesb_axessum_ndimrE   rF   rG   rH   rI   rJ   rC   rD   s               @@r   	tensordotrd     s<   . VFVF{{fkk199))MNNN}Q"""$011 
$t99>>?@@@>&!! 	WF>&!! 	WFuVd]F3344uT{{##6{{H3v;;/000ff-- 8 876?agfo--6777 . 	1@@@@@@@AAA1@@@@@@@AAA		"QWXYY%77Iagixi())A Avv!1AAvv!1A1dAq!Y???r   c                    t          j        |            t          j        |            t          j        |            t	          |t
                    st          d          |dk    rt          j        |           S |dk     rt          j	        |           } |dz  }|dk    rK|dk    r| S |dk    rt          j        | |           S t          j        t          j        | |           |           S d\  }}t          j        |          ddd         D ]:}|| nt          j        ||          }|d	k    r||nt          j        ||          };|S )
a
  Raise a square matrix to the (integer) power `n`.

    Args:
        M (~cupy.ndarray): Matrix to raise by power n.
        n (~int): Power to raise matrix to.

    Returns:
        ~cupy.ndarray: Output array.

    ..seealso:: :func:`numpy.linalg.matrix_power`
    zexponent must be an integerr   r   r!   r   r"   )NNN1)r   _assert_cupy_array_assert_stacked_2d_assert_stacked_squarerU   int	TypeErrorstacked_identity_liker   invr$   matmulbinary_repr)MrI   resultZr   s        r   matrix_powerrs   R  sV    
Q	Q	 ###a 75666Avv*1---	
QJqMM	R 	Avv66H!VV;q!$$$;t{1a00!444 IFAa  2& E EAAAq 1 188 .QQdk&!.D.DFMr   c           
      "   t          | t          j                  }t          |t          j                  }|r|r| |z  S |s|rt          j        | |          S | j        }|j        }|dk    s|dk    rt          j        | |          S |}| j        }|j        }||k    r||k    rd||z
  z  |z   }nd||z
  z  |z   }|}|dz
  }	t          j        | |d| j	        |j	        d||z             }
t          |          D ]}t          j        |
|	          }
|
S )zReturns the kronecker product of two arrays.

    Args:
        a (~cupy.ndarray): The first argument.
        b (~cupy.ndarray): The second argument.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.kron`

    r   )r   r   N)r3   )rU   numbersNumberr$   r.   r'   r)   r   r   r   r\   concatenate_method)r   r   
a_isnumber
b_isnumberrC   rD   r'   a_shapeb_shaper3   r   _s               r   kronr}   ~  sH    Aw~..JAw~..J j 1u #Z #}Q"""VFVF{{fkk}Q"""DgGgGF??fvo.8GGfvo.8GD!8D

	1dAFAFAw'8: :C4[[ 7 7&s666Jr   c                     t                    D ]\  }}||k    r n| S |                     fdt          | j                  D             z             S )Nc                     g | ]}|v|	S r   r   )rQ   ir`   s     r   rR   z&_move_axes_to_head.<locals>.<listcomp>  s    :::aATMMMMMr   )	enumerate	transposer\   r'   )r   r`   idxr3   s    `  r   r^   r^     sq    t__  	T$;;E  ;;::::5==::::< < <r   )N)r   r   r   N)r"   )collections.abcrV   ru   rY   r$   r   
cupy._corer   cupy._core._gufuncsr   cupy.linalgr   r   rn   r   r   r@   rK   rN   rd   rs   r}   r^   r   r   r   <module>r      sj                      ' ' ' ' ' '             
	L	
 
 
:   .> > >0w( w( w( w(t%@ %@ %@PJ J J J(>@ >@ >@ >@D) ) )X) ) )X	< 	< 	< 	< 	<r   