Tensor Network Theory Library  Beta release 1.2.1
A library of routines for performing TNT-based operations
 All Data Structures Functions Variables Groups Pages
tntNodeConn.c
1 /*
2 Authors: Sarah Al-Assam, Stephen Clark and Dieter Jaksch
3 $LastChangedDate: 2015-06-29 20:44:17 +0100 (Mon, 29 Jun 2015) $
4 (c) University of Oxford 2014
5 */
6 
7 #include "../headers/tnt_int.h"
8 #include "../headers/dec_public.h"
9 #include "../headers/dec_tnode.h"
10 #include "../headers/dec_network.h"
11 
24 void tntNodeAddLeg(tntNode A,
25  tntLegLabel legA)
26 {
27  unsigned loop, numl, *lA;
28  TNT_ERR_RET_DEFS /* return value from internal function */
29 
30  ret = _tnt_tnode_checkqn(A);
31  TNT_PUB_ERR_CHK /* NO_COVERAGE */
32 
33  /* Convert characters to integers */
34  ret = _tnt_node_id_strtonum(&numl, &lA, legA);
35  TNT_PUB_ERR_CHK /* NO_COVERAGE */
36 
37  for (loop = 0; loop < numl; loop++) {
38  ret = _tnt_tnode_addleg(A, lA[loop]);
39  TNT_PUB_ERR_CHK /* NO_COVERAGE */
40  }
41 
42  free(lA);
43 }
44 
45 
52 void tntNodeJoin(tntNode A,
53  tntLegLabel legA,
54  tntNode B,
55  tntLegLabel legB)
56 {
57  unsigned numlA, *lA, numlB, *lB, loop;
58  TNT_ERR_RET_DEFS /* return value from internal function */
59 
60  /* Convert characters to integers */
61  ret = _tnt_node_id_strtonum(&numlA, &lA, legA);
62  TNT_PUB_ERR_CHK /* NO_COVERAGE */
63 
64  /* Convert characters to integers */
65  ret = _tnt_node_id_strtonum(&numlB, &lB, legB);
66  TNT_PUB_ERR_CHK /* NO_COVERAGE */
67 
68  if (numlA != numlB) {
69  sprintf(tnt_err.errinfostr,"The number of legs listed for A (%d) does not equal the number of legs listed for B (%d)",numlA, numlB); /* NO_COVERAGE */
70  sprintf(tnt_err.pubfuncname,"tntNodeJoin()"); /* NO_COVERAGE */
71  _tnt_print_error(); /* NO_COVERAGE */
72  } /* NO_COVERAGE */
73 
74  for (loop = 0; loop < numlA; loop++) {
75  ret = _tnt_tnode_join(A, lA[loop], B, lB[loop]);
76  TNT_PUB_ERR_CHK /* NO_COVERAGE */
77  }
78 
79  free(lA);
80  free(lB);
81 }
82 
90 void tntNodeFuse(tntNode A,
91  tntLegLabel fuseLegs,
92  tntLegLabel newLabelA,
93  tntLegLabel newLabelB)
94 {
95  TNT_ERR_RET_DEFS /* return value from internal function */
96  unsigned fuseNum, *fuseLegIds, newlegidA, newlegidB;
97 
98  /* Convert character arrays to numeric ids */
99  ret = _tnt_node_id_strtonum(&fuseNum,&fuseLegIds,fuseLegs);
100  TNT_PUB_ERR_CHK /* NO_COVERAGE */
101 
102  ret = _tnt_node_id_chartonum(&newlegidA,newLabelA[0]);
103  TNT_PUB_ERR_CHK /* NO_COVERAGE */
104 
105  if ((NULL == newLabelB) || (0 == strlen(newLabelB))) {
106  newlegidB = 0;
107  } else {
108  ret = _tnt_node_id_chartonum(&newlegidB,newLabelB[0]);
109  TNT_PUB_ERR_CHK /* NO_COVERAGE */
110  }
111 
112  ret = _tnt_tnode_fuse(A, fuseNum, fuseLegIds, newlegidA, newlegidB);
113  TNT_PUB_ERR_CHK /* NO_COVERAGE */
114 
115  free(fuseLegIds);
116 
117 }
118 
125 void tntNodeMapLegs(tntNode A,
126  tntLegLabel legmap)
127 {
128  unsigned intlegmap[TNT_MAXLEGNUM+1];
129  TNT_ERR_RET_DEFS
130 
131  ret = _tnt_node_id_legmapconv(intlegmap, legmap);
132  TNT_PUB_ERR_CHK /* NO_COVERAGE */
133 
134  ret = _tnt_tnode_maplegids(A,intlegmap);
135  TNT_PUB_ERR_CHK /* NO_COVERAGE */
136 }
137 
145 void tntNodeReplace(tntNode A,
146  tntNode B)
147 {
148 
149  TNT_ERR_RET_DEFS /* return value from internal function */
150 
151  ret = _tnt_tnode_replace(A,B);
152  TNT_PUB_ERR_CHK /* NO_COVERAGE */
153 
154 
155 }
156 
161 void tntNodeSplit(tntNode A,
162  tntNode B)
163 {
164 
165  TNT_ERR_RET_DEFS /* return value from internal function */
166 
167  ret = _tnt_tnode_split(A, B);
168  TNT_PUB_ERR_CHK /* NO_COVERAGE */
169 
170 
171 }
172 
173 
177 void tntNodeInsert(tntNode I,
178  tntLegLabel legIA,
179  tntLegLabel legIB,
180  tntNode A,
181  tntLegLabel legA,
182  tntNode B,
183  tntLegLabel legB)
184 {
185 
186  TNT_ERR_RET_DEFS /* return value from internal function */
187  unsigned lIA, lIB, lA, lB; /* id's (as integers) for the character leg labels */
188 
189  /* Get leg identities for all the characters */
190  ret = _tnt_node_id_chartonum(&lIA, legIA[0]);
191  TNT_PUB_ERR_CHK /* NO_COVERAGE */
192 
193  ret = _tnt_node_id_chartonum(&lIB, legIB[0]);
194  TNT_PUB_ERR_CHK /* NO_COVERAGE */
195 
196  ret = _tnt_node_id_chartonum(&lA, legA[0]);
197  TNT_PUB_ERR_CHK /* NO_COVERAGE */
198 
199  ret = _tnt_node_id_chartonum(&lB, legB[0]);
200  TNT_PUB_ERR_CHK /* NO_COVERAGE */
201 
202  /* Last argument = 0, normal insert, perform dimension checks */
203  ret = _tnt_tnode_insert(I, lIA, A, lA, lIB, B, lB, 0);
204  TNT_PUB_ERR_CHK /* NO_COVERAGE */
205 
206 }
207 
213 void tntNodeInsertAtStart(tntNode I,
214  tntLegLabel legInwstart,
215  tntLegLabel legIfirst,
216  tntNetwork nw)
217 {
218 
219  struct tnode *tnB; /* The node that was previously the first node in the network */
220  unsigned legB; /* Leg id of node that was previously the first node that is connected to the start of the network */
221  unsigned lInwstart, lIfirst;
222  TNT_ERR_RET_DEFS /* return value from internal function */
223 
224  /* Find the first node in the network */
225  ret = _tnt_tnode_findfirst(nw->nw_start, &tnB);
226  TNT_PUB_ERR_CHK /* NO_COVERAGE */
227 
228  /* Turn character identities to integers */
229  ret = _tnt_node_id_chartonum(&lInwstart, legInwstart[0]);
230  TNT_PUB_ERR_CHK /* NO_COVERAGE */
231 
232  ret = _tnt_node_id_chartonum(&lIfirst, legIfirst[0]);
233  TNT_PUB_ERR_CHK /* NO_COVERAGE */
234 
235  /* Find which leg of this node is connected to the start of the network */
236  ret = _tnt_tnode_findtnid(&legB, tnB, nw->nw_start);
237  TNT_PUB_ERR_CHK /* NO_COVERAGE */
238 
239  /* Last argument = 1, special insert, do not perform dimension checks */
240  ret = _tnt_tnode_insert(I, lInwstart, nw->nw_start, TNT_START_ID, lIfirst, tnB, legB, 1);
241  TNT_PUB_ERR_CHK/* NO_COVERAGE */
242 
243 
244 }
245 
252 void tntNodeInsertAtEnd(tntNode I,
253  tntLegLabel legIlast,
254  tntLegLabel legInwend,
255  tntNetwork nw)
256 {
257 
258  struct tnode *tnB; /* The node that was previously the last node in the network */
259  unsigned legB; /* Leg id of node that was previously the last node that is connected to the end of the network */
260  unsigned lIlast, lInwend;
261  TNT_ERR_RET_DEFS /* return value from internal function */
262 
263  /* Find the last node in the network */
264  ret = _tnt_tnode_findlast(nw->nw_end, &tnB);
265  TNT_PUB_ERR_CHK /* NO_COVERAGE */
266 
267  /* Turn character identities to integers */
268  ret = _tnt_node_id_chartonum(&lIlast, legIlast[0]);
269  TNT_PUB_ERR_CHK /* NO_COVERAGE */
270 
271  ret = _tnt_node_id_chartonum(&lInwend, legInwend[0]);
272  TNT_PUB_ERR_CHK /* NO_COVERAGE */
273 
274  /* Find which leg of this node is connected to the start of the network */
275  ret = _tnt_tnode_findtnid(&legB, tnB, nw->nw_end);
276  TNT_PUB_ERR_CHK /* NO_COVERAGE */
277 
278  /* Last argument = 1, special insert, do not perform dimension checks */
279  ret = _tnt_tnode_insert(I, lInwend, nw->nw_end, TNT_END_ID, lIlast, tnB, legB, 1);
280  TNT_PUB_ERR_CHK /* NO_COVERAGE */
281 
282 }
283 
292 void tntNodeSqueeze(tntNode A,
293  tntLegLabel squeezeLegs)
294 {
295  TNT_ERR_RET_DEFS /* return value from internal function */
296  unsigned squeezeNum, *squeezeLegIds;
297 
298  ret = _tnt_tnode_checkqn(A);
299  TNT_PUB_ERR_CHK /* NO_COVERAGE */
300 
301  /* Turn string to integer IDs */
302  ret = _tnt_node_id_strtonum(&squeezeNum, &squeezeLegIds, squeezeLegs);
303  TNT_PUB_ERR_CHK /* NO_COVERAGE */
304 
305  ret = _tnt_tnode_squeeze(A, squeezeNum, squeezeLegIds);
306  TNT_PUB_ERR_CHK /* NO_COVERAGE */
307 
308  free(squeezeLegIds);
309 }
310 
311 
323 #ifdef MAKINGPUBLICDOCS
324 void tntNodeRemove(tntNode A,
325  tntLegLabel connLegs)
326 #else
327 void tntNodeRemove_(tntNode A,
328  tntLegLabel connLegs)
329 #endif
330 {
331 
332  TNT_ERR_RET_DEFS /* return value from internal function */
333  int leg_conn[TNT_MAXLEGNUM + 1];
334  unsigned i, numconn, *conn_ids;
335 
336  /* Initialise all entries of connections to -1 */
337  for (i = 0; i <= TNT_MAXLEGNUM; i++) leg_conn[i] = -1;
338 
339  /* If an leg connections have been provided, set them */
340  if (NULL != connLegs) {
341  /* Find the identities of the connected legs */
342  ret = _tnt_node_id_strtonum(&numconn, &conn_ids, connLegs);
343  TNT_PUB_ERR_CHK /* NO_COVERAGE */
344 
345  /* Check that there are an even number of legs */
346  if (numconn%2) {
347  sprintf(tnt_err.errinfostr,"Invalid second argument %s - an even number of leg labels should be listed",connLegs); /* NO_COVERAGE */
348  sprintf(tnt_err.pubfuncname,"tntNodeRemove()"); /* NO_COVERAGE */
349  _tnt_print_error(); /* NO_COVERAGE */
350  } /* NO_COVERAGE */
351 
352  /* Populate entries for the connected legs */
353  for (i = 0; i < numconn; i+=2) {
354  leg_conn[conn_ids[i]] = conn_ids[i+1];
355  leg_conn[conn_ids[i+1]] = conn_ids[i];
356  }
357  }
358 
359 
360  /* Now remove the node from the network */
361  ret = _tnt_tnode_remove(A, leg_conn);
362  TNT_PUB_ERR_CHK /* NO_COVERAGE */
363 
364  /* Free the leg ids */
365  free(conn_ids);
366 }
367 
void tntNodeFuse(tntNode A, tntLegLabel fuseLegs, tntLegLabel newLabelA, tntLegLabel newLabelB)
Definition: tntNodeConn.c:90
void tntNodeJoin(tntNode A, tntLegLabel legA, tntNode B, tntLegLabel legB)
Definition: tntNodeConn.c:52
void tntNodeRemove(tntNode A, tntLegLabel connLegs)
Definition: tntNodeConn.c:324
void tntNodeSqueeze(tntNode A, tntLegLabel squeezeLegs)
Definition: tntNodeConn.c:292
void tntNodeMapLegs(tntNode A, tntLegLabel legmap)
Definition: tntNodeConn.c:125
void tntNodeInsertAtEnd(tntNode I, tntLegLabel legIlast, tntLegLabel legInwend, tntNetwork nw)
Definition: tntNodeConn.c:252
void tntNodeReplace(tntNode A, tntNode B)
Definition: tntNodeConn.c:145
void tntNodeInsert(tntNode I, tntLegLabel legIA, tntLegLabel legIB, tntNode A, tntLegLabel legA, tntNode B, tntLegLabel legB)
Definition: tntNodeConn.c:177
void tntNodeAddLeg(tntNode A, tntLegLabel legA)
Definition: tntNodeConn.c:24
void tntNodeInsertAtStart(tntNode I, tntLegLabel legInwstart, tntLegLabel legIfirst, tntNetwork nw)
Definition: tntNodeConn.c:213
void tntNodeSplit(tntNode A, tntNode B)
Definition: tntNodeConn.c:161