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() */
static gsl_spmatrix * create_random_sparse(const size_t M, const size_t N, const double density, const gsl_rng *r) { size_t nnzwanted = (size_t) floor(M * N * GSL_MIN(density, 1.0)); gsl_spmatrix *m = gsl_spmatrix_alloc_nzmax(M, N, nnzwanted, GSL_SPMATRIX_TRIPLET); 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); } while (gsl_spmatrix_nnz(m) < nnzwanted) { /* generate a random row and column */ size_t i = gsl_rng_uniform(r) * M; size_t j = gsl_rng_uniform(r) * N; /* generate random m_{ij} and add it */ double x = gsl_rng_uniform(r); gsl_spmatrix_set(m, i, j, x); } return m; } /* create_random_sparse() */
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() */
gsl_spmatrix * gsl_spmatrix_ccs(const gsl_spmatrix *T) { if (!GSL_SPMATRIX_ISTRIPLET(T)) { GSL_ERROR_NULL("matrix must be in triplet format", GSL_EINVAL); } else { const size_t *Tj; /* column indices of triplet matrix */ size_t *Cp; /* column pointers of compressed column matrix */ size_t *w; /* copy of column pointers */ gsl_spmatrix *m; size_t n; m = gsl_spmatrix_alloc_nzmax(T->size1, T->size2, T->nz, GSL_SPMATRIX_CCS); if (!m) return NULL; Tj = T->p; Cp = m->p; /* initialize column pointers to 0 */ for (n = 0; n < m->size2 + 1; ++n) Cp[n] = 0; /* * compute the number of elements in each column: * Cp[j] = # non-zero elements in column j */ for (n = 0; n < T->nz; ++n) Cp[Tj[n]]++; /* compute column pointers: p[j] = p[j-1] + nnz[j-1] */ gsl_spmatrix_cumsum(m->size2, Cp); /* make a copy of the column pointers */ w = (size_t *) m->work; for (n = 0; n < m->size2; ++n) w[n] = Cp[n]; /* transfer data from triplet format to CCS */ for (n = 0; n < T->nz; ++n) { size_t k = w[Tj[n]]++; m->i[k] = T->i[n]; m->data[k] = T->data[n]; } m->nz = T->nz; return m; } }
gsl_spmatrix * gsl_spmatrix_alloc(const size_t n1, const size_t n2) { const double density = 0.1; /* estimate */ size_t nzmax = (size_t) round(n1 * n2 * density); if (nzmax == 0) nzmax = 10; return gsl_spmatrix_alloc_nzmax(n1, n2, nzmax, GSL_SPMATRIX_TRIPLET); } /* gsl_spmatrix_alloc() */
static gsl_spmatrix * create_random_sparse(const size_t M, const size_t N, const double density, const gsl_rng *r) { size_t nnzwanted = (size_t) floor(M * N * GSL_MIN(density, 1.0)); gsl_spmatrix *m = gsl_spmatrix_alloc_nzmax(M, N, nnzwanted, GSL_SPMATRIX_TRIPLET); while (gsl_spmatrix_nnz(m) < nnzwanted) { /* generate a random row and column */ size_t i = gsl_rng_uniform(r) * M; size_t j = gsl_rng_uniform(r) * N; /* generate random m_{ij} and add it */ double x = gsl_rng_uniform(r); gsl_spmatrix_set(m, i, j, x, 0); } return m; } /* create_random_sparse() */
static gsl_spmatrix * create_random_sparse_int(const size_t M, const size_t N, const double density, const gsl_rng *r) { const double lower = 1.0; const double upper = 10.0; size_t nnzwanted = (size_t) floor(M * N * GSL_MIN(density, 1.0)); gsl_spmatrix *m = gsl_spmatrix_alloc_nzmax(M, N, nnzwanted, GSL_SPMATRIX_TRIPLET); while (gsl_spmatrix_nnz(m) < nnzwanted) { /* generate a random row and column */ size_t i = gsl_rng_uniform(r) * M; size_t j = gsl_rng_uniform(r) * N; /* generate random m_{ij} and add it */ int x = (int) (gsl_rng_uniform(r) * (upper - lower) + lower); gsl_spmatrix_set(m, i, j, (double) x); } return m; }
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; }
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() */
static void test_io_binary(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, *A_crs; char filename[] = "test.XXXXXX"; #if !defined( _WIN32 ) int fd = mkstemp(filename); #else char * fd = _mktemp(filename); # define fdopen fopen #endif /* test triplet I/O */ { FILE *f = fdopen(fd, "wb"); gsl_spmatrix_fwrite(f, A); fclose(f); } { FILE *f = fopen(filename, "rb"); gsl_spmatrix *B = gsl_spmatrix_alloc_nzmax(M, N, A->nz, A->sptype); gsl_spmatrix_fread(f, B); status = gsl_spmatrix_equal(A, B) != 1; gsl_test(status, "test_io_binary: fwrite/fread M="F_ZU" N="F_ZU" triplet format", M, N); fclose(f); gsl_spmatrix_free(B); } /* test CCS I/O */ A_ccs = gsl_spmatrix_ccs(A); { FILE *f = fopen(filename, "wb"); gsl_spmatrix_fwrite(f, A_ccs); fclose(f); } { FILE *f = fopen(filename, "rb"); gsl_spmatrix *B = gsl_spmatrix_alloc_nzmax(M, N, A->nz, GSL_SPMATRIX_CCS); gsl_spmatrix_fread(f, B); status = gsl_spmatrix_equal(A_ccs, B) != 1; gsl_test(status, "test_io_binary: fwrite/fread M="F_ZU" N="F_ZU" CCS format", M, N); fclose(f); gsl_spmatrix_free(B); } /* test CRS I/O */ A_crs = gsl_spmatrix_crs(A); { FILE *f = fopen(filename, "wb"); gsl_spmatrix_fwrite(f, A_crs); fclose(f); } { FILE *f = fopen(filename, "rb"); gsl_spmatrix *B = gsl_spmatrix_alloc_nzmax(M, N, A->nz, GSL_SPMATRIX_CRS); gsl_spmatrix_fread(f, B); status = gsl_spmatrix_equal(A_crs, B) != 1; gsl_test(status, "test_io_binary: fwrite/fread M="F_ZU" N="F_ZU" CRS format", M, N); fclose(f); gsl_spmatrix_free(B); } unlink(filename); gsl_spmatrix_free(A); gsl_spmatrix_free(A_ccs); gsl_spmatrix_free(A_crs); }
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); }
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() */
int main (void) { const size_t p = 2000; const size_t n = p + 1; gsl_vector *f = gsl_vector_alloc(n); gsl_vector *x = gsl_vector_alloc(p); /* allocate sparse Jacobian matrix with 2*p non-zero elements in triplet format */ gsl_spmatrix *J = gsl_spmatrix_alloc_nzmax(n, p, 2 * p, GSL_SPMATRIX_TRIPLET); gsl_multilarge_nlinear_fdf fdf; gsl_multilarge_nlinear_parameters fdf_params = gsl_multilarge_nlinear_default_parameters(); struct model_params params; size_t i; params.alpha = 1.0e-5; params.J = J; /* define function to be minimized */ fdf.f = penalty_f; fdf.df = penalty_df; fdf.fvv = penalty_fvv; fdf.n = n; fdf.p = p; fdf.params = ¶ms; for (i = 0; i < p; ++i) { /* starting point */ gsl_vector_set(x, i, i + 1.0); /* store sqrt(alpha)*I_p in upper p-by-p block of J */ gsl_spmatrix_set(J, i, i, sqrt(params.alpha)); } fprintf(stderr, "%-25s %-4s %-4s %-5s %-6s %-4s %-10s %-10s %-7s %-11s %-10s\n", "Method", "NITER", "NFEV", "NJUEV", "NJTJEV", "NAEV", "Init Cost", "Final cost", "cond(J)", "Final |x|^2", "Time (s)"); fdf_params.scale = gsl_multilarge_nlinear_scale_levenberg; fdf_params.trs = gsl_multilarge_nlinear_trs_lm; solve_system(x, &fdf, &fdf_params); fdf_params.trs = gsl_multilarge_nlinear_trs_lmaccel; solve_system(x, &fdf, &fdf_params); fdf_params.trs = gsl_multilarge_nlinear_trs_dogleg; solve_system(x, &fdf, &fdf_params); fdf_params.trs = gsl_multilarge_nlinear_trs_ddogleg; solve_system(x, &fdf, &fdf_params); fdf_params.trs = gsl_multilarge_nlinear_trs_cgst; solve_system(x, &fdf, &fdf_params); gsl_vector_free(f); gsl_vector_free(x); gsl_spmatrix_free(J); return 0; }