3b1b / manim

Animation engine for explanatory math videos
MIT License
60.39k stars 5.7k forks source link

Weird numerical instability controlled by axis increment #1458

Open jeffshrager opened 3 years ago

jeffshrager commented 3 years ago

Describe the bug

See attached, possibly related to #1441

Code: It's in the screen shot, although I'll put the whole code here (the only difference is the axis label step)

class GraphExample(Scene):
    def construct(self):
        #axes = Axes((-10, 10, 5),(-10, 50, 10))                                                                
        axes = Axes((-3, 3, 5),(-1000, 10, 100))
        axes.add_coordinate_labels()
        self.play(Write(axes, lag_ratio=0.01, run_time=1))
        first_graph = lambda x: 2**x
        first_label = "2^{x}"
        graph1 = axes.get_graph(first_graph, color=BLUE)
        label1 = axes.get_graph_label(graph1, first_label)
        self.play(ShowCreation(graph1),FadeIn(label1, RIGHT))
        self.wait(3)
        graphs_and_labels = [
                             #(lambda x: (1/2)**x, "(\\frac{1}{2})^{x}"),                                       
                             #(lambda x: (2**((-3*x)+1))-3, "2^{-3x+1}-3"),                                     
                             #(lambda x: ((1/2)**((x/2)-4))-1, "(\\frac{1}{2})^{(\\frac{x}{2})-4}-1"),          
                             #(lambda x: (-2**(2*x))+(19/93), "(-2^{2x})+\\frac{19}{3}"),                       
                             #(lambda x: (2**(x/4))-3, "2^{(\\frac{x}{4})}-3"),                                 
                             (lambda x: -((1/2)**(-4*x)), "-(\\frac{1}{2})^{-4x}")
                             ]
        for graph, label in graphs_and_labels:
            graph2 = axes.get_graph(graph,color=BLUE)
            label2 = axes.get_graph_label(graph2,label)
            self.play(ReplacementTransform(graph1, graph2),FadeTransform(label1, label2))
            self.wait(3)
            graph1 = graph2
            label1 = label2

Wrong display or Error traceback: n/a

Additional context

n/a

Screen Shot 2021-04-05 at 9 20 59 AM Screen Shot 2021-04-05 at 9 21 28 AM
jeffshrager commented 3 years ago

Ummmm. There's something really weird going on here. I worked up my code a little to generalize the axis display (see below). It replicates the "bug", as expected, but if I include BOTH the "buggy" and "correct" versions, the "bug" does NOT occur. However, if you watch it do the plot, you can see that it's running through the "buggy" version to end up at the correct graph, so it looks like the issue here is some sort of incomplete computation????

class GraphExample(Scene):
    def construct(self):
        first_axes = Axes((-10, 10, 5),(-10, 50, 10))
        first_axes.add_coordinate_labels()
    self.play(Write(first_axes, lag_ratio=0.01, run_time=1))
        first_graph = lambda x: 2**x
        first_label = "2^{x}"
        graph1 = first_axes.get_graph(first_graph, color=BLUE)
        label1 = first_axes.get_graph_label(graph1, first_label)
        self.play(ShowCreation(graph1),FadeIn(label1, RIGHT))
        self.wait(3)
        old_axes = first_axes
        more_graphs = [
            #(lambda x: (1/2)**x, "(\\frac{1}{2})^{x}"),                                                                                      
            #(lambda x: (2**((-3*x)+1))-3, "2^{-3x+1}-3"),                                                                                    
            #(lambda x: ((1/2)**((x/2)-4))-1, "(\\frac{1}{2})^{(\\frac{x}{2})-4}-1"),                                                         
            #(lambda x: (-2**(2*x))+(19/93), "(-2^{2x})+\\frac{19}{3}"),                                                                      
            #(lambda x: (2**(x/4))-3, "2^{(\\frac{x}{4})}-3"),                                                                                
            (lambda x: -((1/2)**(-4*x)), "-(\\frac{1}{2})^{-4x}", Axes((-3, 3, 5),(-1000, 10, 100))),
            # Taking the following line in/out (and adjusting the comma above as needed) replicates the reported behavior
            (lambda x: -((1/2)**(-4*x)), "-(\\frac{1}{2})^{-4x}", Axes((-3, 3, 1),(-1000, 10, 100)))
            ]
        for graph, label, new_axes in more_graphs:
            new_axes.add_coordinate_labels()
            self.play(FadeTransform(old_axes, new_axes))
            graph2 = new_axes.get_graph(graph,color=BLUE)
            label2 = new_axes.get_graph_label(graph2,label)
            self.play(ReplacementTransform(graph1, graph2),FadeTransform(label1, label2))
            self.wait(3)
            graph1 = graph2
            label1 = label2
            old_axes=new_axes