コード例 #1
0
ファイル: bml_allocate_ellpack.c プロジェクト: qmmd/bml
/** Deallocate a matrix.
 *
 * \ingroup allocate_group
 *
 * \param A The matrix.
 */
void
bml_deallocate_ellpack(
    bml_matrix_ellpack_t * A)
{
    bml_free_memory(A->value);
    bml_free_memory(A->index);
    bml_free_memory(A->nnz);
    bml_free_memory(A);
}
コード例 #2
0
ファイル: copy_matrix_typed.c プロジェクト: qmmd/bml
int TYPED_FUNC(
    test_copy) (
    const int N,
    const bml_matrix_type_t matrix_type,
    const bml_matrix_precision_t matrix_precision,
    const int M)
{
    bml_matrix_t *A = NULL;
    bml_matrix_t *B = NULL;
    bml_matrix_t *C = NULL;

    REAL_T *A_dense = NULL;
    REAL_T *B_dense = NULL;
    REAL_T *C_dense = NULL;

    A = bml_random_matrix(matrix_type, matrix_precision, N, M);
    B = bml_copy_new(A);
    C = bml_zero_matrix(matrix_type, matrix_precision, N, M);
    bml_copy(B, C);

    A_dense = bml_convert_to_dense(A, dense_row_major);
    B_dense = bml_convert_to_dense(B, dense_row_major);
    C_dense = bml_convert_to_dense(C, dense_row_major);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, A_dense, 0,
                           N, 0, N);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_dense, 0,
                           N, 0, N);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, C_dense, 0,
                           N, 0, N);
    for (int i = 0; i < N * N; i++)
    {
        if (ABS(A_dense[i] - B_dense[i]) > 1e-12 ||
            ABS(A_dense[i] - C_dense[i]) > 1e-12)
        {
            LOG_ERROR("matrices are not identical; A[%d] = %e\n", i,
                      A_dense[i]);
            return -1;
        }
    }
    bml_free_memory(A_dense);
    bml_free_memory(B_dense);
    bml_free_memory(C_dense);
    bml_deallocate(&A);
    bml_deallocate(&B);
    bml_deallocate(&C);

    LOG_INFO("copy matrix test passed\n");

    return 0;
}
コード例 #3
0
ファイル: normalize_matrix.c プロジェクト: aradi/bml
int
test_function(
    const int N,
    const bml_matrix_type_t matrix_type,
    const bml_matrix_precision_t matrix_precision,
    const int M)
{
    bml_matrix_t *A = NULL;
    bml_matrix_t *B = NULL;
    double *A_gbnd = NULL;
    double *B_gbnd = NULL;
    REAL_T *A_dense = NULL;
    REAL_T *B_dense = NULL;

    double scale_factor = 2.5;
    double threshold = 0.0;

    A = bml_identity_matrix(matrix_type, matrix_precision, N, M);
    bml_scale_inplace(scale_factor, A);
    A_gbnd = bml_gershgorin(A);

    A_dense = bml_export_to_dense(A, dense_row_major);
    A_dense[0] = scale_factor * scale_factor;
    B = bml_import_from_dense(matrix_type, matrix_precision, dense_row_major,
                              N, A_dense, threshold, M);
    B_gbnd = bml_gershgorin(B);

    bml_free_memory(A_dense);
    A_dense = bml_export_to_dense(A, dense_row_major);
    B_dense = bml_export_to_dense(B, dense_row_major);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, A_dense, 0,
                           N, 0, N);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_dense, 0,
                           N, 0, N);

    bml_normalize(B, B_gbnd[0], B_gbnd[1]);

    bml_free_memory(B_dense);
    B_dense = bml_export_to_dense(B, dense_row_major);
    bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_dense, 0,
                           N, 0, N);

    if ((fabs(A_gbnd[0] - scale_factor)) > REL_TOL || A_gbnd[1] > REL_TOL)
    {
        LOG_ERROR
            ("incorrect maxeval or maxminusmin; maxeval = %e maxminusmin = %e\n",
             A_gbnd[0], A_gbnd[1]);
        return -1;
    }

    if ((fabs(B_gbnd[0] - scale_factor * scale_factor)) > REL_TOL ||
        (fabs(B_gbnd[1] - (scale_factor * scale_factor - scale_factor))) >
        REL_TOL)
    {
        LOG_ERROR
            ("incorrect maxeval or maxminusmin; maxeval = %e maxminusmin = %e\n",
             B_gbnd[0], B_gbnd[1]);
        return -1;
    }

    if (fabs(B_dense[0]) > REL_TOL)
    {
        LOG_ERROR
            ("normalize error, incorrect maxeval or maxminusmin; maxeval = %e maxminusmin = %e\n",
             B_gbnd[0], B_gbnd[1]);
        return -1;
    }

    LOG_INFO("normalize matrix test passed\n");
    bml_free_memory(A_dense);
    bml_free_memory(B_dense);
    bml_free_memory(A_gbnd);
    bml_free_memory(B_gbnd);
    bml_deallocate(&A);
    bml_deallocate(&B);

    return 0;
}