
    wg?                         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      `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-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   Realsr1   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   r4   r5   *Rational Point on the conic does not existr   F)
ValueError)r,   rD   rE   rF   rG   rH   rI   okd_dashf_dashrK   rJ   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                  }#t        j!                  |#d
      .|"d   }$|$dk(  rd}!9t#        |$t$        t&        f      rP|$j                  }%t        |%      dk(  rht)        t+        |%            }&t-        t.        j0                  t3        t5        |$d      |&t.        j0                              }'t)        t+        |'            .|&<   t        |%      dk(  rt7        t9        |%            \  }&}(t.        j0                  D ]{  })|$j;                  |&|)      }*t-        t.        j0                  t3        t5        |*d      |(t.        j0                              }+|+j<                  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 )Nr5   r4   rQ   r      l    J)zx y zFr6   Tr/   c              3   @   K   | ]  }|j                          y wr   r?   .0sreps     r&   	<genexpr>z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>   s     'As'A   ) rR   r   limit_denominatorpqr   r   r   absr   r   r   r8   r   free_symbolsdictfromkeysr   intr   nextiterr	   r   Integersr   r   r9   r   r?   r@   tuple)/r,   rD   rE   rF   rG   rH   rI   DrS   KLk1k2l1l2ga1b1c1a2r1b2r2c2r3g1g2g3rL   rM   rN   eq	solutionsflagsolsymssol_zsyms_zrb   p_valuesrc   i
subs_sol_zq_valuesrJ   rK   r^   s/                                                 @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{//mmD!,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/%<r'   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   r9   )r,   eq_listvars      r&   rA   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  r |j                  }t        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              3   2   K   | ]  }t        |        y wr   r   )r\   terms     r&   r_   z.ImplicitRegion.multiplicity.<locals>.<genexpr>P  s     94L&9s   )r   r   r*   r$   	enumerater#   r?   r   r8   minr   )r,   pointmodified_eqr   r   termsms          r&   multiplicityzImplicitRegion.multiplicity2  s    & eU#JJEmm/ 	@FAs%**3eAh?K	@[){ A%$$E9599A
   EU#Ar'   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  r|| j                         }	|	D ]g  }
t        |
 j                  }t        j                  |d      t        |      dk7  rt        fd|
D              }
| j                  |
      |dz
  k(  se|
} 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               )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/   r4   r    c              3   @   K   | ]  }|j                          y wr   rZ   r[   s     r&   r_   z:ImplicitRegion.rational_parametrization.<locals>.<genexpr>  s     !>!!&&+!>r`   rW   r]   s_T)realr6   rr_)r$   r1   r8   r#   r9   r   rB   rA   rO   r   re   rf   rg   rl   r   r   r?   r   r*   r   r   r   )r,   
parameters	reg_pointr$   r1   rL   rM   y_parr   rA   spointr   r   r   r   hnhn_1r   
parameter1r]   tx_par
parameter2r   z_parr^   s                            @r&   rational_parametrizationz'ImplicitRegion.rational_parametrizationW  sA   ^ ==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mmD!,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%&&!##r'   ))r   r]   N)__name__
__module____qualname____doc__r!   propertyr#   r$   r1   rO   r<   r=   rA   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 )Nr4   r   r/   )r   rR   r   coeff)
r#   r$   rL   rM   rD   rE   rF   rG   rH   rI   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'   