
    EdX                         d Z ddlmZ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	lmZ d
dlmZ  G d de          ZdS )zG
Unit system for physical quantities; include definition of constants.
    )DictSet)Add)
DerivativeFunction)Mul)Pow)S)_QuantityMapperQuantity   )	Dimensionc                   >    e Zd ZdZi Zddddi fdeeef         f fdZd Z	d Z
ddddi fdeeef         fd	Zd
 Z fdZ fdZed             Zed             Zed             Zed             Zedeeef         fd            Zd Zd Zdee         fdZ xZS )
UnitSystemz
    UnitSystem represents a coherent set of units.

    A unit system is basically a dimension system with notions of scales. Many
    of the methods are defined in the same way.

    It is much better if all base units have a symbol.
      Nderived_unitsc                 6   | t           j        |<   || _        || _        || _        || _        t          t          |          t          |          z            | _        t          |          | _        || _	        t                                                       d S N)r   _unit_systemsnamedescr_base_units_dimension_systemtupleset_units_derived_unitssuper__init__)self
base_unitsunitsr   r   dimension_systemr   	__class__s          >lib/python3.11/site-packages/sympy/physics/units/unitsystem.pyr!   zUnitSystem.__init__   s    )-
 &	
%!1C
OOc%jj899 ,,+    c                 t    | j         dk    r| j         S dd                    d | j        D                       z  S )z
        Return the name of the system.

        If it does not exist, then it makes a list of symbols (or names) of
        the base dimensions.
        r   zUnitSystem((%s)), c              3   4   K   | ]}t          |          V  d S r   )str.0ds     r'   	<genexpr>z%UnitSystem.__str__.<locals>.<genexpr>8   s9       22 22A22 22 22 22 22 22r(   )r   joinr   r"   s    r'   __str__zUnitSystem.__str__-   sV     9? 	29%		 22 22 $ 022 22 22 )2 )2 2 2r(   c                 0    dt          | j                  z  S )Nz<UnitSystem: %s>)reprr   r2   s    r'   __repr__zUnitSystem.__repr__;   s    !D)9$:$:::r(   c           	          | j         t          |          z   }| j        t          |          z   }t          |||||i | j        |          S )a  Extend the current system into a new one.

        Take the base and normal units of the current system to merge
        them to the base and normal units given in argument.
        If not provided, name and description are overridden by empty strings.
        )r   r   r   r   r   )r"   baser$   r   descriptionr%   r   s          r'   extendzUnitSystem.extend>   sQ     %++-eEll*$t[:JLttObLtfsLtuuur(   c                     | j         S r   )r   r2   s    r'   get_dimension_systemzUnitSystem.get_dimension_systemK   s    %%r(   c                     |                                  j        }||v r||         S t                                          |          S r   )r<   _quantity_dimension_mapr    get_quantity_dimension)r"   unitqdmr&   s      r'   r?   z!UnitSystem.get_quantity_dimensionN   sE    ''))A3; 	t9ww--d333r(   c                     |                                  j        }||v r||         S t                                          |          S r   )r<   _quantity_scale_factorsr    get_quantity_scale_factor)r"   r@   qsfmr&   s      r'   rD   z$UnitSystem.get_quantity_scale_factorT   sE    ((**B4< 	:ww00666r(   c           	         t          | t                    r| S | t          j        vrLt          d                    d                    t          t          j                                                t          j        |          S )NzDUnit system is not supported. Currentlysupported unit systems are {}r*   )
isinstancer   r   
ValueErrorformatr1   sorted)unit_systems    r'   get_unit_systemzUnitSystem.get_unit_systemZ   sz    k:.. 	j66 	006IIfZ%=>>??1 1   '44r(   c                  &    t           j        d         S )NSI)r   r   r   r(   r'   get_default_unit_systemz"UnitSystem.get_default_unit_systemi   s    '--r(   c                 *    t          | j                  S )zr
        Give the dimension of the system.

        That is return the number of units forming the basis.
        )lenr   r2   s    r'   dimzUnitSystem.dimm   s     4#$$$r(   c                 4    |                                  j        S )zI
        Check if the underlying dimension system is consistent.
        )r<   is_consistentr2   s    r'   rT   zUnitSystem.is_consistentv   s     ((**88r(   returnc                     | j         S r   )r   r2   s    r'   r   zUnitSystem.derived_units~   s    ""r(   c                     ddl m} t          |t                    rt           fd|j        D              S t          |t
                    r"                     |j                  |j        z  S t          |t                    r                      |j        d                   S t          |t                    rD                     |j                  }|j        D ] \  }}|                     |          |z  z  }!|S t          |t                    rB fd|j        D             }t          d |D                       rt          j        S  |j        | S t          ||          r                     |          j        S t          j        S )Nr   r   c                 :    g | ]}                     |          S r   get_dimensional_expr)r.   ir"   s     r'   
<listcomp>z3UnitSystem.get_dimensional_expr.<locals>.<listcomp>   s'    III!22155IIIr(   c                 :    g | ]}                     |          S r   rY   r.   argr"   s     r'   r\   z3UnitSystem.get_dimensional_expr.<locals>.<listcomp>   s'    HHHsD--c22HHHr(   c              3   "   K   | ]
}|d k    V  dS r   Nr   )r.   r[   s     r'   r0   z2UnitSystem.get_dimensional_expr.<locals>.<genexpr>   s&      ((a16((((((r(   )sympy.physics.unitsr   rG   r   argsr	   rZ   r8   expr   r   exprvariable_countr   allr
   Onefuncr?   r   )r"   re   r   rR   independentcountrc   s   `      r'   rZ   zUnitSystem.get_dimensional_expr   s   000000dC   	:IIIItyIIIJJc"" 	:,,TY7748CCc"" 	:,,TYq\:::j)) 	:++DI66C&*&9 E E"Ut00==uDDJh'' 	:HHHHdiHHHD((4((((( u49d##h'' 	:..t4499ur(   c                 
    ddl m} t          ||          r|j        |j        fS t          |t
                    rAd}t          d          }|j        D ]$}                     |          \  }}||z  }||z  }%||fS t          |t                    rp                     |j
                  \  }}                     |j                  \  }	}
                                                     |
          rd}
||	z  ||	|
z  z  fS t          |t                    r                     |j        d                   \  }}|j        dd         D ]I}                     |          \  }}||k    r$t          d                    |||                    ||z  }J||fS t          |t"                    r\                     |j        d                   \  }}|j        D ]-\  }}                     |          \  }}|||z  z  }|||z  z  }.||fS t          |t&                    r5 fd|j        D             } |j        d |D              gd |D             R S t          |t                    rt*          j        |fS |t          d          fS )	zU
        Return tuple with scale factor expression and dimension expression.
        r   r   r   Nz,Dimension of "{}" is {}, but it should be {}c                 :    g | ]}                     |          S r   )_collect_factor_and_dimensionr^   s     r'   r\   z<UnitSystem._collect_factor_and_dimension.<locals>.<listcomp>   s<     + + + 55  + + +r(   c              3   &   K   | ]}|d          V  dS )r   Nr   )r.   fs     r'   r0   z;UnitSystem._collect_factor_and_dimension.<locals>.<genexpr>   s&      22!222222r(   c              3   &   K   | ]}|d          V  dS ra   r   r-   s     r'   r0   z;UnitSystem._collect_factor_and_dimension.<locals>.<genexpr>   s&      ((qad((((((r(   )rb   r   rG   scale_factor	dimensionr   r   rc   rn   r	   r8   rd   r<   is_dimensionlessr   rH   rI   r   rf   r   ri   r
   rh   )r"   re   r   factorrs   r_   
arg_factorarg_dimrR   
exp_factorexp_dimaddendaddend_factor
addend_dimrj   rk   ifactoridimfdss   `                  r'   rn   z(UnitSystem._collect_factor_and_dimension   s    	100000dH%% +	&$dn44c"" )	&F!!Iy % %&*&H&H&M&M#
G*$W$		9$$c"" !	&<<TYGGKFC"&"D"DTX"N"NJ((**;;GDD Z'g1E)FFFc"" 	&<<TYq\JJKFC)ABB- ( (66v>> *z*$ 6$..4f"J/5 /56 6 6 -'3;j)) 	&<<TYq\JJKFC&*&9 # #"U $ B B; O O'5.(tU{"3;h'' 	&+ + + + $	+ + +CDI22c2223 *((C(((* * *i(( 	&5$;1%%r(   c                 H    t          t          d | j                            S )zK
        Return the units of the system that do not have a prefix.
        c                 "    | j          o| j         S r   )is_prefixedis_physical_constant)us    r'   <lambda>z3UnitSystem.get_units_non_prefixed.<locals>.<lambda>   s    $5$Ta>T:T r(   )r   filterr   r2   s    r'   get_units_non_prefixedz!UnitSystem.get_units_non_prefixed   s$     6TTVZVabbcccr(   )__name__
__module____qualname____doc__r   tDictr   r   r!   r3   r6   r:   r<   r?   rD   staticmethodrL   rO   propertyrR   rT   r   rZ   rn   tSetr   __classcell__)r&   s   @r'   r   r      s         M)+"BQU  BD  fkluwl  gA      2 2 2; ; ; "$""t  @B v vdijsu}j}d~ v v v v& & &4 4 4 4 47 7 7 7 7 5 5 \5 . . \. % % X% 9 9 X9 #uY%89 # # # X#  ,0& 0& 0&ddX d d d d d d d dr(   r   N)r   typingr   r   r   r   sympy.core.addr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.powerr	   sympy.core.singletonr
   sympy.physics.units.dimensionsr   sympy.physics.units.quantitiesr   
dimensionsr   r   r   r(   r'   <module>r      s    . - - - - - - -       6 6 6 6 6 6 6 6                   " " " " " " : : : : : : 3 3 3 3 3 3 ! ! ! ! ! !|d |d |d |d |d |d |d |d |d |dr(   