A toy <rsb.h>-based C program implementing the power method for computing matrix eigenvalues. Uses rsb_spmv().
- Author
- Michele Martone
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(
const int argc,
char *
const argv[])
{
const int WANT_VERBOSE = 0;
struct rsb_mtx_t *mtxAp = NULL;
const int br = bs, bc = bs;
int i;
oldnorm = 1.0,
*b1 = NULL, *b2 = NULL,
*bnow = NULL, *bnext = NULL;
size_t ds = 0;
return EXIT_FAILURE;
if(!mtxAp)
return EXIT_FAILURE;
b1 = calloc(1,ds);
b2 = calloc(1,ds);
if(! (b1 && b2))
{
goto err;
}
for( i = 0; i < nrA; ++i )
b1[i] = 1;
bnow = b1, bnext = b2;
while( fabs(norm-oldnorm) > tol && it<maxit )
{
++ it;
oldnorm = norm;
goto err;
norm = 0;
for(i=0;i<nrA;++i)
norm += bnext[i]*bnext[i];
norm = sqrt(norm);
norm = 1.0/norm;
for(i=0;i<nrA;++i)
bnext[i] *= norm;
norm = 1.0/norm;
printf("it:%d norm:%lg norm diff:%lg\n",it,norm,norm-oldnorm);
{void *tmp=bnow;bnow=bnext;bnext=tmp;}
if(WANT_VERBOSE)
{
printf("norm:%lg\n",norm);
if(isinf(norm))
goto err;
for(i=0;i<2;++i)
printf("x[%d]=%lg\n",i,((double*)bnext)[i]);
}
}
free(b1);
free(b2);
goto err;
if( it == maxit )
{
printf("ERROR: hit iterations limit without convergence!");
}
return EXIT_SUCCESS;
err:
return EXIT_FAILURE;
}