[1991be]: src / lib / graph / Node.cc Maximize Restore History

Download this file

Node.cc    199 lines (161 with data), 4.2 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <config.h>
#include <graph/Node.h>
#include <graph/NodeError.h>
#include <util/nainf.h>
#include <util/dim.h>
#include <stdexcept>
#include <algorithm>
using std::set;
using std::string;
using std::vector;
using std::logic_error;
using std::copy;
using std::find;
namespace jags {
class DeterminsticNode;
class StochasticNode;
Node::Node(vector<unsigned int> const &dim, unsigned int nchain)
: _parents(0), _stoch_children(0), _dtrm_children(0),
_dim(getUnique(dim)), _length(product(dim)), _nchain(nchain), _data(0),
_index(0)
{
if (nchain==0)
throw logic_error("Node must have at least one chain");
unsigned int N = _length * _nchain;
_data = new double[N];
for (unsigned int i = 0; i < N; ++i) {
_data[i] = JAGS_NA;
}
_dtrm_children = new set<DeterministicNode*>;
_stoch_children = new set<StochasticNode*>;
}
Node::Node(vector<unsigned int> const &dim,
vector<Node const *> const &parents)
: _parents(parents), _stoch_children(0), _dtrm_children(0),
_dim(getUnique(dim)), _length(product(dim)),
_nchain(countChains(parents)), _data(0), _index(0)
{
if (nchain() == 0) {
throw logic_error("Cannot calculate number of chains in Node constructor");
}
unsigned int N = _length * _nchain;
_data = new double[N];
for (unsigned int i = 0; i < N; ++i) {
_data[i] = JAGS_NA;
}
_stoch_children = new set<StochasticNode*>;
_dtrm_children = new set<DeterministicNode*>;
}
Node::~Node()
{
delete [] _data;
delete _stoch_children;
delete _dtrm_children;
}
vector <Node const *> const &Node::parents() const
{
return _parents;
}
set<StochasticNode*> const *Node::stochasticChildren()
{
return _stoch_children;
}
set<DeterministicNode*> const *Node::deterministicChildren()
{
return _dtrm_children;
}
static bool isInitialized(Node const *node, unsigned int n)
{
double const *value = node->value(n);
for (unsigned int i = 0; i < node->length(); ++i) {
if (value[i] == JAGS_NA)
return false;
}
return true;
}
bool Node::initialize(unsigned int n)
{
// Test whether node is already initialized and, if so, skip it
if (isInitialized(this, n))
return true;
// Check that parents are initialized
for (unsigned int i = 0; i < _parents.size(); ++i) {
if (!isInitialized(_parents[i], n)) {
return false; // Uninitialized parent
}
}
deterministicSample(n);
return true;
}
unsigned int Node::nchain() const
{
return _nchain;
}
unsigned int countChains(vector<Node const *> const &parameters)
{
unsigned int nchain = parameters.empty() ? 0 : parameters[0]->nchain();
for (unsigned int i = 1; i < parameters.size(); ++i) {
if (parameters[i]->nchain() != nchain) {
nchain = 0;
break;
}
}
return nchain;
}
void Node::setValue(double const *value, unsigned int length, unsigned int chain)
{
if (length != _length)
throw NodeError(this, "Length mismatch in Node::setValue");
if (chain >= _nchain)
throw NodeError(this, "Invalid chain in Node::setValue");
copy(value, value + _length, _data + chain * _length);
}
void Node::swapValue(unsigned int chain1, unsigned int chain2)
{
double *value1 = _data + chain1 * _length;
double *value2 = _data + chain2 * _length;
for (unsigned int i = 0; i < _length; ++i) {
double v = value1[i];
value1[i] = value2[i];
value2[i] = v;
}
}
/*
bool Node::isDiscreteValued() const
{
return _isdiscrete;
}
void Node::setDiscreteValued()
{
_isdiscrete = true;
}
*/
double const *Node::value(unsigned int chain) const
{
return _data + chain * _length;
}
vector<unsigned int> const &Node::dim() const
{
return _dim;
}
unsigned int Node::length() const
{
return _length;
}
void Node::addChild(DeterministicNode *node) const
{
_dtrm_children->insert(node);
}
void Node::addChild(StochasticNode *node) const
{
_stoch_children->insert(node);
}
void Node::removeChild(DeterministicNode *node) const
{
_dtrm_children->erase(node);
}
void Node::removeChild(StochasticNode *node) const
{
_stoch_children->erase(node);
}
} //namespace jags