comparison toolboxes/FullBNT-1.0.7/graph/triangulate.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 /* triangulate.c written by Ilya Shpitser */
2
3 #include <stdlib.h>
4
5 #ifdef UNIX
6 #include "matlab.h"
7 #endif
8
9 #include "matrix.h"
10 #include "mex.h"
11
12 #include "elim.h"
13 #include "map.h"
14 #include "misc.h"
15
16 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
17
18 int dims [2];
19 int i, j, k, m, n;
20 long index;
21 double * G_pr;
22 double * stage_pr;
23 double * answer_G_pr, * fill_ins_pr;
24 double * matlab_clique_pr;
25 mxArray * matlab_clique;
26 Elimination e;
27 float ** adj_mat;
28 int ** order = (int **) NULL;
29 Iterator iter, iter2;
30 word w, w2;
31 int ** fill_ins;
32 Map cliques;
33 Map clique;
34 mxArray * fill_ins_mat;
35 int * nodes;
36 mxArray * full;
37
38 // (original) full = mlfFull((mxArray *) prhs[0]);
39 full = (mxArray *) mlfFull((mxArray *) prhs[0]); // added typecasting
40 /* Obtain graph matrix information. */
41 m = mxGetM(full);
42 n = mxGetN(full);
43 G_pr = mxGetPr(full);
44
45 if(n < 1 || m < 1){
46 return;
47 }
48
49 /* Allocate and populate the log weight adjacency matrix corresponding
50 to the input graph. */
51 adj_mat = (float **) malloc(sizeof(float *) * m);
52 adj_mat[0] = (float *) malloc(sizeof(float) * m * n);
53 for(i = 1; i < m; i++){
54 adj_mat[i] = adj_mat[i - 1] + n;
55 }
56 /* We no longer have log weight info, but we have a (total) ordering on
57 the nodes already, so we do not need this information. */
58 for(i = 0; i < m; i++){
59 for(j = 0; j < n; j++){
60 index = j * m + i;
61 if(G_pr[index] > 0){
62 adj_mat[i][j] = 1;
63 } else {
64 adj_mat[i][j] = 0;
65 }
66 }
67 }
68
69 /* Convert the total elimination ordering into a partial order argument
70 for the elimination routine. The elimination routine's purpose in this
71 mode of operation is to return cliques and fill-in edges. */
72 if(nrhs > 1){
73 order = (int **) malloc(sizeof(int *) * m);
74 order[0] = (int *) malloc(sizeof(int) * m * n);
75 for(i = 1; i < m; i++){
76 order[i] = order[i - 1] + n;
77 }
78 for(i = 0; i < m; i++){
79 for(j = 0; j < n; j++){
80 order[i][j] = 0;
81 }
82 }
83 stage_pr = mxGetPr(prhs[1]);
84 for(i = 0; i < mxGetN(prhs[1]) - 1; i++){
85 order[(int) stage_pr[i] - 1][(int) stage_pr[i + 1] - 1] = 1;
86 }
87 }
88
89 /* Find the elimination ordering. */
90 e = find_elim(n, adj_mat, order, -1);
91
92 /* Allocate memory for the answer, and set the answer. */
93 plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
94 answer_G_pr = mxGetPr(plhs[0]);
95 cliques = get_cliques(e);
96 /*
97 dims[0] = 1;
98 dims[1] = get_size_Map(cliques);
99 plhs[1] = mxCreateCellArray(2, (const int *) dims);*/
100 plhs[1] = mxCreateCellMatrix(get_size_Map(cliques), 1);
101 fill_ins = get_fill_ins(e);
102 fill_ins_mat = mxCreateDoubleMatrix(m, n, mxREAL);
103 fill_ins_pr = mxGetPr(fill_ins_mat);
104
105 for(i = 0; i < n; i++){
106 for(j = 0; j < m; j++){
107 index = j * m + i;
108 answer_G_pr[index] = G_pr[index];
109 if(fill_ins[i][j] > 0){
110 answer_G_pr[index] = 1;
111 fill_ins_pr[index] = 1;
112 }
113 }
114 }
115 mxDestroyArray(full);
116 // (original) plhs[2] = mlfSparse(fill_ins_mat, NULL, NULL, NULL, NULL, NULL);
117 plhs[2] = (mxArray *) mlfSparse(fill_ins_mat, NULL, NULL, NULL, NULL, NULL); // added typecasting
118 mxDestroyArray(fill_ins_mat);
119 nodes = (int *) malloc(sizeof(int) * n);
120 k = 0;
121 iter = get_Iterator(cliques);
122 while(!is_empty(iter)){
123 w = next_key(iter);
124 clique = (Map) w.v;
125 matlab_clique = mxCreateDoubleMatrix(1, get_size_Map(clique), mxREAL);
126 matlab_clique_pr = mxGetPr(matlab_clique);
127 for(i = 0; i < n; i++){
128 nodes[i] = 0;
129 }
130 iter2 = get_Iterator(clique);
131 while(!is_empty(iter2)){
132 w2 = next_key(iter2);
133 nodes[w2.i] = w2.i + 1;
134 }
135 j = 0;
136 for(i = 0; i < n; i++){
137 if(nodes[i] > 0){
138 matlab_clique_pr[j++] = nodes[i];
139 }
140 }
141 mxSetCell(plhs[1], k++, matlab_clique);
142 }
143 free(nodes);
144
145 /* Finally, free the allocated memory. */
146 destroy_Elimination(e);
147 if(adj_mat){
148 if(adj_mat[0]){
149 free(adj_mat[0]);
150 }
151 free(adj_mat);
152 }
153 if(order){
154 if(order[0]){
155 free(order[0]);
156 }
157 free(order);
158 }
159 }