From a2e54a235adc4b443b977b8f648864cc858bcf69 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Thu, 21 Mar 2013 21:49:12 +0000 Subject: [PATCH] Factor: improve factor multiplication Pass the argument as reference-to-const and also allow chaining of multiplications. --- packages/CLPBN/horus/Factor.cpp | 8 +++++--- packages/CLPBN/horus/Factor.h | 2 +- packages/CLPBN/horus/GenericFactor.cpp | 7 ++++--- packages/CLPBN/horus/GenericFactor.h | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/CLPBN/horus/Factor.cpp b/packages/CLPBN/horus/Factor.cpp index 45d586d7d..f3fbc704b 100644 --- a/packages/CLPBN/horus/Factor.cpp +++ b/packages/CLPBN/horus/Factor.cpp @@ -91,14 +91,16 @@ Factor::sumOutAllExceptIndex (size_t idx) } -void -Factor::multiply (Factor& g) + +Factor& +Factor::multiply (const Factor& g) { if (args_.empty()) { - *this = g; + operator= (g); } else { GenericFactor::multiply (g); } + return *this; } diff --git a/packages/CLPBN/horus/Factor.h b/packages/CLPBN/horus/Factor.h index a1feaf989..1e1cb0ad1 100644 --- a/packages/CLPBN/horus/Factor.h +++ b/packages/CLPBN/horus/Factor.h @@ -30,7 +30,7 @@ class Factor : public GenericFactor { void sumOutAllExceptIndex (size_t idx); - void multiply (Factor&); + Factor& multiply (const Factor&); std::string getLabel() const; diff --git a/packages/CLPBN/horus/GenericFactor.cpp b/packages/CLPBN/horus/GenericFactor.cpp index 8ae9927be..3d10180c2 100644 --- a/packages/CLPBN/horus/GenericFactor.cpp +++ b/packages/CLPBN/horus/GenericFactor.cpp @@ -82,15 +82,15 @@ GenericFactor::operator[] (size_t idx) -template void -GenericFactor::multiply (GenericFactor& g) +template GenericFactor& +GenericFactor::multiply (const GenericFactor& g) { if (args_ == g.arguments()) { // optimization Globals::logDomain ? params_ += g.params() : params_ *= g.params(); - return; + return *this; } unsigned range_prod = 1; bool share_arguments = false; @@ -124,6 +124,7 @@ GenericFactor::multiply (GenericFactor& g) } } } + return *this; } diff --git a/packages/CLPBN/horus/GenericFactor.h b/packages/CLPBN/horus/GenericFactor.h index 5f538d7fd..e771fa7b0 100644 --- a/packages/CLPBN/horus/GenericFactor.h +++ b/packages/CLPBN/horus/GenericFactor.h @@ -49,7 +49,7 @@ class GenericFactor { double& operator[] (size_t idx); - void multiply (GenericFactor& g); + GenericFactor& multiply (const GenericFactor& g); void sumOutIndex (size_t idx);