File: expand_power.cc

package info (click to toggle)
cadabra2 2.3.6.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 77,508 kB
  • sloc: ansic: 132,438; cpp: 86,549; python: 1,365; javascript: 191; xml: 182; sh: 180; objc: 53; makefile: 38
file content (64 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download
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

#include "Cleanup.hh"
#include "algorithms/expand_power.hh"

using namespace cadabra;

expand_power::expand_power(const Kernel& k, Ex& e)
	: Algorithm(k, e)
	{
	}

bool expand_power::can_apply(iterator it)
	{
	if(*it->name=="\\pow") {
		sibling_iterator exponent=tr.begin(it);
		++exponent;
		if(exponent->is_integer())
			return true;
		}
	return false;
	}

Algorithm::result_t expand_power::apply(iterator& it)
	{
	iterator argument=tr.begin(it);
	sibling_iterator exponent=tr.begin(it);
	++exponent;

	int num=to_long(*exponent->multiplier);
	if(num<=1)
		return result_t::l_no_action;

	iterator prodn=tr.insert(argument,str_node("\\prod"));

	// If the current \pow is inside a sum, do not discard the bracket
	// type on \pow but copy it onto each generated \prod element.
	if(tr.is_head(it)==false && *tr.parent(it)->name=="\\sum")
		prodn->fl.bracket=it->fl.bracket;

	sibling_iterator beg=argument;
	sibling_iterator nd=beg;
	++nd;
	argument=tr.reparent(prodn,beg,nd);
	tr.erase(exponent);
	tr.flatten(it);
	multiply(prodn->multiplier, *it->multiplier);
	it=tr.erase(it);

	// Now duplicate the factor num-1 times.
	multiplier_t tot=*argument->multiplier;
	for(int i=0; i<num-1; ++i) {
		iterator tmp=tr.append_child(prodn);
		tot *= *argument->multiplier;
		iterator ins=tr.replace(tmp, argument);
		one(ins->multiplier);
		rename_replacement_dummies(ins);
		}
	one(argument->multiplier);
	multiply(prodn->multiplier, tot);

	cleanup_dispatch(kernel, tr, it);

	return result_t::l_applied;
	}