
    lj[J                    *   d Z ddlmZ ddgZddlmZmZmZ ddlm	Z	 ddl
ZddlmZmZ dd	lT 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 ddlmZmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* dZ+ G d de          Z, G d de          Z-dS )zEMobjects representing objects from probability theory and statistics.    )annotationsSampleSpaceBarChart)IterableMutableSequenceSequence)AnyN)configlogger)*)	Rectangle)Axes)OpenGLVMobject)Brace)MathTexTex)VGroupVMobject)Vector3D)BLUE_E	DARK_GREYGREEN_E
LIGHT_GREYMAROON_BYELLOWParsableManimColorcolor_gradient)tuplifyg-C6?c                       e Zd ZdZddeddedfd7 fdZdefd8dZd9dZ	d:dZ
d;d#Zeegefd<d$Zeegefd<d%Zd=d)Zd=d*Zedfd>d0Zefd?d1Zd@d2Zd@d3ZdAd4ZdBd6Z xZS )Cr   a  A mobject representing a twodimensional rectangular
    sampling space.

    Examples
    --------
    .. manim:: ExampleSampleSpace
        :save_last_frame:

        class ExampleSampleSpace(Scene):
            def construct(self):
                poly1 = SampleSpace(stroke_width=15, fill_opacity=1)
                poly2 = SampleSpace(width=5, height=3, stroke_width=5, fill_opacity=0.5)
                poly3 = SampleSpace(width=2, height=2, stroke_width=5, fill_opacity=0.1)
                poly3.divide_vertically(p_list=np.array([0.37, 0.13, 0.5]), colors=[BLACK, WHITE, GRAY], vect=RIGHT)
                poly_group = VGroup(poly1, poly2, poly3).arrange()
                self.add(poly_group)
                ?heightfloatwidth
fill_colorr   fill_opacitystroke_widthstroke_colordefault_label_scale_valc                d    t                                          ||||||           || _        d S )N)r#   r%   r&   r'   r(   r)   )super__init__r*   )	selfr#   r%   r&   r'   r(   r)   r*   	__class__s	           ]/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/mobject/graphing/probability.pyr-   zSampleSpace.__init__8   sH     	!%%% 	 	
 	
 	
 (?$$$    zSample spacetitlestrbuffreturnNonec                    t          |          }|j        | j        k    r| j        |_        |                    | t          |           || _        |                     |           d S )Nr4   )r   r%   next_toUPr2   add)r.   r2   r4   	title_mobs       r0   	add_titlezSampleSpace.add_titleL   sa     JJ	?TZ''"jIO$...
r1   labelc                    || _         d S N)r>   )r.   r>   s     r0   	add_labelzSampleSpace.add_labelW   s    


r1   p_listfloat | Iterable[float]list[float]c                    t          |          }t          |          }dt          |          z
  }t          |          t          k    r|                    |           |S )Ng      ?)r   listsumabsEPSILONappend)r.   rB   p_list_tuplified
new_p_list	remainders        r0   complete_p_listzSampleSpace.complete_p_listZ   sW    )0*++
#j//)	y>>G##i(((r1   dimintcolorsSequence[ParsableManimColor]vectr   r   c                   |                      |          }t          |t          |                    }|                     |           }t	                      }t          ||d          D ]\  }	}
t                      }|                    |
d           |                    | d           |	                    |	|           |
                    ||            |                    |          }|                    |           |S )NTstrictr!   )stretch)rN   r   lenget_edge_centerr   zipr   set_fillreplacerW   move_tor;   )r.   rB   rO   rQ   rS   p_list_completecolors_in_gradient
last_pointpartsfactorcolorparts               r0   get_division_along_dimensionz(SampleSpace.get_division_along_dimensionb   s     ..v66+FC4H4HII))4%00
 2DTRRR 	 	MFE==DMM%###LLtL,,,LL%%%LLdU+++--d33JIIdOOOOr1   c                2    |                      |d||          S )Nr!   re   r.   rB   rQ   rS   s       r0   get_horizontal_divisionz#SampleSpace.get_horizontal_divisionx        00FDIIIr1   c                2    |                      |d||          S )Nr   rg   rh   s       r0   get_vertical_divisionz!SampleSpace.get_vertical_division   rj   r1   argsr	   kwargsc                ^     | j         |i || _        |                     | j                   d S r@   )ri   horizontal_partsr;   r.   rm   rn   s      r0   divide_horizontallyzSampleSpace.divide_horizontally   s7     < <d Mf M M&'''''r1   c                ^     | j         |i || _        |                     | j                   d S r@   )rl   vertical_partsr;   rq   s      r0   divide_verticallyzSampleSpace.divide_vertically   s7    8d8$I&II$%%%%%r1   ra   labels%list[str | VMobject | OpenGLVMobject]	directionmin_num_quadsc                Z   t                      }t                      }t          ||d          D ]\  }}	t          |	|||          }
t          |t          t
          f          r|}n)t          |          }|                    | j                   |	                    |
||           |
                    |
           t          |t                    sJ |
                    |           ||_        ||_        |                                ||d|_        t          |j        |j                  S )NFrU   )ry   r4   )rv   rx   r4   )r   rZ   r   
isinstancer   r   r   scaler*   r9   r;   bracesrv   copylabel_kwargs)r.   ra   rv   rx   r4   ry   
label_mobsr}   r>   rd   brace	label_mobs               r0   !get_subdivision_braces_and_labelsz-SampleSpace.get_subdivision_braces_and_labels   s'    XX
vuU;;; 	& 	&KE4$	TRRRE%(N!;<< >!		#ENN	 <===eY555JJui22222NN9%%%%! oo''"
 

 elEL111r1   c                T    t          | d          sJ | j        } | j        |||fi |S )Nrp   )hasattrrp   r   )r.   rv   rx   rn   ra   s        r0   get_side_braces_and_labelsz&SampleSpace.get_side_braces_and_labels   sN     t/00000%5t569
 
(.
 
 	
r1   c                ^    t          | d          sJ | j        } | j        ||t          fi |S Nrt   )r   rt   r   r:   r.   rv   rn   ra   s       r0   get_top_braces_and_labelsz%SampleSpace.get_top_braces_and_labels   sB     t-.....#5t5eVRRR6RRRr1   c                ^    t          | d          sJ | j        } | j        ||t          fi |S r   )r   rt   r   DOWNr   s       r0   get_bottom_braces_and_labelsz(SampleSpace.get_bottom_braces_and_labels   sB     t-.....#5t5eVTTTVTTTr1   c                    dD ][}t          | |          st          | |          }dD ]5}t          ||          r#|                     t          ||                     6\d S )N)rp   rt   )r}   rv   )r   getattrr;   )r.   attrra   subattrs       r0   add_braces_and_labelsz!SampleSpace.add_braces_and_labels   s    8 	6 	6D4&& D$''E- 6 65'** 6HHWUG445556		6 	6r1   indexc                    t          | d          r| j        |         }|S t          | d          r| j        |         }|S |                                 |         S )Nrp   rt   )r   rp   rt   split)r.   r   vals      r0   __getitem__zSampleSpace.__getitem__   s_    4+,, 	#4U;CJT+,, 	%e,CJzz||E""r1   )r#   r$   r%   r$   r&   r   r'   r$   r(   r$   r)   r   r*   r$   )r2   r3   r4   r$   r5   r6   )r>   r3   r5   r6   )rB   rC   r5   rD   )
rB   rC   rO   rP   rQ   rR   rS   r   r5   r   )rB   rC   rQ   rR   rS   r   r5   r   )rm   r	   rn   r	   r5   r6   )ra   r   rv   rw   rx   r   r4   r$   ry   rP   r5   r   )rv   rw   rx   r   rn   r	   r5   r   )rv   rw   rn   r	   r5   r   r5   r6   )r   rP   r5   r   )__name__
__module____qualname____doc__r   r   r-   MED_SMALL_BUFFr=   rA   rN   re   r   r   r   ri   r   r   RIGHTrl   rr   ru   
SMALL_BUFFr   LEFTr   r   r   r   r   __classcell__r/   s   @r0   r   r   %   s        ( )2!+5)*? ? ? ? ? ? ?* *	 	 	 	 	         2 18/@	J J J J J 19&/A	J J J J J( ( ( (& & & & !2 2 2 2 2D #

 

 

 

 

S S S SU U U U6 6 6 6# # # # # # # #r1   c                  x     e Zd ZdZddddg ddddfd1 fdZd2dZd2dZd3d!Zd2d"Zdd#e	e
fd4d+Z	 d5d6d0Z xZS )7r   aa  Creates a bar chart. Inherits from :class:`~.Axes`, so it shares its methods
    and attributes. Each axis inherits from :class:`~.NumberLine`, so pass in ``x_axis_config``/``y_axis_config``
    to control their attributes.

    Parameters
    ----------
    values
        A sequence of values that determines the height of each bar. Accepts negative values.
    bar_names
        A sequence of names for each bar. Does not have to match the length of ``values``.
    y_range
        The y_axis range of values. If ``None``, the range will be calculated based on the
        min/max of ``values`` and the step will be calculated based on ``y_length``.
    x_length
        The length of the x-axis. If ``None``, it is automatically calculated based on
        the number of values and the width of the screen.
    y_length
        The length of the y-axis.
    bar_colors
        The color for the bars. Accepts a sequence of colors (can contain just one item).
        If the length of``bar_colors`` does not match that of ``values``,
        intermediate colors will be automatically determined.
    bar_width
        The length of a bar. Must be between 0 and 1.
    bar_fill_opacity
        The fill opacity of the bars.
    bar_stroke_width
        The stroke width of the bars.

    Examples
    --------
    .. manim:: BarChartExample
        :save_last_frame:

        class BarChartExample(Scene):
            def construct(self):
                chart = BarChart(
                    values=[-5, 40, -10, 20, -3],
                    bar_names=["one", "two", "three", "four", "five"],
                    y_range=[-20, 50, 10],
                    y_length=6,
                    x_length=10,
                    x_axis_config={"font_size": 36},
                )

                c_bar_lbls = chart.get_bar_labels(font_size=48)

                self.add(chart, c_bar_lbls)
    N)z#003f5cz#58508dz#bc5090z#ff6361z#ffa600g333333?gffffff?r    valuesMutableSequence[float]	bar_namesSequence[str] | Noney_rangeSequence[float] | Nonex_lengthfloat | Noney_length
bar_colorsIterable[str]	bar_widthr$   bar_fill_opacitybar_stroke_widthrn   r	   c
                v   t          |t                    r#t          j        d           t	          |          }||nt
          j        dz
  }|| _        || _        || _	        || _
        || _        |	| _        dt          | j                  dg}|it          dt          | j                            t          dt          | j                            t!          t          | j                  |z  d          g}n<t          |          dk    r)g |t!          t          | j                  |z  d          }|/t          t          | j                  t
          j        dz
            }dt$          d}|                     |f|
                    dd           f           t+                      | _        d | _        d | _         t3                      j        d||||||
                    d	d
          d|
 |                                  | j        |                                  | j                                         d S )NzPassing a string to `bar_colors` has been deprecated since v0.15.2 and will be removed after v0.17.0, the parameter must be a list.     r   r!         )	font_sizelabel_constructorx_axis_configtipsF)x_ranger   r   r   r   r    )r{   r3   r   warningrF   r
   frame_heightr   r   r   r   r   r   rX   minmaxroundframe_widthr   _update_default_configspopr   barsx_labels
bar_labelsr,   r-   	_add_bars_add_x_axis_labelsy_axisadd_numbers)r.   r   r   r   r   r   r   r   r   r   rn   r   r   r/   s                r0   r-   zBarChart.__init__  s@   & j#&& 	*N X   j))J'3889Lq9P"$" 0 0c$+&&*?As4;''((As4;''((c$+&&1155GG \\QGGs4;'7'7('BA!F!FGG3t{++V-?!-CDDH&(sCC$$vzz/4@@B	
 	
 	
 #HH	'+)- 	
'FE**	
 	
 	
 	
 	
 	>%##%%%!!!!!r1   r5   r6   c                .     | j         j        | j          dS )zInitialize the colors of the bars of the chart.

        Sets the color of ``self.bars`` via ``self.bar_colors``.

        Primarily used when the bars are initialized with ``self._add_bars``
        or updated via ``self.change_bar_values``.
        N)r   set_color_by_gradientr   )r.   s    r0   _update_colorszBarChart._update_colorsZ  s     	(	'9999r1   c                   t          | j        t                    sJ t          j        dt          | j                  d          }t                      }t          t          || j        d                    D ]\  }\  }}| j	        |         dk     rt          nt          }| j                            |          }| j        j        |_        |                    | j                            |          || j        j                   |                    |           || j        _        | j                            |           dS )zEssentially :meth`:~.NumberLine.add_labels`, but differs in that
        the direction of the label with respect to the x_axis changes to UP or DOWN
        depending on the value.

        UP for negative values and DOWN for positive values.
        r"   r!   TrU   r   )rx   r4   N)r{   r   rF   nparangerX   r   	enumeraterZ   r   r:   r   x_axisr   r   r9   number_to_pointline_to_number_buffr;   rv   )r.   	val_rangerv   ivaluebar_namerx   bar_name_labels           r0   r   zBarChart._add_x_axis_labelsd  s3    $.$/////IT^$$a
 
	 $-	4>$777%
 %
 	' 	' A x
 #k!nq00dI&*k&C&CH&M&MN'+{'<N$""++E22#[4 #    JJ~&&&&#r1   
bar_numberrP   r   r   c                   t          |                     d|          d         |                     dd          d         z
            }|                     | j        d          d         |                     dd          d         z
  }t          ||| j        | j                  }|dk    rt          nt          }|                    |                     |dz   d          |d           |S )a]  Creates a positioned bar on the chart.

        Parameters
        ----------
        bar_number
            Determines the x-position of the bar.
        value
            The value that determines the height of the bar.

        Returns
        -------
        Rectangle
            A positioned rectangle representing a bar on the chart.
        r   r!   )r#   r%   r(   r'   r"   r8   )	rH   c2pr   r   r   r   r:   r   r9   )r.   r   r   bar_hbar_wbarposs          r0   _create_barzBarChart._create_bar  s    $ DHHQ&&q)DHHQNN1,==>>++A.!Q1BB..	
 
 
 aZZbbdDHHZ#-q113Q???
r1   c                    t          | j                  D ]6\  }}|                     ||          }| j                            |           7|                                  |                     | j                   d S )N)r   r   )r   r   r   r   r;   r   add_to_back)r.   r   r   tmp_bars       r0   r   zBarChart._add_bars  s|    !$+.. 	# 	#HAu&&!5&AAGIMM'""""#####r1   r   rc   ParsableManimColor | Noner   r4   r   type[MathTex]r   c                   t                      }t          | j        | j        d          D ]\  }} |t	          |                    }|(|                    |                                           n|                    |           ||_        |dk    rt          nt          }	|
                    ||	|           |                    |           |S )a  Annotates each bar with its corresponding value. Use ``self.bar_labels`` to access the
        labels after creation.

        Parameters
        ----------
        color
            The color of each label. By default ``None`` and is based on the parent's bar color.
        font_size
            The font size of each label.
        buff
            The distance from each label to its bar. By default 0.4.
        label_constructor
            The Mobject class to construct the labels, by default :class:`~.Tex`.

        Examples
        --------
        .. manim:: GetBarLabelsExample
            :save_last_frame:

            class GetBarLabelsExample(Scene):
                def construct(self):
                    chart = BarChart(values=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1], y_range=[0, 10, 1])

                    c_bar_lbls = chart.get_bar_labels(
                        color=WHITE, label_constructor=MathTex, font_size=36
                    )

                    self.add(chart, c_bar_lbls)
        FrU   Nr   r8   )r   rZ   r   r   r3   	set_colorget_fill_colorr   r:   r   r9   r;   )
r.   rc   r   r4   r   r   r   r   bar_lblr   s
             r0   get_bar_labelszBarChart.get_bar_labels  s    H XX
diUCCC 	$ 	$JC00U<<G}!!#"4"4"6"67777!!%((( )G!""$COOC4O000NN7####r1   TIterable[float]update_colorsboolc                   t          t          | j        |d                    D ]\  }\  }}| j        |         }|dk    r|                                }t
          }n|                                }t          }|dk    rJ||z  }	|	dk     r|dk    rt          nt
          }|                    t          |	          |j
        z             nK|                     ||          }
| j                            |           | j                            ||
           |                    ||           |r|                                  || j        dt!          t#          |                    <   dS )a  Updates the height of the bars of the chart.

        Parameters
        ----------
        values
            The values that will be used to update the height of the bars.
            Does not have to match the number of bars.
        update_colors
            Whether to re-initalize the colors of the bars based on ``self.bar_colors``.

        Examples
        --------
        .. manim:: ChangeBarValuesExample
            :save_last_frame:

            class ChangeBarValuesExample(Scene):
                def construct(self):
                    values=[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]

                    chart = BarChart(
                        values,
                        y_range=[-10, 10, 2],
                        y_axis_config={"font_size": 24},
                    )
                    self.add(chart)

                    chart.change_bar_values(list(reversed(values)))
                    self.add(chart.get_bar_labels(font_size=24))
        FrU   r   N)r   rZ   r   r   
get_bottomr   get_topr:   stretch_to_fit_heightrH   r#   r   removeinsertr]   r   rX   rF   )r.   r   r   r   r   r   	chart_valbar_limaligned_edgequotienttemp_bars              r0   change_bar_valueszBarChart.change_bar_values  sb   @  )TYu)M)M)MNN 	/ 	/OA|UAI1}}..**#++--! A~~ 9,a<<)2Q22DL ))#h--#**DEEEE  ++Au55	  %%%	  H---KK.... 	"!!!+1'c$v,,'''(((r1   )r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r$   r   r$   rn   r	   r   )r   rP   r   r$   r5   r   )
rc   r   r   r$   r4   r$   r   r   r5   r   )T)r   r   r   r   r5   r6   )r   r   r   r   r-   r   r   r   r   r   r   r   r   r   r   s   @r0   r   r      s       0 0j +/*.!%!%%
 %
 %
 "%"#!I" I" I" I" I" I" I"V: : : :           D   B$ $ $ $ ,0$+.3 3 3 3 3l >BD2 D2 D2 D2 D2 D2 D2 D2 D2r1   ).r   
__future__r   __all__collections.abcr   r   r   typingr	   numpyr   manimr
   r   manim.constantsmanim.mobject.geometry.polygramr   )manim.mobject.graphing.coordinate_systemsr   .manim.mobject.opengl.opengl_vectorized_mobjectr   manim.mobject.svg.bracer   manim.mobject.text.tex_mobjectr   r   &manim.mobject.types.vectorized_mobjectr   r   manim.typingr   manim.utils.colorr   r   r   r   r   r   r   r   manim.utils.iterablesr   rI   r   r   r   r1   r0   <module>r     s   K K " " " " " "*
% @ ? ? ? ? ? ? ? ? ?                               5 5 5 5 5 5 : : : : : : I I I I I I ) ) ) ) ) ) 7 7 7 7 7 7 7 7 C C C C C C C C ! ! ! ! ! !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 * ) ) ) ) )
t# t# t# t# t#) t# t# t#nL2 L2 L2 L2 L2t L2 L2 L2 L2 L2r1   