From 94f7d887981f0626f166f5645fa98115d4f9a478 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Thu, 26 Jul 2018 01:17:53 -0400 Subject: everything compiles now, and fixed a major bug in the graph code --- lib/potts.h | 61 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) (limited to 'lib/potts.h') diff --git a/lib/potts.h b/lib/potts.h index 3ab08bd..e6ea636 100644 --- a/lib/potts.h +++ b/lib/potts.h @@ -33,47 +33,40 @@ class potts_t { typedef vector_t M_t; typedef vector_t F_t; -}; -template -void init(potts_t *p) { - p->x = 0; -} + potts_t() : x(0) {} -template -void free_spin(potts_t s) { - // do nothing! -} + potts_t(q_t x) : x(x) {} -template -potts_t copy(potts_t s) { - return s; -} + inline vector_t operator*(v_t a) const { + vector_t result; + result.fill(0); + result[x] = (int)a; -template -void add(vector_t *s1, int a, potts_t s2) { - (s1->x)[s2.x] += a; -} + return result; + } -template -void add(vector_t *s1, double a, potts_t s2) { - (s1->x)[s2.x] += a; -} + inline vector_t operator*(double a) const { + vector_t result; + result.fill(0.0); + result[x] = a; + + return result; + } +}; template -vector_t scalar_multiple(int factor, potts_t s) { - vector_t M; - M.x = (int *)calloc(q, sizeof(int)); - M.x[s.x] += factor; +inline vector_t& operator+=(vector_t& M, const potts_t &s) { + M[s.x]++; + return M; } template -vector_t scalar_multiple(double factor, potts_t s) { - vector_t F; - F.x = (double *)calloc(q, sizeof(double)); - F.x[s.x] += factor; - return F; +inline vector_t& operator-=(vector_t& M, const potts_t &s) { + M[s.x]--; + + return M; } // we could inherit norm_squared from vector.h, but convention dictates that @@ -81,8 +74,8 @@ vector_t scalar_multiple(double factor, potts_t s) { template double norm_squared(vector_t s) { double total = 0; - for (q_t i = 0; i < q; i++) { - total += pow(s.x[i], 2); + for (double& x : s) { + total += pow(x, 2); } return total * (double)q / ((double)q - 1.0); @@ -92,7 +85,9 @@ double norm_squared(vector_t s) { // to nv we don't need to write the last element template void write_magnetization(vector_t M, FILE *outfile) { - fwrite(M.x, sizeof(int), q - 1, outfile); + for (int& x : M) { + fwrite(&x, sizeof(int), q - 1, outfile); + } } // knock yourself out -- cgit v1.2.3-70-g09d2