
    ljv,                       d Z ddlmZ g dZddlmZmZmZ ddlm	Z	 ddl
ZddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ e	rddlmZmZ ddlmZmZ ddlmZ ddlmZ  G d dee          Z G d de          Z  G d dee          Z!dS )z&Mobjects representing function graphs.    )annotations)ParametricFunctionFunctionGraphImplicitFunction)CallableIterableSequence)TYPE_CHECKINGN)plot_isoline)config)
LinearBase
_ScaleBase)ConvertToOpenGL)VMobject)AnySelf)Point3DPoint3DLike)ParsableManimColor)PURE_YELLOWc                  b     e Zd ZdZd e            ddddfd! fdZd"dZd#dZd$dZd%d Z	 xZ
S )&r   a  A parametric curve.

    Parameters
    ----------
    function
        The function to be plotted in the form of ``(lambda t: (x(t), y(t), z(t)))``
    t_range
        Determines the length that the function spans in the form of (t_min, t_max, step=0.01). By default ``[0, 1]``
    scaling
        Scaling class applied to the points of the function. Default of :class:`~.LinearBase`.
    use_smoothing
        Whether to interpolate between the points of the function after they have been created.
        (Will have odd behaviour with a low number of points)
    use_vectorized
        Whether to pass in the generated t value array to the function as ``[t_0, t_1, ...]``.
        Only use this if your function supports it. Output should be a numpy array
        of shape ``[[x_0, x_1, ...], [y_0, y_1, ...], [z_0, z_1, ...]]`` but ``z`` can
        also be 0 if the Axes is 2D
    discontinuities
        Values of t at which the function experiences discontinuity.
    dt
        The left and right tolerance for the discontinuities.


    Examples
    --------
    .. manim:: PlotParametricFunction
        :save_last_frame:

        class PlotParametricFunction(Scene):
            def func(self, t):
                return (np.sin(2 * t), np.sin(3 * t), 0)

            def construct(self):
                func = ParametricFunction(self.func, t_range = (0, TAU), fill_opacity=0).set_color(RED)
                self.add(func.scale(3))

    .. manim:: ThreeDParametricSpring
        :save_last_frame:

        class ThreeDParametricSpring(ThreeDScene):
            def construct(self):
                curve1 = ParametricFunction(
                    lambda u: (
                        1.2 * np.cos(u),
                        1.2 * np.sin(u),
                        u * 0.05
                    ), color=RED, t_range = (-3*TAU, 5*TAU, 0.01)
                ).set_shade_in_3d(True)
                axes = ThreeDAxes()
                self.add(axes, curve1)
                self.set_camera_orientation(phi=80 * DEGREES, theta=-60 * DEGREES)
                self.wait()

    .. attention::
        If your function has discontinuities, you'll have to specify the location
        of the discontinuities manually. See the following example for guidance.

    .. manim:: DiscontinuousExample
        :save_last_frame:

        class DiscontinuousExample(Scene):
            def construct(self):
                ax1 = NumberPlane((-3, 3), (-4, 4))
                ax2 = NumberPlane((-3, 3), (-4, 4))
                VGroup(ax1, ax2).arrange()
                discontinuous_function = lambda x: (x ** 2 - 2) / (x ** 2 - 4)
                incorrect = ax1.plot(discontinuous_function, color=RED)
                correct = ax2.plot(
                    discontinuous_function,
                    discontinuities=[-2, 2],  # discontinuous points
                    dt=0.1,  # left and right tolerance of discontinuity
                    color=GREEN,
                )
                self.add(ax1, ax2, incorrect, correct)
    r      g:0yE>NTFfunctionCallable[[float], Point3DLike]t_range0tuple[float, float] | tuple[float, float, float]scalingr   dtfloatdiscontinuitiesIterable[float] | Noneuse_smoothingbooluse_vectorizedkwargsr   c                    dfd}	|	| _         t          |          dk    rg |dR }|| _        || _        || _        || _        || _        |\  | _        | _        | _	         t                      j        d	i | d S )
Ntr    returnr   c                >    t          j         |                     S )z0Wrap ``function``'s output inside a NumPy array.)npasarrayr(   r   s    [/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/mobject/graphing/functions.pyinternal_parametric_functionzAParametricFunction.__init__.<locals>.internal_parametric_functionu   s    :hhqkk***       g{Gz?r(   r    r)   r    )r   lenr   r   r!   r#   r%   t_mint_maxt_stepsuper__init__)selfr   r   r   r   r!   r#   r%   r&   r/   	__class__s    `        r.   r9   zParametricFunction.__init__j   s    	+ 	+ 	+ 	+ 	+ 	+ 5w<<1&&&&G.*,.5+
DJ""6"""""r0   r)   Callable[[float], Point3D]c                    | j         S Nr   r:   s    r.   get_functionzParametricFunction.get_function   
    }r0   r(   r   c                ,    |                      |          S r>   r?   )r:   r(   s     r.   get_point_from_functionz*ParametricFunction.get_point_from_function   s    }}Qr0   r   c                     j         t           fd j                   }t          j        t	          |                    }t          j         j         j        g| j        z
  | j        z             }|                                 n j         j        g}t          |dd d         |dd d         d          D ]'\  }}t          j        g  j
                            t          j        || j                             j
                            |                    } j        ra                     |          \  }}}	t          |	t          j                  st          j        |          }	t          j        |||	gd          }
n t          j         fd|D                       }
                     |
d                                         |
dd                     ) j        r                                   S )	Nc                8    j         | cxk    o
j        k    nc S r>   )r5   r6   )r(   r:   s    r.   <lambda>z4ParametricFunction.generate_points.<locals>.<lambda>   s'    $*7777TZ7777 r0   r   r1   r   T)strict)axisc                :    g | ]}                     |          S r3   r?   ).0r(   r:   s     r.   
<listcomp>z6ParametricFunction.generate_points.<locals>.<listcomp>   s%    "E"E"E4==#3#3"E"E"Er0   )r!   filterr+   arraylistr5   r6   r   sortzipr   r   aranger7   r%   
isinstancendarray
zeros_likestackstart_new_pathadd_points_as_cornersr#   make_smooth)r:   r!   discontinuities_arrayboundary_timest1t2r   xyzpointss   `          r.   generate_pointsz"ParametricFunction.generate_points   s   +$7777$ O %'HT/-B-B$C$C!XJJ ,dg5 ,dg5	 N !!!!"j$*5N.A.qt!t0DTRRR 	3 	3FBh\**29RT[+I+IJJL))"-- G " G--001a!!RZ00 )a((A1a)!444"E"E"E"EW"E"E"EFFq	***&&vabbz2222 	r0   Nonec                .    |                                   d S r>   rb   r@   s    r.   init_pointszParametricFunction.init_points       r0   )r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r$   r&   r   )r)   r<   r2   r)   r   r)   rc   )__name__
__module____qualname____doc__r   r9   rA   rD   rb   rf   __classcell__r;   s   @r.   r   r      s        K K` EK(jll26"$# # # # # # #:          ( ( ( (T       r0   r   )	metaclassc                  :     e Zd ZdZdefd fdZddZddZ xZS )r   a  A :class:`ParametricFunction` that spans the length of the scene by default.

    Examples
    --------
    .. manim:: ExampleFunctionGraph
        :save_last_frame:

        class ExampleFunctionGraph(Scene):
            def construct(self):
                cos_func = FunctionGraph(
                    lambda t: np.cos(t) + 0.5 * np.cos(7 * t) + (1 / 7) * np.cos(14 * t),
                    color=RED,
                )

                sin_func_1 = FunctionGraph(
                    lambda t: np.sin(t) + 0.5 * np.sin(7 * t) + (1 / 7) * np.sin(14 * t),
                    color=BLUE,
                )

                sin_func_2 = FunctionGraph(
                    lambda t: np.sin(t) + 0.5 * np.sin(7 * t) + (1 / 7) * np.sin(14 * t),
                    x_range=[-4, 4],
                    color=GREEN,
                ).move_to([0, 1, 0])

                self.add(cos_func, sin_func_1, sin_func_2)
    Nr   Callable[[float], Any]x_range7tuple[float, float] | tuple[float, float, float] | Nonecolorr   r&   r   r)   rc   c                    |t           d          t           d         f}|| _        fd| _        | _         t	                      j        | j        | j        fd|i| d S )Nframe_x_radiusc                D    t          j        |  |           dg          S )Nr   )r+   rN   r-   s    r.   rG   z(FunctionGraph.__init__.<locals>.<lambda>   s'    QJ
 J
 r0   ru   )r   rs   parametric_functionr   r8   r9   )r:   r   rs   ru   r&   r;   s    `   r.   r9   zFunctionGraph.__init__   s     ?/00&9I2JKG@
 @
 @
 @
  !14<WWuWPVWWWWWr0   c                    | j         S r>   r?   r@   s    r.   rA   zFunctionGraph.get_function   rB   r0   r^   r    r   c                ,    |                      |          S r>   )ry   )r:   r^   s     r.   rD   z%FunctionGraph.get_point_from_function   s    ''***r0   )
r   rr   rs   rt   ru   r   r&   r   r)   rc   )r)   rr   )r^   r    r)   r   )	rj   rk   rl   rm   r   r9   rA   rD   rn   ro   s   @r.   r   r      s         > LP$/	X X X X X X X"   + + + + + + + +r0   r   c                  <     e Zd Z	 	 	 	 	 dd fdZddZddZ xZS )r   N     TfuncCallable[[float, float], float]rs   Sequence[float] | Noney_range	min_depthint	max_quadsr#   r$   r&   r   c                
   || _         || _        || _        || _        |pt          j         dz  t          j        dz  g| _        |pt          j         dz  t          j        dz  g| _         t                      j
        di | dS )aP  An implicit function.

        Parameters
        ----------
        func
            The implicit function in the form ``f(x, y) = 0``.
        x_range
            The x min and max of the function.
        y_range
            The y min and max of the function.
        min_depth
            The minimum depth of the function to calculate.
        max_quads
            The maximum number of quads to use.
        use_smoothing
            Whether or not to smoothen the curves.
        kwargs
            Additional parameters to pass into :class:`VMobject`


        .. note::
            A small ``min_depth`` :math:`d` means that some small details might
            be ignored if they don't cross an edge of one of the
            :math:`4^d` uniform quads.

            The value of ``max_quads`` strongly corresponds to the
            quality of the curve, but a higher number of quads
            may take longer to render.

        Examples
        --------
        .. manim:: ImplicitFunctionExample
            :save_last_frame:

            class ImplicitFunctionExample(Scene):
                def construct(self):
                    graph = ImplicitFunction(
                        lambda x, y: x * y ** 2 - x ** 2 * y - 2,
                        color=YELLOW
                    )
                    self.add(NumberPlane(), graph)
        r1   Nr3   )r   r   r   r#   r   frame_widthrs   frame_heightr   r8   r9   )	r:   r   rs   r   r   r   r#   r&   r;   s	           r.   r9   zImplicitFunction.__init__   s    h ""* 
!#"#
  
  1$!##

 	""6"""""r0   r)   r   c                    t          j         j        d          j        d         g          t          j         j        d          j        d         g          }}t	           fd|| j         j                  }d |D             }|D ]:}                     |d                                         |dd                     ; j	        r 
                                  S )Nr   r   c                H                         | d         | d                   S )Nr   r   r?   )ur:   s    r.   rG   z2ImplicitFunction.generate_points.<locals>.<lambda>:  s    qtQqT22 r0   )fnpminpmaxr   r   c                H    g | ]}|g k    t          j        |d dg           S ))r   r   r   )r+   pad)rK   curves     r.   rL   z4ImplicitFunction.generate_points.<locals>.<listcomp>@  s5     
 
 
055B;;BF566*++;;;r0   )r+   rN   rs   r   r   r   r   rW   rX   r#   rY   )r:   p_minp_maxcurvesr   s   `    r.   rb   z ImplicitFunction.generate_points4  s    Hdl1ot|A788Hdl1ot|A788  2222nn
 
 

 
9?
 
 
  	2 	2Ea)))&&uQRRy1111 	r0   rc   c                .    |                                   d S r>   re   r@   s    r.   rf   zImplicitFunction.init_pointsJ  rg   r0   )NNr}   r~   T)r   r   rs   r   r   r   r   r   r   r   r#   r$   r&   r   rh   ri   )rj   rk   rl   r9   rb   rf   rn   ro   s   @r.   r   r      s         +/*."A# A# A# A# A# A# A#F   ,       r0   r   )"rm   
__future__r   __all__collections.abcr   r   r	   typingr
   numpyr+   isosurfacesr   manimr   manim.mobject.graphing.scaler   r   )manim.mobject.opengl.opengl_compatibilityr   &manim.mobject.types.vectorized_mobjectr   r   r   manim.typingr   r   manim.utils.colorr   r   r   r   r   r3   r0   r.   <module>r      s   , , " " " " " "
E
E
E 9 8 8 8 8 8 8 8 8 8                 $ $ $ $ $ $       ? ? ? ? ? ? ? ? E E E E E E ; ; ; ; ; ; 5        11111111444444 ) ) ) ) ) )\ \ \ \ \_ \ \ \ \~2+ 2+ 2+ 2+ 2+& 2+ 2+ 2+j[ [ [ [ [x? [ [ [ [ [ [r0   