7 #define PI 3.141592653589792
8 #define DEFAULT_SIZE 10
17 // End of forward decl
27 State () : m_spins(DEFAULT_SIZE
) {}
28 State (int size
) : m_spins(size
) {}
29 State (const State
& other
) : m_spins(other
.m_spins
) {}
31 Change
trialChange () {
33 ret
.i
= (rand()/(RAND_MAX
+1.0))*size();
34 ret
.j
= (rand()/(RAND_MAX
+1.0))*(size()-1);
35 if (ret
.j
>=ret
.i
) ret
.j
++;
39 State
applyChange (const Change
& change
) {
41 ret
.m_spins
[change
.i
] = !ret
.m_spins
[change
.i
];
42 ret
.m_spins
[change
.j
] = !ret
.m_spins
[change
.j
];
46 int size () const { return m_spins
.size(); }
47 int at (int i
) const { return m_spins
[i
]?1:-1; }
48 bool parallel (int i
, int j
) const { return m_spins
[i
] == m_spins
[j
]; }
50 static State
up (int size
= DEFAULT_SIZE
) {
52 for (int i
=0;i
<size
;i
++) {
53 ret
.m_spins
[i
] = true;
58 static State
down (int size
= DEFAULT_SIZE
) {
60 for (int i
=0;i
<size
;i
++) {
61 ret
.m_spins
[i
] = false;
66 static State
random (int size
= DEFAULT_SIZE
) {
68 for (int i
=0;i
<size
;i
++) {
69 ret
.m_spins
[i
] = rand()/(RAND_MAX
+1.0) < 0.5;
74 friend ostream
& operator << (ostream
&, const State
&);
75 const State
& operator = (const State
& other
) { m_spins
= other
.m_spins
; return *this; }
78 std::vector
<bool> m_spins
;
81 ostream
& operator << (ostream
& out
, const State
& s
) {
82 for (std::vector
<bool>::const_iterator iter
= s
.m_spins
.begin();iter
!= s
.m_spins
.end();iter
++) {
83 if (iter
!=s
.m_spins
.begin() && iter
!=s
.m_spins
.end()) out
<< ", ";
91 double distance (double i
, double j
) { return 2*sin(PI
*abs(i
-j
)); }
93 VarProb() : alpha(1.0) {}
94 VarProb(double a
) : alpha(a
) {}
96 double compute (const State
& state
) {
100 for (int i
=0;i
<L
;i
++) {
102 for (int j
=i
+1;j
<L
;j
++) {
103 double d
= distance(double(i
)/L
, double(j
)/L
);
104 x
+= log(d
*d
)*(state
.parallel(i
, j
)?1.0:-1.0);
107 if (ups
!=0) return 0.0;
119 template <typename Probability
>
124 QMC (const Probability
& p
, const Observable
&o
, const State
& s
) : m_probability(p
), m_obs(o
), m_state(s
) {
125 current_probability
= m_probability
.compute(m_state
);
128 State
state () const { return m_state
; }
129 void setState (const State
& s
) { m_state
= s
; }
130 Probability
probability () const { return m_probability
; }
131 void setProbability (const Probability
& p
) { m_probability
= p
; }
135 State::Change trial
= m_state
.trialChange();
136 cout
<< "original state: " << m_state
<< endl
;
137 State t_state
= m_state
.applyChange(trial
);
138 cout
<< " --> proposed: " << t_state
<< endl
;
139 double prob
= m_probability
.compute(t_state
);
140 cout
<< " --> with probability: " << prob
<< endl
;
141 double a
= prob
/current_probability
;
142 cout
<< " --> and acceptance ratio: " << a
<< endl
;
143 if (a
>(rand()/(RAND_MAX
+1.0))) {
146 current_probability
= prob
;
147 cout
<< " --> accepted" << endl
;
151 cout
<< " --> rejected" << endl
;
158 Probability m_probability
;
159 double current_probability
;
163 int main (int argc
, char **argv
) {
165 QMC
<VarProb
> qmc(VarProb(+0.26), Observable(), State::random(100));
166 for (int i
=0;i
<1e5
;i
++) qmc
.step();
167 cout
<< State::random(30) << endl
;