-
Notifications
You must be signed in to change notification settings - Fork 9
/
cg.py
219 lines (180 loc) · 6.11 KB
/
cg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from re import X
from manimlib import *
def quadratic(u, v, c2p = None):
x = np.array([[u], [v]])
Q = np.array([
[0.5, 0],
[0, 0.5]
])
triple = [u, v, x.T @ Q @ x]
return triple
def c2p_quadratic(axe : ThreeDAxes):
def func(u, v):
return axe.c2p(*quadratic(u, v))
return func
class CG1(Scene):
CONFIG = {
"camera_class" : ThreeDCamera
}
def construct(self):
frame : CameraFrame = self.camera.frame
frame.set_euler_angles(
theta=70 * DEGREES,
phi=30 * DEGREES,
)
self.axes = ThreeDAxes(
x_range=(-5, 5),
y_range=(-5, 5),
z_range=(-4, 4),
height=6,
width=6,
depth=6
)
init_points = np.array([2,2,0])
title = TexText("$f(x)=x^TQx, x\in\mathbb R^2$(GD as optimizer)", font_size=30)
title.fix_in_frame()
title.move_to(UP * 3.2)
surface = ParametricSurface(
u_range=(-2.5, 2.5),
v_range=(-2.5, 2.5),
uv_func=c2p_quadratic(self.axes),
color=BLUE_C,
opacity=0.5
)
dot_on_plane = Sphere(color=RED, radius=DEFAULT_DOT_RADIUS)
dot_on_surface = Sphere(color=YELLOW, radius=DEFAULT_DOT_RADIUS)
dot_on_plane.move_to(self.c2p(init_points))
dot_on_surface.move_to(self.c2p(quadratic(init_points[0], init_points[1])))
# use update to bind
t = ValueTracker(value=2)
dot_on_plane.add_updater(self.plane_update(t))
dot_on_surface.add_updater(self.surface_update(t))
# create tracepath
trace_on_plane = TracedPath(
traced_point_func=dot_on_plane.get_center,
stroke_color=RED,
stroke_width=2
)
trace_on_surface = TracedPath(
traced_point_func=dot_on_surface.get_center,
stroke_color=YELLOW,
stroke_width=2
)
for mob in self.get_mobjects():
mob.scale(1.5)
self.play(ShowCreation(trace_on_plane), ShowCreation(trace_on_surface))
self.play(Write(self.axes))
self.play(FadeIn(surface))
self.play(ShowCreation(dot_on_surface), ShowCreation(dot_on_plane))
self.play(Write(title))
self.play(t.animate.set_value(0), run_time=3)
def c2p(self, point):
if isinstance(point, list):
return self.axes.c2p(*point)
elif isinstance(point, np.ndarray):
return self.axes.c2p(*point.tolist())
def plane_update(self, t : ValueTracker):
def func(dot : Sphere):
y = x = t.get_value()
z = 0
dot.move_to(self.c2p([x, y, z]))
return dot
return func
def surface_update(self, t : ValueTracker):
def func(dot : Sphere):
y = x = t.get_value()
z = quadratic(x, y)[-1]
dot.move_to(self.c2p([x, y, z]))
return dot
return func
class CG2(Scene):
CONFIG = {
"camera_class" : ThreeDCamera
}
def construct(self):
frame : CameraFrame = self.camera.frame
frame.set_euler_angles(
theta=70 * DEGREES,
phi=30 * DEGREES,
)
self.axes = ThreeDAxes(
x_range=(-5, 5),
y_range=(-5, 5),
z_range=(-4, 4),
height=6,
width=6,
depth=6
)
init_points = np.array([2,2,0])
title = TexText("$f(x)=x^TQx, x\in\mathbb R^2$(CG as optimizer)", font_size=30)
tex1 = Tex("(2,2,f(2,2))")
title.fix_in_frame()
title.move_to(UP * 3.2)
surface = ParametricSurface(
u_range=(-2.5, 2.5),
v_range=(-2.5, 2.5),
uv_func=c2p_quadratic(self.axes),
color=BLUE_C,
opacity=0.5
)
dot_on_plane = Sphere(color=RED, radius=DEFAULT_DOT_RADIUS)
dot_on_surface = Sphere(color=YELLOW, radius=DEFAULT_DOT_RADIUS)
dot_on_plane.move_to(self.c2p(init_points))
dot_on_surface.move_to(self.c2p(quadratic(init_points[0], init_points[1])))
# use update to bind
t = ValueTracker(value=2)
dot_on_plane.add_updater(self.plane_update(t))
dot_on_surface.add_updater(self.surface_update(t))
# create tracepath
trace_on_plane = TracedPath(
traced_point_func=dot_on_plane.get_center,
stroke_color=RED,
stroke_width=2
)
trace_on_surface = TracedPath(
traced_point_func=dot_on_surface.get_center,
stroke_color=YELLOW,
stroke_width=2
)
for mob in self.get_mobjects():
mob.scale(1.5)
self.play(ShowCreation(trace_on_plane), ShowCreation(trace_on_surface))
self.play(Write(self.axes))
self.play(FadeIn(surface))
self.play(ShowCreation(dot_on_surface), ShowCreation(dot_on_plane))
self.play(Write(title))
self.play(t.animate.set_value(0), run_time=3)
def c2p(self, point):
if isinstance(point, list):
return self.axes.c2p(*point)
elif isinstance(point, np.ndarray):
return self.axes.c2p(*point.tolist())
def plane_update(self, t : ValueTracker):
def func(dot : Sphere):
tv = t.get_value()
if tv >= 1:
x = 2 * (tv - 1)
y = 2
else:
x = 0
y = 2 * tv
z = 0
dot.move_to(self.c2p([x, y, z]))
return dot
return func
def surface_update(self, t : ValueTracker):
def func(dot : Sphere):
tv = t.get_value()
if tv >= 1:
x = 2 * (tv - 1)
y = 2
else:
x = 0
y = 2 * tv
z = quadratic(x, y)[-1]
dot.move_to(self.c2p([x, y, z]))
return dot
return func
if __name__ == "__main__":
from os import system
system("manimgl {} CG2 -c black -w --hd".format(__file__))