Eigen::SPQR module complains about rows mismatch when solving using least-squares - sparse-matrix

I am using Eigen::SPQR module to solve a least-squares problem Ax = b, and I am getting an error complaining about rows mismatch at the solve step. In general my code consists of initializing A using triplets, b using bracket operators and then computing QR decomposition and finally solving.
Eigen::SparseMatrix<double> A;
// Fill A using triplets
Eigen::VectorXd b(A.rows());
// Fill b using square bracket operator b[i] = ...
Eigen::SPQR< Eigen::SparseMatrix < double > > QR(A);
Eigen::VectorXd X = QR.solve(b); // Line giving error.
Vector b was created with size equal to A.rows() so I am sure it is correct. However, when I check the function QR.rows() it returns the value of A.cols(). Is this some kind of bug? I checked that Eigen::SparseQR solves it without errors but it is a lot slower, so I would like to use the SuiteSparse module instead. Any ideas?

Related

Error when using CVX package with large 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 : )

Why do I keep getting an error that array indices must be positive?

It says the index in position 1 of the diff function must be a positive integer or a logical value which is it so why am I getting this error? I'm trying to implement the basic Euler method in MATLAB
y=zeros(1,6);
h=0;
x(1)= 0;
y(1)= 0;
i=1;
diff(y,x)= x+y
while h<=1
y(i+1)=y(i) + h*f(x(i))
h=h+0.2;
i=i+1;
end
Edit: Changed it to the code below but it still raises the same error in the line y(i+1)=...
y=zeros(1,6);
x=zeros(1,6);
h=0;
i=1;
g=x+y;
while h<=1
y(i+1)=y(i) + h*g(x(i),y(i));
h=h+0.2;
i=i+1;
end
Approach: I would recommend defining an anonymous function
diffh =#(x,y) x + y; % define this prior to use
to use later inside the loop.
Then changing one line
y(ii+1)=y(ii) + h*diffh(x(ii),y(ii));
should work. I've added the "h" to the end as a convention to remind me this is an anonymous function (see note at end).
% MATLAB R2019a
y = zeros(1,6);
x = zeros(1,6);
h=0;
ii=1;
diffh =#(x,y) x + y;
while h <= 1
y(ii+1)=y(ii) + h*diffh(x(ii),y(ii));
x(ii+1) = x(ii)+h;
h=h+0.2;
ii=ii+1;
end
Side note: I've also changed the index i to ii by convention (though MATLAB doesn't require this). Unless you overwrite their values, both i and j default as the sqrt(-1). You can absolutely use them as indices without issue (provided you don't later need complex or imaginary numbers). To ensure this never becomes an issue, many people just use ii and jj as a convention to preserve the default values.
Note that diff is a MATLAB function itself.
Using i as an index is mostly not a good idea in Matlab, because i is also imaginary number. Perhaps another name could solve the problem.

Find the smallest subsequence of k distinct elements in a list

I am super new to sml. I am trying to write a simple code that takes an array of 5 positions with certain numbers and returns the length of the smallest subarray that contains all numbers. However I am getting many error messages that I cannot find in Google. Can anyone help me? The code is the following
fun Min x y = if x>y then return y else return x
local
val a = Array.array (3,0)
val cordela = Array.array(5,0)
val k=0
val front=0
val tail=0
val min=5
update(cordela,0,1)
update(cordela,1,3)
update(cordela,2,3)
update(cordela,3,2)
update(cordela,4,1)
in
fun loop front =
case k>3 of
if sub(a,sub(cordela,front)-1) = 0 then k=k+1 else()
update(a,sub(cordela,front)-1),sub(a,sub(cordela,front)-1)+1)
front = front +1
|
min= Min (front-tail) min
if sub(a,sub(cordela,front)-1) = 0 then k=k-1 else()
update(a,sub(cordela,front)-1),sub(a,sub(cordela,front)-1)-1)
tail=tail+1
if 5>front then loop front+1 else min
end
The error messages that I get are:
pl2.sml:16.13-16.15 Error: syntax error: replacing OF with LBRACKET
pl2.sml:18.36 Error: syntax error: inserting LPAREN
pl2.sml:20.4 Error: syntax error: replacing BAR with EQUALOP
pl2.sml:22.5 Error: syntax error: inserting LPAREN
pl2.sml:26.4 Error: syntax error: inserting LPAREN
pl2.sml:27.2 Error: syntax error found at END
Edit: I am trying to write this code in sml. It is written in c++
while(front < N){
if( k < K ){
if ( e[cordela[front]-1] == 0 ) k += 1;
e[cordela[front]-1] +=1;
front++ ;
}
else{
min = MIN(front - tail ,min);
if ( e[cordela[tail]-1] ==1 ) k -= 1;
e[cordela[tail]-1] -= 1;
tail++;
}
}
As John Coleman says, SML/NJ will not give very helpful error messages. You could try and install Moscow ML instead, since it gives better error messages. Unfortunately, there are some things wrong with this code at a syntactic level that makes it difficult for the compiler to give a meaningful error. Here are some hints to get the syntax right so that you can focus on the algorithm problems:
Don't use local, use let.
Match each ( with a ); you have too many )s.
Declare fun loop ... = ... inside let and in.
Once you've done that, a template for the function that solves your problem could look like:
fun smallest_subarray (needles : Array.array, haystack : Array.array) =
let
val ... = ...
fun loop ... = ...
in
if Array.length needles > Array.length haystack
then ...
else loop ...
end
What if there's no solution to the problem, what will the function return? ~1? NONE?
If you're trying to convert a C++ program to SML, try and include the function part in such a way that it's obvious what identifiers are arguments to the function, and try to name them logically; I have no idea what cordela, e and k are, or if N is a function of the size of the input array, or a constant.
Since an idiomatic solution in SML uses recursion (the function calling itself) rather than iteration (while), you are dealing with both a non-trivial algorithm problem and another paradigm. Try instead to solve a similar, but simpler problem where the algorithm is more trivial and apply the recursion paradigm.
For example, try and write a function that finds the position of an element in a sorted array using binary search:
fun find x arr =
let
fun loop ... = ...
in
loop ...
end
The loop function would take the search bounds (e.g. i and j) as argument and return either SOME i if x is found at position i, or NONE. You could extend this problem in the direction of your original problem by then trying to write a function that determines if an input array, needles, occurs in another input array, haystack, in the order given in needles. You could first assume that needles and haystack are sorted, and then assume that they're not.

Fortran Error # 6366: The shapes of the array expressions do not conform

I encountered this error message while compiling one of my Fortran codes. I found a few similar posts regarding the same error, but none of the situations in those posts apply to my case. I would appreciate any answer or help offered here. Thanks in advance!
(The code is really long, so I only cut out those sentences that are relevant.)
===================================================
DIMENSION A(20), COORDS(3)
REAL B, C, X, Y, Z
B = 1.0
X = COORDS(1)
Y = COORDS(2)
Z = COORDS(3)
DO I = 1,3
A(I) = COORDS(I)
END DO
C = SQRT ( X**2.0 + Y**2.0 ) + B
===================================================
The error message points to the last line:
error #6366: The shapes of the array expressions do not conform. [C]
If I comment out + B, then no error occurs.
I just don't get it. The elements of the array COORDS are passed on to scalar variables X, Y, Z. How come they and B (or C) are not conformable?
I know there must be something I don't quite understand about Fortran array. Please point out my mistake if you catch any.
Thanks a lot!
Justin
Is there a dimension statement elsewhere in the code for any of these variables? The error message seems to point to C; that commenting out +B eliminates the error seems to more solidly point to B.
This is why I like to have all characteristics of a variable declared on a single line.
e.g.,
real, dimension (20) :: a
instead of:
dimension A(20)
real A

Dynamic programming with Data.Vector

am using Data.Vector and am currently in need of computing the contents of a vector for use in computing a cryptographic hash(Sha1). I created the following code.
dynamic :: a -> Int -> (Int -> Vector a -> a) -> Vector a
dynamic e n f =
let
start = Data.Vector.replicate n e
in step start 0
where
step vector i = if i==n then vector
else step (vector // [(i,f i vector)]) (i+1)
I created this so that the function f filling out the vector has access to the partial
results along the way. Surely something like this must already exist in Data.Vector, no?
The problem statement is the following: You are to solve a dynamic programming problem where the finished result is an array. You know the size of the array size and you have a recursive function for filling it out.
You probably already saw the function generate, which takes a size n and a function f of type Int -> a and then produces a Vector a of size n. What you probably weren't aware of is that when using this function you actually do have access to the partial results.
What I mean to say is that inside the function you pass to generate you can refer to the vector you're defining and due to Haskell's laziness it will work fine (unless you make it so that the different items of the vector depend on each other in a circular fashion, of course).
Example:
import Data.Vector
tenFibs = generate 10 fib
where fib 0 = 0
fib 1 = 1
fib n = tenFibs ! (n-1) + tenFibs ! (n-2)
tenFibs is now a vector containing the first 10 Fibonacci numbers.
Maybe you could use one of Data.Vector's scan functions?
http://hackage.haskell.org/packages/archive/vector/0.6.0.2/doc/html/Data-Vector.html#32

Resources