o
    jgQ                     @  s   d Z ddlmZ ddl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lmZ ddlmZ ddlmZ ddlmZ G dd dZG dd deZG dd deeZdS )a  
Definition of physical dimensions.

Unit systems will be constructed on top of these dimensions.

Most of the examples in the doc use MKS system and are presented from the
computer point of view: from a human point, adding length to time is not legal
in MKS but it is in natural system; for a computer in natural system there is
no time dimension (but a velocity dimension instead) - in the basis - so the
question of adding time to length has no meaning.
    )annotationsN)reduce)Basic)DictTuple)S)default_sort_key)Symbol)sympify)Matrix)TrigonometricFunction)ExprPowc                   @  sZ   e Zd ZU i Zded< i Zded< i Zded< dd Zdd Zd	d
 Z	dd Z
dd ZdS )_QuantityMapperzdict[Expr, Expr]_quantity_scale_factors_global,_quantity_dimensional_equivalence_map_global_quantity_dimension_globalc                 O  s   i | _ i | _d S N)_quantity_dimension_map_quantity_scale_factors)selfargskwargs r   V/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/units/dimensions.py__init__$   s   
z_QuantityMapper.__init__c                 C  sZ   ddl m} t|}t|ts|dkrtd}ntdt||r&| |}|| j|< dS )z
        Set the dimension for the quantity in a unit system.

        If this relation is valid in every unit system, use
        ``quantity.set_global_dimension(dimension)`` instead.
        r   Quantity   zexpected dimension or 1N)sympy.physics.unitsr   r
   
isinstance	Dimension
ValueErrorget_quantity_dimensionr   )r   quantity	dimensionr   r   r   r   set_quantity_dimension(   s   



z&_QuantityMapper.set_quantity_dimensionc                   sb   ddl m ddlm  t|}| fdddd }|fddfdd}|j|< d	S )
a  
        Set the scale factor of a quantity relative to another quantity.

        It should be used only once per quantity to just one other quantity,
        the algorithm will then be able to compute the scale factors to all
        other quantities.

        In case the scale factor is valid in every unit system, please use
        ``quantity.set_global_relative_scale_factor(scale_factor)`` instead.
        r   r   Prefixc                   
   t |  S r   r!   xr(   r   r   <lambda>J      
 z;_QuantityMapper.set_quantity_scale_factor.<locals>.<lambda>c                 S     | j S r   )scale_factorr,   r   r   r   r.   K   s    c                   r*   r   r+   r,   r   r   r   r.   O   r/   c                   s
     | S r   )get_quantity_scale_factorr,   r   r   r   r.   P   r/   N)r    r   sympy.physics.units.prefixesr)   r
   replacer   )r   r%   r1   r   )r)   r   r   r   set_quantity_scale_factor:   s   


z)_QuantityMapper.set_quantity_scale_factorc                 C  s   ddl m} || jv r| j| S || jv r| j| S || jv r5| j| }t||r.| |S t| |S t||r?t|j	S tdS )Nr   r   r   )
r    r   r   r   r   r!   r$   r"   get_dimensional_exprname)r   unitr   dep_unitr   r   r   r$   T   s   









z&_QuantityMapper.get_quantity_dimensionc                 C  s@   || j v r
| j | S || jv r| j| \}}|| | S tjS r   )r   r   r2   r   One)r   r9   
mul_factor
other_unitr   r   r   r2   f   s   


z)_QuantityMapper.get_quantity_scale_factorN)__name__
__module____qualname__r   __annotations__r   r   r   r'   r6   r$   r2   r   r   r   r   r      s   
 r   c                      s   e Zd ZdZdZi ZdZdZdZdZ	d*ddZ
edd	 Zed
d Zdd Zdd Zdd Z fddZdd Zdd Zdd Zdd Zdd Z fddZd d! Zd"d# Zd$d% Zed&d' Zd(d) Z  ZS )+r"   a  
    This class represent the dimension of a physical quantities.

    The ``Dimension`` constructor takes as parameters a name and an optional
    symbol.

    For example, in classical mechanics we know that time is different from
    temperature and dimensions make this difference (but they do not provide
    any measure of these quantites.

        >>> from sympy.physics.units import Dimension
        >>> length = Dimension('length')
        >>> length
        Dimension(length)
        >>> time = Dimension('time')
        >>> time
        Dimension(time)

    Dimensions can be composed using multiplication, division and
    exponentiation (by a number) to give new dimensions. Addition and
    subtraction is defined only when the two objects are the same dimension.

        >>> velocity = length / time
        >>> velocity
        Dimension(length/time)

    It is possible to use a dimension system object to get the dimensionsal
    dependencies of a dimension, for example the dimension system used by the
    SI units convention can be used:

        >>> from sympy.physics.units.systems.si import dimsys_SI
        >>> dimsys_SI.get_dimensional_dependencies(velocity)
        {Dimension(length, L): 1, Dimension(time, T): -1}
        >>> length + length
        Dimension(length)
        >>> l2 = length**2
        >>> l2
        Dimension(length**2)
        >>> dimsys_SI.get_dimensional_dependencies(l2)
        {Dimension(length, L): 2}

    g      *@TFNc                 C  st   t |tr
t|}nt|}t |tstdt |tr!t|}n|d ur,t |ts,J t| |}||_||_|S )Nz2Dimension name needs to be a valid math expression)	r!   strr	   r
   r   	TypeError__new___name_symbol)clsr8   symbolobjr   r   r   rD      s   




zDimension.__new__c                 C  r0   r   )rE   r3   r   r   r   r8         zDimension.namec                 C  r0   r   )rF   r3   r   r   r   rH      rJ   zDimension.symbolc                 C  s$   | j du r
d| j S d| j| j f S )zE
        Display the string representation of the dimension.
        NzDimension(%s)zDimension(%s, %s)rH   r8   r3   r   r   r   __str__   s   

zDimension.__str__c                 C  s   |   S r   )rL   r3   r   r   r   __repr__   s   zDimension.__repr__c                 C  s   | S r   r   r3   r   r   r   __neg__   s   zDimension.__neg__c                   sV   ddl m} t|}t|tr)||rtdt|tr#| |kr#| S t 	|S | S Nr   r   z!cannot sum dimension and quantity)
sympy.physics.units.quantitiesr   r
   r!   r   hasrC   r"   super__add__r   otherr   	__class__r   r   rS      s   

zDimension.__add__c                 C  
   |  |S r   )rS   r   rU   r   r   r   __radd__      
zDimension.__radd__c                 C     | | S r   r   rY   r   r   r   __sub__      zDimension.__sub__c                 C  r\   r   r   rY   r   r   r   __rsub__   r^   zDimension.__rsub__c                 C  rX   r   )_eval_powerrY   r   r   r   __pow__   r[   zDimension.__pow__c                 C  s   t |}t| j| S r   )r
   r"   r8   rY   r   r   r   r`      s   zDimension._eval_powerc                   s\   ddl m} t|tr,||rtdt|tr!t| j|j S |js&| S t	 
|S | S rO   )rP   r   r!   r   rQ   rC   r"   r8   free_symbolsrR   __mul__rT   rV   r   r   rc      s   


zDimension.__mul__c                 C  rX   r   )rc   rY   r   r   r   __rmul__   r[   zDimension.__rmul__c                 C  s   | t |d S Nr   rY   r   r   r   __truediv__      zDimension.__truediv__c                 C  s   |t | d S re   )powrY   r   r   r   __rtruediv__  rh   zDimension.__rtruediv__c                 C  s   t dd dd | D dS )Nc                 S  s   | | S r   r   r-   yr   r   r   r.     s    z:Dimension._from_dimensional_dependencies.<locals>.<lambda>c                 s  s    | ]	\}}|| V  qd S r   r   ).0der   r   r   	<genexpr>  s    
z;Dimension._from_dimensional_dependencies.<locals>.<genexpr>r   )r   items)rG   dependenciesr   r   r   _from_dimensional_dependencies  s
   z(Dimension._from_dimensional_dependenciesc                 C  s   t dd ||  D S )a  
        Check if the dimension object has only integer powers.

        All the dimension powers should be integers, but rational powers may
        appear in intermediate steps. This method may be used to check that the
        final result is well-defined.
        c                 s  s    | ]}|j V  qd S r   )
is_Integer)rm   dpowr   r   r   rp     s    z/Dimension.has_integer_powers.<locals>.<genexpr>)allget_dimensional_dependenciesvalues)r   dim_sysr   r   r   has_integer_powers  s   	zDimension.has_integer_powersr   )r>   r?   r@   __doc___op_priority_dimensional_dependenciesis_commutative	is_numberis_positiveis_realrD   propertyr8   rH   rL   rM   rN   rS   rZ   r]   r_   ra   r`   rc   rd   rg   rj   classmethodrs   rz   __classcell__r   r   rV   r   r"   o   s:    +


	
r"   c                   @  s   e Zd ZdZdi fddZedd Zedd Zed	d
 Zdd Z	d'ddZ
dd Zd(ddZdd Zedd Zedd Zedd Zdd Zdd  Zd!d" Zed#d$ Zed%d& ZdS ))DimensionSystema  
    DimensionSystem represents a coherent set of dimensions.

    The constructor takes three parameters:

    - base dimensions;
    - derived dimensions: these are defined in terms of the base dimensions
      (for example velocity is defined from the division of length by time);
    - dependency of dimensions: how the derived dimensions depend
      on the base dimensions.

    Optionally either the ``derived_dims`` or the ``dimensional_dependencies``
    may be omitted.
    r   c                   sf  t |}dd fdd|D }fdd|D }|D ]$}||v r8t|| dks4|| |d dkr8tdt|di||< qdd	 | D ]}|}||vr\||vr\|| qIfd
d  fdd| D }|D ]}||v r}td| ||vrt|di||< qq|j	t
d |j	t
d t| }t| }tdd | D }t| |||}|S )Nc                 S  sL   t | trtt| } | S t | tr	 | S t | tr t| } | S td|  )Nz%s wrong type)r!   rB   r"   r	   rC   dimr   r   r   	parse_dim0  s   


z*DimensionSystem.__new__.<locals>.parse_dimc                      g | ]} |qS r   r   rm   ir   r   r   
<listcomp>;      z+DimensionSystem.__new__.<locals>.<listcomp>c                   r   r   r   r   r   r   r   r   <  r   r   z!Repeated value in base dimensionsc                 S  sJ   t | tr| S t | trtt| S t | trt| S tdt| | f )Nzunrecognized type %s for %s)r!   r"   rB   r	   rC   typer   r   r   r   parse_dim_nameE  s   


z/DimensionSystem.__new__.<locals>.parse_dim_namec                   s   t  fdd|  D S )Nc                   s   i | ]	\}} ||qS r   r   rm   r   jr   r   r   
<dictcomp>U      z?DimensionSystem.__new__.<locals>.parse_dict.<locals>.<dictcomp>)r   rq   )rn   r   r   r   
parse_dictT  s   z+DimensionSystem.__new__.<locals>.parse_dictc                   s   i | ]\}}| |qS r   r   r   )r   r   r   r   r   X      z+DimensionSystem.__new__.<locals>.<dictcomp>z%Dimension %s both in base and derivedkeyc                 S  s   i | ]	\}}|t |qS r   )r   r   r   r   r   r   g  r   )dictlenget
IndexErrorr   keysappendrq   r#   sortr   r   r   rD   )rG   	base_dimsderived_dimsdimensional_dependenciesr   rI   r   )r   r   r   r   rD   -  sB   

zDimensionSystem.__new__c                 C  
   | j d S )Nr   r   r3   r   r   r   r   k     
zDimensionSystem.base_dimsc                 C  r   Nr   r   r3   r   r   r   r   o  r   zDimensionSystem.derived_dimsc                 C  r   )N   r   r3   r   r   r   r   s  r   z(DimensionSystem.dimensional_dependenciesc           
        sH  t trttn	t tstjjr$t| jdiS jj	s,jj
r.i S | jjjrdtt}fddjjD   D ]}| D ]\}}||  |7  < qMqGdd | D S jjrfddjjD  t fdd dd  D r d	 S td
jjrjj}jj}|i ksjjjrfdd| D S tdjjrfddjjD }jj| }	fddjjD  t |	tr| |	S |	jjjkrt jtr d	 i tddifv ri S tdjtdd  D ri S tdj|	S tdtj)Nr   c                   r   r   r   r   get_for_namer   r   r     r   zJDimensionSystem._get_dimensional_dependencies_for_name.<locals>.<listcomp>c                 S  s   i | ]\}}|d kr||qS )r   r   rm   kvr   r   r   r     r   zJDimensionSystem._get_dimensional_dependencies_for_name.<locals>.<dictcomp>c                   r   r   r   r   r   r   r   r     r   c                 3  s    | ]	}| d  kV  qdS )r   Nr   rm   rn   )dictsr   r   rp     s    zIDimensionSystem._get_dimensional_dependencies_for_name.<locals>.<genexpr>r   z6Only equivalent dimensions can be added or subtracted.c                   s   i | ]\}}|| j j qS r   )r8   expr   )r&   r   r   r     r   zFThe exponent for the power operator must be a Symbol or dimensionless.c                 3  s    | ]
}t  |V  qd S r   )r"   rs   )rm   argr   r   r   rp     s    c                   r   r   r   r   r   r   r   r     r   anglezYThe input argument for the function {} must be dimensionless or have dimensions of angle.c                 s  s    | ]}|i kV  qd S r   r   )rm   itemr   r   r   rp     s    z>The input arguments for the function {} must be dimensionless.z8Type {} not implemented for get_dimensional_dependencies)r!   rB   r"   r	   r8   	is_Symbolr   r   r   r   is_NumberSymbol&_get_dimensional_dependencies_for_nameis_Mulcollectionsdefaultdictintr   rq   is_Addrv   rC   is_Powbaser   is_Functionfuncrw   r   formatr   )
r   r&   retrn   r   r   dim_basedim_expr   resultr   )r   r&   r   r   r   w  s\   






z6DimensionSystem._get_dimensional_dependencies_for_nameFc                 C  s.   |  |}|r|i krtddiS t| S r   )r   r"   r   rq   )r   r8   mark_dimensionlessdimdepr   r   r   rw     s   
z,DimensionSystem.get_dimensional_dependenciesc                 C  s   |  |}|  |}||kS r   )rw   )r   dim1dim2deps1deps2r   r   r   equivalent_dims  s   

zDimensionSystem.equivalent_dimsNc                 C  s`   t | j}|r|| tt| jt| t| jt| |}|j| j |j| j |S r   )	r   r   updater   tupler   r   r   r   )r   new_base_dimsnew_derived_dimsnew_dim_depsdepsnew_dim_sysr   r   r   extend  s   

zDimensionSystem.extendc                 C  s   |j dkrdS | |i kS )z
        Check if the dimension object really has a dimension.

        A dimension should have at least one component with non-zero power.
        r   T)r8   rw   )r   r&   r   r   r   is_dimensionless  s   
z DimensionSystem.is_dimensionlessc                 C  s:   t  }| jD ]}|t | |  qtt|tdS )z
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        List all canonical dimension names.
        r   )setr   r   rw   r   r   sortedrB   )r   dimsetr   r   r   r   list_can_dims  s   	
zDimensionSystem.list_can_dimsc                   s"   t dd  fdd jD }|S )ap  
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        Compute the inverse transformation matrix from the base to the
        canonical dimension basis.

        It corresponds to the matrix where columns are the vector of base
        dimensions in canonical basis.

        This matrix will almost never be used because dimensions are always
        defined with respect to the canonical basis, so no work has to be done
        to get them in this basis. Nonetheless if this matrix is not square
        (or not invertible) it means that we have chosen a bad basis.
        c                 S  rX   r   row_joinrk   r   r   r   r.     r/   z7DimensionSystem.inv_can_transf_matrix.<locals>.<lambda>c                      g | ]}  |qS r   dim_can_vectorr   r3   r   r   r         z9DimensionSystem.inv_can_transf_matrix.<locals>.<listcomp>)r   r   )r   matrixr   r3   r   inv_can_transf_matrix  s   z%DimensionSystem.inv_can_transf_matrixc                   s*   t dd  fddt jtdD  S )a!  
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        Return the canonical transformation matrix from the canonical to the
        base dimension basis.

        It is the inverse of the matrix computed with inv_can_transf_matrix().
        c                 S  rX   r   r   rk   r   r   r   r.     r/   z3DimensionSystem.can_transf_matrix.<locals>.<lambda>c                   r   r   r   r   r3   r   r   r     r   z5DimensionSystem.can_transf_matrix.<locals>.<listcomp>r   )r   r   r   rB   invr3   r   r3   r   can_transf_matrix  s
   z!DimensionSystem.can_transf_matrixc                 C  s0   g }| j D ]}|| ||d qt|S )z
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        Dimensional representation in terms of the canonical base dimensions.
        r   )r   r   rw   r   r   )r   r   vecrn   r   r   r   r     s   	
zDimensionSystem.dim_can_vectorc                 C  s   | j t| | S )z
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.


        Vector representation in terms of the base dimensions.
        )r   r   r   )r   r   r   r   r   
dim_vector  s   	zDimensionSystem.dim_vectorc                 C  sD   |  |}dd | jD }tj}t||D ]
\}}||| 9 }q|S )zY
        Give the string expression of a dimension in term of the basis symbols.
        c                 S  s"   g | ]}|j d ur|j n|jqS r   rK   r   r   r   r   r   .  s   " z2DimensionSystem.print_dim_base.<locals>.<listcomp>)r   r   r   r;   zip)r   r   dimssymbolsresspr   r   r   print_dim_base)  s   
zDimensionSystem.print_dim_basec                 C  s
   t | jS )z
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        Give the dimension of the system.

        That is return the number of dimensions forming the basis.
        )r   r   r3   r   r   r   r   4  s   
zDimensionSystem.dimc                 C  s   | j jS )z
        Useless method, kept for compatibility with previous versions.

        DO NOT USE.

        Check if the system is well defined.
        )r   	is_squarer3   r   r   r   is_consistentA  s   zDimensionSystem.is_consistent)F)r   N)r>   r?   r@   r{   rD   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s6    >



?





r   )r{   
__future__r   r   	functoolsr   sympy.core.basicr   sympy.core.containersr   r   sympy.core.singletonr   sympy.core.sortingr   sympy.core.symbolr	   sympy.core.sympifyr
   sympy.matrices.denser   (sympy.functions.elementary.trigonometricr   sympy.core.exprr   sympy.core.powerr   r   r"   r   r   r   r   r   <module>   s$    Q /