-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint-haar.h
More file actions
489 lines (394 loc) · 11.2 KB
/
int-haar.h
File metadata and controls
489 lines (394 loc) · 11.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#ifndef INTHAAR_H
#define INTHAAR_H
// TODO: Fix 1D intervalar compression!
#include <iostream>
#include <cmath>
#include "common.h"
using namespace std;
#ifndef WIN32
#include <interval.hpp>
using namespace cxsc;
#endif
#define HAAR_COMPRESS_ERROR 0.0000000001
#define INTHAAR
#define HAAROPMIZATION
void Haar_Composition(double *vec, int n, bool normal);
void Haar_CompositionStep(double *vec, int n, bool normal);
void Haar_Compression(double *vec, int n, float percentage);
void Haar_PerLevel_Compression(double *vec, int n, double percentage);
void Haar_Matrix_Compression(double **matrix, int n, float percentage);
void Haar_PerLevel_Matrix_Compression(double **matrix, int n, float percentage);
void Haar_Level_Matrix_Compression(double **matrix, int n, float percentage);
void Haar_Decomposition(double *vec, int n, bool normal);
void Haar_DecompositionStep(double *vec, int n, bool normal);
double **Haar_Decomposition_For_Graphs(double *vec, int n, bool normal);
void Haar_MatrixComposition(double **matrix, int rows, int cols, bool normal, bool standard);
void Haar_MatrixDecomposition(double **matrix, int rows, int cols, bool normal, bool standard);
void Haar_NonStandardComposition(double **matrix, int rows, int cols, bool normal);
void Haar_NonStandardDecomposition(double **matrix, int rows, int cols, bool normal);
void Haar_StandardComposition(double **matrix, int rows, int cols, bool normal);
void Haar_StandardDecomposition(double **matrix, int rows, int cols, bool normal);
template <typename T>
void Haar_atrous_DecompositionStep(T *C, T *D, T *lastC, int n, T divisor)
{
for (int j = 0; j < n - 1; ++j)
{
C[j] = (lastC[j] + lastC[j + 1]) / divisor;
D[j] = lastC[j] - C[j];
}
C[n - 1] = (lastC[n - 1] + lastC[0]) / divisor;
D[n - 1] = lastC[n - 1] - C[n - 1];
}
template <typename T>
T** Haar_atrous_Decomposition(T *vec, int n, int levels, bool normal)
{
if (n <= 0 || levels <= 0) return NULL;
T **data = new T*[levels * 2];
T *lastC;
T divisor;
for (int i = 0; i < levels * 2; ++i)
{
data[i] = new T[n];
}
// Setting normalization factor, if needed.
if (normal) divisor = sqrt(T(2.0));
else divisor = T(2.0);
lastC = vec;
// Calculating degree and wavelet coefficients.
for (int i = 0; i < levels; ++i)
{
Haar_atrous_DecompositionStep(data[i * 2], data[i * 2 + 1], lastC, n, divisor);
lastC = data[i * 2];
}
return data;
}
template <typename T>
T* Haar_atrous_Composition(T **data, int n, int levels)
{
if (n <= 0 || levels <= 0) return NULL;
T *result = new T[n];
for (int i = 0; i < n; ++i)
{
result[i] = data[levels * 2 - 2][i];
}
for (int i = 0; i < levels; ++i)
{
for (int j = 0; j < n; ++j)
{
result[j] += data[i * 2 + 1][j];
}
}
return result;
}
template <typename T>
T*** Haar_atrous_StandardDecomposition(T **matrix, int n, int m, int levels, bool normal)
{
T ***result = NULL;
T ** parcialResult = NULL;
T *temp = NULL;
result = new T**[levels * 4];
for (int i = 0; i < levels * 4; ++i)
{
result[i] = new T*[n];
}
for (int i = levels * 2; i < levels * 4; ++i)
{
for (int j = 0; j < n; ++j)
{
result[i][j] = new T[m];
}
}
// Transform all rows and store in results.
for (int i = 0; i < n; ++i)
{
parcialResult = Haar_atrous_Decomposition(matrix[i], m, levels, normal);
for (int j = 0; j < levels; ++j)
{
result[j * 2][i] = parcialResult[j * 2];
result[j * 2 + 1][i] = parcialResult[j * 2 + 1];
}
}
// Will store the input for column transformation.
temp = new T[max(n, m)];
// Transform all columns and store in results.
for (int i = 0; i < m; ++i)
{
for (int w = 0; w < n; ++w)
{
temp[w] = result[levels * 2 - 2][w][i];
}
parcialResult = Haar_atrous_Decomposition(temp, n, levels, normal);
for (int j = 0; j < levels; ++j)
{
for (int w = 0; w < n; ++w)
{
result[levels * 2 + j * 2][w][i] = parcialResult[j * 2][w];
result[levels * 2 + j * 2 + 1][w][i] = parcialResult[j * 2 + 1][w];
}
}
for (int j = 0; j < levels * 2; ++j)
{
delete [] parcialResult[j];
}
delete [] parcialResult;
}
delete [] temp;
return result;
}
template <typename T>
T*** Haar_atrous_NonStandardDecomposition(T **matrix, int n, int m, int levels, bool normal)
{
T ***result = NULL;
T **parcialResult = NULL;
T **lastCMatrix = NULL;
T *temp = NULL;
T *lastC = NULL;
T divisor;
result = new T**[levels * 4];
for (int i = 0; i < levels * 4; ++i)
{
result[i] = new T*[n];
for (int j = 0; j < n; ++j)
{
result[i][j] = new T[m];
}
}
// Setting normalization factor, if needed.
if (normal) divisor = sqrt(T(2.0));
else divisor = T(2.0);
// Will store the input for column transformation.
temp = new T[max(n, m)];
// Create parcial result array for columns tranformation.
parcialResult = new T*[2];
parcialResult[0] = new T[m];
parcialResult[1] = new T[m];
lastCMatrix = matrix;
lastC = matrix[0];
// Iteration for every level step.
for (int i = 0; i < levels; ++i)
{
// Transform 1 level for every row.
for (int j = 0; j < n; ++j)
{
// Calculating degree and wavelet coefficients.
Haar_atrous_DecompositionStep(result[i * 4][j], result[i * 4 + 1][j], lastC, n, divisor);
lastC = lastCMatrix[j + 1];
}
lastC = temp;
// Transform 1 level for every column.
for (int j = 0; j < m; ++j)
{
// Prepare temp buffer.
for (int w = 0; w < n; ++w)
{
temp[w] = result[i * 4][w][j];
}
// Calculating degree and wavelet coefficients.
Haar_atrous_DecompositionStep(parcialResult[0], parcialResult[1], lastC, m, divisor);
// Put results in the right place.
for (int w = 0; w < n; ++w)
{
result[i * 4 + 2][w][j] = parcialResult[0][w];
result[i * 4 + 3][w][j] = parcialResult[1][w];
}
}
// Prepare temp buffer for the next iteration.
for (int j = 0; j < m; ++j)
{
temp[j] = result[i * 4 + 2][0][j];
}
// Setting variables for the next iteration.
lastCMatrix = result[i * 4 + 2];
lastC = lastCMatrix[0];
}
//========== Dealocating memory.
delete [] parcialResult[0];
delete [] parcialResult[1];
delete [] parcialResult;
delete [] temp;
return result;
}
template <typename T>
T*** Haar_atrous_MatrixDecomposition(T **matrix, int n, int m, int levels, bool normal, bool standard)
{
if (standard) return Haar_atrous_StandardDecomposition(matrix, n, m, levels, normal);
else return Haar_atrous_NonStandardDecomposition(matrix, n, m, levels, normal);
}
template <typename T>
T** Haar_atrous_StandardComposition(T ***data, int n, int m, int levels)
{
if (n <= 0 || levels <= 0) return NULL;
T **result = new T*[n];
for (int i = 0; i < n; ++i)
{
result[i] = new T[m];
for (int j = 0; j < m; ++j)
{
result[i][j] = data[levels * 4 - 2][i][j];
}
}
for (int i = 0; i < levels * 4; i += 2)
{
for (int j = 0; j < n; ++j)
{
for (int w = 0; w < m; ++w)
{
result[j][w] += data[i + 1][j][w];
}
}
}
return result;
}
template <typename T>
T** Haar_atrous_NonStandardComposition(T ***data, int n, int m, int levels)
{
if (n <= 0 || levels <= 0) return NULL;
T **result = new T*[n];
for (int i = 0; i < n; ++i)
{
result[i] = new T[m];
for (int j = 0; j < m; ++j)
{
result[i][j] = data[levels * 4 - 2][i][j];
}
}
for (int i = 0; i < levels * 4; i += 2)
{
for (int j = 0; j < n; ++j)
{
for (int w = 0; w < m; ++w)
{
result[j][w] += data[i + 1][j][w];
}
}
}
return result;
}
template <typename T>
T** Haar_atrous_MatrixComposition(T ***data, int n, int m, int levels, bool standard)
{
if (standard) return Haar_atrous_StandardComposition(data, n, m, levels);
else return Haar_atrous_NonStandardComposition(data, n, m, levels);
}
template <typename T>
void Haar_atrous_Normalization(T *vec, T **data, int n, int levels, bool invert = false)
{
if (n <= 0 || levels <= 0) return;
T factor, *D0, *D1;
// Normalizing degree coefficients.
for (int i = 0; i < levels; ++i)
{
// Normalization factor rule.
factor = pow(T(2.0), T(i + 1) / T(2.0));
if (invert)
{
for (int j = 0; j < n; ++j)
{
data[i * 2][j] /= factor;
}
}
else
{
for (int j = 0; j < n; ++j)
{
data[i * 2][j] *= factor;
}
}
}
D0 = vec;
D1 = data[0];
// Recalculating wavelet coefficients.
for (int i = 0; i < levels; ++i)
{
for (int j = 0; j < n ; ++j)
{
data[i * 2 + 1][j] = D0[j] - D1[j];
}
if (i + 1 < levels)
{
D0 = data[i * 2];
D1 = data[(i + 1) * 2];
}
}
}
template <typename T>
void Haar_atrous_MatrixNormalization(T **matrix, T ***data, int n, int m, int levels, bool invert = false)
{
if (n <= 0 || m <= 0 || levels <= 0) return;
T factor, **D0, **D1;
// Normalizing degree coefficients.
for (int i = 0; i < levels * 2; ++i)
{
// Normalization factor rule.
factor = pow(T(2.0), T(i + 1) / T(2.0));
if (invert)
{
for (int j = 0; j < n; ++j)
{
for (int w = 0; w < m; ++w)
{
data[i * 2][j][w] /= factor;
}
}
}
else
{
for (int j = 0; j < n; ++j)
{
for (int w = 0; w < m; ++w)
{
data[i * 2][j][w] *= factor;
}
}
}
}
D0 = matrix;
D1 = data[0];
// Recalculating wavelet coefficients.
for (int i = 0; i < levels * 2; ++i)
{
for (int j = 0; j < n ; ++j)
{
for (int w = 0; w < m; ++w)
{
data[i * 2 + 1][j][w] = D0[j][w] - D1[j][w];
}
}
if (i + 1 < levels * 2)
{
D0 = data[i * 2];
D1 = data[(i + 1) * 2];
}
}
}
#ifdef HAAROPMIZATION
void VinisMatrixNormalization(double **mat, uint n, bool standard, bool invert = false);
void VinisNonStandardMatrixNormalization(double **matrix, uint n, bool invert = false);
void VinisNormalization(double *vec, uint n, bool invert = false);
void VinisStandardMatrixNormalization(double **mat, uint n, bool invert = false);
#endif /* HAAROPMIZATION */
#ifdef INTHAAR
void INT_Haar_Composition(interval *vec, int n, bool normal);
void INT_Haar_CompositionStep(interval *vec, int n, bool normal);
void INT_Haar_Decomposition(interval *vec, int n, bool normal);
void INT_Haar_DecompositionStep(interval *vec, int n, bool normal);
void INT_Haar_MatrixComposition(interval **matrix, int rows, int cols, bool normal, bool standard);
void INT_Haar_MatrixDecomposition(interval **matrix, int rows, int cols, bool normal, bool standard);
void INT_Haar_NonStandardComposition(interval **matrix, int rows, int cols, bool normal);
void INT_Haar_NonStandardDecomposition(interval **matrix, int rows, int cols, bool normal);
void INT_Haar_StandardComposition(interval **matrix, int rows, int cols, bool normal);
void INT_Haar_StandardDecomposition(interval **matrix, int rows, int cols, bool normal);
void INT_Haar_Compression(interval *vec, int n, float percentage);
void INT_Haar_Matrix_Compression(interval **matrix, int n, float percentage);
#ifdef HAAROPMIZATION
void INT_VinisMatrixNormalization(interval **mat, uint n, bool standard, bool invert = false);
void INT_VinisNonStandardMatrixNormalization(interval **matrix, uint n, bool invert = false);
void INT_VinisNormalization(interval *vec, uint n, bool invert = false);
void INT_VinisStandardMatrixNormalization(interval **mat, uint n, bool invert = false);
#endif /* HAAROPMIZATION */
real INT_diameter(interval x);
real INT_error(interval *x, int n);
real INT_error(interval **x, int linhas, int colunas);
real INT_error(interval ***m, uint mat, uint row, uint col);
#endif /* INTHAAR */
#endif /* INTHAAR_H */