-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNdArray.cpp
executable file
·265 lines (214 loc) · 7.37 KB
/
NdArray.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <random>
#include <assert.h>
#include <iostream>
#include "NdArray.h"
#include <string>
using namespace std;
NdArray::NdArray(int ndims, vector<int> cards) {
this->ndims = ndims;
assert(ndims == cards.size());
this->cards = cards;
int temp = 1;
for (int i = 0; i < ndims; ++i) {
temp *= cards[i];
}
this->array_size = temp;
this->array = vector<double>(this->array_size);
}
int NdArray::computeIndex(vector<int> indices) { //indices---S array, cards---M array
assert(this->ndims == indices.size());
vector<int> weights(this->ndims,0);
int currentProduct=1;
weights[this->ndims-1]=1;
for(int i = this->ndims-1; i >= 1; --i){
currentProduct*=this->cards[i];
weights[i-1]=currentProduct;
}
int key=0;
for(int j=0;j<this->ndims;++j){
key+=indices[j]*weights[j];
}
return key;
}
double NdArray::get(vector<int> indices) {
int key = computeIndex(indices); // compute index into 1D array
if (indices.size() == 1) {
string temp = "get index: " + std::to_string(key) + " key: " + std::to_string(key)
+ "value: " + std::to_string(this->array[key]) + "\n";
//cout << temp;
}
return this->array[key];
}
void NdArray::set(vector<int> indices, double value) {
int key = computeIndex(indices); // compute index into 1D array
this->array[key] = value;
}
void NdArray::setData(vector<double> array) {
assert(this->array_size == array.size());
this->array = array; // replace the whole array
}
string NdArray::to_string() {
string s = "[";
for (int i=0; i<this->array.size(); i++) {
if (i != 0)
s += ", ";
s += std::to_string(this->array[i]);
}
s += "]";
return s;
}
double get_random() {
// Returns a random double between 0 and 1
return (double) rand() / (double) RAND_MAX;
}
void init_random_cpt_rec(int ndims, int dim, int card, vector<int> parentsCard, vector<int> indices, NdArray& result) {
if (dim == ndims - 1) {
// base case:
// iterate all parents index
vector<double> cond(card);
for (int i = 0; i < card; ++i) {
cond[i] = get_random();
}
double sum = 0;
for (int i = 0; i < card; ++i) {
sum += cond[i];
}
// normalize cond cpt
for (int i = 0; i < card; ++i) {
cond[i] /= sum;
}
for (int i=0; i<card; ++i) {
vector<int> temp_indices(indices);
temp_indices.push_back(i);
result.set(temp_indices, cond[i]);
}
// set cond under parent index
return;
}
for(int i=0; i < parentsCard[dim];++i){
vector<int> indices2(indices);
indices2.push_back(i);
init_random_cpt_rec(ndims, dim+1, card, parentsCard, indices2, result);
}
}
// initialize a random CPT over (parentsCard,card)
NdArray init_random_cpt(int card, vector<int> parentsCard) {
int ndims = parentsCard.size() + 1;
vector<int> cards(ndims);
for (int i = 0; i < parentsCard.size(); i++) {
cards[i] = parentsCard[i];
}
cards[ndims-1] = card;
NdArray result(ndims,cards);
vector<int> indices;
init_random_cpt_rec(ndims, 0, card, parentsCard, indices, result);
return result;
}
// normalize 1D CPT
NdArray normalize(NdArray CPT) {
assert(CPT.ndims == 1);
NdArray result(CPT);
double sum = 0.0;
for (int i=0; i<result.array_size; ++i) {
sum += result.array[i];
}
for (int i=0; i<result.array_size; ++i) {
result.array[i] /= sum;
}
return result;
}
void project_rec(NdArray CPT, int p_dim, int dim, int index, vector<int> indices, double& sum) {
if (dim == CPT.ndims) {
// base case: add the value at indices to sum
double value = CPT.get(indices);
sum += value;
return;
}
if (dim == p_dim) {
vector<int> indices2(indices);
indices2.push_back(index);
project_rec(CPT, p_dim, dim+1, index, indices2, sum);
}
for (int i = 0; i < CPT.cards[dim]; ++i) {
vector<int> indices2(indices);
indices2.push_back(i);
project_rec(CPT, p_dim, dim+1, index, indices2, sum);
}
}
// sum out other dimensions of CPT except given dimension
NdArray project(NdArray CPT, int p_dim) {
//cout<<"p_dim is "<<p_dim<<" ndims is "<<CPT.ndims<<endl;
assert(0 <= p_dim && p_dim < CPT.ndims);
int p_card = CPT.cards[p_dim];
vector<int> temp(1, p_card);
// sum out other dimension except p_dim
NdArray result(1, temp);
for (int index = 0; index < p_card; ++index) {
double sum = 0;
vector<int> indices;
project_rec(CPT, p_dim, 0, index, indices, sum);
vector<int> temp2(1, index);
//cout<<"calling set point 2"<<endl;
result.set(temp2, sum);
// sum out other dimensions
}
result = normalize(result);
return result;
}
void dotProduct_rec(NdArray CPT, vector<NdArray> operands, NdArray& result, int dim, vector<int> indices){
if(dim == CPT.ndims){
assert(CPT.ndims == indices.size());
double value = CPT.get(indices);
for(int i = 0; i < operands.size(); ++i){
NdArray currOperand = operands[i];
value *= currOperand.get(vector<int> {indices[i]});
}
result.set(indices, value);
return;
}
for(int i=0; i < CPT.cards[dim];++i){
vector<int> indices2(indices);
indices2.push_back(i);
dotProduct_rec(CPT,operands, result, dim+1,indices2);
}
}
// compute product of CPTs and messages from parents and children
NdArray dotProduct(NdArray CPT, vector<NdArray> operands) {
string temp = "start dot product function\n" ;
//cout << temp;
assert(CPT.ndims == operands.size());
for (int i=0;i<operands.size();++i) {
assert(operands[i].ndims=1);
assert(operands[i].cards[0] == CPT.cards[i]);
}
NdArray result(CPT.ndims, CPT.cards);
vector<int> indices;
dotProduct_rec(CPT, operands, result, 0, indices);
temp = "finish dot product function\n";
//cout << temp;
return result;
}
NdArray childProduct(int card, int num_children, vector<NdArray>& operands) {
assert(operands.size() == num_children);
for (int i=0; i<num_children; ++i) {
string temp = "child op " + to_string(i) + ": " + operands[i].to_string() + "\n";
//cout << temp;
}
vector<int> cards(1, card);
NdArray product = NdArray(1, cards);
for (int i = 0; i < card; ++i) {
vector<int> index(1, i);
double value = 1.0;
for (int j = 0; j < num_children; ++j) {
assert(operands[j].ndims == 1);
assert(operands[j].cards[0] == card);
value *= operands[j].get(index);
}
string temp = "index " + to_string(i) + ": " + to_string(value) + "\n";
//cout << temp;
product.set(index, value);
}
string temp = "child product: " + product.to_string() + "\n";
//cout << temp;
return product;
}