when I make sparse matrix in C-MEX , eigs fuctions call doesn't work

when I make sparse matrix in C-MEX , eigs fuctions call doesn't work
(1) I made a sparse matrix M in C-MEX
(2) I called eigs function with the sparse matrix M
[V,T] = eigs(M,1,'sm');
(3) it doesn't work with following error. >> eigs(M,1,'sm');
??? Error using ==> eigs>checkInputs/LUfactorAminusSigmaB at 1136 (A-sigma*I)is singular. The shift is an eigenvalue. Try to use some other shift please.
Error in ==> eigs>checkInputs at 899 [L,U,pp,qq,dgAsB] = LUfactorAminusSigmaB;
Error in ==> eigs at 94 [A,Amatrix,isrealprob,issymA,n,B,classAB,k,eigs_sigma,whch, ...
If I make a sparse matrix, then eigs function works well. what is problem with my sparse matrix made in C-MEX?
my C-MEX sourse is...... ---------------------------------------------------------- percent_sparse=0.10; nzmax = (mwSize)ceil(3000*3000*percent_sparse);
plhs[0] = mxCreateSparse(3000, 3000, nzmax ,mxREAL);
T = mxGetPr(plhs[0]);
irs = mxGetIr(plhs[0]);
jcs = mxGetJc(plhs[0]);
. . . . jcs[x-1] = k;
for(y=0 ; y< (*num_states) ; y++)
{
Tp=1;
for (i=0 ; i< (*num_nodes) ; i++)
{
if(ADotS[i] ==0)
{
if(next_state[i]==current_state[i]) Ts = 1-(*c);
else
Ts = (*c);
}
else
{
if (next_state[i]==1) Ts = 0.5 + (0.5 * tanh(5*ADotS[i]));
else
Ts = 0.5 - (0.5 * tanh(5*ADotS[i]));
}
Tp=Tp*Ts;
}
if( Tp > 0)
{
if( k>=nzmax )
{
mwSize oldnzmax = nzmax;
percent_sparse += 0.01;
nzmax = (mwSize)ceil((3000)*(3000)*percent_sparse);
if(oldnzmax==nzmax) /* make sure nzmax increases atleast by 1 */
nzmax++;
mxSetNzmax(plhs[0],nzmax);
mxSetPr(plhs[0],(double*)mxRealloc(T,nzmax*sizeof(double)));
mxSetIr(plhs[0],(mwIndex*)mxRealloc(irs,nzmax*sizeof(mwIndex)));
T=mxGetPr(plhs[0]);
irs=mxGetIr(plhs[0]);
}
if( x-1 == y )
T[k] = -1;
else
T[k] = Tp;
irs[k]=y;
k++;
}
}
-----------------------------------------------------------------
plhs[0] = mxCreateSparse(3000, 3000, nzmax ,mxREAL);
I think that when i make sparse matrix in C-MEX, it needs nzmax. it is the number of non-zero value. It would be incorrect, because I don't know how many non-zeros there exists. so the sparse matrix is a little bit strange, eigs fuctions doesn't understand the sparse matrix...
please help me!!

1 Comment

What is the error that you get? Also, why do you run the line:
plhs[0] = mxCreateSparse(3000, 3000, nzmax ,mxREAL);
at the end of your code? This overwrites all the values you assign to the sparse matrix. Is this a typo? Please format your code for better readability.

Answers (1)

See this FEX submission by Tim Davis to help you diagnose sparse matrix formation problems in a mex routine:

This question is closed.

Asked:

on 11 May 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!