In Maxima 12.04.0 I have a sum
mysum : sum(u[i]^2, i, 1, N);
now I differentiate it
diff(mysum, u[i]);
now I specify a defined index i=A to differentiate it at
at(%, i=A);
Unfortunately maxima won't replace the u[i] in the sum that way.
How can I bring maxima to a result like
2*u[A]
After you differentiate, push the 2 into the sum, pick out the i-th term, and then substitute i=A:
(%i1) mysum : sum(u[i]^2, i, 1, N);
diff(mysum, u[i]);
intosum(%);
part(%, 1);
%, i=A;
Related
I'd like to plot the following piecewise function with Fourier series in wxMaxima:
for given values of constants.
Here's my current input in wxMaxima:
a_1(t):=A_0+sum(A_n*cos(n*ω*(t-t_0))+B_n*sin(n*ω*(t-t_0)), n, 1, N);
a_2(t):=A_0;
a(t):=if(is(t>=t_0)) then a_1(t) else a_2(t);
N=2$
ω=31.416$
t_0=-0.1614$
A_0=0$
A_1=0.227$
B_1=0$
A_2=0.413$
B_2=0$
plot2d([a(t)], [t,0,0.5])$
Unfortunately, it doesn't work. I get the expression evaluates to non-numeric value everywhere in plotting range error. What can I do to make it work? Is it possible to plot this function in wxMaxima?
UPDATE: It works with modifications suggested by Robert Dodier:
a_1(t):=A[0]+sum(A[n]*cos(n*ω*(t-t_0))+B[n]*sin(n*ω*(t-t_0)), n, 1, N);
a_2(t):=A[0];
a(t):=if t>=t_0 then a_1(t) else a_2(t);
N:2$
ω:31.416$
t_0:-0.1614$
A[0]:0$
A[1]:0.227$
B[1]:0$
A[2]:0.413$
B[2]:0$
wxplot2d([a(t)], [t,0,0.5], [ylabel,"a"])$
I need to count the number of times a certain element appear in a tensor in a differentiable way.
I have a tensor
a = torch.arange(10, dtype = float, requires_grad=True)
print(a)
>>>tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=torch.float64,
requires_grad=True)
Say I'm trying to count the number of times the element 5.0 appear. I found this SO question that is exactly the same, but the accepted answer is non differentiable:
(a == 5).sum()
>>>tensor(1)
(a == 5).sum().requires_grad
>>>False
My goal is to have a loss that enforces the element to appear N times:
loss = N - (a == 5).sum()
What you probably care about is differentiability wrt parameters, so your vector [1,2,3,4,5] is actually an output of f(x | theta). Sicne you casted everything onto integers, this will never create a meaningful gradient for theta, you have two paths:
Change your output, so that you do not output numbers, but rather distributions over number sequences, so instead of having a vector of integers, output a matrix of probabilities, N x K, where K is the maximum number and N number of integers, and an entry p_nk is a probability of nth number to be equal to k. Then, you can just write a nice smooth loss that will take expected number of each digit, lets call it Z (which is of length K) and then we can do
loss(P, Z) := - SUM_k [ || Z_k - [ SUM_n P_nk ] || ]
Treat the whole setup as RL problem, and then you do not need a "differentiable" loss. Just use a difference between expected occurences, and actual occurences as negative reward
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.
Given an array A consisting of N elements and Q queries of type [l,r]. Print the sum of all possible subsets in the range [l,r].
Example: A[]= { 1, 2, 3 } & l= 1, r= 3;
Sol: print {1}, {2}, {3}, {1+2}, {1+3}, {2+3}, {1+2+3}
P.S I am using Segment trees with bit manipulation to find the sum of all possible subsets but it will give tle. Is there any optimized solution?
What am I doing wrong in this code?
atvalue(y(x),[x=0],1)$
desolve(diff(y(x),x)=y(x),y(x));
plot2d(y(x),[x,-6,6]);
Output:
plot2d: expression evaluates to non-numeric value everywhere in plotting range.
plot2d: nothing to plot
false
I want to plot y(x) which is obtained from a differential equation.
In Maxima y(x) = ... is an equation, and y(x) := ... is a function, and those two things are different. Try this:
atvalue (y(x), [x=0], 1)$
desolve (diff(y(x),x)=y(x), y(x));
define (y(x), rhs(%));
plot2d (y(x), [x, -6, 6]);
Here define(y(x), ...) is a different way to define a function. define evaluates the function body rhs(%) to yield exp(x) but := quotes it (not what you want).
The reason is that the result you see after the desolve does not mean y is defined as a function of x; in fact you obtain the same error if you change y(x) with f(x) (or any other unknown function) in plot2d. See the difference:
(%i9) atvalue(y(x),[x=0],1)$
(%i10) desolve(diff(y(x),x)=y(x),y(x));
x
(%o10) y(x) = %e
(%i11) y(x);
(%o11) y(x)
(%i12) y(x):=%e^x;
x
(%o12) y(x) := %e
(%i13) y(x);
x
(%o13) %e
I don't know if there's a way to “transform” the equation (the result) into a function definition automatically. If I find a way, I will complete the answer.