Example #1
0
File: bdd.c Project: dkudrow/cs267
/* index the variables in the boolean encoding of the program */
pos* pos_init(ast_node* program) {
    int n, i, j, k=0;
    pos* new_pos = malloc(sizeof(pos));  /* fill current and next state symtabs */
    for (new_pos->pc_size=0, n=program->id; n; n/=2, ++new_pos->pc_size);
    new_pos->vars = symtab_init(program);
    new_pos->vars_ = symtab_init(program);
    ast_node* next = program->children[PROC_LIST_HEAD];
    for (new_pos->num_procs = 0; next; new_pos->num_procs++)
        next = next->children[NEXT_PROC];  /* determine the number of processes */
    next = program->children[DECL_LIST_HEAD];
    for (new_pos->num_vars = 0; next; new_pos->num_vars++)
        next = next->children[NEXT_DECL];  /* determine the number of global vars */
    new_pos->pc = malloc(sizeof(int*) * new_pos->num_procs);
    for (i=0; i<new_pos->num_procs; i++) {
        new_pos->pc[i] = malloc(sizeof(int) * new_pos->pc_size);
        for (j=0; j<new_pos->pc_size; j++)
            new_pos->pc[i][j] = 2 * new_pos->num_vars + k++;
    }
    new_pos->pc_ = malloc(sizeof(int*) * new_pos->num_procs);
    for (i=0; i<new_pos->num_procs; i++) {
        new_pos->pc_[i] = malloc(sizeof(int) * new_pos->pc_size);
        for (j=0; j<new_pos->pc_size; j++)
            new_pos->pc_[i][j] = 2 * new_pos->num_vars + k++;
    }
    return new_pos;
}
Example #2
0
int pbc_param_init_set_buf(pbc_param_t par, const char *input, size_t len) {
  symtab_t tab;
  symtab_init(tab);
  read_symtab(tab, input, len);
  int res = param_set_tab(par, tab);
  symtab_forall_data(tab, pbc_free);
  symtab_clear(tab);
  return res;
}
/* Function buildSymtab constructs the symbol 
 * table by preorder traversal of the syntax tree
 */
void buildSymtab(TreeNode * syntaxTree){
  symtab_init();
  add_predefines();
  traverse(syntaxTree,symtab_Pre,symtab_Post);
  if (TraceAnalyze){
    printf("\nSymbol table:\n\n");
    print_all_symtab(root_symtab);
  }
}
Example #4
0
symtab_t* symtab_dup(symtab_t* symtab)
{
  symtab_t* n = POOL_ALLOC(symtab_t);
  symtab_init(n, symtab_size(symtab));

  size_t i = HASHMAP_BEGIN;
  symbol_t* sym;

  while((sym = symtab_next(symtab, &i)) != NULL)
    symtab_put(n, sym_dup(sym));

  return n;
}
Example #5
0
void device_db_init(void)
{
	struct db_info * inf;

	if ((inf = db_info_get()) == NULL) {
		/* initialize an empty symbol table */
		symtab_init(slcdev_symbuf, sizeof(slcdev_symbuf));
		return;
	}

	/* initialize the sybol table from the database */
	memcpy(slcdev_symbuf, inf->symbuf, inf->symbuf_sz);
}
Example #6
0
int main( int argc, char **argv )
{
	/* Initialize and fill a syntax tree */
	snode_t base,nodes[64];
	symtab_t smt;
	symtab_init( &smt, 128 );
	int i,n;
	char *tok[128];
	char out[512];
	char st[128] = "s1,s2 + t1,t2 - z1,z2 = v1,v2 + w1,w2 + x1,x2 + y1,y2";
	snode_init( &base, 128 );
	snode_set_string( &base, st );
	build_tree( &base );
	stree_print( &base, 0 );
	execute_tree( &base, out, &smt );
	stree_free( &base );

	return 0;
}
Example #7
0
/* -----------------------------------------------------------------------
 * main function, where all hell breaks loose
 * ----------------------------------------------------------------------- 
 */
int main(int argc, char **argv)
{
  parse_command_line_arguments(argc, argv, &cmdArgs);

  usrdef_init();
  symtab_init();

  /* begin parsing */
  yyparse();

  /* If there were parsing errors, exit. */
  exit_on_errors();

  /* Perform semantic analysis */
  semantic_analysis(program);

  /* If there were errors during semantic analysis, exit. */
  exit_on_errors();

  /* If we should only perform semantic analysis, exit */
  if (cmdArgs.exit_after_sem == 1) {
    exit(0);
  }

  if (cmdArgs.verbose == 1) {
    /* print the user defined data types */
    printf("USER DEFINED DATA TYPES:\n");
    printf("------------------------\n");
    usrdef_print();
    
    /* print the symbol table*/
    printf("\n\n");
    printf("SYMBOL TABLE:\n");
    printf("-------------\n");
    symtab_print(0);
  }

  /* Simple, wasn't it ?!! */

  return 0;
}
Example #8
0
void db_cfg_purge(void)
{
	struct fs_dirent entry;

	/* uncofigure all devices */
	dev_sim_uncofigure_all();

	/* Erase database */
	fs_dirent_get(&entry, FLASHFS_DB_BIN);
	fs_file_unlink(&entry);

	/* Erase config */
	fs_dirent_get(&entry, FLASHFS_CFG_BIN);
	fs_file_unlink(&entry);

	/* Erase strings */
	const_strbuf_purge();

	/* Initialize symbol table */
	symtab_init(slcdev_symbuf, sizeof(slcdev_symbuf));
}
Example #9
0
int
main ( int argc, char **argv )
{
    options ( argc, argv );

    symtab_init ();
    yyparse();

#ifdef DUMP_TREES
    if ( (DUMP_TREES & 1) != 0 )
        node_print ( stderr, root, 0 );
#endif

    simplify_tree ( root );

#ifdef DUMP_TREES
    if ( (DUMP_TREES & 2) != 0 )
        node_print ( stderr, root, 0 );
#endif

    bind_names ( root );

    /* Parsing and semantics are ok, redirect stdout to file (if requested) */
    if ( outfile != NULL )
    {
        if ( freopen ( outfile, "w", stdout ) == NULL )
        {
            fprintf ( stderr, "Could not open output file '%s'\n", outfile );
            exit ( EXIT_FAILURE );
        }
        free ( outfile );
    }

    generate ( stdout, root );

    destroy_subtree ( root );
    symtab_finalize();

    exit ( EXIT_SUCCESS );
}
Example #10
0
avrule_decl_t *avrule_decl_create(uint32_t decl_id)
{
	avrule_decl_t *decl;
	int i;
	if ((decl = calloc(1, sizeof(*decl))) == NULL) {
		return NULL;
	}
	decl->decl_id = decl_id;
	for (i = 0; i < SYM_NUM; i++) {
		if (symtab_init(&decl->symtab[i], symtab_sizes[i])) {
			avrule_decl_destroy(decl);
			free(decl);
			return NULL;
		}
	}

	for (i = 0; i < SYM_NUM; i++) {
		ebitmap_init(&decl->required.scope[i]);
		ebitmap_init(&decl->declared.scope[i]);
	}
	return decl;
}
Example #11
0
static void compile_vm(const char *pgm)
{
	struct fpvm_fragment fragment;
	struct parser_comm comm = {
		.u.fragment = &fragment,
		.assign_default = assign_raw,
		.assign_per_frame = assign_raw,
		.assign_per_vertex = assign_raw_fail,
		.assign_image_name = assign_image_fail,
	};
	int ok;

	init_fpvm(&fragment, 0);
	fpvm_set_bind_mode(&fragment, FPVM_BIND_ALL);
	symtab_init();
	ok = parse(pgm, TOK_START_ASSIGN, &comm);

	if (ok)
		fpvm_dump(&fragment);
	symtab_free();
	if (ok)
		return;

	fflush(stdout);
	fprintf(stderr, "%s\n", comm.msg);
	free((void *) comm.msg);
	exit(1);
}


/* ----- Command-line processing ------------------------------------------- */


static void free_buffer(void)
{
	free((void *) buffer);
}
Example #12
0
symtab_t* symtab_new()
{
  symtab_t* symtab = POOL_ALLOC(symtab_t);
  symtab_init(symtab, 8);
  return symtab;
}
Example #13
0
int main(int argc, char **argv) {
	for (;;) {
		int c = getopt(argc, argv, "y");
		if (c == -1) break;
		switch (c) {
		case 'y':
			option_easy = 1;
			option_prompt = "> ";
			break;
		default:
			fprintf(stderr, "unrecognized option: %c\n", c);
			break;
		}
	}

	field_init_z(Z);
	field_init_multiz(M);
	symtab_init(tab);

	builtin(fun_rnd);
	builtin(fun_random);
	builtin(fun_ord);
	builtin(fun_order);
	builtin(fun_nextprime);
	builtin(fun_sqrt);
	builtin(fun_inv);
	builtin(fun_type);
	builtin(fun_pairing);
	builtin(fun_zmod);
	builtin(fun_poly);
	builtin(fun_polymod);
	builtin(fun_extend);
	builtin(fun_exit);
	builtin(fun_CHECK);
	builtin(fun_init_pairing_a);
	builtin(fun_init_pairing_d);
	builtin(fun_init_pairing_e);
	builtin(fun_init_pairing_f);
	builtin(fun_init_pairing_g);
	builtin(fun_init_pairing_i);
	run_init_pairing_a(NULL);
	symtab_put(reserved, val_new_field(M), "M");
	symtab_put(reserved, val_new_field(Z), "Z");

	if (argc > optind) {
		FILE *fp = fopen(argv[optind], "r");
		if (!fp) pbc_die("fopen failed on %s", argv[optind]);
		YY_BUFFER_STATE st = yy_create_buffer(fp, YY_BUF_SIZE);
		yy_switch_to_buffer(st);
		yywrapfun = yywrap_return1;
		yyparse();
		yy_delete_buffer(st);
	}
	else {
		yywrapfun = yywrap_readline;
		yywrap();
		while (!end_of_input) {
			if (2 == yyparse()) pbc_die("parser out of memory");
		}
		putchar('\n');
	}

	symtab_clear(tab);
	field_clear(M);
	return 0;
}
Example #14
0
int
main ( int argc, char **argv )
{
	outputStage = 12;

    options ( argc, argv );

    symtab_init ();
    
    /* In order to only scan the tokens we call yylex() directly */
    if ( outputStage == 1 ) {
    	do { } while ( yylex() ); // "Token files"
        exit(0);
    }
    
    /* The parser calls yylex(), match the rules and builds the abstract syntax tree */
    // "BuildTree files"
    yyparse();
    if ( outputStage == 2 ) { 
        exit(0); // Exit if we are only printing this stages debug information. "BuildTree files"
    }
    
    /* Print the abstract syntax tree */
    if ( outputStage == 3 ) {
        node_print ( stderr, root, 0 ); // "Tree files"
        exit(0);
    }

    //simplify_tree ( root );
    /* Assign nodes functions according to their type; Handout first time only? */
    assignFunctionsToNodes( root );
    /* Simplify the abstract syntax tree */
    root->simplify( root, 0 );
    
    if ( outputStage == 4 ) { 
        exit(0); // Exit if we are only printing this stages debug information. "Build Simple Tree files"
    }

    /* Print the abstract syntax tree after simplification "Final Simple Tree files" */
    if ( outputStage == 5 ) {
        node_print ( stderr, root, 0 );
        exit(0);
    }

    //bind_names ( root );
    root->bind_names( root, 0);
    if ( outputStage == 6 || outputStage == 7) {
        exit(0); // Exit if we are only printing this stages debug information. "Scopes&String files" and "Symbol table files"
    }
    
    /* Print the .data (strings) segment of the assembly file */
    if ( outputStage == 8) {
        strings_output(stderr);
        exit(0);
    }
    
    /* Print the entries and string indexes in the node tree "Entries files" */
    if ( outputStage == 9) {
        node_print_entries ( stderr, root, 0 );
        exit(0);
    }
    
    root->typecheck(root);
    if (outputStage == 10) {
    	exit(0);
    }


    /* Parsing and semantics are ok, redirect stdout to file (if requested) */
    if ( outfile != NULL )
    {
        if ( freopen ( outfile, "w", stdout ) == NULL )
        {
            fprintf ( stderr, "Could not open output file '%s'\n", outfile );
            exit ( EXIT_FAILURE );
        }
        free ( outfile );
    }

	root->generate(root, 1);
	/*
	if(outputStage > 10 )
    	generate ( NULL, NULL, root ); // Output nothing, for later debugging stages.
    else if(outputStage == 10 )
    	generate ( NULL, stderr, root ); // Output only the traversal process.
    else if(outputStage == -1 )
    	generate ( stderr, NULL, root ); // Output the asm as no debug text is made.
	*/
	
    //destroy_subtree ( stderr, root );
    
    if ( outputStage == 11 ) {
    	destroy_subtree ( stderr, root );
        exit(0);
    } else
    	destroy_subtree ( NULL, root );
    
    symtab_finalize();
    
    yylex_destroy(); // Free internal data structures of the scanner.

    exit ( EXIT_SUCCESS );
}
Example #15
0
static void parse_only(const char *pgm)
{
	static struct patch patch;
	static struct compiler_sc sc = {
		.p = &patch,
	};
	struct parser_comm comm = {
		.u.sc = &sc,
		.assign_default = assign_default,
		.assign_per_frame = assign_per_frame,
		.assign_per_vertex = assign_per_vertex,
		.assign_image_name = assign_image_name,
	};
	int ok;

	symtab_init();
	ok = parse(pgm, TOK_START_ASSIGN, &comm);
	if (symbols) {
		const struct sym *sym;
		int user = 0;

		foreach_sym(sym) {
			if (!user && !(sym->flags & SF_SYSTEM)) {
				printf("\n");
				user = 1;
			}
			if (sym->flags & SF_CONST)
				printf("%s = %g\n", sym->fpvm_sym.name, sym->f);
			else
				printf("%s\n", sym->fpvm_sym.name);
		}
	}
	symtab_free();
	stim_db_free(); /* @@@ */
	stim_put(patch.stim);
	if (ok)
		return;
	fflush(stdout);
	fprintf(stderr, "%s\n", comm.msg);
	free((void *) comm.msg);
	exit(1);
}


/* ----- Compile the patch into PFPU code ---------------------------------- */


/*
 * "sym" and "field" are used to access a field chosen by the caller in the
 * "struct sym". For this, the caller provides a buffer (sym) and a pointer to
 * the respective field inside the buffer. This way, it's perfectly type-safe
 * and we don't need offsetof acrobatics.
 */

static const char *lookup_name(int idx, struct sym *sym, const int *field)
{
	const struct sym *walk;

	foreach_sym(walk) {
		*sym = *walk;
		if (*field == idx)
			return walk->fpvm_sym.name;
	}
	return NULL;
}


static void dump_regs(const int *alloc, struct sym *sym, const int *field,
    const float *values, int n)
{
	const char *mapped[n];
	int i;

	for (i = 0; i != n; i++)
		mapped[i] = NULL;
	for (i = 0; i != n; i++)
		if (alloc[i] != -1)
			mapped[alloc[i]] = lookup_name(i, sym, field);
	for (i = 0; i != n; i++) {
		if (!values[i] && !mapped[i])
			continue;
		printf("R%03d = %f", i, values[i]);
		if (mapped[i])
			printf(" %s", mapped[i]);
		printf("\n");
	}
}


static void show_patch(const struct patch *patch)
{
	int i;
	struct sym sym;

	printf("global:\n");
	for (i = 0; i != COMP_PFV_COUNT; i++)
		if (patch->pfv_initial[i])
			printf("R%03d = %f %s\n", i, patch->pfv_initial[i],
			    lookup_name(i, &sym, &sym.pfv_idx));
	printf("per-frame PFPU fragment:\n");
	dump_regs(patch->pfv_allocation, &sym, &sym.pfv_idx,
	    patch->perframe_regs, COMP_PFV_COUNT);
	pfpu_dump(patch->perframe_prog, patch->perframe_prog_length);
	printf("per-vertex PFPU fragment:\n");
	dump_regs(patch->pvv_allocation, &sym, &sym.pvv_idx,
	     patch->pervertex_regs, COMP_PVV_COUNT);
	pfpu_dump(patch->pervertex_prog, patch->pervertex_prog_length);
}
Example #16
0
int main( int argc, char **argv )
{
	/* Basic variables */
	int i,j,k,p,m;
	int dim = 2;
	int deg = 3;
	int pdim = binomial( dim + deg, dim );
	int plm = 0;
	int radn = 10;
	int nnz;
	int ret,ns;
	double zrt = 1e-6;
	double sum;
	double xx,res;
	double tol = zrt;
	double sft = 0.0;
	int neig = 100;
	double *ev = (double*) malloc( 2 * neig * sizeof(double) );
	double *alpha = (double*) malloc( ( neig + 3 ) * sizeof(double) );
	double *beta = (double*) malloc( ( neig + 3 ) * sizeof(double) );
	double *gamma = (double*) malloc( ( neig + 3 ) * sizeof(double) );

	/* Switches */
	int b_sft = 0; /* Use argument as shift */
	int b_prj = 0; /* Build projected system matrices */
	int b_pc = 0; /* Build a preconditioner */
	int b_grd = 0; /* Load points from grid file */
	int b_ext = 0; /* Load external potential */

	/* Other stuff */
	char gfn[1024];
	FILE *fp;
	rkp_t rk;
	nuclei_t ext;
	symtab_t sym;
	symbol_t ss;

	/* Take in the options */
	int optc;
	while( ( optc = getopt( argc, argv, "C:g:d:s:t:S:r:x:pPc" ) ) != -1 )
        {
                switch( optc )
                {
			case 'C':
				symtab_init( &sym, 1024 );
				cfgread_load_symbols_f( optarg, &sym );
				symtab_print( &sym );
				break;
			case 'r':
				radn = atoi( optarg );
				break;
			case 'c':
				b_pc = 1;
				break;
			case 'd':
				deg = atoi( optarg );
				break;
			case 't':
				tol = atof( optarg );
				break;
			case 'S':
				sft = atof( optarg );
				b_sft = 1;
				break;
			case 'p':
				plm = 1;
				break;
			case 'P':
				b_prj = 1;
				break;
			case 'g':
				b_grd = 1;
				strcpy( gfn, optarg );
				break;
			case 'x':
				b_ext = 1;
				load_nuclei( optarg, &ext, 0 );
				nuclei_print( &ext );
				break;
			case '?':
			default:
				fprintf( stderr, "I don't understand the jibberish you are spouting. Goodbye.\n" );
				return 0;
				break;
		}
	}
	if( !b_ext )
	{
		fprintf( stderr, "Please specify an external potential. Exiting.\n" );
		return 0;
	}

	/* Allocate and generate grid points */
	int nb;
	int npts;
	double *pts,*dlt,*val,*gqw;

	/* Load points from file if switch is set */
	if( b_grd )
	{
		fprintf( stderr, "Loading points from file... " );
		load_points( gfn, &dim, &npts, &pts, 0 );
		fprintf( stderr, "dim = %d npts = %d. Done.\n", dim, npts );
		fflush( stderr );
	}

	/* Set up the plot points */
        int pgx = 70;
        int pgy = 70;
        double x = 5.0;
        double y = 5.0;
        double pdx = x / (double) ( pgx - 1 );
        double pdy = y / (double) ( pgy - 1 );
        int nppts = pgx * pgy;
        double *ppts = (double*) malloc( nppts * dim * sizeof(double) );
        for(i=0;i<pgx;i++)
                for(j=0;j<pgy;j++)
                        ppts[i*pgy*dim+j*dim+0] = (double) i * pdx,
                        ppts[i*pgy*dim+j*dim+1] = (double) j * pdy;

	/* Generate supports */
	dlt = (double*) malloc( npts * sizeof(double) );
	val = (double*) malloc( npts * sizeof(double) );
	gqw = (double*) malloc( npts * sizeof(double) );
	generate_cloud_supports_min( 2, npts, pts, radn, 1.05, dlt, 0.001 );
	for(i=0;i<npts;i++)
                gqw[i] = 1.0;

	/* Build variables */
	double f,g,r,s,rr,a,b;
	double *vv,*ww,*pc,*xv,*xvh,*rv,*rvh,*rrv,*rrvh,*pv,*pvh; /* Variables for CG iteration */
	double *amat,*bmat,*cmat,*cmatt,*smat,*tmat,*temp,*dv,*rhs;
	long *iamat,*jamat,*ibmat,*jbmat,*icmat,*jcmat,*icmatt,*jcmatt,*ismat,*jsmat,*itmat,*jtmat,*list;

	/* Calculate the number of non-zeros */
	for(nnz=0,i=0;i<npts;i++)
	{
		for(j=0;j<npts;j++)
		{
			xx = 0.0;
			for(k=0;k<dim;k++)
				xx += pow( pts[i*dim+k] - pts[j*dim+k], 2.0 );
			if( xx < pow( dlt[j], 2.0 ) )
				++nnz;
		}
	}
	fprintf( stderr, "nnz = %5d\n", nnz );
	nnz = nnz + 1;

	/* Allocate this stuff static for now, but make dynamic ASAP */
	iamat = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jamat = (long*) malloc( nnz * sizeof(long) );
	ibmat = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jbmat = (long*) malloc( nnz * sizeof(long) );
	icmat = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jcmat = (long*) malloc( nnz * sizeof(long) );
	icmatt = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jcmatt = (long*) malloc( nnz * sizeof(long) );
	ismat = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jsmat = (long*) malloc( npts * npts * sizeof(long) );
	itmat = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	jtmat = (long*) malloc( npts * npts * sizeof(long) );
	list = (long*) malloc( ( npts + 1 ) * sizeof(long) );
	temp = (double*) malloc( ( npts + 1 ) * sizeof(double) );

	/* Allocate stuff; can't use stack for this garbage!!! */
	vv = (double*) malloc( npts * npts * sizeof(double) );
        ww = (double*) malloc( npts * npts * sizeof(double) );
	pc = (double*) malloc( npts * sizeof(double) );
	xv = (double*) malloc( npts * sizeof(double) );
	xvh = (double*) malloc( npts * sizeof(double) );
	rv = (double*) malloc( npts * sizeof(double) );
	rvh = (double*) malloc( npts * sizeof(double) );
	rrv = (double*) malloc( npts * sizeof(double) );
	rrvh = (double*) malloc( npts * sizeof(double) );
	pv = (double*) malloc( npts * sizeof(double) );
	pvh = (double*) malloc( npts * sizeof(double) );

	/* Allocate matrix storage */
	amat = (double*) malloc( nnz * sizeof(double) );
	bmat = (double*) malloc( nnz * sizeof(double) );
	cmat = (double*) malloc( nnz * sizeof(double) );
	cmatt = (double*) malloc( nnz * sizeof(double) );
	smat = (double*) malloc( npts * npts * sizeof(double) );
	tmat = (double*) malloc( npts * npts * sizeof(double) );
	rhs = (double*) malloc( npts * sizeof(double) );
	dv = (double*) malloc( npts * sizeof(double) );

	/* Initialize the RKP basis */
	rkp_init( &rk, npts, dim, deg, pts, dlt, gqw, val, cubic, 1.0 );
	rkp_basis_generate_scaled_const( &rk );
	rkp_wavelet_basis_generate_scaled_const( &rk );

	/* Form the Laplace operator correctly with wavelets */
	int ord[2] = { 2, 4 }; /* Indexes which contain the xx and yy second derivatives */

	/* Build the matrices here to simplify things as much as possible */
	for(i=0;i<nnz;i++) /* Don't need this many entries anymore */
		amat[i] = 0.0, bmat[i] = 0.0;
	for(i=0;i<npts;i++)
		iamat[i] = -1, ibmat[i] = -1;
	for(p=0,i=0;i<npts;i++)
	{
		for(j=0;j<npts;j++)
		{
			/* Check to see if window function at j is large enough to be nonzero */
			xx = 0.0;
			for(k=0;k<dim;k++)
				xx += pow( pts[i*dim+k] - pts[j*dim+k], 2.0 );
			if( xx > pow( dlt[j] * rk.wrad, 2.0 ) )
				continue;
			if( iamat[i] == -1 )
				iamat[i] = p; /* The beginning of row i entries in amat */
			if( ibmat[i] == -1 )
				ibmat[i] = p;
			jamat[p] = j;
			jbmat[p] = j;
			for(k=0;k<dim;k++)
				amat[p] -= 2.0 * 0.5 * gqw[j] * rkp_wavelet_term_evaluate_node_scaled_const( &rk, j, ord[k], i ) / dlt[i] / dlt[i];
			amat[p] += gqw[j] * rkp_term_evaluate_node_scaled_const( &rk, j, i ) * coulomb_potential( dim, pts + i * dim, ext.np, ext.pts );
			bmat[p] = gqw[j] * rkp_term_evaluate_node_scaled_const( &rk, j, i );
			++p;
		}
	}
	iamat[npts] = p;
	ibmat[npts] = p;

	/* Save some garbage in case dumb shit happens */
	smsave( "amat.oct", npts, npts, iamat, jamat, amat, 1 );
	smsave( "bmat.oct", npts, npts, ibmat, jbmat, bmat, 1 );

	fprintf( stderr, "nnz = %d\n", p );
	fflush( stderr );

	/* Generate a random initial state vector */
	srand((unsigned)time(0));

	/* Count the number of boundary nodes */
	nb = 0;
	for(i=0;i<npts;i++)
		if( on_boundary( pts + i * dim ) == 1 )
			++nb;

	/* QR factorization first */
	fprintf( stderr, "Running QR factorization... " );
	fflush( stderr );
        eigenvalue_qrfactorize( dim, npts, pts, ibmat, jbmat, bmat, rhs, vv );
	fprintf( stderr, "Done.\n" );
	fflush( stderr );

	/* Now build the subspace projection matrix */
	fprintf( stderr, "Building subspace projection matrix... " );
	fflush( stderr );
	eigenvalue_subprojmat( npts, nb, vv, ww );
	fprintf( stderr, "Done.\n" );
	fflush( stderr );

	/* Save the projection matrix */
	msave( "wmat.oct", npts - nb, npts, ww, 1 );

	/* Build smat and tmat */
	fprintf( stderr, "Building smat and tmat... " );
	fflush( stderr );
	sparse_symbmm( (long) npts, (long) npts, (long) npts,
		ibmat, jbmat, iamat, jamat, ismat, jsmat, list );
	sparse_dgemm( (long) npts, (long) npts, (long) npts,
		ibmat, jbmat, bmat, iamat, jamat, amat, ismat, jsmat, smat, temp );
	sparse_symbmm( (long) npts, (long) npts, (long) npts,
		ibmat, jbmat, ibmat, jbmat, itmat, jtmat, list );
	sparse_dgemm( (long) npts, (long) npts, (long) npts,
		ibmat, jbmat, bmat, ibmat, jbmat, bmat, itmat, jtmat, tmat, temp );
	fprintf( stderr, "Done.\n" );
	fflush( stderr );

	/* Initialize to a normalized random vector */
	eigenvalue_initialize( npts, rhs );

	/* Now subtract out all vectors in vv from rhs */
        eigenvalue_project( npts, nb, vv, rhs );

	/* Normalize first using tmat */
	eigenvalue_normalize( npts, itmat, jtmat, tmat, rhs, temp );

	/* Initialize the preconditioner to the identity */
        for(i=0;i<npts;i++)
                pc[i] = 1.0;

	/* Solve the eigenvalue problem actually */
	fprintf( stderr, "\n" );
	fprintf( stderr, "Solving Jacobi-Davidson method:\n" );
	eigenvalue_solver( npts, iamat, jamat, amat, ibmat, jbmat, bmat, icmat, jcmat, cmat,
				icmatt, jcmatt, cmatt, ismat, jsmat, smat, itmat, jtmat, tmat,
				rv, rvh, rrv, rrvh, pv, pvh, xv, xvh, dv, pc, rhs, temp, r, tol,
				nb, b_pc, -0.145, vv );

	/* Attempt constrained sparse generalized unsymmetric Lanczos biorthogonalization to solve for bottom few eigenvalues */
	fprintf( stderr, "\n" );
	fprintf( stderr, "Solving with constrained Lanczos method:\n" );
	csgulanczoslp( npts, neig, iamat, jamat, amat, ibmat, jbmat, bmat, alpha, beta, gamma, NULL, NULL, nb, vv, 1e-10, 200, 0, &ret );
	//csgulanczoslp( npts, neig, ibmat, jbmat, bmat, iamat, jamat, amat, alpha, beta, gamma, NULL, NULL, nb, vv, 1e-10, 1000, 0, &ret );
	//sgulanczos( npts, neig, iamat, jamat, amat, ibmat, jbmat, bmat, alpha, beta, gamma, NULL, NULL, 1e-10, 200, 0, &ret );
	fprintf( stderr, "lanczos ret = %d\n", ret );
	treigtridqds( neig, alpha + 1, beta + 1, gamma + 1, 100000, ev, &ret, &ns );
	complex_bubble_sort( neig, ev );
        fprintf( stderr, "Reduction = %d Steps = %d\n", ret, ns );
        for(i=0;i<neig;i++)
                fprintf( stderr, "%15.7f+%15.7fi\n", ev[2*i+0], ev[2*i+1] );

	/* Calculate actual eigenvalue */
	eigenvalue_calculate( npts, iamat, jamat, amat, ibmat, jbmat, bmat, rhs, temp, &f );
	fprintf( stderr, "RQ = %15.7f\n", f );

	/* Copy the solution into the rk structure */
	for(i=0;i<npts;i++)
		rk.vals[i] = rhs[i];

	/* Output the function */
	if( plm )
	{
		fp = fopen( "plot", "w" );
		for(i=0;i<pgx*pgy;i++)
                {
                        fprintf( fp, "%15.7f%15.7f%15.7f\n", ppts[i*dim+0], ppts[i*dim+1], rkp_evaluate_scaled_const( &rk, ppts + i * dim, 0.2 ), 0.1 );
                        if( ( i + 1 ) % pgy == 0 )
                                fprintf( fp, "\n" );
                }
		fclose( fp );
	}
	else
	{
		fp = fopen( "plot", "w" );
		for(i=0;i<npts;i++)
			fprintf( fp, "%15.7f%15.7f%15.7f\n", pts[i*dim+0], pts[i*dim+1], rkp_evaluate_node_scaled_const( &rk, i ) );
	}

	free( pts );
	free( dlt );
	free( val );
	free( gqw );
	free( ppts );

	return 0;
}