Пример #1
0
static void
test_ops(const size_t M, const size_t N,
         const double density, const gsl_rng *r)
{
  size_t i, j;
  int status;

  /* test gsl_spmatrix_add */
  {
    gsl_spmatrix *A = create_random_sparse(M, N, density, r);
    gsl_spmatrix *B = create_random_sparse(M, N, density, r);

    gsl_spmatrix *A_ccs = gsl_spmatrix_ccs(A);
    gsl_spmatrix *B_ccs = gsl_spmatrix_ccs(B);
    gsl_spmatrix *C_ccs = gsl_spmatrix_alloc_nzmax(M, N, 1, GSL_SPMATRIX_CCS);

    gsl_spmatrix *A_crs = gsl_spmatrix_crs(A);
    gsl_spmatrix *B_crs = gsl_spmatrix_crs(B);
    gsl_spmatrix *C_crs = gsl_spmatrix_alloc_nzmax(M, N, 1, GSL_SPMATRIX_CRS);
    
    gsl_spmatrix_add(C_ccs, A_ccs, B_ccs);
    gsl_spmatrix_add(C_crs, A_crs, B_crs);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double aij, bij, cij;

            aij = gsl_spmatrix_get(A_ccs, i, j);
            bij = gsl_spmatrix_get(B_ccs, i, j);
            cij = gsl_spmatrix_get(C_ccs, i, j);
            if (aij + bij != cij)
              status = 1;

            aij = gsl_spmatrix_get(A_crs, i, j);
            bij = gsl_spmatrix_get(B_crs, i, j);
            cij = gsl_spmatrix_get(C_crs, i, j);
            if (aij + bij != cij)
              status = 2;
          }
      }

    gsl_test(status == 1, "test_ops: add M="F_ZU" N="F_ZU" CCS", M, N);
    gsl_test(status == 2, "test_ops: add M="F_ZU" N="F_ZU" CRS", M, N);

    gsl_spmatrix_free(A);
    gsl_spmatrix_free(B);
    gsl_spmatrix_free(A_ccs);
    gsl_spmatrix_free(B_ccs);
    gsl_spmatrix_free(C_ccs);
    gsl_spmatrix_free(A_crs);
    gsl_spmatrix_free(B_crs);
    gsl_spmatrix_free(C_crs);
  }
} /* test_ops() */
Пример #2
0
Файл: test.c Проект: atantet/gsl
static void
test_ops(const size_t M, const size_t N, const gsl_rng *r)
{
  size_t i, j;
  int status;

  /* test gsl_spmatrix_add */
  {
    gsl_spmatrix *Ta = create_random_sparse(M, N, 0.2, r);
    gsl_spmatrix *Tb = create_random_sparse(M, N, 0.2, r);
    gsl_spmatrix *a = gsl_spmatrix_compress(Ta, GSL_SPMATRIX_CCS);
    gsl_spmatrix *b = gsl_spmatrix_compress(Tb, GSL_SPMATRIX_CCS);
    gsl_spmatrix *c = gsl_spmatrix_alloc_nzmax(M, N, 1, GSL_SPMATRIX_CCS);
    gsl_spmatrix *ar = gsl_spmatrix_compress(Ta, GSL_SPMATRIX_CRS);
    gsl_spmatrix *br = gsl_spmatrix_compress(Tb, GSL_SPMATRIX_CRS);
    gsl_spmatrix *cr = gsl_spmatrix_alloc_nzmax(M, N, 1, GSL_SPMATRIX_CRS);

    gsl_spmatrix_add(c, a, b);
    gsl_spmatrix_add(cr, ar, br);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double aij = gsl_spmatrix_get(a, i, j);
	    double bij = gsl_spmatrix_get(b, i, j);
	    double cij = gsl_spmatrix_get(c, i, j);
	    double crij = gsl_spmatrix_get(cr, i, j);

	    if ((aij + bij != cij) || (aij + bij != crij))
	      status = 1;
	  }
      }

    gsl_test(status, "test_ops: _add M=%zu N=%zu compressed format", M, N);

    gsl_spmatrix_free(Ta);
    gsl_spmatrix_free(Tb);
    gsl_spmatrix_free(a);
    gsl_spmatrix_free(b);
    gsl_spmatrix_free(c);
    gsl_spmatrix_free(ar);
    gsl_spmatrix_free(br);
    gsl_spmatrix_free(cr);
  }
} /* test_ops() */