CSparse is a C library which implements a number of direct methods for sparse linear systems, by Timothy Davis.
Csparse libary can be used form tdavis/csparse, this block is generated from this github repo.
With this example you can read a matrix saved in a file and solve a linear system.
You need cs_demo.h and cs_demo.c to encampsule some functions to use in the example and a matrix file t1. Then, cs_demo2.c implements the main function. Let’s do it!
$ bii init csparse_example -L
$ cd csparse_example
$ # copy the files below
cs_demo.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include "cs.h"
typedef struct problem_struct
{
cs *A ;
cs *C ;
csi sym ;
double *x ;
double *b ;
double *resid ;
} problem ;
problem *get_problem (FILE *f, double tol) ;
csi demo2 (problem *Prob) ;
csi demo3 (problem *Prob) ;
problem *free_problem (problem *Prob) ;
|
cs_demo.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | #include "cs_demo.h"
#include <time.h>
/* 1 if A is square & upper tri., -1 if square & lower tri., 0 otherwise */
static csi is_sym (cs *A)
{
csi is_upper, is_lower, j, p, n = A->n, m = A->m, *Ap = A->p, *Ai = A->i ;
if (m != n) return (0) ;
is_upper = 1 ;
is_lower = 1 ;
for (j = 0 ; j < n ; j++)
{
for (p = Ap [j] ; p < Ap [j+1] ; p++)
{
if (Ai [p] > j) is_upper = 0 ;
if (Ai [p] < j) is_lower = 0 ;
}
}
return (is_upper ? 1 : (is_lower ? -1 : 0)) ;
}
/* true for off-diagonal entries */
static csi dropdiag (csi i, csi j, double aij, void *other) { return (i != j) ;}
/* C = A + triu(A,1)' */
static cs *make_sym (cs *A)
{
cs *AT, *C ;
AT = cs_transpose (A, 1) ; /* AT = A' */
cs_fkeep (AT, &dropdiag, NULL) ; /* drop diagonal entries from AT */
C = cs_add (A, AT, 1, 1) ; /* C = A+AT */
cs_spfree (AT) ;
return (C) ;
}
/* create a right-hand side */
static void rhs (double *x, double *b, csi m)
{
csi i ;
for (i = 0 ; i < m ; i++) b [i] = 1 + ((double) i) / m ;
for (i = 0 ; i < m ; i++) x [i] = b [i] ;
}
/* infinity-norm of x */
static double norm (double *x, csi n)
{
csi i ;
double normx = 0 ;
for (i = 0 ; i < n ; i++) normx = CS_MAX (normx, fabs (x [i])) ;
return (normx) ;
}
/* compute residual, norm(A*x-b,inf) / (norm(A,1)*norm(x,inf) + norm(b,inf)) */
static void print_resid (csi ok, cs *A, double *x, double *b, double *resid)
{
csi i, m, n ;
if (!ok) { printf (" (failed)\n") ; return ; }
m = A->m ; n = A->n ;
for (i = 0 ; i < m ; i++) resid [i] = -b [i] ; /* resid = -b */
cs_gaxpy (A, x, resid) ; /* resid = resid + A*x */
printf ("resid: %8.2e\n", norm (resid,m) / ((n == 0) ? 1 :
(cs_norm (A) * norm (x,n) + norm (b,m)))) ;
}
static double tic (void) { return (clock () / (double) CLOCKS_PER_SEC) ; }
static double toc (double t) { double s = tic () ; return (CS_MAX (0, s-t)) ; }
static void print_order (csi order)
{
switch (order)
{
case 0: printf ("natural ") ; break ;
case 1: printf ("amd(A+A') ") ; break ;
case 2: printf ("amd(S'*S) ") ; break ;
case 3: printf ("amd(A'*A) ") ; break ;
}
}
/* read a problem from a file; use %g for integers to avoid csi conflicts */
problem *get_problem (FILE *f, double tol)
{
cs *T, *A, *C ;
csi sym, m, n, mn, nz1, nz2 ;
problem *Prob ;
Prob = cs_calloc (1, sizeof (problem)) ;
if (!Prob) return (NULL) ;
T = cs_load (f) ; /* load triplet matrix T from a file */
Prob->A = A = cs_compress (T) ; /* A = compressed-column form of T */
cs_spfree (T) ; /* clear T */
if (!cs_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_dropzeros (A) ; /* drop zero entries */
nz2 = A->p [n] ;
if (tol > 0) cs_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_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_malloc (mn, sizeof (double)) ;
Prob->x = cs_malloc (mn, sizeof (double)) ;
Prob->resid = cs_malloc (mn, sizeof (double)) ;
return ((!Prob->b || !Prob->x || !Prob->resid) ? free_problem (Prob) : Prob) ;
}
/* free a problem */
problem *free_problem (problem *Prob)
{
if (!Prob) return (NULL) ;
cs_spfree (Prob->A) ;
if (Prob->sym) cs_spfree (Prob->C) ;
cs_free (Prob->b) ;
cs_free (Prob->x) ;
cs_free (Prob->resid) ;
return (cs_free (Prob)) ;
}
/* solve a linear system using Cholesky, LU, and QR, with various orderings */
csi demo2 (problem *Prob)
{
cs *A, *C ;
double *b, *x, *resid, t, tol ;
csi k, m, n, ok, order, nb, ns, *r, *s, *rr, sprank ;
csd *D ;
if (!Prob) return (0) ;
A = Prob->A ; C = Prob->C ; b = Prob->b ; x = Prob->x ; resid = Prob->resid;
m = A->m ; n = A->n ;
tol = Prob->sym ? 0.001 : 1 ; /* partial pivoting tolerance */
D = cs_dmperm (C, 1) ; /* randomized dmperm analysis */
if (!D) return (0) ;
nb = D->nb ; r = D->r ; s = D->s ; rr = D->rr ;
sprank = rr [3] ;
for (ns = 0, k = 0 ; k < nb ; k++)
{
ns += ((r [k+1] == r [k]+1) && (s [k+1] == s [k]+1)) ;
}
printf ("blocks: %g singletons: %g structural rank: %g\n",
(double) nb, (double) ns, (double) sprank) ;
cs_dfree (D) ;
for (order = 0 ; order <= 3 ; order += 3) /* natural and amd(A'*A) */
{
if (!order && m > 1000) continue ;
printf ("QR ") ;
print_order (order) ;
rhs (x, b, m) ; /* compute right-hand side */
t = tic () ;
ok = cs_qrsol (order, C, x) ; /* min norm(Ax-b) with QR */
printf ("time: %8.2f ", toc (t)) ;
print_resid (ok, C, x, b, resid) ; /* print residual */
}
if (m != n || sprank < n) return (1) ; /* return if rect. or singular*/
for (order = 0 ; order <= 3 ; order++) /* try all orderings */
{
if (!order && m > 1000) continue ;
printf ("LU ") ;
print_order (order) ;
rhs (x, b, m) ; /* compute right-hand side */
t = tic () ;
ok = cs_lusol (order, C, x, tol) ; /* solve Ax=b with LU */
printf ("time: %8.2f ", toc (t)) ;
print_resid (ok, C, x, b, resid) ; /* print residual */
}
if (!Prob->sym) return (1) ;
for (order = 0 ; order <= 1 ; order++) /* natural and amd(A+A') */
{
if (!order && m > 1000) continue ;
printf ("Chol ") ;
print_order (order) ;
rhs (x, b, m) ; /* compute right-hand side */
t = tic () ;
ok = cs_cholsol (order, C, x) ; /* solve Ax=b with Cholesky */
printf ("time: %8.2f ", toc (t)) ;
print_resid (ok, C, x, b, resid) ; /* print residual */
}
return (1) ;
}
/* free workspace for demo3 */
static csi done3 (csi ok, css *S, csn *N, double *y, cs *W, cs *E, csi *p)
{
cs_sfree (S) ;
cs_nfree (N) ;
cs_free (y) ;
cs_spfree (W) ;
cs_spfree (E) ;
cs_free (p) ;
return (ok) ;
}
/* Cholesky update/downdate */
csi demo3 (problem *Prob)
{
cs *A, *C, *W = NULL, *WW, *WT, *E = NULL, *W2 ;
csi n, k, *Li, *Lp, *Wi, *Wp, p1, p2, *p = NULL, ok ;
double *b, *x, *resid, *y = NULL, *Lx, *Wx, s, t, t1 ;
css *S = NULL ;
csn *N = NULL ;
if (!Prob || !Prob->sym || Prob->A->n == 0) return (0) ;
A = Prob->A ; C = Prob->C ; b = Prob->b ; x = Prob->x ; resid = Prob->resid;
n = A->n ;
if (!Prob->sym || n == 0) return (1) ;
rhs (x, b, n) ; /* compute right-hand side */
printf ("\nchol then update/downdate ") ;
print_order (1) ;
y = cs_malloc (n, sizeof (double)) ;
t = tic () ;
S = cs_schol (1, C) ; /* symbolic Chol, amd(A+A') */
printf ("\nsymbolic chol time %8.2f\n", toc (t)) ;
t = tic () ;
N = cs_chol (C, S) ; /* numeric Cholesky */
printf ("numeric chol time %8.2f\n", toc (t)) ;
if (!S || !N || !y) return (done3 (0, S, N, y, W, E, p)) ;
t = tic () ;
cs_ipvec (S->pinv, b, y, n) ; /* y = P*b */
cs_lsolve (N->L, y) ; /* y = L\y */
cs_ltsolve (N->L, y) ; /* y = L'\y */
cs_pvec (S->pinv, y, x, n) ; /* x = P'*y */
printf ("solve chol time %8.2f\n", toc (t)) ;
printf ("original: ") ;
print_resid (1, C, x, b, resid) ; /* print residual */
k = n/2 ; /* construct W */
W = cs_spalloc (n, 1, n, 1, 0) ;
if (!W) return (done3 (0, S, N, y, W, E, p)) ;
Lp = N->L->p ; Li = N->L->i ; Lx = N->L->x ;
Wp = W->p ; Wi = W->i ; Wx = W->x ;
Wp [0] = 0 ;
p1 = Lp [k] ;
Wp [1] = Lp [k+1] - p1 ;
s = Lx [p1] ;
srand (1) ;
for ( ; p1 < Lp [k+1] ; p1++)
{
p2 = p1 - Lp [k] ;
Wi [p2] = Li [p1] ;
Wx [p2] = s * rand () / ((double) RAND_MAX) ;
}
t = tic () ;
ok = cs_updown (N->L, +1, W, S->parent) ; /* update: L*L'+W*W' */
t1 = toc (t) ;
printf ("update: time: %8.2f\n", t1) ;
if (!ok) return (done3 (0, S, N, y, W, E, p)) ;
t = tic () ;
cs_ipvec (S->pinv, b, y, n) ; /* y = P*b */
cs_lsolve (N->L, y) ; /* y = L\y */
cs_ltsolve (N->L, y) ; /* y = L'\y */
cs_pvec (S->pinv, y, x, n) ; /* x = P'*y */
t = toc (t) ;
p = cs_pinv (S->pinv, n) ;
W2 = cs_permute (W, p, NULL, 1) ; /* E = C + (P'W)*(P'W)' */
WT = cs_transpose (W2,1) ;
WW = cs_multiply (W2, WT) ;
cs_spfree (WT) ;
cs_spfree (W2) ;
E = cs_add (C, WW, 1, 1) ;
cs_spfree (WW) ;
if (!E || !p) return (done3 (0, S, N, y, W, E, p)) ;
printf ("update: time: %8.2f (incl solve) ", t1+t) ;
print_resid (1, E, x, b, resid) ; /* print residual */
cs_nfree (N) ; /* clear N */
t = tic () ;
N = cs_chol (E, S) ; /* numeric Cholesky */
if (!N) return (done3 (0, S, N, y, W, E, p)) ;
cs_ipvec (S->pinv, b, y, n) ; /* y = P*b */
cs_lsolve (N->L, y) ; /* y = L\y */
cs_ltsolve (N->L, y) ; /* y = L'\y */
cs_pvec (S->pinv, y, x, n) ; /* x = P'*y */
t = toc (t) ;
printf ("rechol: time: %8.2f (incl solve) ", t) ;
print_resid (1, E, x, b, resid) ; /* print residual */
t = tic () ;
ok = cs_updown (N->L, -1, W, S->parent) ; /* downdate: L*L'-W*W' */
t1 = toc (t) ;
if (!ok) return (done3 (0, S, N, y, W, E, p)) ;
printf ("downdate: time: %8.2f\n", t1) ;
t = tic () ;
cs_ipvec (S->pinv, b, y, n) ; /* y = P*b */
cs_lsolve (N->L, y) ; /* y = L\y */
cs_ltsolve (N->L, y) ; /* y = L'\y */
cs_pvec (S->pinv, y, x, n) ; /* x = P'*y */
t = toc (t) ;
printf ("downdate: time: %8.2f (incl solve) ", t1+t) ;
print_resid (1, C, x, b, resid) ; /* print residual */
return (done3 (1, S, N, y, W, E, p)) ;
}
|
cs_demo2.c
1 2 3 4 5 6 7 8 9 | #include "cs_demo.h"
/* cs_demo2: read a matrix and solve a linear system */
int main (void)
{
problem *Prob = get_problem (stdin, 1e-14) ;
demo2 (Prob) ;
free_problem (Prob) ;
return (0) ;
}
|
Place t1 file in a new folder called Matrix:
Matrix/t1
1 2 3 4 5 6 7 8 9 10 | 2 2 3.0
1 0 3.1
3 3 1.0
0 2 3.2
1 1 2.9
3 0 3.5
3 1 0.4
1 3 0.9
0 0 4.5
2 1 1.7
|
Create a biicode.conf file in the project folder. Add your [requirements]
depending on tdavis/csparse
and map your [includes]
:
[requirements]
tdavis/csparse: 1
[includes]
*.h: tdavis/csparse/Include
Check with bii deps to show all dependencies are resolved.
Now, build and run the example
$ bii build
$ cd bin
$ your_user_csparse_example_cs_demo2 < ../Matrix/t1
$ # NOTE "your_user" should be your user's name
--- Matrix: 4-by-4, nnz: 10 (sym: 0: nnz 0), norm: 1.11e+001
blocks: 1 singletons: 0 structural rank: 4
QR natural time: 0.00 resid: 1.15e-017
QR amd(A'*A) time: 0.00 resid: 1.53e-017
LU natural time: 0.00 resid: 1.04e-017
LU amd(A+A') time: 0.00 resid: 4.94e-018
LU amd(S'*S) time: 0.00 resid: 4.94e-018
LU amd(A'*A) time: 0.00 resid: 4.94e-018
You can check all the csparse examples in examples/csparse block.
Give it a quick try following the next steps.
Create a new project and open the examples.
~$ bii init csparse_example
~$ cd csparse_example
~$ bii open examples/csparse
~$ bii build
Execute any you want, for example, read a matrix saved in a file and solve a linear system:
$ cd bin
$ examples_csparse_cs_demo2 < ../blocks/examples/csparse/Matrix/t1
--- Matrix: 4-by-4, nnz: 10 (sym: 0: nnz 0), norm: 1.11e+001
blocks: 1 singletons: 0 structural rank: 4
QR natural time: 0.00 resid: 1.15e-017
QR amd(A'*A) time: 0.00 resid: 1.53e-017
LU natural time: 0.00 resid: 1.04e-017
LU amd(A+A') time: 0.00 resid: 4.94e-018
LU amd(S'*S) time: 0.00 resid: 4.94e-018
LU amd(A'*A) time: 0.00 resid: 4.94e-018