void test_compress(const size_t M, const size_t N, const double density, const gsl_rng *r) { int status; size_t i, j; gsl_spmatrix *m, *ccs, *crs, *ccstr; m = create_random_sparse(M, N, density, r); // Compress column sum duplicates ccs = gsl_spmatrix_compress(m, GSL_SPMATRIX_CCS); // Compress row sum duplicates crs = gsl_spmatrix_compress(m, GSL_SPMATRIX_CRS); status = 0; for (i = 0; i < ccs->size1; i++) for (j = 0; j < ccs->size2; j++) if (gsl_spmatrix_get(crs, i, j) != gsl_spmatrix_get(ccs, i, j)) status = 1; gsl_test(status, "test_compress: _compress at M=%zu, N=%zu", M, N); return; // Transpose in place by changing major gsl_spmatrix_transpose(crs); status = 0; for (i = 0; i < crs->size1; i++) for (j = 0; j < crs->size2; j++) if (gsl_spmatrix_get(crs, i, j) != gsl_spmatrix_get(ccs, j, i)) status = 1; gsl_test(status, "test_compress: transpose inplace at M=%zu, N=%zu", M, N); gsl_spmatrix_transpose(crs); // Convert by transpose copy gsl_spmatrix_switch_major(crs, ccs); status = 0; for (i = 0; i < ccs->size1; i++) for (j = 0; j < ccs->size2; j++) if (gsl_spmatrix_get(crs, i, j) != gsl_spmatrix_get(ccs, i, j)) status = 1; gsl_test(status, "test_compress: _switch_major at M=%zu, N=%zu", M, N); gsl_spmatrix_free(m); gsl_spmatrix_free(ccs); gsl_spmatrix_free(crs); gsl_spmatrix_free(ccstr); return; }
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); }