File: canonicalise.cc

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (838 lines) | stat: -rw-r--r-- 28,339 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838

#include "Cleanup.hh"
#include "Exchange.hh"
#include "Functional.hh"
#include "algorithms/canonicalise.hh"
#include "modules/xperm_new.h"
#include "properties/Trace.hh"
#include "properties/Traceless.hh"
#include "properties/Diagonal.hh"
#include "properties/Derivative.hh"
#include "properties/AntiCommuting.hh"

// #define DEBUG 1
// #define XPERM_DEBUG 1

using namespace cadabra;

canonicalise::canonicalise(const Kernel& k, Ex& tr)
	: Algorithm(k, tr), reuse_generating_set(false)
	{
	}

bool canonicalise::can_apply(iterator it)
	{
	if(*(it->name)!="\\prod")
		if(is_single_term(it)==false)
			return false;

	// Canonicalise requires strict monomial structure: no products which contain
	// sums as factors. Products as factors are ok, they do not lead to multiple
	// identically named free indices.

	auto sum_or_prod = find_in_subtree(tr, it, [](Ex::iterator tst) {
		if(*tst->name=="\\sum") return true;
		return false;
		}, false);
	if(sum_or_prod!=tr.end()) {
#ifdef DEBUG
		std::cerr << "trying to canonicalise nested product/sum " << Ex(it) << " " << Ex(sum_or_prod) << std::endl;
#endif
		return false;
		}

	return true;
	}

bool canonicalise::remove_traceless_traces(iterator& it)
	{
	// Remove any traces of traceless tensors (this is best done early).
	sibling_iterator facit=tr.begin(it);
	while(facit!=tr.end(it)) {
		const Traceless *trl=kernel.properties.get<Traceless>(facit);
		if(trl) {
			unsigned int ihits=0;
			tree_exact_less_mod_prel_obj comp(&kernel.properties);
			std::set<Ex, tree_exact_less_mod_prel_obj> countmap(comp);
			index_iterator indit=begin_index(facit);
			while(indit!=end_index(facit)) {
				bool incremented_now=false;
				auto ind=kernel.properties.get<Indices>(indit, true);
				if(ind) {
					// The indexs need to be in the set for which the object is
					// traceless (if specified, otherwise accept all).
					if(trl->index_set_names.find(ind->set_name)!=trl->index_set_names.end() || trl->index_set_names.size()==0) {
						incremented_now=true;
						++ihits;
						}
					}
				else incremented_now=true;
				// Having no name is treated as having the right name
				if(countmap.find(Ex(indit))==countmap.end()) {
					countmap.insert(Ex(indit));
					}
				else if(incremented_now) {
					zero(it->multiplier);
					return true;
					}
				++indit;
				}
			iterator parent=it;
			if (tr.number_of_children(it)==1 && !tr.is_head(it)) parent=tr.parent(it);
			const Trace *trace=kernel.properties.get<Trace>(parent);
			if(trace) {
				int tmp;
				auto impi=kernel.properties.get_with_pattern<ImplicitIndex>(facit, tmp, "");
				if(impi.first->explicit_form.size()>0) {
					// Does the explicit form have two more indices of the right type?
					Ex::iterator eform=impi.first->explicit_form.begin();
					unsigned int ehits=0;
					indit=begin_index(eform);
					while(indit!=end_index(eform)) {
						auto ind=kernel.properties.get<Indices>(indit, true);
						if(trl->index_set_names.find(ind->set_name)!=trl->index_set_names.end() && ind->set_name==trace->index_set_name) ++ehits;
						if(ehits - ihits > 1) {
							zero(it->multiplier);
							return true;
							}
						++indit;
						}
					}
				}
			}
		++facit;
		}
	return false;
	}

bool canonicalise::remove_vanishing_numericals(iterator& it)
	{
	// Remove Diagonal objects with numerical indices which are not all the same.
	sibling_iterator facit=tr.begin(it);
	while(facit!=tr.end(it)) {
		const Diagonal *dgl=kernel.properties.get<Diagonal>(facit);
		if(dgl) {
			index_iterator indit=begin_index(facit);
			if(indit->is_rational()) {
				index_iterator indit2=indit;
				++indit2;
				while(indit2!=end_index(facit)) {
					if(indit2->is_rational()==false)
						break;
					if(indit2->multiplier!=indit->multiplier) {
						zero(it->multiplier);
						return true;
						}
					++indit2;
					}
				}
			}
		++facit;
		}
	return false;
	}

Indices::position_t canonicalise::position_type(iterator it) const
	{
	const Indices *ind=kernel.properties.get<Indices>(it, true);
	if(ind)
		return ind->position_type;
	return Indices::free;
	}

bool canonicalise::only_one_on_derivative(iterator i1, iterator i2) const
	{
	int num=0;
	iterator p1=tr.parent(i1);
	const Derivative *der1=kernel.properties.get<Derivative>(p1);
	if(der1) ++num;

	iterator p2=tr.parent(i2);
	const Derivative *der2=kernel.properties.get<Derivative>(p2);
	if(der2) ++num;

	if(num==1) return true;
	else return false;
	}

Algorithm::result_t canonicalise::apply(iterator& it)
	{
#ifdef DEBUG
	std::cerr << "canonicalise at " << it << std::endl;
#endif
	// std::cerr << is_single_term(it) << std::endl;

	Stopwatch totalsw;
	totalsw.start();
	prod_wrap_single_term(it);

	if(remove_traceless_traces(it)) {
		cleanup_dispatch(kernel, tr, it);
		return result_t::l_applied;
		}

	if(remove_vanishing_numericals(it)) {
		cleanup_dispatch(kernel, tr, it);
		return result_t::l_applied;
		}


	// Now the real thing...
	index_map_t ind_free, ind_dummy;
	classify_indices(it, ind_free, ind_dummy);
	index_position_map_t ind_pos_free, ind_pos_dummy;
	fill_index_position_map(it, ind_free, ind_pos_free);
	fill_index_position_map(it, ind_dummy, ind_pos_dummy);
	const unsigned int total_number_of_indices=ind_free.size()+ind_dummy.size();

#ifdef DEBUG
	//	std::cerr << "free index position map:\n";
	//	for(auto& ip: ind_pos_free)
	//		std::cerr << Ex(ip.first) << " @ " << ip.second << std::endl;;
#endif

	// If there are no indices, there is nothing to do here...
	if(total_number_of_indices==0) {
#ifdef DEBUG
		std::cerr << "no indices on " << Ex(it) << std::endl;
#endif
		prod_unwrap_single_term(it);
		return result_t::l_no_action;
		}

	// We currently do not handle situations in which one of the
	// factors in a product is a sum. To be precise, we do not handle
	// situations in which one or both of the indices in a dummy
	// pair appear multiple times (in the different terms of the sum).
	// Ditto for free indices; these need to sit in one particular
	// place in the tree, not in multiple. For these situations, we
	// will bail out here, but the user can always distribute and then
	// canonicalise.

#ifdef DEBUG
	//	std::cerr << "dummies:\n";
	//	for(auto& dummy: ind_dummy)
	//		std::cerr << dummy.first;
	//	std::cerr << "free:\n";
	//	for(auto& fr: ind_free)
	//		std::cerr << fr.first;
#endif

	for(auto& dummy: ind_dummy)
		if(ind_dummy.count(dummy.first)>2)
			return result_t::l_no_action;

	// PROGRESS
	//	for(auto& free: ind_free)
	//		if(ind_free.count(free.first)>1) {
	//			std::cerr << "bailing out" << std::endl;
	//			return result_t::l_no_action;
	//			}

	result_t res=result_t::l_no_action;

	// Construct the "name to slot" map from the order in ind_free & ind_dummy.
	// Also construct the free and dummy lists.
	// And a map from index number to iterator (for later).
	std::vector<int> vec_perm;


	// We need two arrays: one which maps from the order in which slots appear in
	//	the tensor to the corresponding iterator (this is provided by the standard
	// index routines already), and one which maps from the order in which the indices
	// appear in the base map to an Ex object (so that we can replace).

	std::vector<Ex::iterator> num_to_it_map(total_number_of_indices);
	std::vector<Ex>           num_to_tree_map;

#ifdef DEBUG
	std::cerr << "indices:" << std::endl;
	auto ii=begin_index(it);
	while(ii!=end_index(it)) {
		std::cerr << ii << std::endl;
		++ii;
		}
#endif


	// Handle free indices.

#ifdef DEBUG
	std::cerr << "found " << ind_free.size() << " free indices" << std::endl;
#endif
	index_map_t::iterator sorted_it=ind_free.begin();
	int curr_index=0;
	while(sorted_it!=ind_free.end()) {
		index_position_map_t::iterator ii=ind_pos_free.find(sorted_it->second);
		num_to_it_map.at(ii->second)=ii->first;
		num_to_tree_map.push_back(Ex(ii->first));
#ifdef DEBUG
		std::cerr << sorted_it->second << " free at pos " << ii->second+1 << std::endl;
#endif
		vec_perm.push_back(ii->second+1);

		++sorted_it;
		}
	curr_index=0;


	// Handle dummy indices
	// In order to ensure that dummy indices from different index types do not
	// get mixed up, we need to collect information about the types of all
	// dummy indices.
	// The key in the maps below is the set_name of the Indices property, or the
	// set_name of the parent if applicable. If none, it will be ' undeclared'.

	typedef std::map<std::string, std::vector<int> > dummy_set_t;
	dummy_set_t dummy_sets;
#ifdef DEBUG
	std::cerr << "found " << ind_dummy.size() << " dummies" << std::endl;
#endif
	sorted_it=ind_dummy.begin();
	while(sorted_it!=ind_dummy.end()) {
		// We insert the dummy indices in pairs (canonicalise only acts on
		// expressions which have dummies coming in doublets, not the more
		// general cadabra dummy concept).
		// The lower index come first, and then the upper index.

		index_position_map_t::const_iterator ii=ind_pos_dummy.find(sorted_it->second);
		index_map_t::const_iterator          next_it=sorted_it;
		++next_it;
		index_position_map_t::const_iterator i2=ind_pos_dummy.find(next_it->second);

#ifdef XPERM_DEBUG
		std::cerr << *(ii->first->name) << " at pos " << ii->second+1 << " " << ii->first->fl.parent_rel << std::endl;
#endif

		switch(ii->first->fl.parent_rel) {
			case str_node::p_super:
			case str_node::p_none:
				//				vec_perm.push_back(ii->second+1);
				//				vec_perm.push_back(i2->second+1);
				//				num_to_tree_map.push_back(Ex(ii->first));
				//				num_to_tree_map.push_back(Ex(i2->first));
				break;
			case str_node::p_sub:
				std::swap(ii, i2);
				//				vec_perm.push_back(i2->second+1);
				//				vec_perm.push_back(ii->second+1);
				//				num_to_tree_map.push_back(Ex(i2->first));
				//				num_to_tree_map.push_back(Ex(ii->first));
				break;
			default:
				break;
			}

		vec_perm.push_back(ii->second+1);
		vec_perm.push_back(i2->second+1);
		num_to_tree_map.push_back(Ex(ii->first));
		num_to_tree_map.push_back(Ex(i2->first));

		num_to_it_map[ii->second]=ii->first;
		num_to_it_map[i2->second]=i2->first;

		// If the indices are not in canonical order and they are separated
		// by a Derivative, we cannot raise/lower, so they should stay in this order.
		// Have to do this by putting those indices in a different set and then
		// setting the metric flag to 0. Ditto when only one index is on a derivative
		// (canonicalising usually makes the expression uglier in that case).
		iterator tmp;
		if( ( (separated_by_derivative(tr.parent(ii->first), tr.parent(i2->first),tmp)
		       || only_one_on_derivative(ii->first, i2->first) )
		      && position_type(ii->first)==Indices::fixed ) ||
		      position_type(ii->first)==Indices::independent ) {
			dummy_sets[" NR "+get_index_set_name(ii->first)].push_back(ii->second+1);
			dummy_sets[" NR "+get_index_set_name(i2->first)].push_back(i2->second+1);
			}
		else {
			if( kernel.properties.get<AntiCommuting>(ii->first, true) != 0 ) {
				dummy_sets[" AC "+get_index_set_name(ii->first)].push_back(ii->second+1);
				dummy_sets[" AC "+get_index_set_name(i2->first)].push_back(i2->second+1);
				}
			else {
				dummy_sets[get_index_set_name(ii->first)].push_back(ii->second+1);
				dummy_sets[get_index_set_name(i2->first)].push_back(i2->second+1);
				}
			}

		++sorted_it;
		++sorted_it;
		}

	// FIXME: kludge to handle numerical indices; should be done through lookup
	// in Integer properties. This one does NOT work when there is more than
	// one index set; we would need more clever logic to figure out which
	// index type the numerical index corresponds to.
	//	debugout << index_sets.size() << std::endl;
	//	if(index_sets.size()==1) {
	//		for(unsigned int kk=0; kk<indexpos_to_indextype.size(); ++kk)
	//			if(indexpos_to_indextype[kk]=="numerical")
	//				indexpos_to_indextype[kk]=index_sets.begin()->first;
	//		}
	//

	// TRACE: remove later THIS DOES NOT SOLVE THE PROBLEM, ITERATORS ABOVE SEEM TO BE WRONG.
	//	prod_unwrap_single_term(it);
	//	return result_t::l_no_action;


	// Construct the generating set.

	std::vector<unsigned int> base_here;

	if(!reuse_generating_set || generating_set.size()==0) {
		generating_set.clear();
		// Symmetry of individual tensors.
		sibling_iterator facit=tr.begin(it);
		int curr_pos=0;
		while(facit!=tr.end(it)) {
			const TableauBase *tba=kernel.properties.get<TableauBase>(facit);
			// std::cerr << Ex(facit) << " has tableaubase " << tba << std::endl;
			if(tba) {
				unsigned int num_ind=number_of_indices(facit);

				// Add indices to the base. We used to add everything except the last one, but that
				// seems to be the wrong thing to do after the XPERM -> XPERM_EXT upgrade (see Jose's email).
				for(unsigned int kk=0; kk<num_ind; ++kk)
					base_here.push_back(curr_pos+kk+1);

				// loop over tabs
				for(unsigned int ti=0; ti<tba->size(kernel.properties, tr, facit); ++ti) {
					TableauBase::tab_t tmptab=tba->get_tab(kernel.properties, tr,facit,ti);
					// std::cerr << tmptab << std::endl;
					if(tmptab.number_of_rows()>0) {
						for(unsigned int col=0; col<tmptab.row_size(0); ++col) { // anti-symmetry in all inds in a col
							if(tmptab.column_size(col)>1) {
								// all pairs NEW: SGS
								for(unsigned int indnum1=0; indnum1<tmptab.column_size(col)-1; ++indnum1) {
									//								for(unsigned int indnum2=indnum1+1; indnum2<tmptab.column_size(col); ++indnum2) {
									std::vector<int> permute(total_number_of_indices+2);
									for(unsigned int kk=0; kk<permute.size(); ++kk)
										permute[kk]=kk+1;
									std::swap(permute[tmptab(indnum1,col)+curr_pos],
									          permute[tmptab(indnum1+1,col)+curr_pos]);
									std::swap(permute[total_number_of_indices+1],
									          permute[total_number_of_indices]); // anti-symmetry
									generating_set.push_back(permute);
									}
								}
							}
						}
					if(tmptab.number_of_rows()==1 && tmptab.row_size(0)>1) { // symmetry, if all cols of size 1
						// all pairs
						for(unsigned int indnum1=0; indnum1<tmptab.row_size(0)-1; ++indnum1) {
							//						for(unsigned int indnum2=indnum1+1; indnum2<tmptab.row_size(0); ++indnum2) {
							std::vector<int> permute(total_number_of_indices+2);
							for(unsigned int kk=0; kk<permute.size(); ++kk)
								permute[kk]=kk+1;
							std::swap(permute[tmptab(0,indnum1)+curr_pos],
							          permute[tmptab(0,indnum1+1)+curr_pos]);
							generating_set.push_back(permute);
							}
						}
					else if(tmptab.number_of_rows()>0) {   // find symmetry under equal-length column exchange
						unsigned int column_height=tmptab.column_size(0);
						unsigned int this_set_start=0;
						for(unsigned int col=1; col<=tmptab.row_size(0); ++col) {
							if(col==tmptab.row_size(0) || column_height!=tmptab.column_size(col)) {
								if(col-this_set_start>1) {
									// two or more equal-length columns found, make generating set
									for(unsigned int col1=this_set_start; col1+1<=col-1; ++col1) {
										//									for(unsigned int col2=this_set_start+1; col2<col; ++col2) {
										std::vector<int> permute(total_number_of_indices+2);
										for(unsigned int kk=0; kk<permute.size(); ++kk)
											permute[kk]=kk+1;
										for(unsigned int row=0; row<column_height; ++row) {
											//										txtout << row << " " << col1 << std::endl;
											std::swap(permute[tmptab(row,col1)+curr_pos],
											          permute[tmptab(row,col1+1)+curr_pos]);
											}
										generating_set.push_back(permute);
										}
									}
								this_set_start=col;
								if(col<tmptab.row_size(0))
									column_height=tmptab.column_size(col);
								}
							}
						}
					}
				//					txtout << "loop over tabs done" << std::endl;
				curr_pos+=num_ind;
				}
			else {
				unsigned int num_ind=number_of_indices(facit);
				if(num_ind==1)
					base_here.push_back(curr_pos+1);
				else {
					for(unsigned int kk=0; kk<num_ind; ++kk)
						base_here.push_back(curr_pos+kk+1);
					}
				curr_pos+=number_of_indices(facit); // even if tba=0, this factor may contain indices
				}
			++facit;
			}
		// Symmetry under tensor exchange.
		if(exchange::get_node_gs(kernel.properties, tr, it, generating_set)==false) {
			zero(it->multiplier);
			res=result_t::l_applied;
			}
		}
	// End of construction of generating set.

#ifdef XPERM_DEBUG
	std::cerr << "generating set size = " << generating_set.size() << " multiplier " << *it->multiplier << std::endl;
#endif

	// Even if generating_set.size()==0 we still need to continue,
	// because we still need to do simple index relabelling.
	//	if(generating_set.size()==0) {
	//		prod_unwrap_single_term(it);
	//		return result_t::l_no_action;
	//		}

	if(*it->multiplier!=0) {
		// Fill data for the xperm routines.
		int *gs=0;

		if(generating_set.size()>0) {
			gs=new int[generating_set.size()*generating_set[0].size()];
			for(unsigned int i=0; i<generating_set.size(); ++i) {
				for(unsigned int j=0; j<total_number_of_indices+2; ++j) {
					gs[i*(total_number_of_indices+2)+j]=generating_set[i][j];
#ifdef XPERM_DEBUG
					std::cerr << gs[i*(total_number_of_indices+2)+j] << " ";
#endif
					}
#ifdef XPERM_DEBUG
				std::cerr << std::endl;
#endif
				}
			}

		// Setup the arrays for xperm from our own data structures.

		int    *base=new int[base_here.size()];
		int    *perm=new int[total_number_of_indices+2];
		int   *cperm=new int[total_number_of_indices+2];

		for(unsigned int i=0; i<base_here.size(); ++i)
			base[i]=base_here[i];
		assert(vec_perm.size()==total_number_of_indices);
		for(unsigned int i=0; i<total_number_of_indices; ++i)
			perm[i]=vec_perm[i];
		perm[total_number_of_indices]=total_number_of_indices+1;
		perm[total_number_of_indices+1]=total_number_of_indices+2;

		int  *lengths_of_dummy_sets=new int[dummy_sets.size()];
		int  *dummies              =new int[ind_dummy.size()];
		int  *metric_signatures    =new int[dummy_sets.size()];
		int  dsi=0;
		int  cdi=0;
		dummy_set_t::iterator ds=dummy_sets.begin();
		while(ds!=dummy_sets.end()) {
			lengths_of_dummy_sets[dsi]=ds->second.size();
			for(unsigned int k=0; k<ds->second.size(); ++k)
				dummies[cdi++]=(ds->second)[k];
			if(ds->first.substr(0,4)==" NR ")
				metric_signatures[dsi]=0;
			else {
				if(ds->first.substr(0,4)==" AC ")
					metric_signatures[dsi]=-1;
				else
					metric_signatures[dsi]=1;
				}
			++ds;
			++dsi;
			}

		// Setup repeated sets. These are free indices which appear more
		// than once (so these are coordinates or integers). Take them out
		// of the free set and store them in the repeated sets.

#ifdef DEBUG
		std::cerr << "creating repeated sets" << std::endl;
#endif
		std::vector<int>          ind_repeated_lengths;
		std::vector<Ex::iterator> ind_repeated;
		auto fi = ind_free.begin();
		auto prev=fi;
		if(fi!=ind_free.end()) {
			++fi;
			while(fi!=ind_free.end()) {
				int len=1;
				while(fi!=ind_free.end() && fi->first==prev->first) {
					if(len==1)
						ind_repeated.push_back(prev->second);
					ind_repeated.push_back(fi->second);
					++len;
					++fi;
					}
				if(len!=1) {
					ind_free.erase(prev, fi);
					ind_repeated_lengths.push_back(len);
					}
				if(fi!=ind_free.end()) {
					prev=fi;
					++fi;
					}
				}
			}

		// free_indices stores a list of index slots which contain a free
		// index.
		int              *free_indices=new int[ind_free.size()];
		sorted_it=ind_free.begin();
		curr_index=0;
		while(sorted_it!=ind_free.end()) {
			index_position_map_t::iterator ii=ind_pos_free.find(sorted_it->second);
			free_indices[curr_index++]=ii->second+1;
			++sorted_it;
			}

		//		std::cerr << "repeated sets:\n";
		//		for(auto& f: ind_repeated)
		//			std::cerr << *(f->multiplier) << " ";
		//		std::cerr << "\nrepeated lengths:\n";
		//		for(auto& i: ind_repeated_lengths)
		//			std::cerr << i << " ";
		//		std::cerr << std::endl;

		int *repeated_indices         = new int[ind_repeated.size()];
		int *lengths_of_repeated_sets = new int[ind_repeated_lengths.size()];

		// repeated_indices contains a list of slots which contain repeated indices.
		for(size_t i=0; i<ind_repeated.size(); ++i) {
			auto pos=ind_pos_free.find(ind_repeated[i]);
			repeated_indices[i]=pos->second+1;
			}
		for(size_t i=0; i<ind_repeated_lengths.size(); ++i)
			lengths_of_repeated_sets[i]=ind_repeated_lengths[i];

#ifdef XPERM_DEBUG
		std::cerr << "perm:" << std::endl;
		for(unsigned int i=0; i<total_number_of_indices+2; ++i)
			std::cerr << perm[i] << " ";
		std::cerr << std::endl;
		std::cerr << "base:" << std::endl;
		for(unsigned int i=0; i<base_here.size(); ++i)
			std::cerr << base[i] << " ";
		std::cerr << std::endl;
		std::cerr << "free indices in slots:" << std::endl;
		for(unsigned int i=0; i<ind_free.size(); ++i)
			std::cerr << free_indices[i] << " ";
		std::cerr << std::endl;
		std::cerr << "lengths_of_dummy_sets:" << std::endl;
		for(unsigned int i=0; i<dummy_sets.size(); ++i)
			std::cerr << lengths_of_dummy_sets[i]
			          << " (metric=" << metric_signatures[i] << ") ";
		std::cerr << std::endl;
		std::cerr << "dummies in slots:" << std::endl;
		for(unsigned int i=0; i<ind_dummy.size(); ++i)
			std::cerr << dummies[i] << " ";
		std::cerr << std::endl;
		std::cerr << "lengths_of_repeated_sets:" << std::endl;
		for(unsigned int i=0; i<ind_repeated_lengths.size(); ++i)
			std::cerr << lengths_of_repeated_sets[i];
		std::cerr << std::endl;
		std::cerr << "repeated indices in slots:" << std::endl;
		for(unsigned int i=0; i<ind_repeated.size(); ++i)
			std::cerr << repeated_indices[i];
		std::cerr << std::endl;
#endif

		Stopwatch sw;
		sw.start();

		// JMM now uses a different convention.
		int *perm1 = new int[total_number_of_indices+2];
		int *perm2 = new int[total_number_of_indices+2];
		int *free_indices_new_order = new int[ind_free.size()];
		int *dummies_new_order      = new int[ind_dummy.size()];
		int *repeated_new_order     = new int[ind_repeated.size()];

		inverse(perm, perm1, total_number_of_indices+2);
		for(size_t i=0; i<ind_free.size(); i++) {
			free_indices_new_order[i] = onpoints(free_indices[i], perm1, total_number_of_indices+2);
			}
		for(size_t i=0; i<ind_dummy.size(); i++) {
			dummies_new_order[i] = onpoints(dummies[i], perm1, total_number_of_indices+2);
			}
		for(size_t i=0; i<ind_repeated.size(); i++) {
			repeated_new_order[i] = onpoints(repeated_indices[i], perm1, total_number_of_indices+2);
			}
#ifdef XPERM_DEBUG
		std::cerr << "perm1: ";
		for(unsigned int i=0; i<total_number_of_indices; ++i) {
			std::cerr << perm1[i] << " ";
			}
		std::cerr << std::endl;
#endif
		// Brief reminder of the meaning of the various arrays, using the example in
		// Jose's xPerm paper (not yet updated to reflect the _ext version which allows
		// for multiple dummy sets; see below for repeated (numerical) indices):
		//
		// expression        = R_{b}^{1d1} R_{c}^{bac}
		//
		// sorted_index_set  = {a,d,b,-b,c,-c,1,1}
		// base              = {1,2,3, 4,5, 6,7,8}
		// dummies           = {3,4,5,6}
		//    sorted_index_set[3] and [4] is a dummy pair
		//    sorted_index_set[5] and [6] is a dummy pair
		//    these can just be in sorted order, i.e. only referring
		//    to the sorted_index_set.
		//
		// frees             = {1,2}
		//    sorted_index_set[1] and [2] are free indices
		//
		// dummies_new_order = {dn[1], dn[2], ...}
		//    slot dn[1] and dn[2] form a dummy pair ?
		//
		// free_indices_new_order = {...}
		//    these slots contain free indices ?
		//
		// perm = {4,7,2,8,6,3,1,5,9,10}
		//    1st slot contains sorted_index_set[4] ( = -b )
		//    2nd slot contains sorted_index_set[7] ( = 1 )
		//    etc.
		//
		// perm1 = {p1[1], p1[2], ...}
		//
		//    1st index sits in slot p1[1] ?
		//
		// cperm = {1,3,4,5,2,7,6,8,9,10}
		//
		//    1st slot gets sorted_index_set[1] ( = a )
		//    2nd slot gets sorted_index_set[3] ( = b )
		//    ...
		//
		// for the repeated set logic:
		// (in both perm and free, numbers refer to index names (each index name has a separate number
		// even if it occurs multiple times).
		//
		// R_{3 3 m 3}
		//    perm: 6
		//    2 3 1 4 5 6  (slot 3 gets index name 1, slot 1 index 2, slot 3 index 3, slot 4 index 4)
		//    base: 4
		//    1 2 3 4
		//    free: 1
		//    1            (index name 1 is free)
		//    number of repes: 3
		//    2 3 4        (index names 2 to 4 are repeated);

		// R_{3 m 3 3}
		//    perm: 6
		//    2 1 3 4 5 6   (slot 2 gets index name 1, ...)
		//    base: 4
		//    1 2 3 4       (always the same)
		//    free: 1
		//    1             (index name 1 is free)
		//    number of repes: 3
		//    2 3 4         (2nd, 3rd and 4th index names are repeated: index 1, 3 and 4)


		canonical_perm_ext(perm1,                       // permutation to be canonicalised
		                   total_number_of_indices+2,  // degree (+2 for the overall sign)
		                   1,                          // is this a strong generating set?
		                   base,                       // base for the strong generating set
		                   base_here.size(),           //    its length
		                   gs,                         // generating set
		                   generating_set.size(),      //    its size
		                   free_indices_new_order,     // free indices
		                   ind_free.size(),            // number of free indices
		                   lengths_of_dummy_sets,      // list of lengths of dummy sets
		                   dummy_sets.size(),          //    its length
		                   dummies_new_order,          // list with pairs of dummies
		                   ind_dummy.size(),           //    its length
		                   metric_signatures,          // list of symmetries of metric
		                   lengths_of_repeated_sets,   // list of lengths of repeated-sets
		                   ind_repeated_lengths.size(),//    its length
		                   repeated_new_order,         // list with repeated indices
		                   ind_repeated.size(),        //    its length
		                   perm2);                     // output

		if (perm2[0] != 0) inverse(perm2, cperm, total_number_of_indices+2);
		else copy_list(perm2, cperm, total_number_of_indices+2);

		delete [] dummies_new_order;
		delete [] free_indices_new_order;
		delete [] repeated_new_order;
		delete [] perm1;
		delete [] perm2;

		sw.stop();
		//		std::cerr << "xperm took " << sw << std::endl;

#ifdef XPERM_DEBUG
		std::cerr << "cperm:" << std::endl;
		for(unsigned int i=0; i<total_number_of_indices+2; ++i)
			std::cerr << cperm[i] << " ";
		std::cerr << std::endl;
#endif

		if(cperm[0]!=0) {
			bool has_changed=false;
			for(unsigned int i=0; i<total_number_of_indices+1; ++i) {
				if(perm[i]!=cperm[i]) {
					has_changed=true;
					break;
					}
				}
			if(has_changed) {
				if(static_cast<unsigned int>(cperm[total_number_of_indices+1])==total_number_of_indices+1) {
					flip_sign(it->multiplier);
					}
				res = result_t::l_applied;

				for(unsigned int i=0; i<total_number_of_indices; ++i) {
					// In the new final permutation, e.g.
					//
					// 1 5 6 8 7 2 3 4 10 9
					//
					// we place first the first index (m), which goes to the first slot. Then
					// we put n, which can only go the fifth slot. Then we put p, which can go
					// to 6,7,8, so that it goes to 6. Then we put r (not q), which can go to 7
					// and 8, and so we put it at 7, etc.

#ifdef XPERM_DEBUG
					std::cerr << "putting index " << i+1 << "(" << *num_to_tree_map[i].begin()->name
					          << ", " << num_to_tree_map[i].begin()->fl.parent_rel
					          << ") in slot " << cperm[i] << std::endl;
#endif

					iterator ri = tr.replace_index(num_to_it_map[cperm[i]-1], num_to_tree_map[i].begin());
					//					assert(ri->fl.parent_rel==num_to_tree_map[i].begin()->fl.parent_rel);
					ri->fl.parent_rel=num_to_tree_map[i].begin()->fl.parent_rel;
					}
				}
			}
		else {
			zero(it->multiplier);
			res = result_t::l_applied;
			}

		if(gs)
			delete [] gs;
		delete [] base;

		delete [] repeated_indices;
		delete [] lengths_of_repeated_sets;
		delete [] metric_signatures;
		delete [] lengths_of_dummy_sets;
		delete [] dummies;
		delete [] cperm;
		delete [] perm;
		delete [] free_indices;
		}
#ifdef DEBUG
	std::cerr << "=====\n";
#endif

	cleanup_dispatch(kernel, tr, it);


	totalsw.stop();
	//	std::cerr << "total canonicalise took " << totalsw << std::endl;

	return res;
	}