1D fft on 2nd dim on Armadillo complex matrix - fft

I need to compute the 1D fft of a cx_mat in it's second dimension. So say I have this matrix:
cx_mat A(randu(5,10),randu(5,10));
The MATLAB version would look like:
A_fft=fft(A,[],2);
How would I go about this in Armadillo?
I'm trying to keep this as fast as possible so I figure a for loop running through the columns would not be the best option.
I then saw the .each_col attribute and tried
cx_mat A_fft=A.each_col([](vec& a){fft(a);});
But that would not compile. Maybe that is correct but my syntax is wrong
Any help would be greatly appreciated.

You need to have a complex vector as argument in the lambda function:
cx_mat A_fft=A.each_col([](cx_vec& a){fft(a);});

Related

GNU octave graph

I have plotted a simple velocity against time graph about vibration on GNU Octave and I would like to know the exact values when the time is 1 and 10s. I'm not very sure on this, please help me on this.
As long as your time array t contains those exact values, and assuming your velocity array is v, you could use
v(find(sum(t==[1 10].')))
and put as many values into that bracketed vector as you need.
If t doesn't have those exact values, like t = [0 0.9 10.1], check out the intelligent use of the unique function at this page
https://www.mathworks.com/matlabcentral/answers/375710-find-nearest-value-to-specific-number

An out of bounds index error when using Pytorch gather

I have Two Tensors
I am trying to gather one from each row with the column being specified by these indices. So I am trying to get:
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1]
This is my code for this:
self.manDistMat.gather(1, state.unsqueeze(-1)))
self.manDistMat
being the 16x16 matrix and state.unsqueeze(-1) being the other matrix.
When I try this I get this error.
RuntimeError: index 578437695752307201 is out of bounds for dimension 1 with size 16
What am I doing wrong?
I actually figured out it was cause I was indexing with a uint8 tensor. When I switched it to with .long() it worked. Can anyone explain why it has to be a long tensor?
I encountered the similar problem. It appears to be a bug in pytorch.

Octave: How to solve a one order differential equation with variable coefficients

I'm trying to solve a one order differential equation with variable coefficients of the following form:
xdot(1)=a(t)*x(1)+b;
where b=a constant and where a(t) = a time dependent function. I know that I can solve this equation by hand butt a(t) is a quite complex function.
So, my problem is the following. a(t) is a function which I know its values from an experiment (I've got all the results in a file) --> a(t) is a vector (n x 1) which is a problem because x(1)= xdot(1)=a scalar. So, how could I solve this equation, with lsode ?
Possibly I have underestimated your problem, but the way I read it, you are asking to integrate a first order ODE. In general there are two ways to proceed,, implicit methods and explicit methods. Here is the crudest, but easiest to understand, method I can come up with:
nt=101;a=-ones(1,nt);b=1/2;x=NaN*ones(1,nt);x(1)=pi;dt=0.01;
for kt=2:nt
dxdt=a(kt-1)*x(kt-1)+b;
x(kt)=x(kt-1)+dxdt*dt;
endfor
plot(x)
I have assumed a<0 so there is no tendency to blow up. You will want to set it equal to your observed values.

matlab function which is a function of an intergral

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).

Finding the Maximum

How to find the following Maximum or supremum by computer software such as Mathematica and Matlab: $\sup\frac{(1+s)^{4}+(s+t)^{4}+t^{4}}{1+s^{4}+t^{4}}$?
Instead of numerical approximation, what is the accurate maximum?
Thanks.
Since the question seems a bit like homework, here's an answer that starts a bit like a lecture:
ask yourself what happens to the function as s and t go to small and to large positive and negative values; this will help you to identify the range of values you should be examining; both Mathematica and Matlab can help your figure this out;
draw the graph of your function over the range of values of interest, develop a feel for its shape and try to figure out where it has maxima; for this the Mathematic Plot3D[] function and the Matlab plot() function will both be useful;
since this is a function of 2 variables, you should think about plotting some of its sections, ie hold s (or t) constant, and make a 2D plot of the section function; again, develop some understanding of how the function behaves;
now you should be able to do some kind of search of the s,t values around the maxima of the function and get an acceptably accurate result.
If this is too difficult then you could use the Mathematica function NMaximize[]. I don't think that Matlab has the same functionality for symbolic functions built-in and you'll have to do the computations numerically but the function findmax will help.
In Matlab, one would create a vector/matrix with s and t values, and a corresponding vector with the function values. Then you can pinpoint the maximum using the function max
In Mathematica, use FindMaximum like this:
f[s_,t_]:= ((1+s)^4 + (s+t)^4 + t^4)/(1+s^4+t^4)
FindMaximum[ f[s,t],{s,0},{t,0} ]
This searches for a maximum starting from (s,t)=(0,0).
For more info, see http://reference.wolfram.com/mathematica/ref/FindMaximum.html