Replies: 4 comments 2 replies
-
I am not quite sure what you mean by multi-rule function. Maybe you mean a Multivariable function? |
Beta Was this translation helpful? Give feedback.
-
We might be able to add something like a p1 = PiecewiseFunction((lambda x: x, (0, 1, 0.1)),
(lambda x: 1, (1, 2, 0.1))) This could be a wrapper around FunctionGraph which adds the fancy dots at the end of the intervals and so on. 🤷🏼 Maybe also such that you can change the functions individually? p1[0].set_color(RED)
p1[1].set_color(BLUE) 🤔 maybe someone else has some thoughts on this |
Beta Was this translation helpful? Give feedback.
-
It would be possible to come up with a similar syntax as Numpy's piecewise functions, see https://numpy.org/doc/stable/reference/generated/numpy.piecewise.html -- but ultimately, you can also just write a proper Python function that does the case distinction. The only other thing to keep in mind is to explicitly declare discontinuities, if there are any. Example: class PiecewiseExample(Scene):
def construct(self):
ax = Axes()
def piecewise(x):
if x < -1: return -x
if x <= 1: return x**2
# otherwise x > 1
return 2
graph = ax.plot(piecewise, color=RED, discontinuities=[1])
self.add(ax, graph) |
Beta Was this translation helpful? Give feedback.
-
This is not good code in any way but kind of implements what i wrote before with the addition of using from typing import Callable
from manim import *
import numpy as np
class PiecewiseFunction(VGroup):
def __init__(self, ax: Axes, f_pairs: list[(Callable, list[float])], **kwargs):
super().__init__(**kwargs)
for f, r in f_pairs:
missing_start = r[0] is None
missing_end = r[1] is None
missing_samples = len(r) == 2 or r[2] is None
x_range = ax.get_x_axis().x_range
r = np.where((missing_start, missing_end, missing_samples), x_range, r)
graph: ParametricFunction = ax.plot(f, r)
start_point = ax.input_to_graph_point(r[0], graph)
end_point = ax.input_to_graph_point(r[1], graph)
self.add(graph)
if not missing_start:
graph.add(Dot(start_point, stroke_width=5, fill_color=BLACK))
if not missing_end:
graph.add(
Dot(
end_point,
stroke_width=5,
)
)
class Test(Scene):
def construct(self):
ax = Axes()
p1 = PiecewiseFunction(
ax,
[
(lambda x: -x, (None, -1, 0.1)),
(lambda x: x**2, (-1, 1, 0.1)),
(lambda x: 2, (1, 2, 0.1)),
],
)
self.add(ax)
self.add(p1) |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm new to Manim. I can't figure out how to plot multi-rule function. I checked the documentation and the example gallery, but found nothing.
Thanks, please help!
Beta Was this translation helpful? Give feedback.
All reactions