U
    9%e%                     @   s   d 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d
lmZ ddlmZ ddlmZ ddl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d Z"dddZ#de$dddZ%d ddZ&dS )!zA
Several methods to simplify expressions involving unit objects.
    )reduce)Iterable)Optional)default_sort_key)Add)Tuple)Mul)Pow)ordered)sympify)NonInvertibleMatrixError)	DimensionDimensionSystem)Prefix)Quantity
UnitSystem)siftc           
         s   ddl m}  t| }j|dd fdd|D fddD }t }|t|snd S t fdd|D }|fd	d|D }| fd
d|D }z||}	W n t	k
r   Y d S X |	S )Nr   )MatrixTZmark_dimensionlessc                    s   g | ]}t  |qS  )r   get_dimensional_expr).0x)unit_systemr   W/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/physics/units/util.py
<listcomp>   s     z3_get_conversion_matrix_for_expr.<locals>.<listcomp>c                    s$   g | ]} j |d dD ]}|qqS )Tr   )get_dimensional_dependencies)r   r   i)dimension_systemr   r   r      s       c                    s"   g | ]}| ks  |s|qS r   )addr   r   )seenr   r   r   &   s      
 c                    s    g | ]  fd dD qS )c                    s"   g | ]} j |d ddqS )Tr   r   )r   getr!   )r   jr   r   r   (   s     z>_get_conversion_matrix_for_expr.<locals>.<listcomp>.<listcomp>r   )r   )r   target_dims)r$   r   r   (   s     c                    s   g | ]}  |d qS r   )r#   )r   k)dim_dependenciesr   r   r   )   s     )
Zsympy.matrices.denser   get_dimension_systemr   r   r   setissubsetZsolver   )
exprtarget_unitsr   r   Zexpr_dimZcanon_dim_unitsZcanon_expr_unitsZcamatZexprmatZres_exponentsr   )r(   r   r"   r%   r   r   _get_conversion_matrix_for_expr   s$    r.   SIc                    s   ddl m} |tttfs*gt| trPtfdd| jD S t	| } t	t| t
s| t
r| dd fdd}  fdd	 t| }|d
kr| S  | }|t fddt|D  S )a  
    Convert ``expr`` to the same expression with all of its units and quantities
    represented as factors of ``target_units``, whenever the dimension is compatible.

    ``target_units`` may be a single unit/quantity, or a collection of
    units/quantities.

    Examples
    ========

    >>> from sympy.physics.units import speed_of_light, meter, gram, second, day
    >>> from sympy.physics.units import mile, newton, kilogram, atomic_mass_constant
    >>> from sympy.physics.units import kilometer, centimeter
    >>> from sympy.physics.units import gravitational_constant, hbar
    >>> from sympy.physics.units import convert_to
    >>> convert_to(mile, kilometer)
    25146*kilometer/15625
    >>> convert_to(mile, kilometer).n()
    1.609344*kilometer
    >>> convert_to(speed_of_light, meter/second)
    299792458*meter/second
    >>> convert_to(day, second)
    86400*second
    >>> 3*newton
    3*newton
    >>> convert_to(3*newton, kilogram*meter/second**2)
    3*kilogram*meter/second**2
    >>> convert_to(atomic_mass_constant, gram)
    1.660539060e-24*gram

    Conversion to multiple units:

    >>> convert_to(speed_of_light, [meter, second])
    299792458*meter/second
    >>> convert_to(3*newton, [centimeter, gram, second])
    300000*centimeter*gram/second**2

    Conversion to Planck units:

    >>> convert_to(atomic_mass_constant, [gravitational_constant, speed_of_light, hbar]).n()
    7.62963087839509e-20*hbar**0.5*speed_of_light**0.5/gravitational_constant**0.5

    r   r   c                 3   s   | ]}t | V  qd S N
convert_tor!   r-   r   r   r   	<genexpr>f   s   zconvert_to.<locals>.<genexpr>c                 S   s
   t | tS r0   )
isinstancer   r   r   r   r   <lambda>m       zconvert_to.<locals>.<lambda>c                    s   |   S r0   r1   r6   r3   r   r   r7   n   r8   c                    sZ   t | tr(tdd  fdd| jD S t | trB | j| j S t | trV| S | S )Nc                 S   s   | | S r0   r   )r   yr   r   r   r7   r   r8   z<convert_to.<locals>.get_total_scale_factor.<locals>.<lambda>c                    s   g | ]} |qS r   r   r!   get_total_scale_factorr   r   r   s   s     z>convert_to.<locals>.get_total_scale_factor.<locals>.<listcomp>)	r5   r   r   argsr	   baseexpr   Zget_quantity_scale_factor)r,   )r;   r   r   r   r;   p   s    



z*convert_to.<locals>.get_total_scale_factorNc                 3   s&   | ]\}}d  | | | V  qdS )   Nr   )r   upr:   r   r   r4      s    )sympy.physics.unitsr   get_unit_systemr5   r   r   r   Zfromiterr<   r   r   hasreplacer.   r   zip)r,   r-   r   r   ZdepmatZexpr_scale_factorr   )r;   r-   r   r   r2   3   s,    ,



r2   FN)across_dimensionsc                    s@  | j s| tts| S | t}| dd |D } t| tdd }|D ]X}t|| dkrbqLtt	|| }|d |d j
  |  fdd|dd D } qL|r<|dkrtd	t|}| }|| }|j|d
d}	d}
|j D ]\}}||	kr|}
 qq|
dkr| S |j|
}|r<t| ||} | S )a  Return an equivalent expression in which prefixes are replaced
    with numerical values and all units of a given dimension are the
    unified in a canonical manner by default. `across_dimensions` allows
    for units of different dimensions to be simplified together.

    `unit_system` must be specified if `across_dimensions` is True.

    Examples
    ========

    >>> from sympy.physics.units.util import quantity_simplify
    >>> from sympy.physics.units.prefixes import kilo
    >>> from sympy.physics.units import foot, inch, joule, coulomb
    >>> quantity_simplify(kilo*foot*inch)
    250*foot**2/3
    >>> quantity_simplify(foot - 6*inch)
    foot/2
    >>> quantity_simplify(5*joule/coulomb, across_dimensions=True, unit_system="SI")
    5*volt
    c                 S   s   i | ]}||j qS r   scale_factor)r   rA   r   r   r   
<dictcomp>   s      z%quantity_simplify.<locals>.<dictcomp>c                 S   s   | j S r0   )	dimension)r   r   r   r   r7      r8   z#quantity_simplify.<locals>.<lambda>r?   r   c                    s   i | ]}| |j  qS r   rH   )r   virefr   r   rJ      s      Nz:unit_system must be specified if across_dimensions is TrueTr   )Zis_AtomrD   r   r   atomsxreplacer   lenlistr
   rI   
ValueErrorr   rC   r)   r   r   Zdimensional_dependenciesitemsZderived_unitsr#   r2   )r,   rG   r   rA   dr'   vr   Zdim_exprZdim_depsZtarget_dimensionZds_dimZds_dim_depsZtarget_unitr   rM   r   quantity_simplify   s:    
"


rW   c              	   C   sN  ddl m} ||}dd }| t}| j}|D ]}t }|jD ]}|j	r\|
d qFg }	d}
i }t|D ]D}|trt||}|tr||||}qr|jrrd}
 qqr|	|  |
sF|
tt|	td t|d	krFtd
|qFq6i }| tD ]6}tdd |jD r|jdd |jD  ||< q| |S )z[Return expr if units in addends have the same
    base dimensions, else raise a ValueError.r   r   c                 S   sL   | |}|  D ](\}}|| kr||kr|| |  ||< qdd |  D S )z]Merge dictionaries by adding values of common keys and
        removing keys with value of 0.c                 S   s   i | ]\}}|d kr||qS r&   r   )r   keyvalr   r   r   rJ      s       z5check_dimensions.<locals>.addDict.<locals>.<dictcomp>)rT   )Zdict1Zdict2Zdict3rX   valuer   r   r   addDict   s
    z!check_dimensions.<locals>.addDictr   FT)rX   r?   z(addends have incompatible dimensions: {}c                 s   s   | ]}t |tV  qd S r0   )r5   r   r!   r   r   r   r4      s     z#check_dimensions.<locals>.<genexpr>c                 S   s   g | ]}|j s|qS r   )	is_numberr!   r   r   r   r      s     z$check_dimensions.<locals>.<listcomp>)rB   r   rC   rO   r   r)   r   r*   r<   r\   r    r   Z	make_argsrD   r   r   r   Zfree_symbolsextendrT   tuplesortedr   rQ   rS   formatanyfuncrP   )r,   r   r   r[   ZaddsZDIM_OFaZdesetZaiZdimsskipZdimdictr   Zrepsmr   r   r   check_dimensions   sH    

	






rf   )r/   )FN)r/   )'__doc__	functoolsr   collections.abcr   typingr   Zsympyr   Zsympy.core.addr   Zsympy.core.containersr   Zsympy.core.mulr   Zsympy.core.powerr	   Zsympy.core.sortingr
   Zsympy.core.sympifyr   Zsympy.matrices.commonr   Zsympy.physics.units.dimensionsr   r   Zsympy.physics.units.prefixesr   Zsympy.physics.units.quantitiesr   Zsympy.physics.units.unitsystemr   Zsympy.utilities.iterablesr   r.   r2   boolrW   rf   r   r   r   r   <module>   s(   
QD