
    Ed?                         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#dS )    )Rational)S)symbols)sign)sqrt)gcd)
Complement)BasicTuplediffexpandEqInteger)ordered)_symbol)solvesetnonlinsolvediophantinetotal_degree)Point)corec                        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      ;lib/python3.11/site-packages/sympy/vector/implicitregion.pyr!   zImplicitRegion.__new__9   sY    )U++ 	*y)Ih## 	3|hl2HwwsIx888    c                     | j         d         S )Nr   argsselfs    r&   r#   zImplicitRegion.variablesB       y|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    DM***r'   c                    | j         }t          | j                  dk    r;t          t	          || j        d         t
          j                            d         fS t          | j                  dk    rW| j        dk    rLt          | j        |          x}\  }}}}}}|dz  d|z  |z  k    r | j	        | \  }	}
n | j
        | \  }	}
|	|
fS t          | j                  dk    r| j        \  }}}t          dd          D ]}	t          dd          D ]}
t	          |                    ||	||
i          | j        d         t
          j                  j        s@|	|
t          t	          |                    ||	||
i                              d         fc c S t          |                                           dk    r%t          |                                          d         S t                      )	a1  
        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. Availaible:
          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~!# 	$(DN1,=agNNNOOPQRTT  A% 		${a $,7,Q,QQ)Aq!Qa41Q3q5= H#?4#?#HLE55#>4#>#GLE5e|#t~!# 	fnGAq!sB f f"3^^ f fE#HMM1eQ2F$G$GXYIZcdcjkkkt f %ud8HMM1eUVX]J^<_<_3`3`.a.abc.deeeeeeff t##%%&&!+ 	3,,../22!###r'   c                    ||fdk    o||fdk    o|dz  d|z  |z  k    o||fdk    }|st          d          |dk    r>d|z  |z  d|z  |z  z
  d|z  |z  |dz  z
  }	}|dk    r|	 |z  }
|||
z  z    d|z  z  }nFd}nC|dk    r=d|z  |z  d|z  |z  z
  d|z  |z  |dz  z
  }	}|dk    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   s]   Q6!]q!f&6]1a41Q3q5=]aQRVW]M]B O !MNNNAv "#A#a%!A#a%-1QAQ; #GFNE!E'kNAaC0EEBBa "#A#a%!A#a%-1QAQ; #GFNE!E'kNAaC0EEB Oe|# !MNNNr'   c                 f  . d|z  |z  |dz  z
  }|}|st          d          |dk    r|dk    rd}	d||z  ||z  z
  z  }
n|dk    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k    o|	dk    o|
dk      }|st          d          t          |	                              d          }	t          |
                              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
 |#D             .|"d         }$|$dk    rd}!.t          |$t           t"          f          s|$j        }%t          |%          dk    r|t%          t'          |%                    }&t)          t*          j        t/          t1          |$d          |&t*          j                            }'t%          t'          |'                    .|&<   t          |%          dk    rt3          t5          |%                    \  }&}(t*          j        D ]})|$                    |&|)          }*t)          t*          j        t/          t1          |*d          |(t*          j                            }+|+j        s&|).|&<   t%          t'          |+                    .|(<    nt          |#          dk    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  }-nQ|dk    r&|d|z  |z  z
  ||z  z   |	z  },|||,z  z
  |z
  d|z  z  }-n%|d|z  |z  z
  ||z  z   |	z  }-|||-z  z
  |z
  d|z  z  },|,|-fS )Nr5   r4   rQ   r      l    J)zx y zFc                     i | ]}|d S )r6    .0ss     r&   
<dictcomp>z9ImplicitRegion._regular_point_ellipse.<locals>.<dictcomp>       ***q!***r'   Tr/   c              3   B   K   | ]}|                               V  d S r   r?   r\   r]   reps     r&   	<genexpr>z8ImplicitRegion._regular_point_ellipse.<locals>.<genexpr>   s-      'A'As'A'A'A'A'A'Ar'   )rR   r   limit_denominatorpqr   r   r   absr   r   r   r8   r   free_symbolsr   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_zrf   p_valuesrg   i
subs_sol_zq_valuesrJ   rK   rc   s/                                                 @r&   r=   z%ImplicitRegion._regular_point_ellipse   s   !A1AB O !MNNNAv 9!q& 9qsQqSyMa 9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 O !MNNN--f55A--f55AS!#BS!#BBAR%BR%Bb5!Bb$s2ww***BbeBb$s2ww***BbeBb$s2ww***BbeBCBKK$$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~~" O !MNNND  " "c{/**T***AA: D!%#w88 "/F6{{a' 6 f..#-aj(2eQ<<QRTUT^:_:_#`#`!%d8nn!5!5A6{{a' 
&#GFOO441!" & &A).Aq)9)9J'1!*hr*VWGXGXZ[]^]g>h>h'i'iH#+#4 &)*A)-d8nn)=)=A %&
 4yyA~ ("''A'A'A'AS'A'A'A"A"A1aa$'1a DE36  O !MNNN2r	A2r	A2r	A!A!AAv 0!q& 0Q1qs+Q1qs+a 0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  sS    " =/> 	2 	2CT]C0011GG7D$8$8999r'   c                 p   t          |t                    r|j        }| j        }t	          | j                  D ]$\  }}|                    ||||         z             }%t          |          }t          |j                  dk    r!|j        }t          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                 ,    g | ]}t          |          S rZ   r   )r\   terms     r&   
<listcomp>z/ImplicitRegion.multiplicity.<locals>.<listcomp>P  s     :::D\$'':::r'   )r   r   r*   r$   	enumerater#   r?   r   r8   minr   )r,   pointmodified_eqr   r   termsms          r&   multiplicityzImplicitRegion.multiplicity2  s    & eU## 	JEm// 	@ 	@FAs%**3eAh??KK[)){  A% 	$$E::E:::;;AAEU##Ar'   tr]   Nc                 n	   | j         }| j        }|dk    rrt          | j                  dk    r|fS t          | j                  dk    r1| j        \  }}t	          t          ||                    d         }||fS t                      d}|dk    rf||}nat          |                                           dk    r(t	          |                                           d         }n|                                 }t          |                                           dk    r|                                 }	|	D ]j}
t          |
 j
        }d |D             t          |          dk    rt          fd|
D                       }
|                     |
          |dz
  k    r|
} nkt          |          dk    rt                      |}t          | j                  D ]$\  }}|                    ||||         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        d         || j        d         |i          }|                    | j        d         || j        d         |i          }|||z  z                      |d          |d         z   }|||z  z                      |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        d         || j        d         || j        d         |i          }|                    | j        d         || j        d         || j        d         |i          }|||z  z                      |d          |d         z   }|||z  z                      |d          |d         z   }|||z  z                      |d          |d         z   }|||fS t                      )a  
        Returns the rational parametrization of implict 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   rZ   Nc                     i | ]}|d S )r4   rZ   r[   s     r&   r^   z;ImplicitRegion.rational_parametrization.<locals>.<dictcomp>  r_   r'   c              3   B   K   | ]}|                               V  d S r   ra   rb   s     r&   rd   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   ri   rn   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]   r   x_par
parameter2r   z_parrc   s                            @r&   rational_parametrizationz'ImplicitRegion.rational_parametrizationW  s   ^ =Q; 	,4>""a' , {"T^$$) ,~1Xh2233A6%x)+++ Q; 		1 1!t++--..!3 1 !5!5!7!788;EE ..00Et##%%&&!+ 	"2244O) 	 	f~2**T***t99> ?!!>!>!>!>v!>!>!>>>6$$V,,
: "EE u::? 	(%'''  // 	@ 	@FAs%**3eAh??KK[))T$ 	 	DD!!V+ d
$w*e,, 	'$Jt~!# $	'#AJS  ,Dt,,,Cd+++
...A$.+Qq0A1EFFB99dnQ/DN14EqIJJDR[&&q!,,uQx7ER[&&q!,,uQx7E%<  A% 	'%/"J
j  ,Dt,,,Cd+++
...A
...A$.+Qq0A1dnUVFWYZ[\\B99dnQ/DN14Eq$.YZJ[]^_``DR[&&q!,,uQx7ER[&&q!,,uQx7ER[&&q!,,uQx7E%&&!###r'   )r   N)__name__
__module____qualname____doc__r!   propertyr#   r$   r1   rO   r<   r=   rA   r   r   __classcell__)r%   s   @r&   r   r      s       & &N9 9 9 9 9   X   X + + X+5$ 5$ 5$nO O O4z  z  z x: : :.# # #JT$ T$ T$ T$ T$ T$ T$ T$r'   r   c                    t          |          dk    rt                      | d         }| d         }t          |          }|                    |dz            }|                    ||z            }|                    |dz            }|                    |d                              |d          }|                    |d                              |d          }|                    |d                              |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" ll!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;   rZ   r'   r&   <module>r      sw   ' ' ' ' ' ' " " " " " " % % % % % % 5 5 5 5 5 5 9 9 9 9 9 9 % % % % % % & & & & & & > > > > > > > > > > > > > > > > & & & & & & % % % % % % < < < < < < < < < < $ $ $ $ $ $             & & & & & &Z$ Z$ Z$ Z$ Z$U Z$ Z$ Z$x    r'   