
    MZd?                         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e      Z"d Z#y)    )Rational)S)symbols)sign)sqrt)gcd)
Complement)BasicTuplediffexpandEqInteger)ordered)_symbol)solvesetnonlinsolvediophantine)total_degree)Point)corec                   x     e Zd ZdZ fdZed        Zed        Zed        Zd Z	d Z
d Zd	 Zd
 ZddZ x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                     t        |t              st        | }t        |t              r|j                  |j                  z
  }t
        |   | ||      S N)
isinstancer   r   lhsrhssuper__new__)cls	variablesequation	__class__s      =/usr/lib/python3/dist-packages/sympy/vector/implicitregion.pyr    zImplicitRegion.__new__9   sF    )U+y)Ih#||hll2HwsIx88    c                      | j                   d   S )Nr   argsselfs    r%   r"   zImplicitRegion.variablesB       yy|r&   c                      | j                   d   S )N   r(   r*   s    r%   r#   zImplicitRegion.equationF   r,   r&   c                 ,    t        | j                        S r   )r   r#   r*   s    r%   degreezImplicitRegion.degreeJ   s    DMM**r&   c                    | j                   }t        | j                        dk(  r6t        t	        || j                  d   t
        j                              d   fS t        | j                        dk(  rh| j                  dk(  rYt        | j                  |      x}\  }}}}}}|dz  d|z  |z  k(  r | j                  | \  }	}
|	|
fS  | j                  | \  }	}
|	|
fS t        | j                        dk(  r| j                  \  }}}t        dd      D ]  }	t        dd      D ]y  }
t	        |j                  ||	||
i      | j                  d   t
        j                        j                  rJ|	|
t        t	        |j                  ||	||
i                  d   fc c S   t        | j                               dk7  rt        | j                            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   Realsr0   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zs                 r%   regular_pointzImplicitRegion.regular_pointN   s   6 ==t~~!#(DNN1,=aggNOPQRTT A%{{a,7,QQ)Aq!Qa41Q3q5=#?4#?#?#HLE5 e|# $?4#>#>#GLE5e|#t~~!#nnGAq!sB f"3^ fE#HMM1eQ2F$GXYIZcdcjcjktt %ud8HMM1eUVX]J^<_3`.abc.deeff
 t##%&!+,,./22!##r&   c                    ||fdk7  xr  ||fdk7  xr |dz  d|z  |z  k(  xr ||fdk7  }|st        d      |dk7  r=d|z  |z  d|z  |z  z
  d|z  |z  |dz  z
  }	}|dk7  r|	 |z  }
|||
z  z    d|z  z  }nDd}nA|dk7  r<d|z  |z  d|z  |z  z
  d|z  |z  |dz  z
  }	}|dk7  r|	 |z  }|||z  z    d|z  z  }
nd}|r
fS t        d      )N)r   r   r3   r4   *Rational Point on the conic does not existr   F)
ValueError)r+   rC   rD   rE   rF   rG   rH   okd_dashf_dashrJ   rI   s               r%   r;   z&ImplicitRegion._regular_point_parabola   s1   Q6!]q!f&6]1a41Q3q5=]aQRVW]M]B !MNNAv"#A#a%!A#a%-1QAQ;#GFNE!E'kNAaC0EBa"#A#a%!A#a%-1QAQ;#GFNE!E'kNAaC0EBe|# !MNNr&   c                 J
  / d|z  |z  |dz  z
  }|}|st        d      |dk(  r|dk(  rd}	d||z  ||z  z
  z  }
n|dk7  rM|}	d|dz  z  |dz  z  d|z  |z  |z  |z  z
  d|z  |z  |dz  z  z   d|dz  z  |z  |z  z   d|z  |dz  z  |z  z
  }
n.|}	d|dz  z  |dz  z  d|z  |z  |z  |z  z
  d|dz  z  |z  |z  z   }
|
dk7  xr |	dkD  xr |
dk   }|st        d      t        |	      j                  d      }	t        |
      j                  d      }
|	j                  |	j                  }}|
j                  |
j                  }}t        ||      }||z  |z  }||z  |z  }||z   |z  }t        |      t        t        |      d      z  }t        ||z        }t        |      t        t        |      d      z  }t        ||z        }t        |      t        t        |      d      z  }t        ||z        }t        t        ||      |      }||z  }||z  }||z  }t        ||      }||z  }||z  }||z  }t        ||      }||z  }||z  }||z  }t        ||      }||z  }||z  }||z  }t        d      \  }}}||dz  z  ||dz  z  z   ||dz  z  z   }t        |      } t        |       dk(  rt        d      d	}!| D ]  }"t        |" j                  }#|#D $ci c]  }$|$d
 c}$/|"d   }%|%dk(  rd}!4t        |%t         t"        f      rK|%j                  }&t        |&      dk(  rht%        t'        |&            }'t)        t*        j,                  t/        t1        |%d      |'t*        j,                              }(t%        t'        |(            /|'<   t        |&      dk(  rt3        t5        |&            \  }'})t*        j,                  D ]{  }*|%j7                  |'|*      }+t)        t*        j,                  t/        t1        |+d      |)t*        j,                              },|,j8                  r_|*/|'<   t%        t'        |,            /|)<    n t        |#      dk7  rt;        /fd|"D              \  }}}n|"\  }}}d	}! n |!rt        d      ||z  |z  }||z  |z  }||z  |z  }||z  }||z  }|dk(  r+|dk(  r&||z   d|z  z
  d|z  z  }-||z
  d|z  z
  d|z  z  }.|-|.fS |dk7  r)|d|z  |z  z
  ||z  z   |	z  }-|||-z  z
  |z
  d|z  z  }.|-|.fS |d|z  |z  z
  ||z  z   |	z  }.|||.z  z
  |z
  d|z  z  }-|-|.fS c c}$w )Nr4   r3   rP   r      l    J)zx y zFr5   Tr.   c              3   @   K   | ]  }|j                          y wr   r>   .0sreps     r%   	<genexpr>z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>   s     'As'A   )rQ   r   limit_denominatorpqr   r   r   absr   r   r   r7   r   free_symbolsr   intr   nextiterr	   r   Integersr   r   r8   r   r>   r?   tuple)0r+   rC   rD   rE   rF   rG   rH   DrR   KLk1k2l1l2ga1b1c1a2r1b2r2c2r3g1g2g3rK   rL   rM   eq	solutionsflagsolsymsr\   sol_zsyms_zra   p_valuesrb   i
subs_sol_zq_valuesrI   rJ   r]   s0                                                  @r%   r<   z%ImplicitRegion._regular_point_ellipse   s   !A1AB !MNNAv!q&qsQqSyMaadF1a4K!A#a%'!)+ac!eAqDj81QT6!8A:E1QPQT	RSSadF1a4K!A#a%'!)+a1fQhqj8a0A!a%0B !MNN--f5A--f5ASS!##BSS!##BBAR%BR%Bb5!Bb$s2w**BbeBb$s2w**BbeBb$s2w**BbeBCBK$AABABABRBBBBBBBRBBBBBBBRBBBBBBBg&GAq!AqD2ad7"R1W,B#BI9~" !MNND  "c{//%)*q!t*AA:D!%#w8"//F6{a' f.#-ajj(2eQ<QRTUT^T^:_#`!%d8n!5A6{a'#GFO41!" &A).Aq)9J'1!**hr*VWGXZ[]^]g]g>h'iH#+#4#4)*A)-d8n)=A %& 4yA~"''AS'A"A1a$'1a DE"H  !MNN2r	A2r	A2r	A!A!AAv!q&Q1qs+Q1qs+ %< aQqSUQqS!+QuWq1Q3/
 %< QqSUQqS!+QuWq1Q3/%<k +s   
T c                     | j                   g}| j                  D ]  }|t        | j                   |      gz  } 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   r8   )r+   eq_listvars      r%   r@   zImplicitRegion.singular_points  sR    " ==/>> 	2CT]]C011G	2 7D$899r&   c                    t        |t              r|j                  }| j                  }t	        | j
                        D ]  \  }}|j                  ||||   z         } t        |      }t        |j                        dk7  r0|j                  }t        |D cg c]  }t        |       c}      }|S |}t        |      }|S c c}w )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   )r   r   r)   r#   	enumerater"   r>   r   r7   minr   )r+   pointmodified_eqr   r   termstermms           r%   multiplicityzImplicitRegion.multiplicity2  s    & eU#JJEmm/ 	@FAs%**3eAh?K	@[){ A%$$EE:D\$':;A
   EU#A ;s   C c                 p   | j                   }| j                  }|dk(  rht        | j                        dk(  r|fS t        | j                        dk(  r+| j                  \  }}t	        t        ||            d   }||fS t               d}|dk(  rN||}nIt        | j                               dk7  rt	        | j                               d   }n| j                         }t        | j                               dk7  rw| j                         }	|	D ]b  }
t        |
 j                  }|D ci c]  }|d c}t        |      dk7  rt        fd|
D              }
| j                  |
      |dz
  k(  s`|
} n t        |      dk(  r
t               |}t        | j                        D ]  \  }}|j                  ||||   z         } t        |      }dx}}|j                   D ]  }t#        |      |k(  r||z  }||z  } d|z  }t%        |t              s|f}t        | j                        dk(  r|d   }|dk(  rt'        dd	
      }nt'        dd	
      }t'        |d	
      }|j                  | j                  d   || j                  d   |i      }|j                  | j                  d   || j                  d   |i      }|||z  z  j                  |d      |d   z   }|||z  z  j                  |d      |d   z   }||fS t        | j                        dk(  r|\  }}d|v rt'        dd	
      }nt'        dd	
      }t'        |d	
      }t'        |d	
      }|j                  | j                  d   || j                  d   || j                  d   |i      }|j                  | j                  d   || j                  d   || j                  d   |i      }|||z  z  j                  |d      |d   z   }|||z  z  j                  |d      |d   z   }|||z  z  j                  |d      |d   z   }|||fS t               c c}w )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.   r3   r    c              3   @   K   | ]  }|j                          y wr   rY   rZ   s     r%   r^   z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>  s     !>!!&&+!>r_   rV   r\   s_T)realr5   rr_)r#   r0   r7   r"   r8   r   rA   r@   rN   r   rd   ri   r   r   r>   r   r)   r   r   r   )r+   
parameters	reg_pointr#   r0   rK   rL   y_parr   r@   spointr   r\   r   r   r   hnhn_1r   
parameter1tx_par
parameter2r   z_parr]   s                            @r%   rational_parametrizationz'ImplicitRegion.rational_parametrizationW  sK   ^ ==Q;4>>"a' {"T^^$)~~1Xh23A6%x)++ Q;$!t++-.!3 !5!5!78;E ..0Et##%&!+"224O) 	f~22%)*q!t*t9>!!>v!>>6$$V,
:"E	 u:?%''  / 	@FAs%**3eAh?K	@[)T$$ 	DD!V+d
		 $w*e,$Jt~~!##AJS Dt,Cd+
.A$..+Qq0A1EFB99dnnQ/DNN14EqIJDR[&&q!,uQx7ER[&&q!,uQx7E%< A%%/"J
j Dt,Cd+
.A
.A$..+Qq0A1dnnUVFWYZ[\B99dnnQ/DNN14Eq$..YZJ[]^_`DR[&&q!,uQx7ER[&&q!,uQx7ER[&&q!,uQx7E%&&!##M +s   #
P3))r   r\   N)__name__
__module____qualname____doc__r    propertyr"   r#   r0   rN   r;   r<   r@   r   r   __classcell__)r$   s   @r%   r   r      sn    &N9     + +5$nO4z x:.#JT$r&   r   c                    t        |      dk7  r
t               | d   }| d   }t        |      }|j                  |dz        }|j                  ||z        }|j                  |dz        }|j                  |d      j                  |d      }|j                  |d      j                  |d      }|j                  |d      j                  |d      }	||||||	fS )Nr3   r   r.   )r   rQ   r   coeff)
r"   r#   rK   rL   rC   rD   rE   rF   rG   rH   s
             r%   r:   r:     s    H"l!A!AhHq!tAqsAq!tAq!""1a(Aq!""1a(Aq!""1a(AaAq!r&   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   r:   r   r&   r%   <module>r      sI    ' " % 5 9 % & > > & % < < $   &Z$U Z$xr&   