/* solve a linear system using Cholesky, LU, and QR, with various orderings */ cs_long_t demo2 (problem *Prob) { cs_cl *A, *C ; cs_complex_t *b, *x, *resid ; double t, tol ; cs_long_t k, m, n, ok, order, nb, ns, *r, *s, *rr, sprank ; cs_cld *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_cl_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_cl_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_cl_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_cl_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_cl_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) ; }
int main() { FILE *fp; Order *o; fp = fopen(filename, "wb"); o = new_order(1, 100, 3); print_order(o); fwrite(o, sizeof(Order), 1, fp); free(o); o = new_order(2, 50, 5); print_order(o); fwrite(o, sizeof(Order), 1, fp); free(o); fclose(fp); Order o2; fp = fopen(filename, "rb"); fread(&o2, sizeof(Order), 1, fp); print_order(&o2); fread(&o2, sizeof(Order), 1, fp); print_order(&o2); fclose(fp); fp = fopen(filename, "rb"); int id, price, num; fread(&id, sizeof(int), 1, fp); fread(&price, sizeof(int), 1, fp); fread(&num, sizeof(int), 1, fp); print(id, price, num); fread(&id, sizeof(int), 1, fp); fread(&price, sizeof(int), 1, fp); fread(&num, sizeof(int), 1, fp); print(id, price, num); fclose(fp); void *o3 = new_order(10, 1000, 2); id = *(int*)(o3 + 0); price = *(int*)(o3 + 4); num = *(int*)(o3 + 8); print(id, price, num); free(o3); return 0; }
/* * used by the next function to write * bit of the integer in the right order .... */ static void print_order(unsigned int x, int nb, int b){ if (nb<=0) return; print_order(x/b, nb-1, b); printf("%u",x%b); return; }
/* * print nb digits of the chain x in base "b" * b must be between 1 and 10 */ static void print_integer(unsigned int x, int b, int nb){ if ((b < 2)||(b>16)){ fprintf(stderr," ERROR: You musn't print number with a base larger than 10 or less than 2 \n"); return; } print_order(x, nb, b); return; }
void vect_write (vector<Order>& v, string filename){ //Writes a file from a vector ofstream os(filename.c_str()); vector<Order>::iterator check = v.end(); --check; //See list_write comment on check for(vector<Order>::iterator iter = v.begin(); iter < v.end(); ++iter){ os << print_order(*iter); if(iter != check) os << endl; } }
void list_write(list<Order> l, string filename){ //Writes a file from a list of orders ofstream os(filename.c_str()); list<Order>::iterator check = l.end(); --check; //Check points to the second to last list element; this way there will not be a trailing empty line, which would create issues for(list<Order>::iterator iter = l.begin(); iter != l.end(); ++iter){ os << print_order(*iter); if(iter != check) os << endl; } }
int main(void) { struct entry entries[6]; int i; printf("input id and nature.\n"); for (i = 0; i < 6; i ++) { scanf("%d %d", &entries[i].id, &entries[i].nature); } int ns[] = {0, 4, 5}; for (i = 0; i < 3; i ++) { int n = ns[i]; int row; for (row = 0; row < 65536; row ++) { uint seed = (uint)entries[n].id << 16 | row; if (is_match(entries, seed)) { printf("0x%.8x", seed); printf(" ("); print_order(entries, seed); printf(")\n"); } } } printf("finish\n"); return 0; }
/* Cholesky update/downdate */ int demo3 (problem *Prob) { cs *A, *C, *W = NULL, *WW, *WT, *E = NULL, *W2 ; int 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)) ; }
int main( void ) { test_init(); int i=1; /* Simple insert/current test */ printf("--test %d\n", i++); vlc_epg_t *p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 42, 20, "A" ); EPG_ADD( p_epg, 62, 20, "B" ); EPG_ADD( p_epg, 82, 20, "C" ); EPG_ADD( p_epg, 102, 20, "D" ); print_order( p_epg ); assert_events( p_epg, "ABCD", 4 ); assert_current( p_epg, NULL ); vlc_epg_SetCurrent( p_epg, 82 ); assert_current( p_epg, "C" ); vlc_epg_Delete( p_epg ); /* Test reordering / head/tail inserts */ printf("--test %d\n", i++); p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 82, 20, "C" ); EPG_ADD( p_epg, 62, 20, "B" ); EPG_ADD( p_epg, 102, 20, "D" ); EPG_ADD( p_epg, 42, 20, "A" ); print_order( p_epg ); assert_events( p_epg, "ABCD", 4 ); vlc_epg_Delete( p_epg ); /* Test reordering/bisect lookup on insert */ printf("--test %d\n", i++); p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 142, 20, "F" ); EPG_ADD( p_epg, 122, 20, "E" ); EPG_ADD( p_epg, 102, 20, "D" ); EPG_ADD( p_epg, 82, 20, "C" ); EPG_ADD( p_epg, 42, 20, "A" ); EPG_ADD( p_epg, 62, 20, "B" ); print_order( p_epg ); assert_events( p_epg, "ABCDEF", 6 ); vlc_epg_Delete( p_epg ); /* Test deduplication and current pointer rebasing on insert */ printf("--test %d\n", i++); p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 62, 20, "E" ); EPG_ADD( p_epg, 62, 20, "F" ); EPG_ADD( p_epg, 42, 20, "A" ); vlc_epg_SetCurrent( p_epg, 62 ); EPG_ADD( p_epg, 82, 20, "C" ); EPG_ADD( p_epg, 62, 20, "B" ); EPG_ADD( p_epg, 102, 20, "D" ); print_order( p_epg ); assert_events( p_epg, "ABCD", 4 ); assert_current( p_epg, "B" ); vlc_epg_Delete( p_epg ); /* Test epg merging */ printf("--test %d\n", i++); p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 142, 20, "F" ); EPG_ADD( p_epg, 122, 20, "E" ); EPG_ADD( p_epg, 42, 20, "A" ); EPG_ADD( p_epg, 62, 20, "B" ); print_order( p_epg ); vlc_epg_t *p_epg2 = vlc_epg_New( 0, 0 ); assert(p_epg2); EPG_ADD( p_epg2, 102, 20, "D" ); EPG_ADD( p_epg2, 82, 20, "C" ); print_order( p_epg2 ); vlc_epg_Merge( p_epg, p_epg2 ); printf("merged " ); print_order( p_epg ); assert_events( p_epg, "ABCDEF", 6 ); assert_events( p_epg2, "CD", 2 ); /* should be untouched */ vlc_epg_Delete( p_epg ); vlc_epg_Delete( p_epg2 ); /* Test event overlapping */ printf("--test %d\n", i++); p_epg = vlc_epg_New( 0, 0 ); assert(p_epg); EPG_ADD( p_epg, 42, 20, "A" ); EPG_ADD( p_epg, 62, 20, "B" ); EPG_ADD( p_epg, 82, 20, "C" ); EPG_ADD( p_epg, 102, 20, "D" ); print_order( p_epg ); vlc_epg_SetCurrent( p_epg, 62 ); p_epg2 = vlc_epg_New( 0, 0 ); assert(p_epg2); EPG_ADD( p_epg2, 41, 30, "E" ); print_order( p_epg2 ); vlc_epg_Merge( p_epg, p_epg2 ); printf("merged " ); print_order( p_epg ); assert_events( p_epg, "ECD", 3 ); assert_current( p_epg, "E" ); EPG_ADD( p_epg2, 70, 42, "F" ); print_order( p_epg2 ); vlc_epg_Merge( p_epg, p_epg2 ); printf("merged " ); print_order( p_epg ); assert_events( p_epg, "F", 1 ); /* Test current overwriting */ printf("--test %d\n", i++); vlc_epg_SetCurrent( p_epg, 70 ); assert_current( p_epg, "F" ); print_order( p_epg ); print_order( p_epg2 ); vlc_epg_Merge( p_epg, p_epg2 ); printf("merged " ); print_order( p_epg ); assert_events( p_epg, "F", 1 ); assert_current( p_epg, "F" ); printf("--test %d\n", i++); print_order( p_epg ); EPG_ADD( p_epg2, 270, 42, "Z" ); vlc_epg_SetCurrent( p_epg2, 270 ); print_order( p_epg2 ); vlc_epg_Merge( p_epg, p_epg2 ); printf("merged " ); print_order( p_epg ); assert_current( p_epg, "Z" ); vlc_epg_Delete( p_epg ); vlc_epg_Delete( p_epg2 ); return 0; }