summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/yap/example/autodiff_library/PNode.cpp
blob: ee07a1f22b37f87f0b875455f3ec764e38907bd4 (plain)
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
/*
 * PNode.cpp
 *
 *  Created on: 8 Apr 2013
 *      Author: s0965328
 */

#include "PNode.h"
#include "Stack.h"
#include "Tape.h"
#include "EdgeSet.h"

namespace AutoDiff {

PNode::PNode(double value):pval(value) {
	assert(!isnan(value));
}

PNode::~PNode() {
}

void PNode::inorder_visit(int level,ostream& oss){
	oss<<this->toString(level)<<endl;
}

void PNode::collect_vnodes(boost::unordered_set<Node*>& nodes,unsigned int& total)
{
	//do not fill this to nodes, as this is a parameter node
	total++;
}

void PNode::eval_function()
{
	SV->push_back(pval);
}

string PNode::toString(int level)
{
	ostringstream oss;
	string s(level,'\t');
	oss<<s<<"[PNode]("<<pval<<")";
	return oss.str();
}

void PNode::grad_reverse_0()
{
	SV->push_back(pval);
}
void PNode::grad_reverse_1_init_adj()
{
	//do nothing as PNode does not have adjoint
}

void PNode::grad_reverse_1()
{
	//do nothing
	//this is a parameter
}

void PNode::update_adj(double& v)
{
	//do nothing
	//no adj for PNode
}

unsigned int PNode::hess_reverse_0()
{
	if(index==0)
	{
		TT->set(pval);
		assert(TT->index == TT->index);
		index = TT->index;
	}
	return index;
}

void PNode::hess_reverse_0_get_values(unsigned int i,double& x,double& x_bar,double& w,double& w_bar)
{
	x = TT->get(--i);
	x_bar = 0;
	w = 0;
	w_bar = 0;
}

void PNode::hess_reverse_1(unsigned int i)
{
	n_in_arcs--;
	//leaf node do nothing
}

void PNode::hess_reverse_1_init_x_bar(unsigned int)
{
	//do nothing as Parameter does not have x_bar
}

void PNode::update_x_bar(unsigned int i ,double v)
{
	//do nothing as Parameter does not have x_bar
}
void PNode::update_w_bar(unsigned int i ,double v)
{
	//do nothing as Parameter does not have w_bar
}
void PNode::hess_reverse_1_get_xw(unsigned int i, double& w,double& x)
{
	//do nothing as Parameter does not have w
	x = TT->get(i-1);
	w = 0;
}
void PNode::hess_reverse_get_x(unsigned int i, double& x)
{
	x = TT->get(i-1);
}

void PNode::nonlinearEdges(EdgeSet& edges)
{
	for(std::list<Edge>::iterator it=edges.edges.begin();it!=edges.edges.end();)
	{
		Edge e = *it;
		if(e.a == this || e.b == this)
		{
			it = edges.edges.erase(it); //erase invalidate the iterator
		}
		else
		{
			it++;
		}
	}
}

#if FORWARD_ENABLED
void PNode::hess_forward(unsigned int len, double** ret_vec)
{
	//it's a scalar
	(*ret_vec) = new double[len];
	std::fill_n(*ret_vec,len,0);
	SV->push_back(this->pval);
	assert(SV->size()==1);
}
#endif


TYPE PNode::getType()
{
	return PNode_Type;
}
}