Frobby  0.9.5
TermExtra.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3  Copyright (C) 2010 University of Aarhus
4  Contact Bjarke Hammersholt Roune for license information (www.broune.com)
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see http://www.gnu.org/licenses/.
18 */
19 #include "stdinc.h"
20 #include "TermExtra.h"
21 
22 #include <algorithm>
23 #include <vector>
24 
25 Exponent median(const Exponent* a, size_t varCount) {
26  if (varCount == 0)
27  return 0;
28 
29  Term t(a, varCount);
30  Exponent* middle = t.begin() + varCount / 2;
31  nth_element(t.begin(), middle, t.end());
32  return *middle;
33 }
34 
35 Exponent medianPositive(const Exponent* a, size_t varCount) {
36  vector<Exponent> exps;
37  exps.reserve(varCount);
38  for (size_t var = 0; var < varCount; ++var)
39  if (a[var] > 0)
40  exps.push_back(a[var]);
41  if (exps.empty())
42  return 0;
43 
44  vector<Exponent>::iterator middle = exps.begin() + exps.size() / 2;
45  nth_element(exps.begin(), middle, exps.end());
46  return *middle;
47 }
48 
49 void totalDegree(mpz_class& res, const Exponent* a, size_t varCount) {
50  res = a[0];
51  for (size_t var = 1; var < varCount; ++var)
52  res += a[var];
53 }
54 
55 Exponent minimumPositive(const Exponent* a, size_t varCount) {
56  for (size_t var = 0; var < varCount; ++var) {
57  if (a[var] != 0) {
58  Exponent min = a[var];
59  for (; var < varCount; ++var)
60  if (a[var] != 0 && a[var] < min)
61  min = a[var];
62  return min;
63  }
64  }
65  return 0;
66 }
67 
68 Exponent maximum(const Exponent* a, size_t varCount) {
69  Exponent max = 0;
70  for (size_t var = 0; var < varCount; ++var)
71  if (a[var] > max)
72  max = a[var];
73  return max;
74 }
Exponent median(const Exponent *a, size_t varCount)
Returns the lower median exponent of a.
Definition: TermExtra.cpp:25
Exponent medianPositive(const Exponent *a, size_t varCount)
Returns the lower median of the positive exponents of a.
Definition: TermExtra.cpp:35
void totalDegree(mpz_class &res, const Exponent *a, size_t varCount)
Puts the sum of the entries of a into res.
Definition: TermExtra.cpp:49
Exponent minimumPositive(const Exponent *a, size_t varCount)
Returns the smallest positive exponent of a.
Definition: TermExtra.cpp:55
Exponent maximum(const Exponent *a, size_t varCount)
Returns the largest exponent of a.
Definition: TermExtra.cpp:68
Term represents a product of variables which does not include a coefficient.
Definition: Term.h:49
Exponent * end()
Definition: Term.h:82
Exponent * begin()
Definition: Term.h:79
unsigned int Exponent
Definition: stdinc.h:89