Error when using CVX package with large sparse matrix - sparse-matrix

The error description is as follows:
Error using full request 68813x68813 (35.3GB) array to exceed the preset maximum array size Creating an array larger than this limit can take a long time and result in no response from MATLAB for more information, see Array Size Limits or Default Items panel.
Error schurmat_sblk (line 35)
if issparse(schur); schur = full(schur); end;
The function file schurmat_sblk is a file in cvx\sdpt3\Solver,
How can I do to avoid this error?
My cvx codes are as follows:
The value you may need are: n=8; d=2^n;m=(d^2)*0.05;the size of Pauli is m*d^2, it's a sparse matrix. The size of y is m*1;
function [rhoE] = test_compressed_cc(n,~,m,Pauli,y)
d = 2^n;
cvx_begin sdp quiet
% how to define the variable ?
variable rhoE(d,d) hermitian;
rhoE == hermitian_semidefinite(d);
% ||x||_tr=tr(sqrt(x^\daggerx))=tr(sqrt(x^2))=Tr(x)
minimize(trace(rhoE));
subject to
(d/m)*(Pauli * vec(rhoE)) == y;
rhoE >= 0;
cvx_end
end
On the other hand, maybe CVX can't solve the 8 qubit case, does anyone know how SVT should be used to solve this convex program.
Paper link: https://arxiv.org/pdf/0909.3304.pdf
Welcome any comment : )

Related

Error in Matlab Coder: Index exceeds array dimensions

I am trying to convert .m script to C++ using MATLAB Coder.
function P=r_p(1,var1,var3)
p=[[3,7]
[10,15]
[6,19]
[21,19]
[43,11]
[969,2]
[113,9]
[43,59]
[21,15]
[6,15]
[10,18]
[3,15]];
tmax=sum(p(:,1))+41;
coder.varsize('x');
x=ones(9,11).*[0:10:100]; % getting error in this line: [9x11]~=[1x11]. Since size of x is varying in for loop, so i should tell coder that it is variable size, So I used Varsize
for t=11:tmax
a1=(rand-0.5)*1;
a9=(rand-0.5)*1.25;
a2=(rand-0.5)*1.5;
a8=(rand-0.5)*1.75;
a3=(rand-0.5)*2.0;
a7=(rand-0.5)*2.25;
a4=(rand-0.5)*2.5;
a6=(rand-0.5)*2.75;
a5=(rand-0.5)*3;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1) % loop 1: x(1,11)+a1 value is is writing to x(1,12) So coder gives error "Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x".
x(1,t+1)=x(1,t); % In matlab it works fine, but coder throws error.
end
end
My question is Let say loop 1,
x(1,12)= x(1,11)+a1 In matlab this assignment works fine, but when converting it is throwing error " Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x" As I declared x as variable size coder should assign x(1,11)+a1 value to x(1,12) but it is not doing, instead throwing error. Why?
Since t is looping for 1289, if I specify bounds for x like
coder.varsize('x',[1290,1290],[0,0]) then Coder gives error in other part of the code i.e dimensions doesn't match. Ofcourse it should because dimension of x doesn't match with [ones(12,9)p(1,2)/9;(P_1s+var3/100P_1s.*randn(size(P_1s))/2)/9;zeros(30,9)].
is declaring x as variable size is correct step or not? if yes then what should be the work around for "index exceeds array dimensions error"
Please Let me know, what am I missing to convert it to C++ code
MATLAB Coder doesn't support 2 things you're using here: implicit expansion and growing arrays by assigning past the end of a dimension.
For implicit expansion, you can use:
x=bsxfun(#times,ones(9,11),[0:10:100]);
Assigning past the end of an array in MATLAB will grow the array. That's an error in Coder. There are 2 ways to overcome this:
Allocate your array to have the right number of elements up front
Use concatenation to grow an array: x = [x, newColumn]
In this example, you know tmax so I'd suggest just changing the allocation of x to have the right number of columns up front:
% Current initial value
x=bsxfun(#times,ones(9,11),[0:10:100]);
% Extra columns - please check my upper bound value
x=[x, zeros(9,tmax)];

Calculating sum of array elements and reiterate for entire array in MATLAB

I have a vector A of size 7812x1 and would like to calculate the sum of fixed windows of length 21 (so 372 blocks). This should be reiterated, so that the output should return a vector of size 372x1.
I have t=7812, p=372, w=21;
for t=1:p
out = sum(A((t*w-w+1):(t*w)));
end
This code, however, does not work. My idea is that the part ((t*w-w+1):(t*w)) allows for something like a rolling window. The window is of length 21, so there is not really a need to express is with variables, yet I think it keeps some flexibility.
I've seen potentially related questions (such a partial sum of a vector), yet I'm not sure whether this would result the output desired.
Reshape into a matrix so that each block of A is a column, and compute the sum of each colum:
result = sum(reshape(A, w, []), 1);
Following your idea of using a rolling/moving window (requires Matlab 2016a or later):
t = 7812; w = 21; % your parameters
A = rand(t,1); % generate some test data
B = movsum(A,w); % the sum of a moving window with width w
out = B(ceil(w/2):w:end); % get every w'th element

MATLAB to C-code

I am following MathWorks guide to converting MATLAB code to C-code.
The first step is to enter
%#codegen
after every function that I want converted to C-code, however doing so has given me the following prompt on the code below.
function lanes=find_lanes(B,h, stats)
% Find the regions that look like lanes
%#codegen
lanes = {};
l=0;
for k = 1:length(B)
metric = stats(k).MajorAxisLength/stats(k).MinorAxisLength;
%testlane(k);
%end
%function testlane(k)
coder.inline('never');
if metric > 5 & all(B{k}(:,1)>100)
l=l+1;
lanes(l,:)=B(k);
else
delete(h(k))
end
end
end
around the curly braces:
code generation only supports cell operations for "varargin" and
"varargout"
Another prompt says
Code generation does not support variable "lanes" size growth through indexing
where lanes is mentioned for the second time.
The input Arguments for the function are:
B - Is the output of the bwboundaries Image Processing toolbox function. It is a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.
h - plots the boundaries of the objects with a green outline while being a matrix of size 1 X length(B), holding the values of the boundaries like so like so:
h(K)=plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);//boundary(:,1) - Y coordinate, boundary(:,2) - X coordinate.
stats - 19x1 struct array acquired using the regionprops function from the Image Processing toolbox with fields:
MajorAxisLength and
MinorAxisLength (of the object)
I would really appreciate any input you can give in helping me clear this error. Thanks in Advance!
Few points about your code generation -
Only a subset of functions in MATLAB and Image Processing Toolbox support code generation - Image Processing Toolbox support for code generation.
Cell arrays do not support code generation yet - Cell array support.
In your code, it seems like your variable is growing i.e. the initial size of the array is not able to support your workflow. You should follow code generation for variable sized inputs.
I had a similar error i.e. code generation does not support variable size growth through indexing. Inside my for loop I had a statement as such which had the same error:
y(i) = k;
I introduced a temporary storage variable u and modified my code to:
u = y;
u(i) = k;
y = u;
I suggest you do the same for your variable lanes.

Why is mxCreateNumericMatrix maximum size smaller than system maximum array size?

I am trying to create a matrix in a MEX function. The following works:
uint64_t N;
N = 2147483647; // N = 2*2^30 -1
plhs[0] = mxCreateNumericMatrix(N,1,mxUINT8_CLASS,mxREAL);
However, I am unable to create an array that is this size:
uint64_t N;
N = 2147483648; // N = 2*2^30
plhs[0] = mxCreateNumericMatrix(N,1,mxUINT8_CLASS,mxREAL);
The preceding code throws the error:
maximum variable size allowed by the function exceeded
Which is confusing since my system (64-bit Linux running 64-bit Matlab 2010b) tells me the maximum array size is, in fact, very large.
[~,M] = computer
M =
281474976710655 % 2^48 -1 for those of you keeping track
Furthermore, from the command line, I am able to create very large arrays, and have been quite happily for some time, with calls like the following:
a = zeros(16*2^30,1,'uint8');
disp(uint64(numel(a)))
17179869184
My question is, why am I not able to create arrays in my mex function that I am clearly able to create from the command line, or from other *.m functions?
Thank you.
P.S. - I have also asked this question in the Mathworks forum. I figured I'd cast as large a net as possible. If it is answered there first, I'll post it here.
The answer lies in the compiler options. By default, Matlab limits the size to 2^31-1. To increase the size, the following option must be included in your mex compile command.
mex -largeArrayDims myFunction.c

How do I make a plot in Matlab if I do not know the specific size of the array?

I have some code
function runTubulin()
n = 10;
for j = 1:n
TubulinModel();
end
plot(TubulinModel(), n);
So my problem is that TubulinModel has a random number of outputs So I keep getting
??? Error using ==> TubulinModel Too
many output arguments.
Error in ==> runTubulin at 11
plot(TubulinModel(), n);
Is there A way to plot the data when I do not know the size of the array?
The error you are getting (Too many output arguments) implies that the function TubulinModel doesn't actually return any outputs. The function TubulinModel is expected to pass at least one output argument for the PLOT command to use, which it doesn't appear to be doing. You can check this by trying the following:
a = TubulinModel(); %# Place the output in a variable instead
If this gives you an error, then it means you will have to modify TubulinModel so that it returns the data you need, or places that data in a global variable that you can access outside of the function and use for the plot.
Your loop doesn't appear to do anything different with the TublinModel function on subsequent iterations. Also, the plot function calls the function again, the same way the loops did. Assuming different data of random lengths is returned by each loop, you can store each set of data in an object array, then find out what parameters to use before plotting.
function runTubulin()
n = 10;
max_length = 0; max_pos = 0; max_neg = 0;
for j = 1:n
data{j} = TublinModel(); % get your data, then characterize it
if max(data(j)) > max_pos, max_pos = max(data(j)); end
if max(-data(j)) > max_neg, max_neg = max(-data(j)); end
end
figure(1); % new axes
axis([0 10 -max_neg max_pos]); hold on; % scale the axis and freeze it
for j = 1:n
plot(length(data(j)),data(j));
end
Hope that helps!
When you call plot with two parameters, the first will be the x-axis data, and the second the y-axis data. Is this what you intend? If you want TubulinModel() to be the y-axis data, you can do plot(TubulinModel()). See help plot for more information.
I don't understand why you call TubulinModel() ten times in the loop before calling it an eleventh time in plot?

Resources