コード例 #1
0
ファイル: test.c プロジェクト: jfcaron3/gsl-steffen-devel
static gsl_spmatrix *
create_random_sparse(const size_t M, const size_t N, const double density,
                     const gsl_rng *r)
{
  gsl_spmatrix *m = gsl_spmatrix_alloc(M, N);
  size_t nnzwanted = (size_t) round(M * N * GSL_MIN(density, 1.0));
  size_t n = 0;
  size_t i;

  /* set diagonal entries to try to ensure non-singularity */
  for (i = 0; i < GSL_MIN(M, N); ++i)
    {
      double x = gsl_rng_uniform(r);
      gsl_spmatrix_set(m, i, i, x);
      ++n;
    }

  while (n <= nnzwanted)
    {
      /* generate a random row and column */
      size_t i = gsl_rng_uniform(r) * M;
      size_t j = gsl_rng_uniform(r) * N;
      double x;

      /* check if this position is already filled */
      if (gsl_spmatrix_get(m, i, j) != 0.0)
        continue;

      /* generate random m_{ij} and add it */
      x = gsl_rng_uniform(r);
      gsl_spmatrix_set(m, i, j, x);
      ++n;
    }

  return m;
} /* create_random_sparse() */
コード例 #2
0
ファイル: test.c プロジェクト: atantet/gsl
static void
test_getset(const size_t M, const size_t N, const gsl_rng *r)
{
  int status;
  size_t i, j;

  /* test triplet versions of _get and _set */
  {
    size_t k = 0;
    gsl_spmatrix *m = gsl_spmatrix_alloc(M, N);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double x = (double) ++k;
	    double y;

	    gsl_spmatrix_set(m, i, j, x, 0);
	    y = gsl_spmatrix_get(m, i, j);
	    if (x != y)
	      status = 1;
	  }
      }

    gsl_test(status, "test_getset: M=%zu N=%zu _get != _set", M, N);

    /* test setting an element to 0 */
    gsl_spmatrix_set(m, 0, 0, 1.0, 0);
    gsl_spmatrix_set(m, 0, 0, 0.0, 0);

    status = gsl_spmatrix_get(m, 0, 0) != 0.0;
    gsl_test(status, "test_getset: M=%zu N=%zu m(0,0) = %f",
	     M, N, gsl_spmatrix_get(m, 0, 0));

    /* test gsl_spmatrix_set_zero() */
    gsl_spmatrix_set(m, 0, 0, 1.0, 0);
    gsl_spmatrix_set_zero(m);
    status = gsl_spmatrix_get(m, 0, 0) != 0.0;
    gsl_test(status, "test_getset: M=%zu N=%zu set_zero m(0,0) = %f",
	     M, N, gsl_spmatrix_get(m, 0, 0));

    /* resassemble matrix to ensure nz is calculated correctly */
    k = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double x = (double) ++k;
	    gsl_spmatrix_set(m, i, j, x, 0);
	  }
      }

    status = gsl_spmatrix_nnz(m) != M * N;
    gsl_test(status, "test_getset: M=%zu N=%zu set_zero nz = %zu",
	     M, N, gsl_spmatrix_nnz(m));

    gsl_spmatrix_free(m);
  }

  /* test duplicate values are handled correctly */
  {
    size_t min = GSL_MIN(M, N);
    size_t expected_nnz = min;
    size_t nnz;
    size_t k = 0;
    gsl_spmatrix *m = gsl_spmatrix_alloc(M, N);

    status = 0;
    for (i = 0; i < min; ++i)
      {
	for (j = 0; j < 5; ++j)
	  {
	    double x = (double) ++k;
	    double y;

	    gsl_spmatrix_set(m, i, i, x, 0);
	    y = gsl_spmatrix_get(m, i, i);
	    if (x != y)
	      status = 1;
	  }
      }

    gsl_test(status, "test_getset: duplicate test M=%zu N=%zu _get != _set", M, N);

    nnz = gsl_spmatrix_nnz(m);
    status = nnz != expected_nnz;
    gsl_test(status, "test_getset: duplicate test M=%zu N=%zu nnz=%zu, expected=%zu",
	     M, N, nnz, expected_nnz);

    gsl_spmatrix_free(m);
  }

  /* test compressed version of gsl_spmatrix_get() */
  {
    gsl_spmatrix *T = create_random_sparse(M, N, 0.3, r);
    gsl_spmatrix *C = gsl_spmatrix_compress(T, GSL_SPMATRIX_CCS);
    gsl_spmatrix *CR = gsl_spmatrix_compress(T, GSL_SPMATRIX_CRS);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double Tij = gsl_spmatrix_get(T, i, j);
	    double Cij = gsl_spmatrix_get(C, i, j);

	    if (Tij != Cij)
	      status = 1;
	  }
      }

    gsl_test(status, "test_getset: M=%zu N=%zu compressed column _get", M, N);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double Tij = gsl_spmatrix_get(T, i, j);
	    double Cij = gsl_spmatrix_get(CR, i, j);

	    if (Tij != Cij)
	      status = 1;
	  }
      }

    gsl_test(status, "test_getset: M=%zu N=%zu compressed row _get", M, N);

    gsl_spmatrix_free(T);
    gsl_spmatrix_free(C);
    gsl_spmatrix_free(CR);
  }
} /* test_getset() */
コード例 #3
0
ファイル: test.c プロジェクト: atantet/gsl
void
test_prop(const size_t M, const size_t N, const double density,
	  const double d, const gsl_rng *r)
{
  gsl_spmatrix *m, *test, *ccs, *crs;
  size_t n, p, outerIdx;
  int status, any;
 
  m = create_random_sparse(M, N, density, r);
  ccs = gsl_spmatrix_compress(m, GSL_SPMATRIX_CCS);
  crs = gsl_spmatrix_compress(m, GSL_SPMATRIX_CRS);
  
  // For triplet
  /** Test greater than */
  test = gsl_spmatrix_gt_elements(m, d);
  status = 0;
  for (n = 0; n < m->nz; n++)
    {
      if ((m->data[n] > d) != (gsl_spmatrix_get(test, m->i[n], m->p[n])))
	status = 1;
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _gt_elements triplet", M, N);
  gsl_spmatrix_free(test);
  
  /** Test greater or equal than */
  test = gsl_spmatrix_ge_elements(m, d);
  status = 0;
  for (n = 0; n < m->nz; n++)
    {
      if ((m->data[n] >= d) != (gsl_spmatrix_get(test, m->i[n], m->p[n])))
	status = 1;
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _ge_elements triplet", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower than */
  test = gsl_spmatrix_lt_elements(m, d);
  status = 0;
  for (n = 0; n < m->nz; n++)
    {
      if ((m->data[n] < d) != (gsl_spmatrix_get(test, m->i[n], m->p[n])))
	status = 1;
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _lt_elements triplet", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower or equal than */
  test = gsl_spmatrix_le_elements(m, d);
  status = 0;
  any = 0;
  for (n = 0; n < m->nz; n++)
    {
      if ((m->data[n] <= d) != (gsl_spmatrix_get(test, m->i[n], m->p[n])))
	status = 1;
      if (m->data[n] <= d)
	any = 1;
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _le_elements triplet", M, N);
  gsl_spmatrix_free(test);
  
  /** Test any */
  status = !(any == (int) gsl_spmatrix_any(test));
  gsl_test(status, "test_prop: M=%zu N=%zu _any triplet", M, N);

  // For CCS
  /** Test greater than */
  test = gsl_spmatrix_gt_elements(ccs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < ccs->outerSize; outerIdx++)
    {
      for (p = ccs->p[outerIdx]; p < ccs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, ccs->i[p], outerIdx)) != (ccs->data[p] > d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _gt_elements CCS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test greater or equal than */
  test = gsl_spmatrix_ge_elements(ccs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < ccs->outerSize; outerIdx++)
    {
      for (p = ccs->p[outerIdx]; p < ccs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, ccs->i[p], outerIdx)) != (ccs->data[p] >= d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _ge_elements CCS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower than */
  test = gsl_spmatrix_lt_elements(ccs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < ccs->outerSize; outerIdx++)
    {
      for (p = ccs->p[outerIdx]; p < ccs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, ccs->i[p], outerIdx)) != (ccs->data[p] < d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _lt_elements CCS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower or equal than */
  test = gsl_spmatrix_le_elements(ccs, d);
  status = 0;
  any = 0;
  for (outerIdx = 0; outerIdx < ccs->outerSize; outerIdx++)
    {
      for (p = ccs->p[outerIdx]; p < ccs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, ccs->i[p], outerIdx)) != (ccs->data[p] <= d))
	    status = 1;
	  if (ccs->data[p] <= d)
	    any = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _le_elements CCS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test any */
  status = !(any == (int) gsl_spmatrix_any(test));
  gsl_test(status, "test_prop: M=%zu N=%zu _any CCS", M, N);
  

    // For CRS
  /** Test greater than */
  test = gsl_spmatrix_gt_elements(crs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < crs->outerSize; outerIdx++)
    {
      for (p = crs->p[outerIdx]; p < crs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, outerIdx, crs->i[p])) != (crs->data[p] > d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _gt_elements CRS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test greater or equal than */
  test = gsl_spmatrix_ge_elements(crs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < crs->outerSize; outerIdx++)
    {
      for (p = crs->p[outerIdx]; p < crs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, outerIdx, crs->i[p])) != (crs->data[p] >= d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _ge_elements CRS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower than */
  test = gsl_spmatrix_lt_elements(crs, d);
  status = 0;
  for (outerIdx = 0; outerIdx < crs->outerSize; outerIdx++)
    {
      for (p = crs->p[outerIdx]; p < crs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, outerIdx, crs->i[p])) != (crs->data[p] < d))
	    status = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _lt_elements CRS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test lower or equal than */
  test = gsl_spmatrix_le_elements(crs, d);
  status = 0;
  any = 0;
  for (outerIdx = 0; outerIdx < crs->outerSize; outerIdx++)
    {
      for (p = crs->p[outerIdx]; p < crs->p[outerIdx + 1]; p++)
	{
	  if (((int) gsl_spmatrix_get(test, outerIdx, crs->i[p])) != (crs->data[p] <= d))
	    status = 1;
	  if (crs->data[p] <= d)
	    any = 1;
	}
    }
  gsl_test(status, "test_prop: M=%zu N=%zu _le_elements CRS", M, N);
  gsl_spmatrix_free(test);
  
  /** Test any */
  status = !(any == (int) gsl_spmatrix_any(test));
  gsl_test(status, "test_prop: M=%zu N=%zu _any CRS", M, N);
  

  gsl_spmatrix_free(m);
  gsl_spmatrix_free(ccs);
  gsl_spmatrix_free(crs);
  
  return;
}
コード例 #4
0
ファイル: test.c プロジェクト: atantet/gsl
void
test_manip(const size_t M, const size_t N, const double density,
	   const gsl_rng *r)
{
  int status;
  gsl_spmatrix *tri, *ccs, *crs, *test;
  gsl_matrix *dense, *denseDivRows, *denseDivCols;
  double sum, sumDense;
  gsl_vector *v;
  gsl_vector *denseRowSum, *denseColSum;
  size_t i, j;

  tri = create_random_sparse(M, N, density, r);
  dense = gsl_matrix_alloc(M, N);
  gsl_spmatrix_sp2d(dense, tri);

  /** Get row sum and col sum aswell as divided matrices for dense */
  denseDivRows = gsl_matrix_calloc(M, N);
  denseDivCols = gsl_matrix_calloc(M, N);  
  denseRowSum = gsl_vector_calloc(M);
  denseColSum = gsl_vector_calloc(N);
  sumDense = 0.;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  denseRowSum->data[i * denseRowSum->stride] += gsl_matrix_get(dense, i, j);
	  denseColSum->data[j * denseColSum->stride] += gsl_matrix_get(dense, i, j);
	  sumDense += gsl_matrix_get(dense, i, j);
	}
    }
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_pow_2(denseRowSum->data[i * denseRowSum->stride]) > 1.e-12)
	    {
	      gsl_matrix_set(denseDivRows, i, j, gsl_matrix_get(dense, i, j)
			     / denseRowSum->data[i * denseRowSum->stride]);
	    }
	  else
	    {
	      gsl_matrix_set(denseDivRows, i, j, gsl_matrix_get(dense, i, j));
	    }

	  if (gsl_pow_2(denseColSum->data[j * denseColSum->stride]) > 1.e-12)
	    {
	      gsl_matrix_set(denseDivCols, i, j, gsl_matrix_get(dense, i, j)
			     / denseColSum->data[j * denseColSum->stride]);
	    }
	  else
	    {
	      gsl_matrix_set(denseDivCols, i, j, gsl_matrix_get(dense, i, j));
	    }
	}
    }
							   
  
  // Compress
  ccs = gsl_spmatrix_compress(tri, GSL_SPMATRIX_CCS);
  crs = gsl_spmatrix_compress(tri, GSL_SPMATRIX_CRS);

  
  /** TOTAL SUM */
  /** Triplet */
  sum = gsl_spmatrix_get_sum(tri);
  status = !(sum == sumDense);
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_sum triplet", M, N);
  
  /** CCS */
  sum = gsl_spmatrix_get_sum(ccs);
  status = !(sum == sumDense);
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_sum CCS", M, N);
  
  /** CRS */
  sum = gsl_spmatrix_get_sum(crs);
  status = !(sum == sumDense);
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_sum CRS", M, N);


  /** COLUMN SUM AND DIVIDE */
  /** Triplet */
  /* Sum */
  v = gsl_vector_alloc(M);
  gsl_spmatrix_get_rowsum(v, tri);
  status = 0;
  for (i = 0; i < M; i++)
    if (v->data[i * v->stride] != denseRowSum->data[i * denseRowSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_rowsum triplet", M, N);
  /* Div */
  test = gsl_spmatrix_alloc_nzmax(crs->size1, crs->size2, 0, GSL_SPMATRIX_TRIPLET);
  gsl_spmatrix_memcpy(test, tri);
  gsl_spmatrix_div_rows(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_matrix_get(denseDivRows, i, j) != gsl_spmatrix_get(test, i, j))
	    status = 1;
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_rows triplet", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);

  /** CCS */
  /* Sum */
  v = gsl_vector_alloc(M);
  gsl_spmatrix_get_rowsum(v, ccs);
  status = 0;
  for (i = 0; i < M; i++)
    if (v->data[i * v->stride] != denseRowSum->data[i * denseRowSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_rowsum CCS", M, N);
  /* Div */
  test = gsl_spmatrix_alloc_nzmax(ccs->size1, ccs->size2, 0, GSL_SPMATRIX_CCS);
  gsl_spmatrix_memcpy(test, ccs);
  gsl_spmatrix_div_rows(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_matrix_get(denseDivRows, i, j) != gsl_spmatrix_get(test, i, j))
	    status = 1;
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_rows CCS", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);
  
  /* CRS */
  /* Sum */
  v = gsl_vector_alloc(M);
  gsl_spmatrix_get_rowsum(v, crs);
  status = 0;
  for (i = 0; i < M; i++)
    if (v->data[i * v->stride] != denseRowSum->data[i * denseRowSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_rowsum CRS", M, N);
  /* Div */
  test = gsl_spmatrix_alloc_nzmax(crs->size1, crs->size2, 0, GSL_SPMATRIX_CRS);
  gsl_spmatrix_memcpy(test, crs);
  gsl_spmatrix_div_rows(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_matrix_get(denseDivRows, i, j) != gsl_spmatrix_get(test, i, j))
	    status = 1;
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_rows CRS", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);


  /** COLUMN SUM AND DIVIDE */
  /** Triplet */
  /* Sum */
  v = gsl_vector_alloc(N);
  gsl_spmatrix_get_colsum(v, tri);
  status = 0;
  for (j = 0; j < N; j++)
    if (v->data[j * v->stride] != denseColSum->data[j * denseColSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_colsum triplet", M, N);
  /* Div */
  test = gsl_spmatrix_alloc_nzmax(tri->size1, tri->size2, 0, GSL_SPMATRIX_TRIPLET);
  gsl_spmatrix_memcpy(test, tri);
  gsl_spmatrix_div_cols(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_fcmp(gsl_matrix_get(denseDivCols, i, j), gsl_spmatrix_get(test, i, j), 1.e-12))
	    {
	      fprintf(stdout, "mismatch: (%zu, %zu) %lf != %lf\n", i, j, gsl_matrix_get(denseDivCols, i, j),
		      gsl_spmatrix_get(test, i, j));
	      status = 1;
	    }
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_cols triplet", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);

  /** CCS */
  /** Sum */
  v = gsl_vector_alloc(N);
  gsl_spmatrix_get_colsum(v, ccs);
  status = 0;
  for (j = 0; j < N; j++)
    if (v->data[j * v->stride] != denseColSum->data[j * denseColSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_colsum CCS", M, N);
  /** Div */
  test = gsl_spmatrix_alloc_nzmax(ccs->size1, ccs->size2, 0, GSL_SPMATRIX_CCS);
  gsl_spmatrix_memcpy(test, ccs);
  gsl_spmatrix_div_cols(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_matrix_get(denseDivCols, i, j) != gsl_spmatrix_get(test, i, j))
	    status = 1;
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_cols CCS", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);
  
  /** CRS */
  /* Sum */
  v = gsl_vector_alloc(N);
  gsl_spmatrix_get_colsum(v, crs);
  status = 0;
  for (j = 0; j < N; j++)
    if (v->data[j * v->stride] != denseColSum->data[j * denseColSum->stride])
      status = 1;
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _get_colsum CRS", M, N);
  /* Div */
  test = gsl_spmatrix_alloc_nzmax(crs->size1, crs->size2, 0, GSL_SPMATRIX_CRS);
  gsl_spmatrix_memcpy(test, crs);
  gsl_spmatrix_div_cols(test, v);
  status = 0;
  for (i = 0; i < M; i++)
    {
      for (j = 0; j < N; j++)
	{
	  if (gsl_matrix_get(denseDivCols, i, j) != gsl_spmatrix_get(test, i, j))
	    status = 1;
	}
    }
  gsl_test(status, "test_manip: M=%zu N=%zu _get != _div_cols CRS", M, N);
  gsl_vector_free(v);
  gsl_spmatrix_free(test);


  /** Free */
  gsl_spmatrix_free(tri);
  gsl_spmatrix_free(ccs);
  gsl_spmatrix_free(crs);
  gsl_matrix_free(dense);
  gsl_matrix_free(denseDivRows);
  gsl_matrix_free(denseDivCols);
  gsl_vector_free(denseRowSum);
  gsl_vector_free(denseColSum);
  
  return;
}
コード例 #5
0
ファイル: test.c プロジェクト: atantet/gsl
static void
test_memcpy(const size_t M, const size_t N, const gsl_rng *r)
{
  int status;

  {
    gsl_spmatrix *at = create_random_sparse(M, N, 0.2, r);
    gsl_spmatrix *ac = gsl_spmatrix_compress(at, GSL_SPMATRIX_CCS);
    gsl_spmatrix *ar = gsl_spmatrix_compress(at, GSL_SPMATRIX_CRS);
    gsl_spmatrix *bt, *bc, *br;

    bt = gsl_spmatrix_alloc(M, N);
    gsl_spmatrix_memcpy(bt, at);

    status = gsl_spmatrix_equal(at, bt) != 1;
    gsl_test(status, "test_memcpy: _memcpy M=%zu N=%zu triplet format", M, N);

    bc = gsl_spmatrix_alloc_nzmax(M, N, ac->nzmax, GSL_SPMATRIX_CCS);
    gsl_spmatrix_memcpy(bc, ac);

    status = gsl_spmatrix_equal(ac, bc) != 1;
    gsl_test(status, "test_memcpy: _memcpy M=%zu N=%zu compressed column format", M, N);

    br = gsl_spmatrix_alloc_nzmax(M, N, ar->nzmax, GSL_SPMATRIX_CRS);
    gsl_spmatrix_memcpy(br, ar);

    status = gsl_spmatrix_equal(ar, br) != 1;
    gsl_test(status, "test_memcpy: _memcpy M=%zu N=%zu compressed row format", M, N);

    gsl_spmatrix_free(at);
    gsl_spmatrix_free(ac);
    gsl_spmatrix_free(ar);
    gsl_spmatrix_free(bt);
    gsl_spmatrix_free(bc);
    gsl_spmatrix_free(br);
  }

  /* test transpose_memcpy */
  {
    gsl_spmatrix *A = create_random_sparse(M, N, 0.3, r);
    gsl_spmatrix *B = gsl_spmatrix_compress(A, GSL_SPMATRIX_CCS);
    gsl_spmatrix *BR = gsl_spmatrix_compress(A, GSL_SPMATRIX_CRS);
    gsl_spmatrix *AT = gsl_spmatrix_alloc(N, M);
    gsl_spmatrix *BT = gsl_spmatrix_alloc_nzmax(N, M, 1, GSL_SPMATRIX_CCS);
    gsl_spmatrix *BTR = gsl_spmatrix_alloc_nzmax(N, M, 1, GSL_SPMATRIX_CRS);
    size_t i, j;

    gsl_spmatrix_transpose_memcpy(AT, A);
    gsl_spmatrix_transpose_memcpy(BT, B);
    gsl_spmatrix_transpose_memcpy(BTR, BR);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double Aij = gsl_spmatrix_get(A, i, j);
	    double ATji = gsl_spmatrix_get(AT, j, i);

	    if (Aij != ATji)
	      status = 1;
	  }
      }

    gsl_test(status, "test_memcpy: _transpose_memcpy M=%zu N=%zu triplet format", M, N);

    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 BTji = gsl_spmatrix_get(BT, j, i);

	    if ((Bij != BTji) || (Aij != Bij))
	      status = 1;
	  }
      }

    gsl_test(status, "test_memcpy: _transpose_memcpy M=%zu N=%zu column format", M, N);

    status = 0;
    for (i = 0; i < M; ++i)
      {
	for (j = 0; j < N; ++j)
	  {
	    double Aij = gsl_spmatrix_get(A, i, j);
	    double BRij = gsl_spmatrix_get(BR, i, j);
	    double BTRji = gsl_spmatrix_get(BTR, j, i);
	    
	    if ((Aij != BRij) || (BRij != BTRji))
	      status = 1;
	  }
      }
    gsl_test(status, "test_memcpy: _transpose_memcpy M=%zu N=%zu row format", M, N);

    gsl_spmatrix_free(A);
    gsl_spmatrix_free(AT);
    gsl_spmatrix_free(B);
    gsl_spmatrix_free(BT);
    gsl_spmatrix_free(BR);
    gsl_spmatrix_free(BTR);
  }
} /* test_memcpy() */
コード例 #6
0
ファイル: spprop.c プロジェクト: FMX/gsl
int
gsl_spmatrix_equal(const gsl_spmatrix *a, const gsl_spmatrix *b)
{
  const size_t M = a->size1;
  const size_t N = a->size2;

  if (b->size1 != M || b->size2 != N)
    {
      GSL_ERROR_VAL("matrices must have same dimensions", GSL_EBADLEN, 0);
    }
  else if (a->sptype != b->sptype)
    {
      GSL_ERROR_VAL("trying to compare different sparse matrix types", GSL_EINVAL, 0);
    }
  else
    {
      const size_t nz = a->nz;
      size_t n;

      if (nz != b->nz)
        return 0; /* different number of non-zero elements */

      if (GSL_SPMATRIX_ISTRIPLET(a))
        {
          /*
           * triplet formats could be out of order but identical, so use
           * gsl_spmatrix_get() on b for each aij
           */
          for (n = 0; n < nz; ++n)
            {
              double bij = gsl_spmatrix_get(b, a->i[n], a->p[n]);

              if (a->data[n] != bij)
                return 0;
            }
        }
      else if (GSL_SPMATRIX_ISCCS(a))
        {
          /*
           * for compressed column, both matrices should have everything
           * in the same order
           */

          /* check row indices and data */
          for (n = 0; n < nz; ++n)
            {
              if ((a->i[n] != b->i[n]) || (a->data[n] != b->data[n]))
                return 0;
            }

          /* check column pointers */
          for (n = 0; n < a->size2 + 1; ++n)
            {
              if (a->p[n] != b->p[n])
                return 0;
            }
        }
      else
        {
          GSL_ERROR_VAL("unknown sparse matrix type", GSL_EINVAL, 0);
        }

      return 1;
    }
} /* gsl_spmatrix_equal() */
コード例 #7
0
static void
test_getset(const size_t M, const size_t N,
            const double density, const gsl_rng *r)
{
  int status;
  size_t i, j;

  /* test triplet versions of _get and _set */
  {
    const double val = 0.75;
    size_t k = 0;
    gsl_spmatrix *m = gsl_spmatrix_alloc(M, N);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double x = (double) ++k;
            double y;

            gsl_spmatrix_set(m, i, j, x);
            y = gsl_spmatrix_get(m, i, j);
            if (x != y)
              status = 1;
          }
      }

    gsl_test(status, "test_getset: M="F_ZU" N="F_ZU" _get != _set", M, N);

    /* test setting an element to 0 */
    gsl_spmatrix_set(m, 0, 0, 1.0);
    gsl_spmatrix_set(m, 0, 0, 0.0);

    status = gsl_spmatrix_get(m, 0, 0) != 0.0;
    gsl_test(status, "test_getset: M="F_ZU" N="F_ZU" m(0,0) = %f",
             M, N, gsl_spmatrix_get(m, 0, 0));

    /* test gsl_spmatrix_set_zero() */
    gsl_spmatrix_set(m, 0, 0, 1.0);
    gsl_spmatrix_set_zero(m);
    status = gsl_spmatrix_get(m, 0, 0) != 0.0;
    gsl_test(status, "test_getset: M="F_ZU" N="F_ZU" set_zero m(0,0) = %f",
             M, N, gsl_spmatrix_get(m, 0, 0));

    /* resassemble matrix to ensure nz is calculated correctly */
    k = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double x = (double) ++k;
            gsl_spmatrix_set(m, i, j, x);
          }
      }

    status = gsl_spmatrix_nnz(m) != M * N;
    gsl_test(status, "test_getset: M="F_ZU" N="F_ZU" set_zero nz = "F_ZU,
             M, N, gsl_spmatrix_nnz(m));

    /* test gsl_spmatrix_ptr() */
    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double mij = gsl_spmatrix_get(m, i, j);
            double *ptr = gsl_spmatrix_ptr(m, i, j);

            *ptr += val;
            if (gsl_spmatrix_get(m, i, j) != mij + val)
              status = 2;
          }
      }

    gsl_test(status == 2, "test_getset: M="F_ZU" N="F_ZU" triplet ptr", M, N);

    gsl_spmatrix_free(m);
  }

  /* test duplicate values are handled correctly */
  {
    size_t min = GSL_MIN(M, N);
    size_t expected_nnz = min;
    size_t nnz;
    size_t k = 0;
    gsl_spmatrix *m = gsl_spmatrix_alloc(M, N);

    status = 0;
    for (i = 0; i < min; ++i)
      {
        for (j = 0; j < 5; ++j)
          {
            double x = (double) ++k;
            double y;

            gsl_spmatrix_set(m, i, i, x);
            y = gsl_spmatrix_get(m, i, i);
            if (x != y)
              status = 1;
          }
      }

    gsl_test(status, "test_getset: duplicate test M="F_ZU" N="F_ZU" _get != _set", M, N);

    nnz = gsl_spmatrix_nnz(m);
    status = nnz != expected_nnz;
    gsl_test(status, "test_getset: duplicate test M="F_ZU" N="F_ZU" nnz="F_ZU", expected="F_ZU,
             M, N, nnz, expected_nnz);

    gsl_spmatrix_free(m);
  }

  /* test CCS version of gsl_spmatrix_get() */
  {
    const double val = 0.75;
    gsl_spmatrix *T = create_random_sparse(M, N, density, r);
    gsl_spmatrix *C = gsl_spmatrix_ccs(T);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double Tij = gsl_spmatrix_get(T, i, j);
            double Cij = gsl_spmatrix_get(C, i, j);
            double *ptr = gsl_spmatrix_ptr(C, i, j);

            if (Tij != Cij)
              status = 1;

            if (ptr)
              {
                *ptr += val;
                Cij = gsl_spmatrix_get(C, i, j);
                if (Tij + val != Cij)
                  status = 2;
              }
          }
      }

    gsl_test(status == 1, "test_getset: M="F_ZU" N="F_ZU" CCS get", M, N);
    gsl_test(status == 2, "test_getset: M="F_ZU" N="F_ZU" CCS ptr", M, N);

    gsl_spmatrix_free(T);
    gsl_spmatrix_free(C);
  }

  /* test CRS version of gsl_spmatrix_get() */
  {
    const double val = 0.75;
    gsl_spmatrix *T = create_random_sparse(M, N, density, r);
    gsl_spmatrix *C = gsl_spmatrix_crs(T);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double Tij = gsl_spmatrix_get(T, i, j);
            double Cij = gsl_spmatrix_get(C, i, j);
            double *ptr = gsl_spmatrix_ptr(C, i, j);

            if (Tij != Cij)
              status = 1;

            if (ptr)
              {
                *ptr += val;
                Cij = gsl_spmatrix_get(C, i, j);
                if (Tij + val != Cij)
                  status = 2;
              }
          }
      }

    gsl_test(status == 1, "test_getset: M="F_ZU" N="F_ZU" CRS get", M, N);
    gsl_test(status == 2, "test_getset: M="F_ZU" N="F_ZU" CRS ptr", M, N);

    gsl_spmatrix_free(T);
    gsl_spmatrix_free(C);
  }
} /* test_getset() */
コード例 #8
0
static void
test_transpose(const size_t M, const size_t N,
               const double density, const gsl_rng *r)
{
  int status;
  gsl_spmatrix *A = create_random_sparse(M, N, density, r);
  gsl_spmatrix *AT = gsl_spmatrix_alloc_nzmax(M, N, A->nz, A->sptype);
  gsl_spmatrix *AT2 = gsl_spmatrix_alloc_nzmax(M, N, A->nz, A->sptype);
  gsl_spmatrix *AT2_ccs, *AT2_crs;
  size_t i, j;

  /* test triplet transpose */

  gsl_spmatrix_memcpy(AT, A);
  gsl_spmatrix_memcpy(AT2, A);

  gsl_spmatrix_transpose(AT);
  gsl_spmatrix_transpose2(AT2);

  status = 0;
  for (i = 0; i < M; ++i)
    {
      for (j = 0; j < N; ++j)
        {
          double Aij = gsl_spmatrix_get(A, i, j);
          double ATji = gsl_spmatrix_get(AT, j, i);
          double AT2ji = gsl_spmatrix_get(AT2, j, i);

          if (Aij != ATji)
            status = 1;

          if (Aij != AT2ji)
            status = 2;
        }
    }

  gsl_test(status == 1, "test_transpose: transpose M="F_ZU" N="F_ZU" triplet format",
           M, N);
  gsl_test(status == 2, "test_transpose: transpose2 M="F_ZU" N="F_ZU" triplet format",
           M, N);

  /* test CCS transpose */

  AT2_ccs = gsl_spmatrix_ccs(A);
  gsl_spmatrix_transpose2(AT2_ccs);

  status = 0;
  for (i = 0; i < M; ++i)
    {
      for (j = 0; j < N; ++j)
        {
          double Aij = gsl_spmatrix_get(A, i, j);
          double AT2ji = gsl_spmatrix_get(AT2_ccs, j, i);

          if (Aij != AT2ji)
            status = 2;
        }
    }

  gsl_test(status == 2, "test_transpose: transpose2 M="F_ZU" N="F_ZU" CCS format",
           M, N);

  /* test CRS transpose */

  AT2_crs = gsl_spmatrix_crs(A);
  gsl_spmatrix_transpose2(AT2_crs);

  status = 0;
  for (i = 0; i < M; ++i)
    {
      for (j = 0; j < N; ++j)
        {
          double Aij = gsl_spmatrix_get(A, i, j);
          double AT2ji = gsl_spmatrix_get(AT2_crs, j, i);

          if (Aij != AT2ji)
            status = 2;
        }
    }

  gsl_test(status == 2, "test_transpose: transpose2 M="F_ZU" N="F_ZU" CRS format",
           M, N);

  gsl_spmatrix_free(A);
  gsl_spmatrix_free(AT);
  gsl_spmatrix_free(AT2);
  gsl_spmatrix_free(AT2_ccs);
  gsl_spmatrix_free(AT2_crs);
}
コード例 #9
0
static void
test_memcpy(const size_t M, const size_t N,
            const double density, const gsl_rng *r)
{
  int status;

  {
    gsl_spmatrix *A = create_random_sparse(M, N, density, r);
    gsl_spmatrix *A_ccs = gsl_spmatrix_ccs(A);
    gsl_spmatrix *A_crs = gsl_spmatrix_crs(A);
    gsl_spmatrix *B_t, *B_ccs, *B_crs;
  
    B_t = gsl_spmatrix_alloc(M, N);
    gsl_spmatrix_memcpy(B_t, A);

    status = gsl_spmatrix_equal(A, B_t) != 1;
    gsl_test(status, "test_memcpy: _memcpy M="F_ZU" N="F_ZU" triplet format", M, N);

    B_ccs = gsl_spmatrix_alloc_nzmax(M, N, A_ccs->nzmax, GSL_SPMATRIX_CCS);
    B_crs = gsl_spmatrix_alloc_nzmax(M, N, A_ccs->nzmax, GSL_SPMATRIX_CRS);

    gsl_spmatrix_memcpy(B_ccs, A_ccs);
    gsl_spmatrix_memcpy(B_crs, A_crs);

    status = gsl_spmatrix_equal(A_ccs, B_ccs) != 1;
    gsl_test(status, "test_memcpy: _memcpy M="F_ZU" N="F_ZU" CCS", M, N);

    status = gsl_spmatrix_equal(A_crs, B_crs) != 1;
    gsl_test(status, "test_memcpy: _memcpy M="F_ZU" N="F_ZU" CRS", M, N);

    gsl_spmatrix_free(A);
    gsl_spmatrix_free(A_ccs);
    gsl_spmatrix_free(A_crs);
    gsl_spmatrix_free(B_t);
    gsl_spmatrix_free(B_ccs);
    gsl_spmatrix_free(B_crs);
  }

  /* test transpose_memcpy */
  {
    gsl_spmatrix *A = create_random_sparse(M, N, density, r);
    gsl_spmatrix *AT = gsl_spmatrix_alloc(N, M);
    gsl_spmatrix *B = gsl_spmatrix_ccs(A);
    gsl_spmatrix *BT = gsl_spmatrix_alloc_nzmax(N, M, 1, GSL_SPMATRIX_CCS);
    gsl_spmatrix *C = gsl_spmatrix_crs(A);
    gsl_spmatrix *CT = gsl_spmatrix_alloc_nzmax(N, M, 1, GSL_SPMATRIX_CRS);
    size_t i, j;

    gsl_spmatrix_transpose_memcpy(AT, A);
    gsl_spmatrix_transpose_memcpy(BT, B);
    gsl_spmatrix_transpose_memcpy(CT, C);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double Aij = gsl_spmatrix_get(A, i, j);
            double ATji = gsl_spmatrix_get(AT, j, i);

            if (Aij != ATji)
              status = 1;
          }
      }

    gsl_test(status, "test_memcpy: _transpose_memcpy M="F_ZU" N="F_ZU" triplet format", M, N);

    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 BTji = gsl_spmatrix_get(BT, j, i);

            if ((Bij != BTji) || (Aij != Bij))
              status = 1;
          }
      }

    gsl_test(status, "test_memcpy: _transpose_memcpy M="F_ZU" N="F_ZU" CCS format", M, N);

    status = 0;
    for (i = 0; i < M; ++i)
      {
        for (j = 0; j < N; ++j)
          {
            double Aij = gsl_spmatrix_get(A, i, j);
            double Cij = gsl_spmatrix_get(C, i, j);
            double CTji = gsl_spmatrix_get(CT, j, i);

            if ((Cij != CTji) || (Aij != Cij))
              status = 1;
          }
      }

    gsl_test(status, "test_memcpy: _transpose_memcpy M="F_ZU" N="F_ZU" CRS format", M, N);

    gsl_spmatrix_free(A);
    gsl_spmatrix_free(AT);
    gsl_spmatrix_free(B);
    gsl_spmatrix_free(BT);
    gsl_spmatrix_free(C);
    gsl_spmatrix_free(CT);
  }
} /* test_memcpy() */