2

Getting confused with something that should be simple. Spent a bit of time trying to debug this and am not getting too far. Would appreciate if someone could help me out.

I am trying to define a sparse matrix in arrayfire by specifying the value/column/row triples as specified in this function. I want to store the following matrix as sparse:

3 3 4 
3 10 0 
4 0 3

I code it up as follows:

int row[] = {0,0,0,1,1,2,2};
int col[] = {0,1,2,0,1,0,2};
double values[] = { 3,3, 4,3,10,4,3};
array rr = sparse(3,3,array(7,values),array(7,row),array(7,col));
af_print(rr);
af_print(dense(rr));

I get the following output:

rr                                                                      
Storage Format : AF_STORAGE_CSR   
[3 3 1 1]
rr: Values
[7 1 1 1]
1.0000                                                              
2.0000                                                              
4.0000                                                              
3.0000
10.0000                                                              
4.0000                                                              
3.0000

rr: RowIdx                                                              
[7 1 1 1]                                                               
         0                                                              
         0                                                              
         0                                                              
         1                                                              
         1                                                              
         2                                                              
         2  

rr: ColIdx                                                              
[7 1 1 1]                                                               
         0                                                              
         1                                                              
         2                                                              
         0                                                              
         1                                                              
         0                                                              
         2  

dense(rr)                                                               
[3 3 1 1]                                                               
    0.0000     0.0000     0.0000                                        
    0.0000     0.0000     3.0000                                        
    3.0000     0.0000     0.0000 

When printing out stored matrix in dense format, I get something completely different than intended.

How do I make the output of printing the dense version of rr give:

3 3 4 
3 10 0 
4 0 3
stantheman
  • 195
  • 8

1 Answers1

2

Arrayfire uses (a modified) CSR format, so the rowarray has to be of length number_of_rows + 1. Normally it would be filled with the number of non-zero entries per row, i.e. {0, 3 ,2, 2}. But for Arrayfire, you need to take the cumulative sum, i.e. {0, 3, 5, 7}. So this works for me:

int row[] = {0,3,5,7};
int col[] = {0,1,2,0,1,0,2};
float values[] = {3,3,4,3,10,4,3};
array rr = sparse(3,3,array(7,values),array(4,row),array(7,col));
af_print(rr);
af_print(dense(rr)); 

However, this is not really convenient, since it is quite different from your input format. As an alternative, you could specify the COO format:

int row[] = {0,0,0,1,1,2,2};
int col[] = {0,1,2,0,1,0,2};
float values[] = { 3,3, 4,3,10,4,3};
array rr = sparse(3,3,array(7,values),array(7,row),array(7,col), AF_STORAGE_COO);
af_print(rr);
af_print(dense(rr));

which produces:

rr
Storage Format : AF_STORAGE_COO
[3 3 1 1]
rr: Values
[7 1 1 1]
    3.0000 
    3.0000 
    4.0000 
    3.0000 
   10.0000 
    4.0000 
    3.0000 

rr: RowIdx
[7 1 1 1]
         0 
         0 
         0 
         1 
         1 
         2 
         2 

rr: ColIdx
[7 1 1 1]
         0 
         1 
         2 
         0 
         1 
         0 
         2 

dense(rr)
[3 3 1 1]
    3.0000     3.0000     4.0000 
    3.0000    10.0000     0.0000 
    4.0000     0.0000     3.0000 

See also https://github.com/arrayfire/arrayfire/issues/2134.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75