bugGNU Scientific Library - Bugs: bug #51951, sparse add function gives wrong...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #51951: sparse add function gives wrong result (shorter than expected)

Submitter:  Alfredo Correa <alfc>
Submitted:  Wed 06 Sep 2017 07:17:21 AM UTC
   
 
Category:  Runtime error Severity:  3 - Normal
Operating System:  linux fedora 26 Status:  Fixed
Assigned to:  None Open/Closed:  Closed
Release:  2.3

Thu 07 Sep 2017 08:41:59 AM UTC, comment #3: 

Fixed in commit 90542b9fc42acfefcd85e225727a57d02bc66723

Patrick Alken <psa>
Group administrator
Thu 07 Sep 2017 07:39:36 AM UTC, comment #2: 

Hi Patrick, sorry, for some reason the code posted removed the '*' symbols. (I am also attaching the file this time)

This should compile with `gcc sparse.c -Wfatal-errors -lgsl -lgslcblas -lm`

```
#include <stdio.h>
#include <stdlib.h>

#include <gsl/gsl_spmatrix.h>

int main(){

gsl_spmatrix* A = gsl_spmatrix_alloc(5, 4); /* triplet format */
gsl_spmatrix* B, *C, *D;
size_t i, j;

/* build the sparse matrix */
gsl_spmatrix_set(A, 0, 2, 3.1);
gsl_spmatrix_set(A, 0, 3, 4.6);
gsl_spmatrix_set(A, 1, 0, 1.0);
gsl_spmatrix_set(A, 1, 2, 7.2);
gsl_spmatrix_set(A, 3, 0, 2.1);
gsl_spmatrix_set(A, 3, 1, 2.9);
gsl_spmatrix_set(A, 3, 3, 8.5);
gsl_spmatrix_set(A, 4, 0, 4.1);

printf("printing all matrix elements:\n");
for (i = 0; i < 5; ++i)
for (j = 0; j < 4; ++j)
printf("A(%zu,%zu) = %g\n", i, j,
gsl_spmatrix_get(A, i, j));

/* print out elements in triplet format */
printf("matrix in triplet format (i,j,Aij):\n");
gsl_spmatrix_fprintf(stdout, A, "%.1f");

/* convert to compressed row format */
C = gsl_spmatrix_crs(A);
D = gsl_spmatrix_crs(A);

gsl_spmatrix_add(D, C, C);

printf("matrix in compressed row format:\n");
printf("i = [ ");
for (i = 0; i < C->nz; ++i)
printf("%zu, ", C->i[i]);
printf("]\n");

printf("p = [ ");
for (i = 0; i < C->size1 + 1; ++i)
printf("%zu, ", C->p[i]);
printf("]\n");

printf("d = [ ");
for (i = 0; i < C->nz; ++i)
printf("%g, ", C->data[i]);
printf("]\n");

printf("D matrix in compressed row format:\n");
printf("i = [ ");
for (i = 0; i < D->nz; ++i)
printf("%zu, ", D->i[i]);
printf("]\n");

printf("p = [ ");
for (i = 0; i < D->size1 + 1; ++i)
printf("%zu, ", D->p[i]);
printf("]\n");

printf("d = [ ");
for (i = 0; i < D->nz; ++i)
printf("%g, ", D->data[i]);
printf("]\n");

printf("end");
gsl_spmatrix_free(A);
gsl_spmatrix_free(B);
gsl_spmatrix_free(C);
gsl_spmatrix_free(D);
return 0;
}
```

(file #41753)

Alfredo Correa <alfc>
Thu 07 Sep 2017 07:29:51 AM UTC, comment #1: 

Please provide a complete working code which demonstrates the problem. Your code below doesn't even compile due to numerous errors.

Patrick Alken <psa>
Group administrator
Wed 06 Sep 2017 07:17:21 AM UTC, original submission:  

This program produces the wrong sum D = C + C:

C is
```
matrix in compressed row format:
i = [ 2, 3, 0, 2, 0, 1, 3, 0, ]
p = [ 0, 2, 4, 4, 7, 8, ]
d = [ 3.1, 4.6, 1, 7.2, 2.1, 2.9, 8.5, 4.1, ]
```
while D (erroneously) results in
```
D matrix in compressed row format:
i = [ 2, 3, 0, 1, ]
p = [ 0, 2, 3, 3, 4, 4, ]
d = [ 6.2, 9.2, 2, 5.8, ]
```




```
int main(){

  gsl_spmatrix A = gsl_spmatrix_alloc(5, 4); / triplet format */
  gsl_spmatrix *B, *C, *D;
  size_t i, j;

  /* build the sparse matrix */
  gsl_spmatrix_set(A, 0, 2, 3.1);
  gsl_spmatrix_set(A, 0, 3, 4.6);
  gsl_spmatrix_set(A, 1, 0, 1.0);
  gsl_spmatrix_set(A, 1, 2, 7.2);
  gsl_spmatrix_set(A, 3, 0, 2.1);
  gsl_spmatrix_set(A, 3, 1, 2.9);
  gsl_spmatrix_set(A, 3, 3, 8.5);
  gsl_spmatrix_set(A, 4, 0, 4.1);

  printf("printing all matrix elements:\n");
  for (i = 0; i < 5; ++i)
    for (j = 0; j < 4; ++j)
      printf("A(%zu,%zu) = %g\n", i, j,
             gsl_spmatrix_get(A, i, j));

  /* print out elements in triplet format */
  printf("matrix in triplet format (i,j,Aij):\n");
  gsl_spmatrix_fprintf(stdout, A, "%.1f");

  /* convert to compressed row format */
  C = gsl_spmatrix_crs(A);
  D = gsl_spmatrix_crs(A);
 
  gsl_spmatrix_add(D, C, C);

  printf("matrix in compressed row format:\n");
  printf("i = [ ");
  for (i = 0; i < C->nz; ++i)
    printf("%zu, ", C->i[i]);
  printf("]\n");

  printf("p = [ ");
  for (i = 0; i < C->size1 + 1; ++i)
    printf("%zu, ", C->p[i]);
  printf("]\n");

  printf("d = [ ");
  for (i = 0; i < C->nz; ++i)
    printf("%g, ", C->data[i]);
  printf("]\n");
 
   printf("D matrix in compressed row format:\n");
  printf("i = [ ");
  for (i = 0; i < D->nz; ++i)
    printf("%zu, ", D->i[i]);
  printf("]\n");

  printf("p = [ ");
  for (i = 0; i < D->size1 + 1; ++i)
    printf("%zu, ", D->p[i]);
  printf("]\n");

  printf("d = [ ");
  for (i = 0; i < D->nz; ++i)
    printf("%g, ", D->data[i]);
  printf("]\n");

  gsl_spmatrix_free(A);
  gsl_spmatrix_free(B);
  gsl_spmatrix_free(C);
```

Alfredo Correa <alfc>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #41753:  sparse.c added by alfc (2KiB - text/x-csrc - full code)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by psa (Posted a comment)
  • -email is unavailable- added by alfc (Submitted the item)
  •  

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-09-07 psa StatusNone Fixed
        Open/ClosedOpen Closed
    2017-09-07 alfc Attached File- Added sparse.c, #41753

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code