[ff1121]: src / lib / graph / LogicalNode.cc Maximize Restore History

Download this file

LogicalNode.cc    106 lines (90 with data), 2.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
#include <config.h>
#include <graph/LogicalNode.h>
#include <function/FuncError.h>
#include <function/Function.h>
#include <graph/GraphMarks.h>
#include <graph/Graph.h>
#include <util/dim.h>
#include <stdexcept>
#include <vector>
#include <string>
#include <math.h>
using std::vector;
using std::string;
using std::set;
using std::logic_error;
namespace jags {
static vector<vector<double const *> >
mkParams(vector<Node const*> const &parents, unsigned int nchain)
{
vector<vector<double const *> > ans(nchain);
for (unsigned int n = 0; n < nchain; ++n) {
ans[n].reserve(parents.size());
for (unsigned long j = 0; j < parents.size(); ++j) {
ans[n].push_back(parents[j]->value(n));
}
}
return ans;
}
LogicalNode::LogicalNode(vector<unsigned int> const &dim,
vector<Node const *> const &parameters,
Function const *function)
: DeterministicNode(dim, parameters), _func(function), _discrete(false),
_parameters(mkParams(parameters, nchain()))
{
if (!checkNPar(function, parameters.size())) {
throw FuncError(function, "Incorrect number of parameters");
}
vector<bool> mask(parents().size());
for (unsigned long j = 0; j < parents().size(); ++j) {
mask[j] = parents()[j]->isDiscreteValued();
}
_discrete = _func->isDiscreteValued(mask);
}
string LogicalNode::deparse(vector<string> const &parents) const
{
string name = "(";
name.append(_func->deparse(parents));
name.append(")");
return name;
}
bool LogicalNode::isClosed(set<Node const *> const &ancestors,
ClosedFuncClass fc, bool fixed) const
{
vector<Node const *> const &par = parents();
vector<bool> mask(par.size());
vector<bool> fixed_mask;
unsigned int nmask = 0;
for (unsigned int i = 0; i < par.size(); ++i) {
mask[i] = ancestors.count(par[i]);
if (mask[i]) {
++nmask;
}
if (fixed) {
fixed_mask.push_back(par[i]->isFixed());
}
}
if (nmask == 0) {
throw logic_error("Invalid mask in LogicalNode::isClosed");
}
switch(fc) {
case DNODE_LINEAR:
return _func->isLinear(mask, fixed_mask);
break;
case DNODE_SCALE:
return _func->isScale(mask, fixed_mask);
break;
case DNODE_SCALE_MIX:
return (nmask == 1) && _func->isScale(mask, fixed_mask);
break;
case DNODE_POWER:
return _func->isPower(mask, fixed_mask);
break;
}
return false; //Wall
}
bool LogicalNode::isDiscreteValued() const
{
return _discrete;
}
} //namespace jags