diff options
Diffstat (limited to 'src/full-binary-tree-array-representation-experiment.c')
| -rw-r--r-- | src/full-binary-tree-array-representation-experiment.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/full-binary-tree-array-representation-experiment.c b/src/full-binary-tree-array-representation-experiment.c new file mode 100644 index 0000000..1faad64 --- /dev/null +++ b/src/full-binary-tree-array-representation-experiment.c | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <math.h> | ||
| 6 | #include <time.h> | ||
| 7 | |||
| 8 | #include "dynamic-array.h" | ||
| 9 | |||
| 10 | // fGold(x1,x2) = [ 1 + ( x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2)] * | ||
| 11 | // [30 + (2*x1 - 3*x2 )^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2)] | ||
| 12 | // -2 <= xi <= +2, i = 1:2 | ||
| 13 | |||
| 14 | #define FUNCTIONS \ | ||
| 15 | FUNCTION (ADD, "+", l + r ) \ | ||
| 16 | FUNCTION (SUB, "-", l - r ) \ | ||
| 17 | FUNCTION (MUL, "*", l * r ) \ | ||
| 18 | FUNCTION (DIV, "/", l / r ) \ | ||
| 19 | FUNCTION (ABS_LEFT, "abs", fabs (l)) \ | ||
| 20 | FUNCTION (ABS_RIGHT, "abs", fabs (r)) \ | ||
| 21 | FUNCTION (SIN_LEFT, "sin", sinf (l)) \ | ||
| 22 | FUNCTION (SIN_RIGHT, "sin", sinf (r)) \ | ||
| 23 | FUNCTION (COS_LEFT, "cos", cosf (l)) \ | ||
| 24 | FUNCTION (COS_RIGHT, "cos", cosf (r)) \ | ||
| 25 | FUNCTION (EXP_LEFT, "exp", expf (l)) \ | ||
| 26 | FUNCTION (EXP_RIGHT, "exp", expf (r)) | ||
| 27 | |||
| 28 | #define FUNCTION(SYM, STR, EXP) FUNCTION_##SYM, | ||
| 29 | enum function { | ||
| 30 | FUNCTIONS | ||
| 31 | FUNCTION_COUNT | ||
| 32 | }; | ||
| 33 | #undef FUNCTION | ||
| 34 | |||
| 35 | #define FUNCTION(SYM, STR, EXP) [FUNCTION_##SYM] = STR, | ||
| 36 | const char *function_strs[] = { | ||
| 37 | FUNCTIONS | ||
| 38 | }; | ||
| 39 | #undef FUNCTION | ||
| 40 | |||
| 41 | union uif { unsigned int u; float f; }; | ||
| 42 | #define FUNCTION__SYM_TO_NAN(SYM) ((union uif) ((SYM) << 1 | 0xFFE00001)).f | ||
| 43 | #define FUNCTION__NAN_TO_SYM(NAN) ((((union uif) (NAN)).u & 0x001FFFFE) >> 1) | ||
| 44 | #define FUNCTION__NAN_TO_STR(NAN) function_strs[FUNCTION__NAN_TO_SYM (NAN)] | ||
| 45 | |||
| 46 | #define TREE_DEPTH 4 | ||
| 47 | #define NODE_COUNT ((2 << TREE_DEPTH - 1) - 1) | ||
| 48 | |||
| 49 | float tree[NODE_COUNT]; | ||
| 50 | |||
| 51 | void | ||
| 52 | print_tree (float tree[NODE_COUNT], const char *filename) | ||
| 53 | { | ||
| 54 | const char *ext = strchr (filename, '.'); | ||
| 55 | if (!ext) return; | ||
| 56 | char *command; | ||
| 57 | asprintf (&command, "dot -T%s -o %s", ext + 1, filename); | ||
| 58 | FILE *dot = popen (command, "w"); | ||
| 59 | // FILE *dot = fopen ("dump.dot", "w"); | ||
| 60 | free (command); | ||
| 61 | |||
| 62 | fprintf (dot, "digraph G {\n"); | ||
| 63 | |||
| 64 | int stack[TREE_DEPTH] = { TREE_DEPTH }; | ||
| 65 | int top = 1; | ||
| 66 | for (int i = 0; i < NODE_COUNT; ++i) { | ||
| 67 | if (isnan (tree[i])) | ||
| 68 | fprintf (dot, " n%d [label=\"%d: %s\"];\n", i, i, FUNCTION__NAN_TO_STR (tree[i])); | ||
| 69 | else | ||
| 70 | fprintf (dot, " n%d [label=\"%d: %f\"];\n", i, i, tree[i]); | ||
| 71 | |||
| 72 | int depth = stack[--top]; | ||
| 73 | if (depth == 1) continue; // leaf | ||
| 74 | stack[top++] = depth - 1; | ||
| 75 | stack[top++] = depth - 1; | ||
| 76 | |||
| 77 | int left = i + 1; | ||
| 78 | int right = i + ((1 << depth - 1)); | ||
| 79 | fprintf (dot, " n%d -> n%d;\n", i, left); | ||
| 80 | fprintf (dot, " n%d -> n%d;\n", i, right); | ||
| 81 | } | ||
| 82 | |||
| 83 | fprintf (dot, "}\n"); | ||
| 84 | |||
| 85 | pclose (dot); | ||
| 86 | } | ||
| 87 | |||
| 88 | int | ||
| 89 | main (int argc, char *argv[]) | ||
| 90 | { | ||
| 91 | srand (time (NULL)); | ||
| 92 | |||
| 93 | printf ("node_count: %d\n", NODE_COUNT); | ||
| 94 | |||
| 95 | for (int i = 0; i < NODE_COUNT; ++i) { | ||
| 96 | if (rand () % 2) | ||
| 97 | tree[i] = rand () /(float) RAND_MAX; | ||
| 98 | else | ||
| 99 | tree[i] = FUNCTION__SYM_TO_NAN (rand () % FUNCTION_COUNT); | ||
| 100 | } | ||
| 101 | |||
| 102 | print_tree (tree, "tree.svg"); | ||
| 103 | |||
| 104 | return EXIT_SUCCESS; | ||
| 105 | } | ||
