From a1c5f64f3c8c9812cb0b4658630f79dab1856c0c Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Tue, 25 Feb 2020 19:40:35 -0500 Subject: Fance inheritance --- animation.hpp | 8 ++--- euclidean.hpp | 113 ++++++++++++++++------------------------------------------ mod.hpp | 10 ++++++ vector.hpp | 3 +- 4 files changed, 47 insertions(+), 87 deletions(-) create mode 100644 mod.hpp diff --git a/animation.hpp b/animation.hpp index 857354e..1689a0c 100644 --- a/animation.hpp +++ b/animation.hpp @@ -43,7 +43,7 @@ private: public: Animation(T L, unsigned w, int argc, char* argv[], unsigned wait, bool periodic = false) - : s₀⁻¹(0), wait(wait), L(1000 * L) { + : s₀⁻¹(L), wait(wait), L(1000 * L) { n = 0; glutInit(&argc, argv); @@ -72,10 +72,10 @@ public: glColor3d(1.0, 0.0, 0.0); glLineWidth(3); glBegin(GL_LINES); - Vector r = (t->r.t).template cast() / 2.0; + Vector r = t->r.t / 2.0; double θ = atan2(r(1), r(0)); - Vector v1 = s₀⁻¹.template act({r(0) + L * sin(θ), r(1) - L * cos(θ)}); - Vector v2 = s₀⁻¹.template act({r(0) - L * sin(θ), r(1) + L * cos(θ)}); + Vector v1 = s₀⁻¹.act({r(0) + L * sin(θ), r(1) - L * cos(θ)}); + Vector v2 = s₀⁻¹.act({r(0) - L * sin(θ), r(1) + L * cos(θ)}); glVertex2d(v1(0), v1(1)); glVertex2d(v2(0), v2(1)); glEnd(); diff --git a/euclidean.hpp b/euclidean.hpp index fb2dbcd..f0e54d4 100644 --- a/euclidean.hpp +++ b/euclidean.hpp @@ -1,6 +1,7 @@ #pragma once +#include "mod.hpp" #include "matrix.hpp" #include "spin.hpp" #include "vector.hpp" @@ -14,12 +15,16 @@ public: Vector relativePosition; }; -template class Euclidean { +template class Affine { +protected: + virtual Vector actV(const Vector& x) const { return t + r * x; } + Affine actA(const Affine& x) const { return Affine(act(x.t), r * x.r); } + public: Vector t; Matrix r; - Euclidean(T L) { + Affine() { for (unsigned i = 0; i < D; i++) { t(i) = 0; r(i, i) = 1; @@ -29,21 +34,13 @@ public: } } - Euclidean(Vector t0, Matrix r0) { - t = t0; - r = r0; - } - - Vector act(const Vector& x) const { return t + r * x; } + Affine(const Vector& t0, const Matrix& r0) : t(t0), r(r0) {} - template Vector act(const Vector& x) const { - return t.template cast() + r.template cast() * x; - } + Affine inverse() const { return Affine(-r.transpose() * t, r.transpose()); } + Vector act(const Vector& x) const { return actV(x); } Radius act(Radius r) const { return r; } - IsingSpin act(IsingSpin s) const { return s; } - Dimer act(const Dimer& d) const { return {.radius = d.radius, .relativePosition = r * d.relativePosition}; } @@ -51,90 +48,42 @@ public: template Spin act(const Spin& s) const { return {.x = act(s.x), .s = act(s.s)}; } +}; - Euclidean act(const Euclidean& x) const { return Euclidean(r * x.t + t, r * x.r); } +template class Euclidean : public Affine { +public: + Euclidean(T L = 0) : Affine() {} + Euclidean(const Affine& a) : Affine(a) {} + Euclidean(Vector t0, Matrix r0) : Affine(t0, r0) {} + + Euclidean act(const Euclidean& t) const { return Affine::actA(t); }; - Euclidean inverse() const { return Euclidean(-r.transpose() * t, r.transpose()); } + using Affine::act; }; -template class TorusGroup { +template class TorusGroup : public Affine{ private: T L; -public: - Vector t; - Matrix r; - - /** brief TorusGroup - default constructor, constructs the identity - */ - TorusGroup(T L) : L(L) { - for (unsigned i = 0; i < D; i++) { - t(i) = 0; - r(i, i) = 1; - for (unsigned j = 1; j < D; j++) { - r(i, (i + j) % D) = 0; - } - } - } - - TorusGroup(T L, Vector t0, Matrix r0) : L(L) { - t = t0; - r = r0; - } - - template Spin act(const Spin& s) const { - Spin s_new; - - s_new.x = t + r * s.x; - s_new.s = s.s; - - for (unsigned i = 0; i < D; i++) { - s_new.x(i) = fmod(L + s_new.x(i), L); - } - - return s_new; - } - - Vector act(const Vector& s) const { - Vector s_new = t + r * s; - - for (unsigned i = 0; i < D; i++) { - s_new(i) = fmod(L + s_new(i), L); - } - - return s_new; - } - - template Vector act(const Vector& s) const { - Vector s_new = t.template cast() + r.template cast() * s; +protected: + Vector actV(const Vector& s) const override { + Vector s_new = Affine::actV(s); for (unsigned i = 0; i < D; i++) { - s_new(i) = fmod(L + s_new(i), L); + s_new(i) = mod(s_new(i), L); } return s_new; } - TorusGroup act(const TorusGroup& x) const { - Vector tnew = r * x.t + t; - Matrix rnew = r * x.r; - - for (unsigned i = 0; i < D; i++) { - tnew(i) = fmod(L + tnew(i), L); - } - - TorusGroup pnew(this->L, tnew, rnew); - - return pnew; - } - - TorusGroup inverse() const { - Vector tnew = -r.transpose() * t; - Matrix rnew = r.transpose(); +public: + TorusGroup(T L) : Affine(), L(L) {} + TorusGroup(T L, Vector t0, Matrix r0) : Affine(), L(L) {} + TorusGroup(T L, const Affine& t) : Affine(t), L(L) {} - TorusGroup pnew(this->L, tnew, rnew); + TorusGroup act(const TorusGroup& t) const { return TorusGroup(L, Affine::actA(t)); } + TorusGroup inverse() const { return TorusGroup(L, Affine::inverse()); } - return pnew; - } + using Affine::act; }; diff --git a/mod.hpp b/mod.hpp new file mode 100644 index 0000000..f0da7c4 --- /dev/null +++ b/mod.hpp @@ -0,0 +1,10 @@ +#pragma once +#include + +double mod(double a, double b) { + if (a >= 0) { + return fmod(a, b); + } else { + return fmod(a + b * ceil(-a / b), b); + } +} diff --git a/vector.hpp b/vector.hpp index 2e87acd..f642816 100644 --- a/vector.hpp +++ b/vector.hpp @@ -1,6 +1,7 @@ #pragma once +#include "mod.hpp" #include template using Vector = Eigen::Matrix; @@ -9,7 +10,7 @@ template Vector diff(T L, Vector v1, Vector v Vector v; for (unsigned i = 0; i < D; i++) { - v(i) = std::abs(v1(i) - v2(i)); + v(i) = std::abs(mod(v1(i), L) - mod(v2(i), L)); if (v(i) > L / 2) { v(i) = L - v(i); } -- cgit v1.2.3