Search code examples
pythonmanim

Multiple linear/non linear transformations in Manim


I'm writing a script in manim to perform some transformations. And I want to apply multiple linear/non linear transformations in a sequence to a graph. And I tried it but failed. Here is my code

from manim import *
import numpy as np

class transformation(LinearTransformationScene):
    def construct(self):
        # Linear transformation
        self.apply_matrix([[1, 0], [1, 1]])              # <============

        # Non-linear transformation
        self.apply_nonlinear_transformation(self.func)   # <============

        self.wait()

    def func(self, dot):
        return np.array((max(dot[0], 0), max(dot[1], 0), dot[2]))

And there is a strange behavior which I'm unable to understand is that if I run only one of the highlighted lines. Then that transformation works fine. But if I run both lines at the same time then it throughs this error:

ValueError: operands could not be broadcast together with shapes (4,3) (200,3)

So what is the reason of this error? And how to run both transformations?


Solution

  • Add self.wait() between the transformations.

    from manim import *
    import numpy as np
    
    class transformation(LinearTransformationScene):
        def construct(self):
            # Linear transformation
            self.apply_matrix([[1, 0], [1, 1]])              # <============
    
             self.wait(0) #just add this
    
            # Non-linear transformation
            self.apply_nonlinear_transformation(self.func)   # <============
    
            self.wait()
    
        def func(self, dot):
            return np.array((max(dot[0], 0), max(dot[1], 0), dot[2]))
    

    edit: changed self.wait() to self.wait(0)