U
    9%e?                     @   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/Darija-Ai-API/env/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rt| 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 qqt|  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rd| | 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           .         s6  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rt dd	}!| D ]N}"t|" j}#d
d |#D  |"d }$|$dkrd}!qt|$ttfs|$j}%t|%dkrttt|%}&ttjtt|$d|&tj}'tt|' |&< t|%dkrtt|%\}&}(tjD ]T})|$|&|)}*ttjtt|*d|(tj}+|+js|) |&< tt|+ |(<  qqt|#dkrt fdd|"D \}}}n
|"\}}}d	}! q2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 zFc                 S   s   i | ]
}|d qS )r/   r$   .0sr$   r$   r%   
<dictcomp>   s      z9ImplicitRegion._regular_point_ellipse.<locals>.<dictcomp>Tr*   c                 3   s   | ]}|  V  qd S r   r7   rL   repr$   r%   	<genexpr>   s     z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>)rH   r   limit_denominatorpqr   r   r   absr   r   r   r1   r
   free_symbolsr   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_zrU   Zp_valuesrV   iZ
subs_sol_zZq_valuesrA   rB   r$   rQ   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   g | ]}t |qS r$   r   )rM   termr$   r$   r%   
<listcomp>P  s     z/ImplicitRegion.multiplicity.<locals>.<listcomp>)r   r   r'   r!   	enumerater    r7   r   r1   minr   )r)   pointmodified_eqrn   ro   Ztermsmr$   r$   r%   multiplicity2  s    
zImplicitRegion.multiplicitytrN   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rt|  d }n|  }t|  dkr |  }	|	D ]\}
t	|
 j
}dd |D  t|dkrt fdd	|
D }
| |
|d kr|
} q qt|dkr4t |}t| jD ]\}}|||||  }qBt|}d }}|jD ]&}t||kr||7 }n||7 }qxd
| }t|ts|f}t| jdkr~|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rt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                 S   s   i | ]
}|d qS )r-   r$   rL   r$   r$   r%   rO     s      z;ImplicitRegion.rational_parametrization.<locals>.<dictcomp>c                 3   s   | ]}|  V  qd S r   rP   rL   rQ   r$   r%   rS     s     z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>rJ   rN   Zs_T)realr/   rZr_)r!   r+   r1   r    r2   r   r:   r9   rF   r
   rX   r\   rw   rr   r7   r   r'   r   r   r   )r)   
parametersZ	reg_pointr!   r+   rC   rD   Zy_parrt   r9   Zspointrm   ru   rn   ro   ZhnZhn_1rp   Z
parameter1rN   ry   Zx_parZ
parameter2r{   Zz_parr$   rQ   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$       _