
    g                         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          Z/var/www/html/ai-engine/env/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__-   sS     9??9%		 22 22 $ 022 22 22 )2 )2 2 2r(   c                 0    dt          | j                  z  S )Nz<UnitSystem: %s>)reprr   r1   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   r1   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   sB    ''))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'   rC   z$UnitSystem.get_quantity_scale_factorT   sB    ((**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formatr0   sorted)unit_systems    r'   get_unit_systemzUnitSystem.get_unit_systemZ   sx    k:.. 	j6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   r1   s    r'   dimzUnitSystem.dimm   s     4#$$$r(   c                 4    |                                  j        S )zI
        Check if the underlying dimension system is consistent.
        )r;   is_consistentr1   s    r'   rS   zUnitSystem.is_consistentv   s     ((**88r(   returnc                     | j         S r   )r   r1   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   rX   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-   rZ   s     r'   r/   z2UnitSystem.get_dimensional_expr.<locals>.<genexpr>   s&      ((a16((((((r(   )sympy.physics.unitsr   rF   r   argsr	   rY   r7   expr   r   exprvariable_countr   allr
   Onefuncr>   r   )r"   rc   r   rQ   independentcountra   s   `      r'   rY   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                 V    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 ]k}                     |          \  }}                                                     ||          s$t!          d                    |||                    ||z  }l||fS t          |t$                    r\                     |j        d                   \  }}|j        D ]-\  }}                     |          \  }}|||z  z  }|||z  z  }.||fS t          |t(                    r9 fd|j        D             } fd|D             } |j        d |D              g|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'    PPPs455c::PPPr(   c                     g | ]F}                                                     |d                    rt          d           n|d          GS )r   )r;   is_dimensionlessr   )r-   r.   r"   s     r'   r[   z<UnitSystem._collect_factor_and_dimension.<locals>.<listcomp>   sQ    nnnefD$=$=$?$?$P$PQRSTQU$V$V`IaLLL\]^_\`nnnr(   c              3   &   K   | ]}|d          V  dS )r   Nr   )r-   fs     r'   r/   z;UnitSystem._collect_factor_and_dimension.<locals>.<genexpr>   s&      22!222222r(   )r`   r   rF   scale_factor	dimensionr   r   ra   rl   r	   r7   rb   r;   rn   r   equivalent_dimsrG   rH   r   rd   r   rg   r
   rf   )r"   rc   r   factorrr   r^   
arg_factorarg_dimrQ   
exp_factorexp_dimaddendaddend_factor
addend_dimrh   ri   ifactoridimfdsdimss   `                   r'   rl   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0022BB3
SS 6$..4f"J/5 /56 6 6 -'3;j)) 	&<<TYq\JJKFC&*&9 # #"U $ B B; O O'5.(tU{"3;h'' 	&PPPPdiPPPCnnnnjmnnnDDI22c2223;d;;;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   r1   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!   r2   r5   r9   r;   r>   rC   staticmethodrK   rN   propertyrQ   rS   r   rY   rl   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#  ,/& /& /&b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(   