I have a little problem, I would like to convert a matrix 10*10 in a CSR or COO sparse matrix/format. The matrix is:
1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
-0.45 0.10 -0.45 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 -0.45 0.10 -0.45 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 -0.45 0.10 -0.45 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 -0.45 0.10 -0.45 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 -0.45 0.10 -0.45 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 -0.45 0.10 -0.45 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 -0.45 0.10 -0.45 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 -0.45 0.10 -0.45
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00
I am using the "CUSP" functions but it did not work, once tha matrix A I would like just to convert in other format. Can you help me?
Well I would like also to use this matrix to solve the system Ax=b, using bicgstab:
b=
0.00000
0.34202
0.64279
0.86603
0.98481
0.98481
0.86603
0.64279
0.34202
0.00000
My code for this is:
int n = 10, r;
cusp::coo_matrix<int,float,cusp::device_memory> A(n, n, 3*n - 4);
cusp::array1d<float, cusp::device_memory> x(A.num_rows, 0);
cusp::array1d<float, cusp::device_memory> b(A.num_rows, 1);
b[0]=0.00000;
b[1]=0.34202;
b[2]=0.64279;
b[3]=0.86603;
b[4]=0.98481;
b[5]=0.98481;
b[6]=0.86603;
b[7]=0.64279;
b[8]=0.34202;
b[9]=0.00000;
i=0;
// row 0
A.row_indices[i] = 0.0;
A.column_indices[i] = 0.0;
A.values[i] = 1.00;
++i;
// rows 1 through n - 2
for (r = 1; r != n - 1; ++r) {
A.row_indices[i] = r;
A.column_indices[i] = r - 1;
A.values[i] = -0.45;
++i;
A.row_indices[i] = r;
A.column_indices[i] = r;
A.values[i] = 0.10;
++i;
A.row_indices[i] = r;
A.column_indices[i] = r + 1;
A.values[i] = -0.45;
++i;
}
// row n - 1
A.row_indices[i] = n - 1;
A.column_indices[i] = n - 1;
A.values[i] = 1.00;
++i;
// set stopping criteria:
// iteration_limit = 100
// relative_tolerance = 1e-3
cusp::verbose_monitor<ValueType> monitor(b, 100, 1e-3);
// set preconditioner (identity)
cusp::identity_operator<ValueType, MemorySpace> M(A.num_rows, A.num_rows);
// solve the linear system A x = b
cusp::krylov::bicgstab(A, x, b, monitor, M);
cusp::print(x);
The result using Octave should be something similar to:
0.00000
0.32441
0.60970
0.82144
0.93411
0.93411
0.82144
0.60970
0.32441
0.00000
But is also with negative numbers, so WRONG.