Counting on NumberLine - manim

I've been searching everywhere and I can't seem to find how one counts (with "bouncing curved arrows") on a NumberLine. How to do so?
This is my desired output:
I'm still quite new to manim. Thank you in advance.

You can use CurvedArrow for that.
Example :
class CountingNumberLine(Scene):
def construct(self):
n = NumberLine(x_range=[-5, 5, 1], include_numbers=True)
self.add(n)
for i in range(3):
self.add(CurvedArrow(n.number_to_point(i), n.number_to_point(i + 1), angle=- TAU / 4))
Result
Note that if you want to have only an arrow for the last count (like in your image) you can use ArcBetweenPoint instead of CurvedArrow.

Related

How do I apply multiple linear transformations?

I'm trying to use LinearTransformationScene's apply_matrix multiple times:
from manim import *
class LT(LinearTransformationScene):
def __init__(self):
super().__init__(
self,
show_coordinates=True,
leave_ghost_vectors=True,
)
def construct(self):
P = [[1, 1], [1, -1]];
D = [[2, 0], [0, 0.5]];
P_inv = [[0.5, 0.5], [0.5, 0.5]];
self.apply_matrix(P);
self.wait();
self.apply_matrix(D);
self.wait();
self.apply_matrix(P_inv);
self.wait();
But I get this error: submobjects must be of type VMobject.
I'm hoping to create an animation that:
Applies the matrix P
Pauses briefly
Applies another matrix D
Pauses briefly again
And finally, applies the inverse of P, P_inv.
How do I accomplish this? There were similar questions posted, but no one posted about this specific error.
These specialized Scene classes are unfortunately not very well maintained, this is a known issue. There is a simple workaround though: after calling self.apply_matrix, add
self.moving_mobjects = []
and the next application of apply_matrix will work as intended again.

Maxima numerical integration syntax

I'm trying to obtain a numerical solution to the following integral:
1
The correct answer is -0.324 + 0.382i but as seen below I am not getting a numerical answer and would appreciate help with the Maxima syntax.
2
Perhaps related to why I am not getting a numerical output are two specific questions:
I read that e and i in Maxima need to be preceded by % in input but should these also appear as %e and %i as seen in the Maxima output?
Why is dy missing at the end of the integral in the Maxima output?
Thank you!
Looks to me like your input is okay, however, the function to compute approximations to integrals is named quad_qags. (There are actually several related functions. See ?? quad_ for more info.) Also, a wrinkle here is that the integrand is a complex-valued function (of a real variable), and quad_qags can only work on real-valued integrands, so we'll have to work around it. Here's how I would arrange it.
myintegrand: exp(%i*(1 + %i*y))/(1 + %i*y + 1/(1 + %i*y));
result_realpart: quad_qags (realpart (myintegrand), y, 0, 6);
result_imagpart: quad_qags (imagpart (myintegrand), y, 0, 6);
result: result_realpart[1] + %i*result_imagpart[1];
I get 0.3243496676292901*%i + 0.3820529930785175 as the final result. That's a little different from what you said; maybe a minus sign went missing? or there's a missing or extra factor of %i?
A quick approximation
0.1 * lsum (x, x, float (rectform (makelist (ev (myintegrand, y = k/10), k, 0, 60))));
seems to show the result from quad_qags is reasonable.

Using quantile in Flux (Julia) in loss function

I am trying to use quantile in a loss function to train! (for some robustness, like least trimmed squares), but it mutates the array and Zygote throws an error Mutating arrays is not supported, coming from sort! . Below is a simple example (the content does not make sense of course):
using Flux, StatsBase
xdata = randn(2, 100)
ydata = randn(100)
model = Chain(Dense(2,10), Dense(10, 1))
function trimmedLoss(x,y; trimFrac=0.f05)
yhat = model(x)
absRes = abs.(yhat .- y) |> vec
trimVal = quantile(absRes, 1.f0-trimFrac)
s = sum(ifelse.(absRes .> trimVal, 0.f0 , absRes ))/(length(absRes)*(1.f0-trimFrac))
#s = sum(absRes)/length(absRes) # using this and commenting out the two above works (no surprise)
end
println(trimmedLoss(xdata, ydata)) #works ok
Flux.train!(trimmedLoss, params(model), zip([xdata], [ydata]), ADAM())
println(trimmedLoss(xdata, ydata)) #changed loss?
This is all in Flux 0.10 with Julia 1.2
Thanks in advance for any hints or workaround!
Ideally, we'd define a custom adjoint for quantile so that this works out of the box. (Feel free to open an issue to remind us to do this.)
In the mean time there's a quick workaround. It's actually the sorting that causes trouble here so if you do quantile(xs, p, sorted=true) it'll work. Obviously this requires xs to be sorted to get correct results, so you might need to use quantile(sort(xs), ...).
Depending on your Zygote version you might also need an adjoint for sort. That one's pretty easy:
julia> using Zygote: #adjoint
julia> #adjoint function sort(x)
p = sortperm(x)
x[p], x̄ -> (x̄[invperm(p)],)
end
julia> gradient(x -> quantile(sort(x), 0.5, sorted=true), [1, 2, 3, 3])
([0.0, 0.5, 0.5, 0.0],)
We'll make that built-in in the next Zygote release, but for now if you add that to your script it'll get your code working.

Torch Tutorial: meaning of "trainData.data[{ {},i,{},{} }]:mean()" in 1_data.lua

In the torch tutorial, I found the line:
mean[i] = trainData.data[{ {},i,{},{} }]:mean()
Is there anyone who can explain what the indexing { {},i,{},{} } is doing?
I could guess, but wanted to know the exact mechanism.
Thanks in advance.
This is actually a concise syntax for tensor narrowing / slicing, detailed here in the documentation.
Inside the [{ ... }], you can for each dimension of a tensor:
pass a number n to only keep the n-th component along this dimension,
pass a range {start,end} to keep all the components from start to end along this dimension,
pass {} to keep all the components along this dimension.
In this precise case, it's a narrowing from a u * v * w * x tensor to a u * 1 * w * x tensor by keeping only the i-th component along the 2nd dimension.

More compact Solution in Maxima

I have the following code:
for n:1 thru 11 do for j:1 thru 21 do v[n,j]:1/sqrt(dp)*
(sum(eigenfunctionsort[n,j]*exp(%i*2*%pi*m*x/dp),m,-10,10));
Where eigenfunctionsort is defined earlier,x is a variable I will integrate over later and I am summing over m.
When I print say v[1,1], I get a big long nasty equation. How can i have Maxima boil this down in to something meanigful so I can check my results.
Best,
Ben
Try the 'trigsimp' function or maybe map(trigsimp, your_expression). Not sure it will help, but it's worth a try. Also look at 'demoivre'.
I don't know what your vector eigenfunctionsort or your "big long nasty equation" looks like, but I often get complex eigenvalues and eigenvectors from Maxima even when I know they should be simple and real.
For example,
(%i1) A : matrix([1, 4, 1], [4, 1, 9], [1, 9, 1]);
(%i2) eigenvalues(A);
makes a mess. It can be simplified by applying rectform to transform the output to Cartesian form, followed by trigreduce to reduce the imaginary part of the result. Finally you can convert the
result into floating point:
(%i3) rectform(%)$
(%i4) trigreduce(%)$
(%i5) float(%);

Resources