YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Spin.h
Go to the documentation of this file.
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #ifndef yap_spin_h
22 #define yap_spin_h
23 
24 #include "fwd/Spin.h"
25 
26 #include "MathUtilities.h"
27 
28 #include <algorithm>
29 #include <cstdlib>
30 #include <numeric>
31 #include <string>
32 
33 namespace yap {
34 
36 inline std::string spin_to_string(int twoJ)
37 { return is_even(twoJ) ? std::to_string(twoJ / 2) : std::to_string(twoJ) + "/2"; }
38 
40 inline std::string to_string(const SpinVector& two_j)
41 {
42  return std::accumulate(two_j.begin(), two_j.end(), std::string(""),
43  [](const std::string & s, const SpinVector::value_type & j)
44  {return s + " + " + spin_to_string(j);}).erase(0, 3);
45 }
46 
48 inline std::string to_string(const SpinProjectionVector& two_m)
49 {
50  return std::accumulate(two_m.begin(), two_m.end(), std::string(""),
51  [](const std::string & s, const SpinProjectionVector::value_type & m)
52  {return s + " + " + spin_to_string(m);}).erase(0, 3);
53 }
54 
60 constexpr bool triangle(unsigned two_a, unsigned two_b, unsigned two_c)
61 { return is_even(two_a + two_b + two_c) and std::abs((int)two_a - (int)two_b) <= (int)two_c and two_c <= (two_a + two_b); }
62 
68 constexpr bool conserves(unsigned two_J, unsigned two_j1, unsigned two_j2, int l)
69 {
70  // check that the spins are consistent, and that the triangle requirements for (Jlj) and (j1j2j) can be met simultaneously
71  return is_even(two_J + two_j1 + two_j2)
72  and (std::min<int>(two_j1 + two_j2, two_J + 2 * l) >= std::max(std::abs((int)two_j1 - (int)two_j2), std::abs((int)two_J - 2 * l)));
73 }
74 
77 inline const SpinProjectionVector projections(unsigned two_j)
78 {
79  SpinProjectionVector spv;
80  spv.reserve(two_j + 1);
81  for (int two_m = -two_j; two_m <= (int)two_j; two_m += 2)
82  spv.push_back(two_m);
83  return spv;
84 }
85 
88 inline const std::vector<SpinProjectionVector> projections(const SpinVector& two_J)
89 {
90  // initialize vector of spin projections to -two_j
91  std::vector<int> two_M;
92  two_M.reserve(two_J.size());
93  std::transform(two_J.begin(), two_J.end(), std::back_inserter(two_M),
94  [](const SpinVector::value_type & two_j) {return -two_j;});
95 
96  std::vector<SpinProjectionVector> SPV;
97  // fill SPV with "odometer"-style looping
98  while (two_M.back() <= (int)two_J.back()) {
99  SPV.push_back(two_M);
100  two_M[0] += 2;
101  for (size_t i = 0; (i < two_M.size() - 1) and (two_M[i] > (int)two_J[i]); ++i) {
102  two_M[i] = -two_J[i];
103  two_M[i + 1] += 2;
104  }
105  }
106  return SPV;
107 }
108 
109 }
110 
111 #endif
const SpinProjectionVector projections(unsigned two_j)
Definition: Spin.h:77
constexpr bool conserves(unsigned two_J, unsigned two_j1, unsigned two_j2, int l)
Definition: Spin.h:68
constexpr T abs(const Vector< T, N > &A)
Definition: Vector.h:371
constexpr bool triangle(unsigned two_a, unsigned two_b, unsigned two_c)
Definition: Spin.h:60
constexpr bool is_even(int val)
Definition: MathUtilities.h:34
std::string to_string(const SpinProjectionVector &two_m)
convert SpinVector to string
Definition: Spin.h:48
std::string spin_to_string(int twoJ)
convert 2*J to string (e.g. 1/2, 1, 3/2, etc.)
Definition: Spin.h:36
std::string to_string(const CachedValue::Status &S)
streaming operator for CachedValue::Status
Definition: CachedValue.cxx:27