U
    ZÊ¹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__© úZ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/vector/implicitregion.pyr   9   s
    

zImplicitRegion.__new__c                 C   s
   | j d S )Nr   ©Úargs©Úselfr$   r$   r%   r    B   s    zImplicitRegion.variablesc                 C   s
   | j d S )Né   r&   r(   r$   r$   r%   r!   F   s    zImplicitRegion.equationc                 C   s
   t | jƒS r   )r   r!   r(   r$   r$   r%   ÚdegreeJ   s    zImplicitRegion.degreec                 C   s\  | j }t| jƒdkr4tt|| jd tjd�ƒd fS t| jƒdkr¢| jdkr¢t| j|ƒ }\}}}}}}|d d| | krŒ| j	|Ž \}	}
n| j
|Ž \}	}
|	|
fS t| jƒdk�r0| j\}}}tddƒD ]f}	tddƒD ]V}
t| ||	||
i¡| jd tjd�jsÖ|	|
tt| ||	||
i¡ƒƒd f    S qÖqÈt|  ¡ ƒdk�rRt|  ¡  d S tƒ ‚d	S )
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öÿÿÿé
   N)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_pointN   s&     
&0zImplicitRegion.regular_pointc                 C   s,  ||fdko6||fdko6|d d| | ko6||fdk}|sDt dƒ‚|dkrªd| | d| |  d| | |d   }}	|dkr¤|	 | }
|||
   d|  }nd}nh|dk�rd| | d| |  d| | |d   }}	|dk�r|	 | }|||   d|  }
nd}|�r ||
fS t dƒ‚d S )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           .         s4  d| | |d  }|}|s$t dƒ‚|dkrN|dkrNd}	d|| ||   }
n²|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�rØt dƒ‚d	}!| D �]L}"t|"Ž j}#t |#d
¡‰ |"d }$|$dk�rd}!�qàt|$ttfƒ�sà|$j}%t|%ƒdk�rrtt|%ƒƒ}&ttjtt|$dƒ|&tjƒƒ}'tt|'ƒƒˆ |&< t|%ƒdk�rìtt|%ƒƒ\}&}(tjD ]T})|$ |&|)¡}*ttjtt|*dƒ|(tjƒƒ}+|+j�s–|)ˆ |&< tt|+ƒƒˆ |(<  �qì�q–t|#ƒdk�rt‡ fdd„|"D ƒƒ\}}}n
|"\}}}d	}! �q0�qà|!�r>t dƒ‚|| | }|| | }|| | }|| }|| }|dk�r¸|dk�r¸|| d|  d|  },|| d|  d|  }-nt|dk�rø|d| |  ||  |	 },|||,  | d|  }-n4|d| |  ||  |	 }-|||-  | d|  },|,|-fS )Nr.   r-   rG   r   éÿÿÿÿé   l    J)£zx y zFr/   Tr*   c                 3   s   | ]}|  ˆ ¡V  qd S r   ©r7   ©Ú.0Ús©Úrepr$   r%   Ú	<genexpr>   s     z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>) rH   r   Úlimit_denominatorÚpÚqr   r   r   Úabsr   r   r   r1   r
   Úfree_symbolsÚdictÚfromkeysr   Úintr   Ú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a1Úb1Úc1Za2Úr1Úb2Úr2Úc2Zr3Úg1Úg2Zg3rC   rD   rE   ÚeqZ	solutionsÚflagZsolÚsymsZsol_zZsyms_zrT   Zp_valuesrU   ÚiZ
subs_sol_zZq_valuesrA   rB   r$   rP   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rn|j}t	dd„ |D ƒƒ}n|}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   )rN   Útermr$   r$   r%   rR   P  s     z.ImplicitRegion.multiplicity.<locals>.<genexpr>)r   r   r'   r!   Ú	enumerater    r7   r   r1   Úminr   )r)   ÚpointÚmodified_eqro   rp   ZtermsÚmr$   r$   r%   Úmultiplicity2  s    
zImplicitRegion.multiplicity©ÚtrO   Nc                    sˆ  | j }| j}|dkr`t| jƒdkr(|fS t| jƒdkrZ| j\}}tt||ƒƒd }||fS tƒ ‚d}|dkr¤|dk	rz|}n*t|  ¡ ƒdkrœt|  ¡ ƒd }n|  ¡ }t|  ¡ ƒdk�r|  ¡ }	|	D ]Z}
t	|
Ž j
}t |d¡‰ t|ƒdk�r t‡ fdd„|
D ƒƒ}
|  |
¡|d krÂ|
} �qqÂt|ƒdk�r2tƒ ‚|}t| jƒD ]\}}| ||||  ¡}�q@t|ƒ}d }}|jD ]&}t|ƒ|k�r’||7 }n||7 }�qvd| }t|tƒ�s¸|f}t| jƒdk�r||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|k�r¬t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ƒ ‚dS )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   s   | ]}|  ˆ ¡V  qd S r   rL   rM   rP   r$   r%   rR   ¨  s     z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>rJ   rO   Zs_T)Úrealr/   ÚrZr_)r!   r+   r1   r    r2   r   r:   r9   rF   r
   rW   rX   rY   r]   rw   rr   r7   r   r'   r   r   r   )r)   Ú
parametersZ	reg_pointr!   r+   rC   rD   Zy_parrt   r9   Zspointrn   ru   ro   rp   ZhnZhn_1rq   Z
parameter1rO   ry   Zx_parZ
parameter2r{   Zz_parr$   rP   r%   Úrational_parametrizationW  s„    /





((
z'ImplicitRegion.rational_parametrization)rx   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr    r!   r+   rF   r4   r5   r9   rw   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   rH   r   Zcoeff)
r    r!   rC   rD   r;   r<   r=   r>   r?   r@   r$   r$   r%   r3   í  s    r3   N)$Zsympy.core.numbersr   Zsympy.core.singletonr   Zsympy.core.symbolr   Z$sympy.functions.elementary.complexesr   Z(sympy.functions.elementary.miscellaneousr   Zsympy.polys.polytoolsr   Zsympy.sets.setsr   Z
sympy.corer	   r
   r   r   r   r   Zsympy.core.sortingr   r   Zsympy.solversr   r   r   Zsympy.polysr   Zsympy.geometryr   Zsympy.ntheory.factor_r   r   r3   r$   r$   r$   r%   Ú<module>   s$       _