File: collect_components.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 (65 lines) | stat: -rw-r--r-- 1,561 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

#include "algorithms/collect_components.hh"
#include "algorithms/evaluate.hh"

using namespace cadabra;

// #define DEBUG 1

collect_components::collect_components(const Kernel& k, Ex& tr)
	: Algorithm(k, tr)
	{
	}

bool collect_components::can_apply(iterator st)
	{
	assert(tr.is_valid(st));
	if(*st->name=="\\sum") return true;
	return false;
	}

Algorithm::result_t collect_components::apply(iterator& st)
	{
	result_t res=result_t::l_no_action;
	evaluate eval(kernel, tr, tr);

	sibling_iterator s1=tr.begin(st);

#ifdef DEBUG
	std::cerr << "collect_components::apply: acting on " << st << std::endl;
#endif
	while(s1!=tr.end(st)) { // Walk over all terms in the sum, find the first components node.
		if(*s1->name=="\\components") { // First term found, now collect all others.
			sibling_iterator s2=s1;//tr.begin(st);
			++s2;
			while(s2!=tr.end(st)) {
				if(*s2->name=="\\components") {
#ifdef DEBUG
					std::cerr << "collect_components::apply: merging" << std::endl;
#endif
					eval.merge_components(s1,s2);
					s2=tr.erase(s2);
					res=result_t::l_applied;
					}
				else ++s2;
				}
			break; // Exit outer loop; we have found the first components node.
			}
		++s1;
		}

	if(s1!=tr.end(st)) {
		// Exited the main loop before the end, which means that we have
		// found at least one components node, stored at s1.
#ifdef DEBUG
		std::cerr << "collect_components::apply: merged components node now " << s1 << std::endl;
#endif
		auto comma=tr.end(s1);
		--comma;
		if(tr.number_of_children(comma)==0)
			node_zero(s1);
		}

	return res;
	}