o
    h                     @   s0   d Z ddlZddlZddlZdd Zdd ZdS )a)  Analytical transformations from raw image moments to central moments.

The expressions for the 2D central moments of order <=2 are often given in
textbooks. Expressions for higher orders and dimensions were generated in SymPy
using ``tools/precompute/moments_sympy.py`` in the GitHub repository.

    Nc           	      C   s  | j }| jd d }| j}| jtjdd} t| }|dks"|dvr&td| }|dkr|d	 |d
  }|d |d
  }|d
 |d
< |dkrj|d ||d   |d< |d ||d	   |d< |d ||d   |d< |dkr|d d| |d   ||d   |d |d   || |d	   |d< |d d| |d   ||d   d| | |d   |d< |d d| |d   d|d  |d	   |d< |d d| |d   d|d  |d   |d< n|d |d  }|d |d  }|d |d  }|d |d< |dkrS| |d  |d  |d< | |d  |d  |d< | |d  |d  |d< | |d  |d  |d< | |d  |d  |d< | |d  |d  |d< |dkr}d|d  |d  d| |d   |d  |d< | |d  d| ||d  |d    |d  |d< |d |d  d| |d   |||d  |d    |d   |d < d|d  |d  d| |d   |d!  |d!< | |d  d| ||d  |d    |d"  |d"< | |d  |||d  |d    |||d  |d    |d#  |d#< | |d  d| | |d  |d    |d$  |d$< |d |d  d| |d   |||d  |d    |d%  |d%< |d |d  d| |d   |||d  |d    |d&  |d&< d|d  |d  d| |d   |d'  |d'< |j|ddS )(aO  Analytical formulae for 2D and 3D central moments of order < 4.

    `moments_raw_to_central` will automatically call this function when
    ndim < 4 and order < 4.

    Parameters
    ----------
    moments_raw : ndarray
        The raw moments.

    Returns
    -------
    moments_central : ndarray
        The central moments.
    r      F)copy         z:This function only supports 2D or 3D moments of order < 4.r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   )r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )ndimshapedtypeastypenpfloat64
zeros_like
ValueError)	moments_rawr   orderfloat_dtypemoments_centralmcxcycz r   ^/var/www/html/scripts/venv/lib/python3.10/site-packages/skimage/measure/_moments_analytical.py_moments_raw_to_central_fast   s   
""00

r   c                 C   s  | j }| jd d }|dv r|dk rt| S t| }| }t|ttj|td |d|   }|dkrt|d D ]X}t|d D ]O}|| |krKqBt|d D ]?}t	
||}	|	|d  ||  9 }	t|d D ]%}
t	
||
}||d  ||
  9 }|||f  |	| |||
f  7  < qjqQqBq:|S tjt|d f|  D ]?}t||krqtjdd	 |D  D ]+}|| }t|||D ]\}}}|t	
||9 }|| ||  9 }q||  |7  < qq|S )
Nr   r   r   r   )r
   )r   r   c                 S   s   g | ]}t |d  qS )r   )range).0or   r   r   
<listcomp>   s    z*moments_raw_to_central.<locals>.<listcomp>)r   r	   r   r   r   tupleeyeintr   mathcomb	itertoolsproductsumzip)r   r   r   r   r   centerspqiterm1jterm2ordersidxsvali_ordercidxr   r   r   moments_raw_to_centralq   sD   
&&
r5   )__doc__r$   r"   numpyr   r   r5   r   r   r   r   <module>   s    c