Summing a number of matrices a number of times - algorithm

I wrote a Matlab code below which is suppose to solve a system of equations:
A(n)U(n)=U(n-1)*(1-Walpha(2))+ Walpha(2)*U(0)+SUM((Walpha(j)-Walpha(j+1))*U(n-j));
The code below is running, but, i suspect it is only returning WU(n) at the last j step, but it is suppose to calculate and save the values at each j step and then add these values together for each n step and save the result in WU.
U=cell(N,1);
RHS=cell(N,1);
WU=cell(N,1);
RHS{1}=Um0;
U{1}=U1;
WU{1}=zeros(M-1,1);
for n=2:N-1;
for j=1:n-1;
WU{n}=(Walpha(j)-Walpha(j+1))*U{n-j};
end
RHS{n}=(1-Walpha(2)).*U{n-1}+Walpha(n).*U0+ WU{n}-UU{n};
U{n}=inv(A{n})*RHS{n};
end
Can somebody please explain as to how i should re-write my codes such that the summation part in the system is evaluated correctly.

Related

Never ending 'for' loop prevents my RStudio notebook from being rendered into a .md file

I'm trying to calculate the Kolmogorov-Smirnov statistic in R. I have the following sample, which clearly comes from a random variable that follows a long-tailed distribution.
Download link
https://drive.google.com/file/d/1hIgqikX7p343zdyc-Goq34THUpsZA63n/view?usp=sharing
As you may know, the Kolmogorov-Smirnov statistic requires the calculation of the empirical cumulative distribution function and the presumed cumulative distribution function. For both calculations I take the following approach: first, I create a vector with the same length as the length of the sample, and then I modify each of the components of the vector so as for it to contain the empirical cdf (or presumed cdf) of the corresponding observation of the sample.
For the sake of illustration, I'll show you the code I wrote in order to calculate the empirical cdf.
I'm assuming that the data has been read and stored in a dataframe called data.
ecdf = vector("numeric", length(data$logueos))for (i in 1:length(data$logueos)) {ecdf[i] = sum (data$logueos <= data$logueos[i])/length(data$logueos)}
The code I wrote for the calculation of the presumed cdf is analogous to the preceding one; the only difference is that I set each component of the pcdf vector equal to the formula $P(X<=t)$ —where t is the corresponding observation of the sample— according to the distribution that I'm assuming.
The problem is that this 'for' loop never ends. If I force it to end by clicking RStudio's stop button it works: it makes the vector store what I want it to store. But, if I press Ctrl+Shift+k in order to render my notebook and preview it, the load gets stuck when trying to execute the first chunk encountered that contains one of those loops.
First of all, your loop is not endless. It will finish, eventually.
You start initializing a vector with as much elements as the number of observations (1.245.888, which is a lot of iterations). This vector is FULL OF ZEROS.
What your loop does is iterate while changing each zero with the calculus sum (data$logueos <= data$logueos[i])/length(data$logueos). Check that when you stop the execution, the first values of your vector will be values between 0 and 1 while the last values is going to be 0s (because the loop hasn't arrived there yet).
So, you will have to wait more time.
In order to make the execution faster, you could consider loop parallelization (because standard loops go sequentially, one by one, and if it's too much wait, parallelization makes it faster. For example, executing 4 by 4, depending of your computer capacities). Here you'll find some information about it: https://nceas.github.io/oss-lessons/parallel-computing-in-r/parallel-computing-in-r.html
Then, my proposal to you:
if(!require(foreach)){install.packages("foreach")}; require(foreach)
registerDoParallel(detectCores() - 1)
ecdf = vector("numeric", length(data$logueos))
foreach (i=1:length(data$logueos)) %do% {
print(i)
ecdf[i] = sum (data$logueos <= data$logueos[i])/length(data$logueos)
}
The first line will download and load foreach library, that you
need for parallelization.
detectCores() - 1 is going to use all the
processors that your computer has except one (to avoid freezing your
machine) for computing this loop. You'll see that is going to be
faster!
registerDoParallel function is what tells to foreach how many cores use.

How to save output of for loop operation in matlab

I have a matrix A which has a size of 54x100. For some specific condition I perform an operation on each row of A. I need to save the output of this for loop. I've tried the following but it did not work.
S=zeros(54,100);
for i=1:54;
Ri=A(i,:);
answer=mean(reshape(Ri,5,20),1);
S(i)=answer;
end
Firstly, judging by your question I'd recommend some basic Matlab tutorials like this or just detailed documentation like this.
To actually help you with your issue though; you can do this:
%% Make up A (since I don't know what it actually is)
n = 54; m = 100;
A = randn(n,m); % N x m matrix of random numbers
%% Loop over each row of A
S = cell(n,1);
for j = 1:n;
Rj = A(j,:); % j'th row
answer = mean(reshape(Rj,5,20),1); % some operation
S{j} = answer; % store the answer in cell S
end
The problem was that your answer was not a single number (1x1 matrix) but a vector and so you got a dimension mismatch error. Above I'm putting the answers into a cell object of size n. The result of your operation on j'th row can then be retrieved by calling S{j}.
Also:
Do not using i as an iterator since it also represents the imaginary unit.
Do not hard-code values but reference the existing ones. For example here I referenced n in the for-loop declaration as opposed to just writing for j = 1:54 because otherwise, if I got struck by a fancy to use my code for a 53x100 array it would not work anymore.
When you post your code I reccomend adding a minimal working example - a pece of code which people can just copy and paste into their Matlab (or whatever interpreter of whatever language) and run to reproduce your problem. Here you have not included anything which tells the code what A is, for example.
This is quite a good read in general and should help you in the future

Make a previously unknown number of parallel operations. In VHDL

Im working on a project for which I need to make calculations with vectors (orthogonalizing a matrix using gram schmidt method). The length of this vectors is unknown now, the program must be able to adapt to different lengths. One of such calculations is calculating a new vector (C) which is the result of adding A and B. Each element of the vectors is a number in fixed-point.
I want C(i)=A(i)+B(i). For all the elements of the vector (for i=0 to N, where N is the vector length).
I can find 2 solutions for this but both present some problems:
1- I can declare in the entity, vectors whose length changes according to a generic and then just create a for loop which goes through all the vector.
for I in 0 to N loop
C(I)<=A(I)+B(I);
end loop;
The problem with this solution is that the execution would be sequential, and therefore slow. Im not completly sure about this and I dont know how to check it but I guess that the compiler is not smart enough to notice that it can be processed in parallel. In this application speed is a key factor.
2- I can declare vectors which are as long as the maximum possible length for the actual data and fill them with zeroes. Then I could just assign:
C(0)<=A(0)+B(0);
C(1)<=A(1)+B(1);
C(2)<=A(2)+B(2);
...
C(Nmax)<=A(Nmax)+B(Nmax);
This is not an elegant solution and in this application N can be between 3 and 300 therefore it could be a complete waste and tedious to program.
3- I want to find a third solution which could be able to create a number (asigned by the generic) of combinational calculations following a template such as C(i)=A(i)+B(i). Is there any solution like this? It is actually creating a loop which would not be executed sequentially but instead all at the same time.
I know that similar stuff can be done using CUDA but this project is actually a comparison between GPUs and FPGAs, so changing the platform is not a suitable solution either.
Thank you in advance
Edit: I have tought of another unsatisfactory solution but I want to share it in case it is helpful for somebody else checking this in the future. Given that A and B have the same length, you can write them in a 1-D format, that is: A(normal)=[1001,1100,0011], A(1-D)=100111000011. The same would be done with B.
If you know before hand that the sum of any two possible numbers can be expressed with the same amount of bits, there will be no problems. So with 4 unsigned bits you should make sure that in any possible case the numbers in A or B are !>0111 (not higher than 0111). You could just write C(1-D)=A(1-D)+B(1-D) and then just asign C(0)=C(1-D)(3 downto 0), C(1)=C(1-D)(7 downto 4) etc.
If you cannot make sure that the numbers are not higher than 0111 (in the 4 bit case) it wont work.
You might be able to use the length attribute to create a loop depending on the size of your vector.
https://www.csee.umbc.edu/portal/help/VHDL/attribute.html
As mentioned in the comment to the question the loop should be unrolled as long as it is not synchronized to the clock.

How to run a method in parallel using Julia?

I was reading Parallel Computing docs of Julia, and having never done any parallel coding, I was left wanting a gentler intro. So, I thought of a (probably) simple problem that I couldn't figure out how to code in parallel Julia paradigm.
Let's say I have a matrix/dataframe df from some experiment. Its N rows are variables, and M columns are samples. I have a method pwCorr(..) that calculates pairwise correlation of rows. If I wanted an NxN matrix of all the pairwise correlations, I'd probably run a for-loop that'd iterate for N*N/2 (upper or lower triangle of the matrix) and fill in the values; however, this seems like a perfect thing to parallelize since each of the pwCorr() calls are independent of others. (Am I correct in thinking this way about what can be parallelized, and what cannot?)
To do this, I feel like I'd have to create a DArray that gets filled by a #parallel for loop. And if so, I'm not sure how this can be achieved in Julia. If that's not the right approach, I guess I don't even know where to begin.
This should work, first you need to propagate the top level variable (data) to all the workers:
for pid in workers()
remotecall(pid, x->(global data; data=x; nothing), data)
end
then perform the computation in chunks using the DArray constructor with some fancy indexing:
corrs = DArray((20,20)) do I
out=zeros(length(I[1]),length(I[2]))
for i=I[1], j=I[2]
if i<j
out[i-minimum(I[1])+1,j-minimum(I[2])+1]= 0.0
else
out[i-minimum(I[1])+1,j-minimum(I[2])+1] = cor(vec(data[i,:]), vec(data[j,:]))
end
end
out
end
In more detail, the DArray constructor takes a function which takes a tuple of index ranges and returns a chunk of the resulting matrix which corresponds to those index ranges. In the code above, I is the tuple of ranges with I[1] being the first range. You can see this more clearly with:
julia> DArray((10,10)) do I
println(I)
return zeros(length(I[1]),length(I[2]))
end
From worker 2: (1:10,1:5)
From worker 3: (1:10,6:10)
where you can see it split the array into two chunks on the second axis.
The trickiest part of the example was converting from these 'global' index ranges to local index ranges by subtracting off the minimum element and then adding back 1 for the 1 based indexing of Julia.
Hope that helps!

How to implement this iteration/convergence step by guessing a value in Matlab?

I have two parameters fL and fV, both functions of T and P. If I make a function called func(T), which takes only T as input, then how do I go about implementing this step in Matlab:
Guess P
if |(fL/fV)-1|<0.0001 % where fL and fV are both functions of T and P
then print P
else P=P*(fL/fV)
Initially it is advised to guess the P in the beginning of the algorithm. All other steps before this involve formula calculation and doesn't involve any converging, so I didn't write all those formulas. The important thing to note is even though we take only T as input for our function, the pressure is guessed in the beginning of the code and is not part of any input by the user.
Thanks!
In order to "guess" P, you can either proceed using a) an educated guess or b) a random guess. So, for example if you were dealing with pressure in the day to day surroundings, 100kPa would be a reasonable guess. A random guess would mean initializing P to a random variable generated over a meaningful domain. So in my example, it could be a random variable uniformly distributed between 90kPa and 110kPa. Which of these approaches you choose depends on your specific problem.
You can code your requirements as follows
minP=90;maxP=110;
P=minP+(maxP-minP)*rand;%# a random guess between 90 & 100
<some code here where you calculate fL and fV
if abs(fL/fV-1)<0.0001
fprintf('%f',P)
else
P=P*fL/fV;
end

Resources