o
    §ƒðj?  ã                   @   sà   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mZmZmZmZ d d	lmZ d d
lmZ d dlmZmZmZ d dlmZ d dlmZ d dl m!Z! G dd„ deƒZ"dd„ Z#dS )é    )ÚRational)ÚS)Úsymbols)Úsign)Úsqrt)Úgcd)Ú
Complement)ÚBasicÚTupleÚdiffÚexpandÚEqÚInteger)Úordered)Ú_symbol)ÚsolvesetÚnonlinsolveÚdiophantine©Útotal_degree)ÚPoint)Úcorec                       sv   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zddd„Z‡  ZS )ÚImplicitRegiona¡  
    Represents an implicit region in space.

    Examples
    ========

    >>> from sympy import Eq
    >>> from sympy.abc import x, y, z, t
    >>> from sympy.vector import ImplicitRegion

    >>> ImplicitRegion((x, y), x**2 + y**2 - 4)
    ImplicitRegion((x, y), x**2 + y**2 - 4)
    >>> ImplicitRegion((x, y), Eq(y*x, 1))
    ImplicitRegion((x, y), x*y - 1)

    >>> parabola = ImplicitRegion((x, y), y**2 - 4*x)
    >>> parabola.degree
    2
    >>> parabola.equation
    -4*x + y**2
    >>> parabola.rational_parametrization(t)
    (4/t**2, 4/t)

    >>> r = ImplicitRegion((x, y, z), Eq(z, x**2 + y**2))
    >>> r.variables
    (x, y, z)
    >>> r.singular_points()
    EmptySet
    >>> r.regular_point()
    (-10, -10, 200)

    Parameters
    ==========

    variables : tuple to map variables in implicit equation to base scalars.

    equation : An expression or Eq denoting the implicit equation of the region.

    c                    s8   t |tƒs	t|Ž }t |tƒr|j|j }tƒ  | ||¡S ©N)Ú
isinstancer
   r   ÚlhsÚrhsÚsuperÚ__new__)ÚclsÚ	variablesÚequation©Ú	__class__© ú�/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/vector/implicitregion.pyr   9   s
   

zImplicitRegion.__new__c                 C   ó
   | j d S )Nr   ©Úargs©Úselfr$   r$   r%   r    B   ó   
zImplicitRegion.variablesc                 C   r&   )Né   r'   r)   r$   r$   r%   r!   F   r+   zImplicitRegion.equationc                 C   s
   t | jƒS r   )r   r!   r)   r$   r$   r%   ÚdegreeJ   r+   zImplicitRegion.degreec                 C   sZ  | j }t| jƒdkrtt|| jd tjd�ƒd fS t| jƒdkrT| jdkrTt| j|ƒ }\}}}}}}|d d| | krI| j	|Ž \}	}
|	|
fS | j
|Ž \}	}
|	|
fS t| jƒdkrš| j\}}}tddƒD ]3}	tddƒD ]+}
t| ||	||
i¡| jd tjd�js˜|	|
tt| ||	||
i¡ƒƒd f    S qmqft|  ¡ ƒdkrªt|  ¡  d S tƒ ‚)	a0  
        Returns a point on the implicit region.

        Examples
        ========

        >>> from sympy.abc import x, y, z
        >>> from sympy.vector import ImplicitRegion
        >>> circle = ImplicitRegion((x, y), (x + 2)**2 + (y - 3)**2 - 16)
        >>> circle.regular_point()
        (-2, -1)
        >>> parabola = ImplicitRegion((x, y), x**2 - 4*y)
        >>> parabola.regular_point()
        (0, 0)
        >>> r = ImplicitRegion((x, y, z), (x + y + z)**4)
        >>> r.regular_point()
        (-10, -10, 20)

        References
        ==========

        - Erik Hillgarter, "Rational Points on Conics", Diploma Thesis, RISC-Linz,
          J. Kepler Universitat Linz, 1996. Available:
          https://www3.risc.jku.at/publications/download/risc_1355/Rational%20Points%20on%20Conics.pdf

        r,   r   )Údomainé   é   é   iöÿÿÿé
   )r!   Úlenr    Úlistr   r   ÚRealsr-   Úconic_coeffÚ_regular_point_parabolaÚ_regular_point_ellipseÚrangeÚsubsÚis_emptyÚsingular_pointsÚNotImplementedError)r*   r!   ÚcoeffsÚaÚbÚcÚdÚeÚfÚx_regÚy_regÚxÚyÚzr$   r$   r%   Úregular_pointN   s,    
ÿ&,ÿÿzImplicitRegion.regular_pointc                 C   s"  ||fdko||fdko|d d| | ko||fdk}|s"t dƒ‚|dkrUd| | d| |  d| | |d  }}	|dkrR|	 | }
|||
   d|  }n5d}n2|dkr‡d| | d| |  d| | |d  }}	|dkr…|	 | }|||   d|  }
nd}|r�||
fS t dƒ‚)N)r   r   r/   r0   ú*Rational Point on the conic does not existr   F)Ú
ValueError)r*   r?   r@   rA   rB   rC   rD   ÚokÚd_dashÚf_dashrF   rE   r$   r$   r%   r7   …   s$   8.
.
z&ImplicitRegion._regular_point_parabolac           .         s4  d| | |d  }|}|st dƒ‚|dkr'|dkr'd}	d|| ||   }
nY|dkr`|}	d|d  |d  d| | | |  d| | |d   d|d  | |  d| |d  |  }
n |}	d|d  |d  d| | | |  d|d  | |  }
|
dkoŒ|	dko‹|
dk  }|s“t dƒ‚t|	ƒ d¡}	t|
ƒ d¡}
|	j|	j}}|
j|
j}}t||ƒ}|| | }|| | }||  | }t|ƒtt|ƒdƒ }t	|| ƒ}t|ƒtt|ƒdƒ }t	|| ƒ}t|ƒtt|ƒdƒ }t	|| ƒ}tt||ƒ|ƒ}|| }|| }|| }t||ƒ}|| }|| }|| }t||ƒ}|| }|| }|| }t||ƒ}|| }|| }|| }t
dƒ\}}}||d  ||d   ||d   }t|ƒ} t| ƒdk�rit dƒ‚d	}!| D ]¤}"t|"Ž j}#t |#d
¡‰ |"d }$|$dk�r‡d}!�qmt|$ttfƒ�s|$j}%t|%ƒdk�rµtt|%ƒƒ}&ttjtt|$dƒ|&tjƒƒ}'tt|'ƒƒˆ |&< t|%ƒdk�rñtt|%ƒƒ\}&}(tjD ])})|$ |&|)¡}*ttjtt|*dƒ|(tjƒƒ}+|+j�sï|)ˆ |&< tt|+ƒƒˆ |(<  n�qÇt|#ƒdk�rt‡ fdd„|"D ƒƒ\}}}n|"\}}}d	}! n�qm|!�rt dƒ‚|| | }|| | }|| | }|| }|| }|dk�rY|dk�rY|| d|  d|  },|| d|  d|  }-|,|-fS |dk�r||d| |  ||  |	 },|||,  | d|  }-|,|-fS |d| |  ||  |	 }-|||-  | d|  },|,|-fS )Nr0   r/   rK   r   éÿÿÿÿé   l    J)£zx y zFr1   Tr,   c                 3   ó   � | ]}|  ˆ ¡V  qd S r   ©r:   ©Ú.0Ús©Úrepr$   r%   Ú	<genexpr>   ó   € z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>) rL   r   Úlimit_denominatorÚpÚqr   r   r   Úabsr   r   r   r3   r
   Úfree_symbolsÚdictÚfromkeysr   Úintr   ÚnextÚiterr   r   ÚIntegersr   r   r4   r   r:   r;   Útuple).r*   r?   r@   rA   rB   rC   rD   ÚDrM   ÚKÚLÚk1Úk2Úl1Úl2ÚgÚa1Úb1Úc1Úa2Úr1Úb2Úr2Úc2Úr3Úg1Úg2Úg3rG   rH   rI   ÚeqÚ	solutionsÚflagÚsolÚsymsÚsol_zÚsyms_zr\   Úp_valuesr]   ÚiÚ
subs_sol_zÚq_valuesrE   rF   r$   rW   r%   r8   Ÿ   sÄ   f<



$


ý
ç
ùýz%ImplicitRegion._regular_point_ellipsec                 C   s6   | j g}| jD ]}|t| j |ƒg7 }qt|t| jƒƒS )aŸ  
        Returns a set of singular points of the region.

        The singular points are those points on the region
        where all partial derivatives vanish.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy.vector import ImplicitRegion
        >>> I = ImplicitRegion((x, y), (y-1)**2 -x**3 + 2*x**2 -x)
        >>> I.singular_points()
        {(1, 1)}

        )r!   r    r   r   r4   )r*   Úeq_listÚvarr$   r$   r%   r<     s   
zImplicitRegion.singular_pointsc                 C   s€   t |tƒr|j}| j}t| jƒD ]\}}| ||||  ¡}qt|ƒ}t|jƒdkr8|j}t	dd„ |D ƒƒ}|S |}t
|ƒ}|S )a  
        Returns the multiplicity of a singular point on the region.

        A singular point (x,y) of region is said to be of multiplicity m
        if all the partial derivatives off to order m - 1 vanish there.

        Examples
        ========

        >>> from sympy.abc import x, y, z
        >>> from sympy.vector import ImplicitRegion
        >>> I = ImplicitRegion((x, y, z), x**2 + y**3 - z**4)
        >>> I.singular_points()
        {(0, 0, 0)}
        >>> I.multiplicity((0, 0, 0))
        2

        r   c                 s   s   � | ]}t |ƒV  qd S r   r   )rU   Útermr$   r$   r%   rY   P  s   € z.ImplicitRegion.multiplicity.<locals>.<genexpr>)r   r   r(   r!   Ú	enumerater    r:   r   r3   Úminr   )r*   ÚpointÚmodified_eqrƒ   r‡   ÚtermsÚmr$   r$   r%   Úmultiplicity2  s   
ýzImplicitRegion.multiplicity©ÚtrV   Nc                    sr  | j }| j}|dkr0t| jƒdkr|fS t| jƒdkr-| j\}}tt||ƒƒd }||fS tƒ ‚d}|dkrR|dur=|}nt|  ¡ ƒdkrNt|  ¡ ƒd }n|  ¡ }t|  ¡ ƒdkrŒ|  ¡ }	|	D ]+}
t	|
Ž j
}t |d¡‰ t|ƒdkr~t‡ fdd„|
D ƒƒ}
|  |
¡|d kr‹|
} nq`t|ƒdkr•tƒ ‚|}t| jƒD ]\}}| ||||  ¡}qœt|ƒ}d }}|jD ]}t|ƒ|krÃ||7 }q¶||7 }q¶d| }t|tƒsÔ|f}t| jƒdk�r5|d }|d	krëtd
dd�}ntd	dd�}t|dd�}| | jd || jd |i¡}| | jd || jd |i¡}|||   |d¡|d  }|||   |d¡|d  }||fS t| jƒdk�r¶|\}}d|v �rMtddd�}ntddd�}t|dd�}t|dd�}| | jd || jd || jd |i¡}| | jd || jd || jd |i¡}|||   |d¡|d  }|||   |d¡|d  }|||   |d¡|d  }|||fS tƒ ‚)aÉ  
        Returns the rational parametrization of implicit region.

        Examples
        ========

        >>> from sympy import Eq
        >>> from sympy.abc import x, y, z, s, t
        >>> from sympy.vector import ImplicitRegion

        >>> parabola = ImplicitRegion((x, y), y**2 - 4*x)
        >>> parabola.rational_parametrization()
        (4/t**2, 4/t)

        >>> circle = ImplicitRegion((x, y), Eq(x**2 + y**2, 4))
        >>> circle.rational_parametrization()
        (4*t/(t**2 + 1), 4*t**2/(t**2 + 1) - 2)

        >>> I = ImplicitRegion((x, y), x**3 + x**2 - y**2)
        >>> I.rational_parametrization()
        (t**2 - 1, t*(t**2 - 1))

        >>> cubic_curve = ImplicitRegion((x, y), x**3 + x**2 - y**2)
        >>> cubic_curve.rational_parametrization(parameters=(t))
        (t**2 - 1, t*(t**2 - 1))

        >>> sphere = ImplicitRegion((x, y, z), x**2 + y**2 + z**2 - 4)
        >>> sphere.rational_parametrization(parameters=(t, s))
        (-2 + 4/(s**2 + t**2 + 1), 4*s/(s**2 + t**2 + 1), 4*t/(s**2 + t**2 + 1))

        For some conics, regular_points() is unable to find a point on curve.
        To calulcate the parametric representation in such cases, user need
        to determine a point on the region and pass it using reg_point.

        >>> c = ImplicitRegion((x, y), (x  - 1/2)**2 + (y)**2 - (1/4)**2)
        >>> c.rational_parametrization(reg_point=(3/4, 0))
        (0.75 - 0.5/(t**2 + 1), -0.5*t/(t**2 + 1))

        References
        ==========

        - Christoph M. Hoffmann, "Conversion Methods between Parametric and
          Implicit Curves and Surfaces", Purdue e-Pubs, 1990. Available:
          https://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1827&context=cstech

        r,   r/   r   r$   Nc                 3   rR   r   rS   rT   rW   r$   r%   rY   ¨  rZ   z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>rP   rV   Ús_T)Úrealr1   ÚrÚr_)r!   r-   r3   r    r4   r   r=   r<   rJ   r
   r_   r`   ra   rf   r�   r‰   r:   r   r(   r   r   r   )r*   Ú
parametersÚ	reg_pointr!   r-   rG   rH   Úy_parr‹   r<   Úspointr   rŒ   rƒ   r‡   ÚhnÚhn_1rˆ   Ú
parameter1rV   r‘   Úx_parÚ
parameter2r”   Úz_parr$   rW   r%   Úrational_parametrizationW  s†   /

þ




((
z'ImplicitRegion.rational_parametrization)r�   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr    r!   r-   rJ   r7   r8   r<   r�   r    Ú__classcell__r$   r$   r"   r%   r      s    '	


7|%r   c           
      C   s    t |ƒdkr	tƒ ‚| d }| d }t|ƒ}| |d ¡}| || ¡}| |d ¡}| |d¡ |d¡}| |d¡ |d¡}| |d¡ |d¡}	||||||	fS )Nr/   r   r,   )r   rL   r   Úcoeff)
r    r!   rG   rH   r?   r@   rA   rB   rC   rD   r$   r$   r%   r6   í  s   r6   N)$Úsympy.core.numbersr   Úsympy.core.singletonr   Úsympy.core.symbolr   Ú$sympy.functions.elementary.complexesr   Ú(sympy.functions.elementary.miscellaneousr   Úsympy.polys.polytoolsr   Úsympy.sets.setsr   Ú
sympy.corer	   r
   r   r   r   r   Úsympy.core.sortingr   r   Úsympy.solversr   r   r   Úsympy.polysr   Úsympy.geometryr   Úsympy.ntheory.factor_r   r   r6   r$   r$   r$   r%   Ú<module>   s&        _