CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM_Array_math.hh
Go to the documentation of this file.
1 #ifndef CASM_Array_math
2 #define CASM_Array_math
3 
5 //#include "casm/global/eigen.hh"
8 
9 /*
10 //Maybe we should transition to boost math library?
11 //#include <boost/math/special_functions/binomial.hpp>
12 #include <boost/math/special_functions/factorials.hpp>
13 #include <cmath>
14 #include <complex>
15 #include <cstddef>
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19 */
20 
21 namespace CASM {
22 
23 // //////////////////////////////////////////
24 // //////////////////////////////////////////
25 // Array Function declarations:
26 
27 // *******************************************************************************************
28 
29 // returns 'i' if 'input' is equivalent to 'unique[i]', w.r.t. permutation of
30 // the equivalent elements of 'input'. equivalent elements are specified by
31 // 'ind_equiv' if 'input' specifies a new combination of integers, unique.size()
32 // is returned
34  const Array<Index>::X2 &unique,
35  const Array<Index>::X2 &ind_equiv);
36 
37 // *******************************************************************************************
38 
40  const Array<Index>::X2 &unique);
41 
42 // *******************************************************************************************
44 int lcm(const Array<int> &series);
45 
46 // *******************************************************************************************
47 
49 
50 // *******************************************************************************************
51 
52 template <typename IntType>
53 IntType multinomial_coeff(const Array<IntType> &exponents) {
54  IntType tcoeff(1), tsum(0);
55  for (Index i = 0; i < exponents.size(); i++) {
56  tsum += exponents[i];
57  tcoeff *= nchoosek(tsum, exponents[i]);
58  }
59  return tcoeff;
60 }
61 
62 // *******************************************************************************************
63 // get multinomial coefficient for only a subset of the coeffs
64 template <typename IntType>
65 IntType multinomial_coeff(const Array<IntType> &exponents,
66  const Array<Index> &sublist) {
67  IntType tcoeff(1), tsum(0);
68  for (Index i = 0; i < sublist.size(); i++) {
69  tsum += exponents[sublist[i]];
70  tcoeff *= nchoosek(tsum, exponents[sublist[i]]);
71  }
72  return tcoeff;
73 }
74 // ************************************************************
75 template <typename T>
76 bool almost_equal(const Array<T> &A, const Array<T> &B, double tol = TOL) {
77  if (A.size() != B.size()) return false;
78  for (Index i = 0; i < A.size(); i++) {
79  if (!almost_equal(A[i], B[i], tol)) return false;
80  }
81  return true;
82 }
83 
84 // ************************************************************
85 // cumulative sum, first element is 0 and final elment is array.sum()
86 template <typename T>
88  Array<T> result;
89  result.reserve(arr.size() + 1);
90  result.push_back(0);
91  for (Index i = 0; i < arr.size(); i++) {
92  result.push_back(result[i] + arr[i]);
93  }
94  return result;
95 }
96 
97 // ************************************************************
98 
99 template <typename Derived>
101  const Eigen::MatrixBase<Derived> &value, double tol = TOL) {
102  Array<Index> subspace_dims;
103  Index last_i = 0;
104  for (Index i = 1; i < value.size(); i++) {
105  if (!almost_equal(value[last_i], value[i], tol)) {
106  subspace_dims.push_back(i - last_i);
107  last_i = i;
108  }
109  }
110  subspace_dims.push_back(value.size() - last_i);
111  return subspace_dims;
112 }
113 
114 } // namespace CASM
115 
116 #endif
Index size() const
Definition: Array.hh:131
void reserve(Index new_max)
Definition: Array.hh:411
void push_back(const T &toPush)
Definition: Array.hh:431
Main CASM namespace.
Definition: APICommand.hh:8
bool almost_equal(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Check if ClusterInvariants are equal.
Index which_unique_combination(const Array< Index > &input, const Array< Index >::X2 &unique, const Array< Index >::X2 &ind_equiv)
IntType nchoosek(IntType n, IntType k)
Definition: CASM_math.hh:207
ReturnArray< Array< int > > get_prime_factors(int target)
const double TOL
Definition: definitions.hh:30
int lcm(const Array< int > &series)
Find least common multiple.
ReturnArray< Index > partition_distinct_values(const Eigen::MatrixBase< Derived > &value, double tol=TOL)
ReturnArray< T > cum_sum(const Array< T > &arr)
IntType multinomial_coeff(const Array< IntType > &exponents)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39