o
    8Vad>                     @   s   d dl mZmZmZmZmZmZ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 )    )SRationalgcdsqrtsignsymbols
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()
    {(0, 0, 0)}
    >>> 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   ZlhsZrhssuper__new__)cls	variablesequation	__class__ =/usr/lib/python3/dist-packages/sympy/vector/implicitregion.pyr   3   s
   

zImplicitRegion.__new__c                 C   
   | j d S )Nr   argsselfr"   r"   r#   r   <      
zImplicitRegion.variablesc                 C   r$   )N   r%   r'   r"   r"   r#   r   @   r)   zImplicitRegion.equationc                 C   s
   t | jS r   )r   r   r'   r"   r"   r#   degreeD   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rt|   d S t )	a1  
        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. Availaible:
          https://www3.risc.jku.at/publications/download/risc_1355/Rational%20Points%20on%20Conics.pdf

        r*   r   )domain         i
   )r   lenr   listr   r   ZRealsr+   conic_coeff_regular_point_parabola_regular_point_ellipserangesubsis_emptysingular_pointsNotImplementedError)r(   r   Zcoeffsabcdefx_regy_regxyzr"   r"   r#   regular_pointH   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rd| | d| |  d| | |d  }}	|dkr|	 | }|||   d|  }
nd}|r||
fS t d)N)r   r   r-   r.   *Rational Point on the conic does not existr   F)
ValueError)r(   r;   r<   r=   r>   r?   r@   okZd_dashZf_dashrB   rA   r"   r"   r#   r4      s$   8.
.
z&ImplicitRegion._regular_point_parabolac           .         s>  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  }|st 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}#d
d |#D  |"d }$|$dkrd}!qmt|$tst|$ts|$j}%t|%dkrtt|%}&ttjtt|$d|&tj}'tt|' |&< t|%dkrtt|%\}&}(tjD ])})|$|&|)}*ttjtt|*d|(tj}+|+js|) |&< tt|+ |(<  nqt|#dkrt fdd|"D \}}}n|"\}}}d	}! nqm|!rt d|| | }|| | }|| | }|| }|| }|dkr^|dkr^|| d|  d|  },|| d|  d|  }-|,|-fS |dkr|d| |  ||  |	 },|||,  | d|  }-|,|-fS |d| |  ||  |	 }-|||-  | d|  },|,|-fS )Nr.   r-   rG   r      l    J)zx y zFc                 S      i | ]}|d qS )r/   r"   .0sr"   r"   r#   
<dictcomp>       z9ImplicitRegion._regular_point_ellipse.<locals>.<dictcomp>Tr*   c                 3       | ]}|  V  qd S r   r7   rM   Zrepr"   r#   	<genexpr>       z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>)rH   r   Zlimit_denominatorpqr   r   r   absr   r   r   r1   r
   free_symbolsr   r   intnextiterr   r   ZIntegersr   r   r2   r   r7   r8   tuple).r(   r;   r<   r=   r>   r?   r@   DrI   KLZk1Zk2l1l2gZa1Zb1Zc1Za2Zr1Zb2Zr2Zc2Zr3Zg1Zg2Zg3rC   rD   rE   eqZ	solutionsflagZsolsymsZsol_zZsyms_zrW   Zp_valuesrX   iZ
subs_sol_zZq_valuesrA   rB   r"   rT   r#   r5      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   r2   )r(   Zeq_listvarr"   r"   r#   r9     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   g | ]}t |qS r"   r   )rN   termr"   r"   r#   
<listcomp>J  s    z/ImplicitRegion.multiplicity.<locals>.<listcomp>)r   r   r&   r   	enumerater   r7   r   r1   minr   )r(   pointmodified_eqrh   ri   Ztermsmr"   r"   r#   multiplicity,  s   
zImplicitRegion.multiplicitytrO   Nc                    s~  | 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
}dd |D  t|dkrt fdd	|
D }
| |
|d kr|
} nq`t|dkrt |}t| jD ]\}}|||||  }qt|}d }}|jD ]}t||kr||7 }q||7 }qd
| }t|ts|f}t| jdkr6|d }|dkrt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ksL|dkrSt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 implict 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                 S   rL   )r-   r"   rM   r"   r"   r#   rP     rQ   z;ImplicitRegion.rational_parametrization.<locals>.<dictcomp>c                 3   rR   r   rS   rM   rT   r"   r#   rU     rV   z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>rJ   rO   Zs_T)realr/   rZr_)r   r+   r1   r   r2   r   r:   r9   rF   r
   rZ   r^   rq   rl   r7   r   r&   r   r   r   )r(   Z
parametersZ	reg_pointr   r+   rC   rD   Zy_parrn   r9   Zspointrg   ro   rh   ri   ZhnZhn_1rj   Z
parameter1rO   rs   Zx_parZ
parameter2ru   Zz_parr"   rT   r#   rational_parametrizationQ  s   /





((
z'ImplicitRegion.rational_parametrization)rr   N)__name__
__module____qualname____doc__r   propertyr   r   r+   rF   r4   r5   r9   rq   rv   __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   rH   r   Zcoeff)
r   r   rC   rD   r;   r<   r=   r>   r?   r@   r"   r"   r#   r3     s   r3   N)Zsympyr   r   r   r   r   r   r   Z
sympy.corer	   r
   r   r   r   r   Zsympy.core.compatibilityr   Zsympy.core.symbolr   Zsympy.solversr   r   r   Zsympy.polysr   Zsympy.geometryr   Zsympy.ntheory.factor_r   r   r3   r"   r"   r"   r#   <module>   s   $     _