o
    oh?                     @   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__ o/var/www/html/construction_image-detection-poc/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rt|   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rd| | 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  }|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}#t|#d
 |"d }$|$dkrd}!qmt|$ttf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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rt |}t| jD ]\}}|||||  }qt|}d }}|jD ]}t||kr||7 }q||7 }qd| }t|ts|f}t| jdkr5|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|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&        _