CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM_Array_math.cc
Go to the documentation of this file.
2 
3 namespace CASM {
4 
5 // returns 'i' if 'input' is equivalent to 'unique[i]', w.r.t. permutation of
6 // the equivalent elements of 'input'. equivalent elements are specified by
7 // 'ind_equiv' if 'input' specifies a new combination of integers, unique.size()
8 // is returned
10  const Array<Array<Index> > &unique,
11  const Array<Array<Index> > &ind_equiv) {
12  Index tval, tcount;
13  Index i, j, k, l;
14  for (i = 0; i < unique.size(); i++) {
15  // Is input equivalent to unique[i] w.r.t. ind_equiv?
16  // Loop over groups of equivalent indices
17  if (unique[i].size() != input.size()) continue;
18  for (j = 0; j < ind_equiv.size(); j++) {
19  // Loop over indices that are equivalent
20  for (k = 0; k < ind_equiv[j].size(); k++) {
21  tval = input[ind_equiv[j][k]];
22  tcount = 0;
23  for (l = 0; l < ind_equiv[j].size(); l++)
24  tcount += int(unique[i][ind_equiv[j][l]] == tval) -
25  int(input[ind_equiv[j][l]] == tval);
26 
27  if (tcount != 0) break;
28  }
29  if (k != ind_equiv[j].size()) break;
30  }
31  if (j == ind_equiv.size()) return i;
32  }
33  return i;
34 }
35 
36 //****************************************
37 
39  const Array<Array<Index> > &unique) {
40  Index tval, tcount;
41  Index i, j, l;
42  for (i = 0; i < unique.size(); i++) {
43  // Is input equivalent to unique[i] w.r.t. ind_equiv?
44  // Loop over groups of equivalent indices
45  if (unique[i].size() != input.size()) continue;
46  for (j = 0; j < input.size(); j++) {
47  tval = input[j];
48  tcount = 0;
49  for (l = 0; l < input.size(); l++)
50  tcount += int(unique[i][l] == tval) - int(input[l] == tval);
51 
52  if (tcount != 0) break;
53  }
54 
55  if (j == input.size()) return i;
56  }
57  return i;
58 }
59 
60 //************************************************************
62 int lcm(const Array<int> &series) {
63  if (!series.size()) return 0;
64  int lcm_val(series[0]);
65  for (Index i = 1; i < series.size(); i++) lcm_val = lcm(lcm_val, series[i]);
66 
67  return lcm_val;
68 }
69 
70 //************************************************************
71 
73  Array<Array<int> > factors_array;
74  Array<int> factor_list;
75 
76  if (target <= 1) {
77  std::cerr << "WARNING in global/definitions::get_prime_factors"
78  << std::endl;
79  std::cerr << "You're asking for prime factors of " << target
80  << ". Returning empty array." << std::endl
81  << std::endl;
82  return factors_array;
83  }
84  int factor = 2;
85 
86  while (target != 1) {
87  while (target % factor == 0) {
88  factor_list.push_back(factor);
89  target = target / factor;
90  }
91 
92  factor++;
93 
94  if (factor_list.size() > 0) {
95  factors_array.push_back(factor_list);
96  }
97  factor_list.clear();
98  }
99 
100  return factors_array;
101 }
102 
103 } // namespace CASM
Index size() const
Definition: Array.hh:131
void clear()
Definition: Array.hh:182
void push_back(const T &toPush)
Definition: Array.hh:431
Main CASM namespace.
Definition: APICommand.hh:8
Index which_unique_combination(const Array< Index > &input, const Array< Index >::X2 &unique, const Array< Index >::X2 &ind_equiv)
ReturnArray< Array< int > > get_prime_factors(int target)
int lcm(const Array< int > &series)
Find least common multiple.
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39