Example #1
0
/**
  \brief Create an editable graph starting with (optionally) a \a graph.

  \param[out] ged  A editable graph (of type GMRFLib_ged_tp) is returned in \a *ged
  \param[in] graph An optional graph to initialise the editable graph object with

  \sa GMRFLib_ged_build(), GMRFLib_ged_free()
*/
int GMRFLib_ged_init(GMRFLib_ged_tp ** ged, GMRFLib_graph_tp * graph)
{
	/*
	 * initialise the 'ged' object starting with graph. graph can of'course be NULL. 
	 */
	*ged = Calloc(1, GMRFLib_ged_tp);
	spmatrix_init_hint(&((*ged)->Q), (mapkit_size_t) (graph ? (2 * graph->n) : 1000));
	map_ii_init(&((*ged)->tags));
	(*ged)->max_node = -1;				       /* yes, this is correct */

	if (graph) {
		GMRFLib_ged_append_graph(*ged, graph);
	}

	return GMRFLib_SUCCESS;
}
Example #2
0
/*!

  \brief Computes the graph corresponding to the precision matrix <em>\b Q</em> based on the weight
  function.

  This function converts a graph definition on the weights \f$ w_{ij} \f$ of a weighted average GMRF
  model, as defined by <b>(GMRF-8)</b> in \ref specification, to the graph defining the precision
  matrix <em>\b Q</em> of the GMRF <em>\b x</em>.

  \param[out] wa_problem At output, \a wa_problem is allocated as a pointer to a \c
  GMRFLib_wa_problem_tp, holding the graph of the GMRF <em>\b x</em>, the function defining the
  <em>\b Q</em> -matrix and a \em character -pointer holding the address of the variable or data
  structure holding it's arguments.

  \param[in] wagraph The graph of the weights \f$ {w_{ij}} \f$ in the density function of <em>\b
  x</em>, of the form <b>(GMRF-8)</b>.

  \param[in] wafunc A pointer to a function returning the weights \f$ w_{ij} \f$. The function is to
  be of the same format as the function defining the elements of the <em>\b Q</em> -matrix of a
  general GMRF, such that the argument list and return value should be the same as for the template
  \c GMRFLib_Qfunc_tp().  The arguments should be the indices \em i and \em j and a \em character
  -pointer referring to additional arguments to the function. If \f$ w_{ij}=0 \f$ initially, it is
  required to be kept unchanged.

  \param[in] wafunc_arg A \em character -pointer holding the address of a variable or data structure
  defining additional arguments to the function \a wafunc.

  \remarks Based on the weight function, defining the weights in <b>(GMRF-8)</b>, the routine
  computes the graph corresponding to the precision matrix <em>\b Q</em> of the GMRF \em x. Also,
  the function defining the elements of <em>\b Q</em> and the set of arguments (in addition to the
  indices \em i and \em j) are generated. These are returned as members of the \c
  GMRFLib_wa_problem_tp -object <em>(*problem)</em>.

  \note The weights \f$ w_{ij} \f$ that are initially defined to be 0, should be kept fixed. The
  function \c GMRFLib_prune_graph() is called on the resulting graph of the GMRF <em>\b x</em>,
  removing elements of the graph corresponding to <em>\b Q (i,j) = 0</em>.  Re-setting the zero
  weights, for example by letting the weights depend on parameters that might change while running
  the programs, will invalidate this graph reduction.\n This routine will in most cases compute
  <em>\b Q (i,j)</em> less efficiently using more memory than a tailored implementation, but may
  save you for a lot of work!!! \n There is a global variable \c GMRFLib_use_wa_table_lookup() which
  controls the internal behaviour: if it is \c #GMRFLib_TRUE (default value) then internal
  lookup-tables are build that (can really) speed up the computation, and if it is \c
  #GMRFLib_FALSE, then it does not use internal lookup-tables.  The storage requirement is \f$
  {\mathcal O}(n^{3/2}) \f$.

  \par Example:
  See \ref ex_wa
  
  \sa GMRFLib_free_wa_problem, GMRFLib_prune_graph.
 */
int GMRFLib_init_wa_problem(GMRFLib_wa_problem_tp **wa_problem, 
			    GMRFLib_graph_tp *wagraph, GMRFLib_Qfunc_tp *wafunc, char *wafunc_arg)
{
    /*
      wagraph is the graph defining

      (*) \sum_i (w_ii x_i - \sum_{j~i} w_ij x_j)^2 = x^T Q x

      where 'wafunc(i,j)' returns w_ij with arguments wafunc_arg. this routine compute the
      corresponding graph defining Q, a function returning Q_ij and its arguments.
    */

    int i, j, k, kk, jj, jjj, kkk, n, *memsiz = NULL, nnb, *hold = NULL, indx;
    GMRFLib_graph_tp *graph = NULL, *ngraph = NULL;
    GMRFLib_waQfunc_arg_tp *wa_arg = NULL;

    GMRFLib_make_empty_graph(&graph);
    n           = wagraph->n;
    graph->n    = n;
    graph->nnbs = Calloc(n, int);   MEMCHK(graph->nnbs);
    graph->nbs  = Calloc(n, int *); MEMCHK(graph->nbs);
    memsiz      = Calloc(n, int);   MEMCHK(memsiz);
     
    for(i=0;i<n;i++)
    {
	graph->nnbs[i] = wagraph->nnbs[i];
	memsiz[i]      = MAX(1, 2*graph->nnbs[i]);
	graph->nbs[i]  = Calloc(memsiz[i], int);
	if (graph->nnbs[i]) memcpy(graph->nbs[i], wagraph->nbs[i], graph->nnbs[i]*sizeof(int));
    }

    /* 
       build the new graph
    */
    if (0)
    {
	/* 
	   old code, slow algorithm.....
	*/
	for(i=0;i<n;i++)
	    for(j=0;j<wagraph->nnbs[i];j++)
		for(jj=j+1;jj<wagraph->nnbs[i];jj++)
		{
		    k  = wagraph->nbs[i][j];	
		    kk = wagraph->nbs[i][jj];	

		    if (!GMRFLib_is_neighb(k, kk, graph))
		    {
			/* 
			   add, must do it symmetrically! must also sort the neighbours, since the
			   'is_neig' function assume they are sorted.
			*/
			graph->nnbs[k]++;
			if (graph->nnbs[k] > memsiz[k])
			{
			    memsiz[k] *= 2;
			    graph->nbs[k] = (int *)realloc(graph->nbs[k], memsiz[k]*sizeof(int));
			}
			graph->nbs[k][graph->nnbs[k]-1] = kk;
			qsort(graph->nbs[k], (size_t)graph->nnbs[k], sizeof(int), GMRFLib_intcmp);

			graph->nnbs[kk]++;
			if (graph->nnbs[kk] > memsiz[kk])
			{
			    memsiz[kk] *= 2;
			    graph->nbs[kk] = (int *)realloc(graph->nbs[kk], memsiz[kk]*sizeof(int));
			}
			graph->nbs[kk][graph->nnbs[kk]-1] = k;
			qsort(graph->nbs[kk], (size_t)graph->nnbs[kk], sizeof(int), GMRFLib_intcmp);
		    }
		}
    }
    else
    {
	/* 
	   new version, runs faster, use slightly more memory, but not that much
	*/
	if (0) printf("\n\n%s:%1d:NEW CODE HERE, NOT PROPERLY TESTED!!!\n\n", __FILE__, __LINE__);
	for(i=0;i<n;i++)
	    for(j=0;j<wagraph->nnbs[i];j++)
	    {
		k  = wagraph->nbs[i][j];
		for(jj=j+1;jj<wagraph->nnbs[i];jj++)
		{
		    kk = wagraph->nbs[i][jj];	

		    if (graph->nnbs[k] >= memsiz[k])
		    {
			qsort(graph->nbs[k], (size_t)graph->nnbs[k], sizeof(int), GMRFLib_intcmp);
			for(jjj=1, kkk=0; jjj<graph->nnbs[k];jjj++)
			    if (graph->nbs[k][jjj] != graph->nbs[k][kkk]) graph->nbs[k][++kkk] = graph->nbs[k][jjj];
			graph->nnbs[k] = kkk+1;
		    }
		    if (graph->nnbs[k] >= memsiz[k])
		    {
			memsiz[k]    *= 2;
			graph->nbs[k] = (int *)realloc(graph->nbs[k], memsiz[k]*sizeof(int));
		    }
		    graph->nbs[k][graph->nnbs[k]++] = kk;

		    if (graph->nnbs[kk] >= memsiz[kk])
		    {
			qsort(graph->nbs[kk], (size_t)graph->nnbs[kk], sizeof(int), GMRFLib_intcmp);
			for(jjj=1, kkk=0; jjj<graph->nnbs[kk];jjj++)
			    if (graph->nbs[kk][jjj] != graph->nbs[kk][kkk]) graph->nbs[kk][++kkk] = graph->nbs[kk][jjj];
			graph->nnbs[kk] = kkk+1;
		    }
		    if (graph->nnbs[kk] >= memsiz[kk])
		    {
			memsiz[kk]    *= 2;
			graph->nbs[kk] = (int *)realloc(graph->nbs[kk], memsiz[kk]*sizeof(int));
		    }	
		    graph->nbs[kk][graph->nnbs[kk]++] = k;
		}
	    }
	for(i=0;i<n;i++)			  /* this step is needed */
	    if (graph->nnbs[i])
	    {
		qsort(graph->nbs[i], (size_t)graph->nnbs[i], sizeof(int), GMRFLib_intcmp);
		for(j=1, k=0;j<graph->nnbs[i];j++)
		    if (graph->nbs[i][j] != graph->nbs[i][k]) graph->nbs[i][++k] = graph->nbs[i][j];
		graph->nnbs[i] = k+1;
	    }
    }
    
    for(i=0, nnb=0;i<n;i++) nnb += graph->nnbs[i];
    if (nnb)
    {
	hold = Calloc(nnb, int); MEMCHK(hold);	  /* use a linear storage */
    }
    for(i=0, indx=0;i<n;i++) 
    {
	if (graph->nnbs[i])
	{
	    memcpy(&hold[indx], graph->nbs[i], graph->nnbs[i]*sizeof(int));
	    FREE(graph->nbs[i]);
	    graph->nbs[i] = &hold[indx]; 
	}
	else
	    FREE(graph->nbs[i]);

	indx += graph->nnbs[i];
    }

    FREE(memsiz);
    GMRFLib_prepare_graph(graph);

    /* 
       setup the new types
    */
    wa_arg              = Calloc(1, GMRFLib_waQfunc_arg_tp); MEMCHK(wa_arg);
    wa_arg->waQgraph    = graph;
    wa_arg->waQfunc     = wafunc;
    wa_arg->waQfunc_arg = wafunc_arg;
    GMRFLib_copy_graph(&(wa_arg->wagraph), wagraph); /* yes, make a copy! */

    if (GMRFLib_use_wa_table_lookup)
    {
	/* 
	   build hash table for idx = (i,j) -> neigb_info[idx]
	*/
	double idx=0.0;
	spmatrix_init_hint(&(wa_arg->neigb_idx_hash), (mapkit_size_t)GMRFLib_nQelm(graph)); /* give a hint of the size */
	wa_arg->neigb_idx_hash.alwaysdefault = 0;
	for(i=0;i<graph->n;i++)
	{
	    spmatrix_set(&(wa_arg->neigb_idx_hash), i, i, idx); idx++;
	    for(j=0;j<graph->nnbs[i];j++)
	    {
		spmatrix_set(&(wa_arg->neigb_idx_hash), i, graph->nbs[i][j], idx); idx++;
	    }
	}
	wa_arg->n_neigb_info = (int) idx;
	/* 
	   hold list of neighbors and common neigbhbors, to gain some real speedup....
	*/
	wa_arg->neigb_info = Calloc(wa_arg->n_neigb_info, GMRFLib_node_list_tp *); MEMCHK(wa_arg->neigb_info);
    }
    else