File: Combinatorics.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 (98 lines) | stat: -rw-r--r-- 2,446 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
/*

	Cadabra: a field-theory motivated computer algebra system.
	Copyright (C) 2001-2014  Kasper Peeters <kasper.peeters@phi-sci.com>

This program is free software: you can redistribute it and/or
	modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "Combinatorics.hh"

unsigned long combin::factorial(unsigned int x)
	{
	unsigned long ret=1;
	while(x) {
		ret*=x--;
		}
	return ret;
	}

int combin::determine_intersection_ranges(const combin::range_vector_t& prod,
      const combin::range_vector_t& indv,
      combin::range_vector_t& target)
	{
	int ret=1;
	for(unsigned int i=0; i<prod.size(); ++i) { // all ranges in prod
		for(unsigned int k=0; k<indv.size(); ++k) { // all ranges in indv
			combin::range_t newrange;
			for(unsigned int ic=0; ic<prod[i].size(); ++ic) {
				for(unsigned int kc=0; kc<indv[k].size(); ++kc) {
					if(prod[i][ic] == indv[k][kc])
						newrange.push_back(indv[k][kc]);
					}
				}
			if(newrange.size()>=2) {
				ret*=factorial(newrange.size());
				target.push_back(newrange);
				}
			}
		}

	return ret;
	}

long combin::vector_sum(const std::vector<int>& v)
	{
	long ret=0;
	for(unsigned int i=0; i<v.size(); ++i)
		ret+=v[i];
	return ret;
	}

unsigned long combin::vector_prod(const std::vector<unsigned int>& v)
	{
	unsigned long ret=1;
	for(unsigned int i=0; i<v.size(); ++i)
		ret*=v[i];
	return ret;
	}

unsigned long combin::vector_prod_fact(const std::vector<unsigned int>& v)
	{
	unsigned long ret=1;
	for(unsigned int i=0; i<v.size(); ++i)
		ret*=fact(v[i]);
	return ret;
	}

bool combin::operator==(const std::vector<unsigned int>& one, const std::vector<unsigned int>& two)
	{
	if(one.size()!=two.size()) return false;

	for(unsigned int k=0; k<one.size(); ++k)
		if(one[k]!=two[k]) return false;

	return true;
	}

long combin::hash(const std::vector<unsigned int>& one)
	{
	long ret=1;
	for(unsigned int k=0; k<one.size(); ++k) {
		ret*=17;
		ret+=one[k];
		}
	return ret;
	}