### Quickstart Scene with Reactive Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/front/install.md A basic Manim scene demonstrating the use of Reactive Manim for dynamic mathematical expressions. This example requires Manim Community v0.18 or v0.19. ```python from manim import * from reactive_manim import * class Quickstart(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) c = MathTex("c", color=GREEN) tex = MathTex([[ a, "x^2" ], "+", [ b, "x" ], "+", [ c ]],"=", 0) self.add(tex).wait(1) b.swap(lambda: Fraction(b, a.pop())) c.swap(lambda: Fraction(c, a)) self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Install Reactive Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Install the reactive-manim library using pip. Ensure you are using ManimCE version 0.18.1. ```sh pip install reactive-manim ``` -------------------------------- ### Piecewise Animation Example Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates creating and animating a piecewise function using MathCases and CaseLine components. ```python class CasesScene(Scene): def construct(self): tex = MathTex("f(x) =", MathCases( CaseLine(r"b, ", r"\text{even}(x)"), CaseLine(r"¬b,", r"\text{odd}(x)"), CaseLine(r"?,", r"\text{otherwise}"), )) self.add(tex).wait(1) cases = tex[1] cases[1].condition.set_color(ORANGE) cases[2].output.set_color(ORANGE) self.play(TransformInStages.progress(tex)) cases[1].condition = cases[2].condition cases.lines = [ cases[0], cases[1] ] self.play(TransformInStages.progress(tex, lag_ratio=0.4, run_time=1.5)) self.wait(2) ``` -------------------------------- ### Install Reactive Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/front/install.md Use pip to install the reactive-manim package. Ensure you have Manim Community v0.18 or v0.19 installed, as the OpenGL renderer is not supported. ```shell $ pip install reactive-manim ``` -------------------------------- ### Create Math Matrix Animation in Python Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates creating and animating a matrix equation using MathTex and MathMatrix. This example utilizes auto-disconnect functionality. ```python class MatrixScene(Scene): def construct(self): y0, y1 = Term("y", subscript=0), Term("y", subscript=1) x0, x1 = Term("x", subscript=0), Term("x", subscript=1) w00, w01 = Term("w", subscript=\"00"), Term("w", subscript=\"01") w10, w11 = Term("w", subscript=\"10"), Term("w", subscript=\"11") tex1 = MathTex(y0, "=", w00, x0, "+", w01, x1) tex2 = MathTex(y1, "=", w10, x0, "+", w11, x1) group = DGroup(tex1, tex2).arrange(DOWN).shift(UP) self.add(group).wait(2) tex3 = MathTex( MathMatrix([ [ y0 ], [ y1 ] ]), "=", MathMatrix([ [ w00, w01 ], [ w10, w11 ] ]), MathMatrix([ [ x0 ], [ x1 ] ]) ) tex3.shift(DOWN) self.play(TransformInStages.from_copy(group, tex3[0], lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, tex3[1], lag_ratio=0.4, run_time=1.4)) self.play(TransformInStages.from_copy(group, tex3[2], lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, tex3[3] - x0, lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, x0, lag_ratio=0.4, run_time=2.5)) self.wait(2) ``` -------------------------------- ### Exponent Animation Example Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates animating terms and superscripts within a MathTex object using Reactive Manim components. ```python class ExponentScene(Scene): def construct(self): term1, term2 = Term("2"), Term("2", "4") tex = MathTex(term1, Parentheses(term2)) self.add(tex).wait(1) term2.term.save_y() term1.term.target_id = term2.term.id term2.superscript.set_tex_string("5") tex.terms = [ term2 ] term2.term.restore_y() self.play(TransformInStages.progress(tex, lag_ratio=0.4)) self.wait(1) ``` -------------------------------- ### Natural Log Animation Example Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Shows how to transform terms into exponential and then natural logarithm functions within a MathTex object. ```python class NaturalLogScene(Scene): def construct(self): tex = MathTex("a", "=", "b") self.add(tex).wait(0.5) tex[0] = Term("e", tex[0]) tex[2] = Term("e", tex[2]) self.play(TransformInStages.progress(tex, lag_ratio=0.6)) tex[0] = Function(r"\ln", tex[0]) tex[2] = Function(r"\ln", tex[2]) self.play(TransformInStages.progress(tex, lag_ratio=0.6)) tex[0] = MathTex(tex[0].input.superscript.pop(), tex[0]) tex[2] = MathTex(tex[2].input.superscript.pop(), tex[2]) self.play(TransformInStages.progress(tex)) tex[0] = tex[0][0] tex[2] = tex[2][0] self.play(TransformInStages.progress(tex, lag_ratio=0.8)) ``` -------------------------------- ### Transform Piecewise Function with MathCases Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/cases.md Demonstrates transforming a piecewise function using MathCases. This example shows how to modify conditions and outputs, and apply transformations like TransformInStages. ```python class PredicatePartition(Scene): def construct(self): tex = MathTex("f(x) =", MathCases( CaseLine(r"b, ", r"\text{even}(x)"), CaseLine(r"\neg b,", r"\text{odd}(x)"), CaseLine(r"?,", r"\text{otherwise}"), )) self.add(tex) cases = tex[1] cases[1].condition.set_color(ORANGE) cases[2].output.set_color(ORANGE) self.play(TransformInStages.progress(tex)) cases[1].condition = cases[2].condition cases.lines = [ cases[0], cases[1] ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Define Piecewise Function with MathCases Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/cases.md Use MathCases to define a piecewise function for a neuron's voltage over time. This example shows how to set different expressions for different time conditions. ```python class NeuronVoltage(Scene): def construct(self): cases = MathCases( CaseLine("0,", "t < 0"), CaseLine("te^{-t},", "t \geq 0") ) tex = MathTex("v(t) = ", cases) self.add(tex) tex.set_color(LIGHT_BROWN) cases[1].output.set_color(GREEN) cases[1].condition.set_color(BLUE) ``` -------------------------------- ### Transform from Copy Example Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Utilize `from_copy` to animate a transformation where the target mobject is a copy of parts of the source. This is useful for creating animations where elements are reused or modified. ```python tex1 = MathTex("a", "b", "c").shift(UP) tex2 = MathTex(tex1[1].copy(), tex1[2].copy(), "d") self.add(tex1) self.wait(1) self.play(TransformInStages.from_copy(tex1, tex2, lag_ratio=0.5)) tex1.set_color(RED) tex2.set_color(GREEN) self.wait(2) ``` -------------------------------- ### Distributing Terms in an Algebraic Expression Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/functionality-showcase.md Shows the distribution of terms in an algebraic expression using Manim. This example utilizes Scene, MathTex, Parentheses, and TransformInStages. ```python class DistributionScene(Scene): def construct(self): a, x, m, y = MathTex("a", "x", "-", "y") tex = MathTex(a, Parentheses([ x, m, y ])) self.add(tex).wait(1) m.save_y() tex.terms = [[a, x], m, [a, y]] m.restore_y() self.play(TransformInStages.progress(tex, lag_ratio=0)) ``` -------------------------------- ### Replacement Transform Example Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Use `replacement_transform` to animate the transformation of one mobject to another, where the source is removed and the target is added. Ensure copied mobjects retain their IDs using `Mobject.copy()`. ```python tex1 = MathTex("a", "b", "c") tex2 = MathTex(tex1[1].copy(), tex1[2].copy(), "d") self.add(tex1) self.wait(1) self.play(TransformInStages.replacement_transform(tex1, tex2, lag_ratio=0.5)) tex1.set_color(RED) tex2.set_color(GREEN) self.wait(2) ``` -------------------------------- ### Create a Basic Fraction Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/fraction.md Demonstrates how to create a simple fraction with custom numerator, denominator, and vinculum colors. ```python class Fraction1(Scene): def construct(self): frac = Fraction("a", "b") self.add(frac) frac.numerator.set_color(RED) frac.vinculum.set_color(LIGHT_BROWN) frac.denominator.set_color(BLUE) ``` -------------------------------- ### Create and Style a Basic Root Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/root.md Demonstrates how to create a basic root object and set the colors of its components (index, symbol, radicand). ```python class Root1(Scene): def construct(self): root = Root("x", "n") self.add(root) root.index.set_color(RED) root.symbol.set_color(LIGHT_BROWN) root.radicand.set_color(BLUE) ``` -------------------------------- ### Create and Manipulate Fraction Component Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates creating a Fraction component, setting colors for its parts, and transforming it by swapping numerator and denominator. ```python class FractionComponent(Scene): def construct(self): attach_progress_interceptors(self) fraction = Fraction("a", "b") fraction.numerator.set_color(RED) fraction.denominator.set_color(BLUE) fraction.vinculum.set_color(LIGHT_BROWN) self.add(fraction).wait(2) fraction.numerator, fraction.denominator = fraction.denominator, fraction.numerator self.play(TransformInStages.progress(fraction)) ``` -------------------------------- ### Create Function and Parentheses Components Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Shows how to create a Function component with a specified name and input, and a Parentheses component. Colors can be applied to different parts of these components. ```python class FunctionComponent(Scene): def construct(self): attach_progress_interceptors(self) function = Function(r"\text{ln}", "x") function.function_name.set_color(YELLOW) function.input.set_color(LIGHT_BROWN) function.shift(UP) parentheses = Parentheses("x") parentheses.inner.set_color(LIGHT_BROWN) self.add(function, parentheses) ``` -------------------------------- ### Create and Modify a Basic MathMatrix Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/matrix.md Demonstrates how to create a MathMatrix and modify the color of specific elements and brackets. Use this for basic matrix visualization. ```python class MathMatrix1(Scene): def construct(self): mat = MathMatrix([[ 1, 0 ], [ 0, 1 ]]) self.add(mat) mat[0][0].set_color(BLUE) mat[1][1].set_color(BLUE) mat.brackets.set_color(LIGHT_BROWN) ``` -------------------------------- ### Create and Style Integral Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/integral.md Demonstrates how to create an Integral object in Manim and style its components like limits and the function. ```python class Integral1(Scene): def construct(self): integral = Integral("f(x) \: dx", "a", "b") self.add(integral) integral.a.set_color(RED) integral.b.set_color(BLUE) integral.symbol.set_color(WHITE) integral.function.set_color(WHITE) ``` -------------------------------- ### Import Reactive Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Import Manim and Reactive Manim. Note that reactive_manim overwrites MathTex, so it must be imported after manim. ```python from manim import * from reactive_manim import * ``` -------------------------------- ### Create MathCases with CaseLine Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates using CaseLine within MathCases to define output and conditions. Colors can be applied individually to the output and condition parts of a CaseLine. ```python cases = MathCases( "a, & b", CaseLine("aaa,", "b") ) self.add(cases) cases.set_color(GREEN) cases[1].output.set_color(RED) cases[1].condition.set_color(BLUE) ``` -------------------------------- ### Create and Style a Term Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/term.md Demonstrates how to create a Term object with a base and exponent and then style its components (term and superscript) using Manim's color properties. ```python class Term1(Scene): def construct(self): term = Term("x", "n") self.add(term) term.term.set_color(BLUE) term.superscript.set_color(RED) ``` -------------------------------- ### Create a Basic Function Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/function.md Instantiate and display a simple function like sin(theta). Colors can be applied to different parts of the function object. ```python class Function1(Scene): def construct(self): func = Function("\text{sin}", "\theta") self.add(func) func.function.set_color(GREEN) func.input.set_color(LIGHT_BROWN) func.parentheses.set_color(WHITE) ``` -------------------------------- ### Fraction Equation Operation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/fraction.md Shows how to perform operations on fractions within an equation context using MathTex and TransformInStages. ```python class Fraction2(Scene): def construct(self): V, I, R = MathTex("V", "I", "R") tex = MathTex(V, "=", [ I, R ]) self.add(tex).wait(1) tex.LHS = Fraction(V, I.pop()) self.play(TransformInStages.progress(tex)) self.wait(1) tex.LHS = V tex.RHS.terms = [ I, R ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Basic Parentheses Usage Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/parentheses.md Shows how to create and add a basic Parentheses object to the scene. The input and parentheses colors can be customized. ```python class Parentheses1(Scene): def construct(self): paren = Parentheses("x") self.add(paren) paren.input.set_color(BLUE) paren.parentheses.set_color(WHITE) ``` -------------------------------- ### Apply Power Rule with Term Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/term.md Illustrates the application of the power rule for differentiation using the Term class within Manim. It shows the transformation of a mathematical expression. ```python class PowerRule(Scene): def construct(self): x = MathTex("x", color=BLUE) n = MathTex("n", color=GREEN) tex = MathTex(Term(x, n)) self.add(tex).wait(1) tex.terms = [ n, Term(x, [ n, "-", 1 ]) ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Adding Fractions Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/fraction.md Illustrates how to add two fractions together, combining their numerators and denominators using Reactive Manim's animation capabilities. ```python class FractionMerge(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) c = MathTex("c", color=GREEN) frac1 = Fraction( Term(b, 2), [ 4, Term(a, 2) ]) frac2 = Fraction([ 4, a, c ], [ 4, Term(a, 2) ]) tex = MathTex(frac1, "+", frac2) self.add(tex) frac1.numerator = MathTex(frac1.numerator, tex[1], frac2.numerator) frac1.denominator.merge(frac2.denominator) tex.terms = [ frac1 ] self.play(TransformInStages.progress(tex, lag_ratio=0.5)) ``` -------------------------------- ### Apply Quotient Property of Roots Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/root.md Illustrates the quotient property of roots by transforming a root of a fraction into a fraction of roots. ```python class RootToFrac(Scene): def construct(self): frac = Fraction("x-1", "x+1") root = Root(frac) tex = MathTex(root) self.add(tex).wait(1) root_n = Root(frac.numerator, symbol=root.symbol) root_d = Root(frac.denominator, symbol=root.symbol) tex.terms = [ Fraction(root_n, root_d, vinculum=frac.vinculum) ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Transforming a Root Expression Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/functionality-showcase.md Demonstrates the transformation of a root expression into a fraction using Manim's scene and animation capabilities. Requires importing Scene, Fraction, Root, MathTex, and TransformInStages. ```python class RootToFrac(Scene): def construct(self): frac = Fraction("x-1", "x+1") root = Root(frac) tex = MathTex(root) self.add(tex).wait(1) root_n = Root(frac.numerator, symbol=root.symbol) root_d = Root(frac.denominator, symbol=root.symbol) tex[0] = Fraction(root_n, root_d, vinculum=frac.vinculum) self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Visualize Linear Equations with MathMatrix Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/matrix.md Shows how to represent and transform linear equations using MathMatrix objects within Manim. This is useful for educational purposes to illustrate matrix multiplication. ```python class LinearEquations(Scene): def construct(self): x0, x1, y0, y1 = MathTex("x_0", "x_1", "y_0", "y_1") w00, w01, w10, w11 = MathTex("w_{00}", "w_{01}", "w_{10}", "w_{11}") tex1 = MathTex(y0, "=", w00, x0, "+", w01, x1) tex2 = MathTex(y1, "=", w10, x0, "+", w11, x1) group = DGroup(tex1, tex2).arrange(DOWN).shift(UP) self.add(group).wait(1) tex3 = MathTex( MathMatrix([ [ y0 ], [ y1 ] ]), "=", MathMatrix([ [ w00, w01 ], [ w10, w11 ] ]), MathMatrix([ [ x0 ], [ x1 ] ]) ) tex3.shift(DOWN) self.play(TransformInStages.from_copy(group, tex3[0], lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, tex3[1], lag_ratio=0.4, run_time=1.4)) self.play(TransformInStages.from_copy(group, tex3[2], lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, tex3[3] - x0, lag_ratio=0.4, run_time=2.5)) self.play(TransformInStages.from_copy(group, x0, lag_ratio=0.4, run_time=2.5)) ``` -------------------------------- ### Sequential Target-Mobjects Without Auto-Disconnect Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates `from_copy()` where target mobject components remain linked to the original. Changes to original components after transformation will affect the target. ```python class Scene1(Scene): def construct(self): x = MathString("x") y = MathString("y") tex1 = MathTex(x, y) tex1.shift(UP) self.add(tex1).wait(1) tex2 = MathTex(y.copy(), x.copy()) self.play(TransformInStages.from_copy(tex1, tex2)) x.set_color(RED) y.set_color(RED) self.wait(3) ``` -------------------------------- ### Create and Transform MathMatrix Component Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Shows the creation of a MathMatrix, its transformation by changing its internal matrix structure, and its use as a superscript. Edits to the matrix must be done via the `matrix.matrix` setter. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) root = DGroup() mat = MathMatrix([[ "x", "z" ], [ "y", "w" ]]).set_color(LIGHT_PINK) self.add(root.add(mat)).wait(1) mat.matrix = [ [ "a", mat[0][0] ], [ "b", mat[1][0] ] ] self.play(TransformInStages.progress(root, lag_ratio=0.4, run_time=1.5)) self.wait(1) root.mobjects = [ Term("e", mat) ] self.play(TransformInStages.progress(root, lag_ratio=0.4, run_time=1.5)) self.wait(1) ``` -------------------------------- ### Transforming a Quadratic Equation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/functionality-showcase.md Demonstrates the transformation of a quadratic equation using Manim's animation features. This snippet requires Scene, MathTex, Fraction, and TransformInStages. ```python class QuadraticScene(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) c = MathTex("c", color=GREEN) tex = MathTex([[ a, "x^2" ], "+", [ b, "x" ], "+", [ c ]],"=", 0) self.add(tex).wait(1) b.swap(lambda: Fraction(b, a.pop())) c.swap(lambda: Fraction(c, a)) self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Animate MathList in Python Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates animating a MathList by removing and appending elements, and using `animation.intercept()` with a custom `Transform` for specific element animations. This feature is experimental. ```python class MathListScene(Scene): def construct(self): tex = MathList("4", "7", "1", "2") self.add(tex).wait(1) tex.remove(tex[1]) self.play(TransformInStages.progress(tex, lag_ratio=0.8)) self.wait(1) tex.append("3") self.play(TransformInStages.progress(tex, lag_ratio=0.8)) self.wait(1) four = tex[0] tex.remove(four) tex.append(four) animation = TransformInStages.progress(tex, lag_ratio=0.5, track_run_time=1.5) animation.intercept(four).set_animation( lambda source, target: Transform(source, target, path_arc=(3*PI)/4) ) self.play(animation) ``` -------------------------------- ### Create and Modify MathCases Component Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Illustrates the creation of a MathCases component, adding and removing elements, and reordering lines. The `cases.lines` attribute controls the order of displayed cases. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) cases = MathCases("a", "b", "c").set_color(GREEN) self.add(cases).wait(1) cases.remove(cases[0]) cases.append("d") self.play(TransformInStages.progress(cases, lag_ratio=0.5)) cases.lines = [ cases[0], cases[2], cases[1] ] cases.shift(LEFT) self.play(TransformInStages.progress(cases, lag_ratio=0.5)) self.wait(1) ``` -------------------------------- ### Animate Quadratic Expressions with Reactive Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md This Python code snippet demonstrates how to animate mathematical expressions involving quadratic terms using Reactive Manim. It utilizes `MathTex` for creating LaTeX expressions and `TransformInStages` for animating transformations between different stages of the expression. ```python class QuadraticScene(Scene): def construct(self): a1, p1, b1 = MathTex("a", "+", "b") tex1 = MathTex(Parentheses([ a1, p1, b1 ]), Parentheses([ "a", "-", "b" ])).shift(UP) self.add(tex1).wait(1) tex1[1].set_color(BLUE) self.play(TransformInStages.progress(tex1)) paren_2 = tex1[1] tex2 = MathTex([ a1, paren_2 ], [ [ p1, b1 ], paren_2 ]) self.play(TransformInStages.from_copy(tex1, tex2[0], run_time=1.5)) self.play(TransformInStages.from_copy(tex1, tex2[1], run_time=1.5)) self.wait(1) a2, m2, b2 = tex2[0][1].inner a3, m3, b3 = tex2[1][1].inner part1 = Term(a1, "2") a2.target_id = a1.id part2 = MathTex(m2, a1.clone(), b2) part3 = MathTex(p1, a3, b1.clone()) part4 = MathTex(m3, Term(b2, "2")) b3.target_id = b2.id tex3 = MathTex(part1, [ part2, part3 ], part4).shift(DOWN).set_color(WHITE) self.play(TransformInStages.from_copy(tex2, part1, lag_ratio=0.4, track_run_time=1.5)) self.play(TransformInStages.from_copy(tex2, part2, lag_ratio=0.4, track_run_time=1.5)) self.play(TransformInStages.from_copy(tex2, part3, lag_ratio=0.4, track_run_time=1.5)) self.play(TransformInStages.from_copy(tex2, part4, lag_ratio=0.4, track_run_time=1.5)) point_mobject = tex3[1].get_point_dynamic_mobject().shift(DOWN*0.05) tex3.terms = [ tex3[0], tex3[2] ] anim = TransformInStages.progress(tex3, lag_ratio=0) anim.intercept(point_mobject).set_target(point_mobject) self.play(anim) self.wait(1) ``` -------------------------------- ### Animate Equation Variables with Intercept in Python Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Shows how to animate equation variables using MathTex and the experimental `animation.intercept()` feature. This method is still in development. ```python class EquationVariablesScene(Scene): def construct(self): y_pred = MathString("6.75") y_act = MathString("9.0") tex = MathTex(r"E = \frac{1}{2}(", "y", "-", "a", ")^2") y_pred.move_to(np.array([ -1.0, 1.25, 0 ])) y_act.move_to(np.array([ 1.0, 1.25, 0 ])) tex.move_to(np.array([ 0, -0.5, 0 ])) self.add(y_pred, y_act, tex).wait(1) tex[1] = y_pred.copy() anim = TransformInStages.progress(tex, lag_ratio=0, run_time=1.5) anim.intercept(y_pred).set_source(y_pred) self.play(anim) self.wait(1) tex[0].save_center() tex[3] = y_act.copy() tex[0].restore_center() anim = TransformInStages.progress(tex, lag_ratio=0, run_time=1.5) anim.intercept(y_act).set_source(y_act) self.play(anim) self.wait(1) ``` -------------------------------- ### Pop and Swap Mobjects Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/mobject-controls.md Use `pop()` to disconnect a Mobject from its parent and `swap()` to replace one Mobject with another in relation to its parent. These are useful for dynamic scene manipulation during animations. ```python class QuadScene(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) c = MathTex("c", color=GREEN) tex = MathTex([[ a, "x^2" ], "+", [ b, "x" ], "+", [ c ]],"=", 0) self.add(tex).wait(1) a.pop() b.swap(lambda: Fraction(b, a)) c.swap(lambda: Fraction(c, a)) self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Distributive Property with Parentheses Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/parentheses.md Demonstrates the distributive property using MathTex and Parentheses. This snippet shows how to represent and animate mathematical expressions involving parentheses. ```python class DistributiveProperty(Scene): def construct(self): a, x, m, y = MathTex("a", "x", "-", "y") tex = MathTex(a, Parentheses([ x, m, y ])) self.add(tex) m.save_y() tex.terms = [[a, x], m, [a, y]] m.restore_y() self.play(TransformInStages.progress(tex, lag_ratio=0)) ``` -------------------------------- ### Display and Customize an Evaluated Expression Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/evaluate.md This snippet shows how to instantiate and add an Evaluate object to the scene. It also demonstrates how to change the colors of the expression's components (numerator, denominator, and symbol). ```python class Evaluate1(Scene): def construct(self): eval = Evaluate("\\frac{1}{x} \\mspace{4mu} ", "1", "e") self.add(eval) eval.a.set_color(RED) eval.b.set_color(BLUE) eval.symbol.set_color(LIGHT_BROWN) ``` -------------------------------- ### DGroup Animation with TransformInStages Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Demonstrates animating a `DGroup` using `TransformInStages.progress`. `DGroup` acts as a reactive VGroup, allowing terms to be moved between equations as transformers rather than introducers/removers. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) tex1 = MathTex("a", r"\\", "b") tex2 = MathString("x + y") group = DGroup(tex1, tex2).arrange(DOWN, buff=LARGE_BUFF) self.add(group).wait(1) tex1.terms = [ tex1[0], tex1[2] ] self.play(TransformInStages.progress(group)) self.wait(1) ``` -------------------------------- ### Sequential Target-Mobjects With Auto-Disconnect Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Illustrates `from_copy()` using auto-disconnect, enabling the creation of independent target mobjects. Changes to original components after transformation do not affect the target. ```python class Scene1(Scene): def construct(self): x = MathString("x") y = MathString("y") tex1 = MathTex(x, y) tex1.shift(UP) self.add(tex1).wait(1) tex2 = MathTex(y, x) self.play(TransformInStages.from_copy(tex1, tex2)) x.set_color(RED) y.set_color(RED) self.wait(1) tex3 = MathTex(x, y) tex3.shift(DOWN) self.play(TransformInStages.from_copy(tex2, tex3)) x.set_color(BLUE) y.set_color(BLUE) self.wait(3) ``` -------------------------------- ### Animating the Fundamental Theorem of Calculus Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/functionality-showcase.md Illustrates the Fundamental Theorem of Calculus with animated mathematical expressions. This snippet requires Scene, MathTex, Integral, Function, and TransformInStages. ```python class FundementalTheorem(Scene): def construct(self): a = MathTex("0", color=GREEN) b = MathTex("\pi", color=GREEN) integral = Integral("\\cos(x) \\: dx", a, b) tex = MathTex(integral) self.add(tex).wait(1) tex.terms = [ integral, "=", [ Function("\sin", b), "-", Function("\sin", a) ] ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Term Component Animation Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Shows how to animate a `Term` component using `TransformInStages.progress`. `Term` allows for individual styling of the term, subscript, and superscript, and supports `None` for missing parts. ```python class TermComponent(Scene): def construct(self): attach_progress_interceptors(self) term = Term("a", "b", "c") term.term.set_color(RED) term.subscript.set_color(YELLOW) term.superscript.set_color(BLUE) self.add(term).wait(2) term.subscript = None term.superscript.set_tex_string("2") self.play(TransformInStages.progress(term)) self.wait(2) ``` -------------------------------- ### Log of Exponent Representation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/function.md Illustrates the representation of a logarithmic function applied to an exponent term, showing the transformation process. ```python class LogOfExponent(Scene): def construct(self): a = MathTex("a", color=RED) k = MathTex("k", color=BLUE) log = Function("\\log", Term(a, k)) tex = MathTex(log) self.add(tex) log.input = a tex.terms = [ k, log ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Transform using from_copy Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/transform-types.md Use `TransformInStages.from_copy` to animate a transformation where the target Mobject is created as a copy of the source. This is useful for showing how one object evolves into another. ```python class EquationScene1(Scene): def construct(self): x, y, _1, _3, eq, mn = MathTex("x", "y", "1", "3", "=", "-") x.set_color(BLUE) y.set_color(GREEN) tex1 = MathTex( x, eq, [ Term(y, _3), "+", _1 ]); eq.save_x() tex2 = MathTex( [ x, mn, _1 ], eq, Term(y, _3) ) tex3 = MathTex(Root([ x, mn, _1 ], _3), eq, y ) tex4 = MathTex( tex3.RHS, eq, tex3.LHS ) group = VGroup(tex1, tex2, tex3, tex4).arrange(DOWN, buff=MED_LARGE_BUFF) [ tex[1].restore_x() for tex in group ] self.add(tex1) self.play(TransformInStages.from_copy(tex1, tex2)) self.play(TransformInStages.from_copy(tex2, tex3)) self.play(TransformInStages.from_copy(tex3, tex4)) ``` -------------------------------- ### Transform using replacement_transform Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/transform-types.md Employ `TransformInStages.replacement_transform` to animate a transformation where the source Mobject is replaced by the target Mobject. This is suitable for showing a direct substitution or change. ```python class EquationScene2(Scene): def construct(self): x, y, _1, _3, eq, mn = MathTex("x", "y", "1", "3", "=", "-") x.set_color(BLUE) y.set_color(GREEN) tex1 = MathTex( x, eq, [ Term(y, _3), "+", _1 ]); eq.save_x() tex2 = MathTex( [ x, mn, _1 ], eq, Term(y, _3) ) tex3 = MathTex(Root([ x, mn, _1 ], _3), eq, y ) tex4 = MathTex( tex3.RHS, eq, tex3.LHS ) group = VGroup(tex1, tex2, tex3, tex4) [ tex[1].restore_x() for tex in group ] self.add(tex1) self.play(TransformInStages.replacement_transform(tex1, tex2)) self.play(TransformInStages.replacement_transform(tex2, tex3)) self.play(TransformInStages.replacement_transform(tex3, tex4)) ``` -------------------------------- ### Transform using progress Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/transform-types.md Utilize `TransformInStages.progress` to animate a transformation by progressively updating the current Mobject to match a new state. This is effective for showing gradual changes or steps in an equation. ```python class EquationScene3(Scene): def construct(self): x, y, _1, _3, eq, mn = MathTex("x", "y", "1", "3", "=", "-") x.set_color(BLUE) y.set_color(GREEN) tex = MathTex( x, eq, [ Term(y, _3), "+", _1 ]); eq.save_x() self.add(tex) tex.terms = [ [ x, mn, _1 ], eq, Term(y, _3) ]; eq.restore_x() self.play(TransformInStages.progress(tex)) tex.terms = [ Root([ x, mn, _1 ], _3), eq, y ]; eq.restore_x() self.play(TransformInStages.progress(tex)) tex.terms = [ tex.RHS, eq, tex.LHS ]; eq.restore_x() self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Function Equation Operation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/function.md Demonstrates transforming an equation by applying a function to both sides, showing intermediate steps of the transformation. ```python class Function2(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) tex = MathTex(a, "=", b) self.add(tex) tex.LHS = Function("\\sin", tex.LHS) tex.RHS = Function("\\sin", tex.RHS) self.play(TransformInStages.progress(tex)) tex.LHS = tex.LHS.input tex.RHS = tex.RHS.input self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Fundamental Theorem of Calculus Visualization Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/integral.md Visualizes the Fundamental Theorem of Calculus by transforming an integral expression into its evaluated form using Manim. ```python class FundementalTheorem(Scene): def construct(self): a = MathTex("0", color=GREEN) b = MathTex("\pi", color=GREEN) integral = Integral("\cos(x) \: dx", a, b) tex = MathTex(integral) self.add(tex).wait(1) tex.terms = [ integral, "=", [ Function("\sin", b), "-", Function("\sin", a) ] ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Manipulate Root Equation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/root.md Shows how to manipulate an equation involving a root term, specifically transforming the right-hand side to include a root operation. ```python class Root2(Scene): def construct(self): tex = MathTex(Term("A", 2), "=", "B") self.add(tex).wait(1) tex.RHS = MathTex("\pm", Root(tex.RHS)) tex.LHS = tex.LHS.term self.play(TransformInStages.progress(tex, lag_ratio=0.25)) ``` -------------------------------- ### Merge Mobjects for Animation Source: https://github.com/philip-murray/reactive-manim/blob/main/docs/source/tutorial/mobject-controls.md The `merge()` method directs the animation generator to use a clone of the first Mobject as the target for the second. This is useful for creating animations where one Mobject visually transforms into another. ```python class QuadraticMerge(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) paren1 = Paren([ "x", "+", Fraction(b, [ 2, a ])]) paren2 = Paren([ "x", "+", Fraction(b, [ 2, a ])]) tex = MathTex(paren1, paren2) self.add(tex) tex.terms = [ Term(paren1, 2) ] # paren2 is removed, but merges onto paren1 for remover-animation paren1.merge(paren2) self.play(TransformInStages.progress(tex, lag_ratio=0.5)) ``` ```python class FractionMerge(Scene): def construct(self): a = MathTex("a", color=RED) b = MathTex("b", color=BLUE) c = MathTex("c", color=GREEN) frac1 = Fraction( Term(b, 2), [ 4, Term(a, 2) ] ) frac2 = Fraction( [ 4, a, c ], [ 4, Term(a, 2) ] ) tex = MathTex(frac1, "+", frac2) self.add(tex) frac1.numerator = MathTex(frac1.numerator, tex[1], frac2.numerator) frac1.denominator.merge(frac2.denominator) tex.terms = [ frac1 ] self.play(TransformInStages.progress(tex, lag_ratio=0.5)) ``` -------------------------------- ### Clone Mobject in Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Use `clone()` instead of `copy()` when placing a copy of an mobject within the same graph to avoid ID conflicts. `TransformInStages` recognizes clones by matching `source_id`. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) tex = MathTex(Term("x", "n")) self.add(tex).wait(1) tex.terms = [ tex[0], tex[0].clone() ] self.play(TransformInStages.progress(tex)) ``` -------------------------------- ### Restore Mobject Position in Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Use `save_y()` and `restore_y()` to maintain the vertical position of an mobject during transformations, ensuring it only moves horizontally. By default, `restore` adjusts the root mobject's position based on the difference between the mobject's before and after positions. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) term1 = Term("2") term2 = Term("2", "4") tex = MathTex(term1, Parentheses(term2)) self.add(tex).wait(1) term2.term.save_y() term1.term.target_id = term2.term.id term2.superscript.set_tex_string("5") tex.terms = [ term2 ] term2.term.restore_y() self.play(TransformInStages.progress(tex, lag_ratio=0.4)) ``` ```python In this example, the original 2 remains at the same position during the animation. This is due to `save_y()` and`restore_y()` being called. By default, `restore` shifts the root mobject by the difference of the before and after positions of the mobject that `restore` is being called on. ``` -------------------------------- ### Pop Child Mobject in Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Use `B.pop()` as a shorthand for `A.remove(B)` when B is a child of A. This may raise a `NotImplementedError` if the parent `A` does not override the abstract `remove` method. ```python In this example, `n.pop()` executes `x.remove(n)`. Term's implementation of `remove` recognizes that n is its superscript, and sets its superscript to `None`. Then `n` is available to be placed inside `tex`. ``` -------------------------------- ### Auto-Disconnect Child Mobject in Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Auto-disconnect enables operations like `A[0] = Component(A[0])` by detaching a child from its parent without altering the parent's list size. If a parent/child relationship isn't explicitly disconnected, constructing a child component will rerender the parent with a clone of the child. ```python In this example, `x` is rerendered using `n.clone()` in place of `n`. ``` -------------------------------- ### Merge Mobjects with target_id in Manim Source: https://github.com/philip-murray/reactive-manim/blob/main/README.md Set `target_id` on a remover-mobject to enable merging onto another mobject during transformation. Both `source_id` and `target_id` are reset to `None` after the transformation. ```python class Scene1(Scene): def construct(self): attach_progress_interceptors(self) term1 = Term("2") term2 = Term("2", "4") tex = MathTex(term1, Parentheses(term2)) self.add(tex).wait(1) term1.term.target_id = term2.term.id term2.superscript.set_tex_string("5") tex.terms = [ term2 ] self.play(TransformInStages.progress(tex, lag_ratio=0.4)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.