Skip to content

Instantly share code, notes, and snippets.

@alphaville
Last active December 11, 2015 19:28
Show Gist options
  • Select an option

  • Save alphaville/4648422 to your computer and use it in GitHub Desktop.

Select an option

Save alphaville/4648422 to your computer and use it in GitHub Desktop.
Multiply a dense vector with a sparse matrix.
#ifndef csi
#define csi ptrdiff_t
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
typedef struct cs_sparse {
csi nzmax;
csi m;
csi n;
csi *p;
csi *i;
double *x;
csi nz;
} cs;
double dvsm_quad(cs* Q, double* x) {
int nzmax = Q->nz;
unsigned int k;
double ell = 0;
for (k = 0; k < nzmax; k++) {
int i = (Q->i)[k];
int j = (Q->p)[k];
ell += (Q->x)[k] * x[i] * x[j];
}
printf("L_inside=%g\n",ell);
return (ell);
}
/*
*
*/
int main(int argc, char** argv) {
ptrdiff_t i[2]= {0,1};
ptrdiff_t p[2]= {0,1};
double vals[2]={1.0,1.0};
cs* Q = malloc(sizeof(Q));
double x[2] = {5.0, 5.0};
Q->i=i;
Q->p=p;
Q->nz=2;
Q->nzmax=2;
Q->n=2;
Q->m=2;
Q->x=vals;
double L= dvsm_quad(Q, x);
printf("L_outside=%g\n",L);
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment