Пример #1
0
/* read a problem from a file; use %g for integers to avoid cs_long_t conflicts */
problem *get_problem (FILE *f, double tol)
{
    cs_cl *T, *A, *C ;
    cs_long_t sym, m, n, mn, nz1, nz2 ;
    problem *Prob ;
    Prob = cs_cl_calloc (1, sizeof (problem)) ;
    if (!Prob) return (NULL) ;
    T = cs_cl_load (f) ;                   /* load triplet matrix T from a file */
    Prob->A = A = cs_cl_compress (T) ;     /* A = compressed-column form of T */
    cs_cl_spfree (T) ;                     /* clear T */
    if (!cs_cl_dupl (A)) return (free_problem (Prob)) ; /* sum up duplicates */
    Prob->sym = sym = is_sym (A) ;      /* determine if A is symmetric */
    m = A->m ; n = A->n ;
    mn = CS_MAX (m,n) ;
    nz1 = A->p [n] ;
    cs_cl_dropzeros (A) ;                  /* drop zero entries */
    nz2 = A->p [n] ;
    if (tol > 0) cs_cl_droptol (A, tol) ;  /* drop tiny entries (just to test) */
    Prob->C = C = sym ? make_sym (A) : A ;  /* C = A + triu(A,1)', or C=A */
    if (!C) return (free_problem (Prob)) ;
    printf ("\n--- Matrix: %g-by-%g, nnz: %g (sym: %g: nnz %g), norm: %8.2e\n",
            (double) m, (double) n, (double) (A->p [n]), (double) sym,
            (double) (sym ? C->p [n] : 0), cs_cl_norm (C)) ;
    if (nz1 != nz2) printf ("zero entries dropped: %g\n", (double) (nz1 - nz2));
    if (nz2 != A->p [n]) printf ("tiny entries dropped: %g\n",
            (double) (nz2 - A->p [n])) ;
    Prob->b = cs_cl_malloc (mn, sizeof (cs_complex_t)) ;
    Prob->x = cs_cl_malloc (mn, sizeof (cs_complex_t)) ;
    Prob->resid = cs_cl_malloc (mn, sizeof (cs_complex_t)) ;
    return ((!Prob->b || !Prob->x || !Prob->resid) ? free_problem (Prob) : Prob) ;
}
Пример #2
0
int main (void)
{
    cs_cl *T, *A, *Eye, *AT, *C, *D ;
    cs_long_t i, m ;
    T = cs_cl_load (stdin) ;               /* load triplet matrix T from stdin */
    printf ("T:\n") ; cs_cl_print (T, 0) ; /* print T */
    A = cs_cl_compress (T) ;               /* A = compressed-column form of T */
    printf ("A:\n") ; cs_cl_print (A, 0) ; /* print A */
    cs_cl_spfree (T) ;                     /* clear T */
    AT = cs_cl_transpose (A, 1) ;          /* AT = A' */
    printf ("AT:\n") ; cs_cl_print (AT, 0) ; /* print AT */
    m = A ? A->m : 0 ;                  /* m = # of rows of A */
    T = cs_cl_spalloc (m, m, m, 1, 1) ;    /* create triplet identity matrix */
    for (i = 0 ; i < m ; i++) cs_cl_entry (T, i, i, 1) ;
    Eye = cs_cl_compress (T) ;             /* Eye = speye (m) */
    cs_cl_spfree (T) ;
    C = cs_cl_multiply (A, AT) ;           /* C = A*A' */
    D = cs_cl_add (C, Eye, 1, cs_cl_norm (C)) ;   /* D = C + Eye*norm (C,1) */
    printf ("D:\n") ; cs_cl_print (D, 0) ; /* print D */
    cs_cl_spfree (A) ;                     /* clear A AT C D Eye */
    cs_cl_spfree (AT) ;
    cs_cl_spfree (C) ;
    cs_cl_spfree (D) ;
    cs_cl_spfree (Eye) ;
    return (0) ;
}