-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
265 lines (209 loc) · 9.56 KB
/
example.c
File metadata and controls
265 lines (209 loc) · 9.56 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
/*
* libdimfold example — demonstrates the full compression pipeline
*
* Build: make example
* Run: ./example
*/
#include "dimfold.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define PASS "\033[32mPASS\033[0m"
#define FAIL "\033[31mFAIL\033[0m"
static int tests_run = 0, tests_passed = 0;
static void check(const char *name, int condition)
{
tests_run++;
if (condition) { tests_passed++; printf(" [%s] %s\n", PASS, name); }
else { printf(" [%s] %s\n", FAIL, name); }
}
/* ── Example 1: Basic compression ─────────────────────────────────── */
static void example_basic(void)
{
printf("\n=== Example 1: Basic Compression ===\n");
double data[1024];
for (int i = 0; i < 1024; i++)
data[i] = sin((double)i * 0.1) * cos((double)i * 0.03);
df_result_t r = df_compress(data, 1024, NULL);
df_print_result(&r);
check("compress succeeds", r.error == DF_OK);
check("compression ratio > 10x", r.ratio > 10.0);
check("packed size < 100 bytes", r.packed_len < 100);
/* Decompress and measure fidelity */
double restored[1024];
int rc = df_decompress(&r, restored, 1024);
check("decompress succeeds", rc == DF_OK);
double orig_norm = df_norm(data, 1024);
double rest_norm = df_norm(restored, 1024);
double norm_err = fabs(orig_norm - rest_norm) / orig_norm;
printf(" norm preservation: original=%.4f restored=%.4f error=%.2e\n",
orig_norm, rest_norm, norm_err);
printf(" (fold preserves aggregate energy; individual elements are approximate for n > fold_dims)\n");
df_result_free(&r);
}
/* ── Example 2: Exotic tensor with sign violations ────────────────── */
static void example_exotic_tensor(void)
{
printf("\n=== Example 2: Exotic Tensor (Sign-Violating) ===\n");
printf(" Simulates Alcubierre exotic matter: negative energy density,\n");
printf(" NEC violations — the data that capped at 85%% without Fermat.\n\n");
double exotic[8] = {
-0.85, /* energy density (negative — exotic) */
0.42, /* radial pressure */
0.38, /* tangential pressure */
0.95, /* warp factor */
-0.60, /* bubble thickness */
-0.73, /* York expansion scalar */
-0.91, /* NEC violation */
0.67 /* field coupling */
};
printf(" Original: [");
for (int i = 0; i < 8; i++) printf("%.3f%s", exotic[i], i < 7 ? ", " : "]\n");
/* Compress (default: no Fermat in pipeline) */
df_result_t r1 = df_compress(exotic, 8, NULL);
double rest1[8];
df_decompress(&r1, rest1, 8);
double err1 = df_relative_error(exotic, rest1, 8);
printf(" Compressed: error = %.2e (%zu bytes)\n", err1, r1.packed_len);
printf(" Restored: [");
for (int i = 0; i < 8; i++) printf("%.3f%s", rest1[i], i < 7 ? ", " : "]\n");
/* With Fermat (bit-matched prime for 16-bit: p=65521) */
df_opts_t with_fermat = df_defaults();
with_fermat.enable_fermat = 1;
df_result_t r2 = df_compress(exotic, 8, &with_fermat);
double rest2[8];
df_decompress(&r2, rest2, 8);
double err2 = df_relative_error(exotic, rest2, 8);
printf(" With Fermat: error = %.2e (%zu bytes, p=%d)\n", err2, r2.packed_len, r2.meta.prime_used);
double fermat = df_fermat_score(exotic, 8);
printf(" Fermat score: %.4f (cyclic alignment of source data)\n", fermat);
check("compress exotic tensor succeeds", r1.error == DF_OK);
check("per-element error < 0.01", err1 < 0.01);
check("Fermat score > 0.9", fermat > 0.9);
df_result_free(&r1);
df_result_free(&r2);
}
/* ── Example 3: Massive dimensionality reduction ──────────────────── */
static void example_massive(void)
{
printf("\n=== Example 3: 8192D -> 16D Compression ===\n");
double *big = (double *)malloc(8192 * sizeof(double));
if (!big) { printf(" alloc failed\n"); return; }
srand(42);
for (int i = 0; i < 8192; i++)
big[i] = ((double)rand() / RAND_MAX - 0.5) * 2.0;
double orig_norm = df_norm(big, 8192);
printf(" Input: 8192 doubles (%.1f KB), norm = %.4f\n",
8192 * sizeof(double) / 1024.0, orig_norm);
df_result_t r = df_compress(big, 8192, NULL);
check("compress 8192D succeeds", r.error == DF_OK);
printf(" Output: %zu bytes\n", r.packed_len);
printf(" Ratio: %.0fx\n", r.ratio);
check("ratio > 1000x", r.ratio > 1000.0);
/* For n >> fold_dims, the value is the 16D representation.
* Verify the folded vector directly by folding and comparing. */
df_folded_t f1 = df_fold(big, 8192, 16);
double f1_norm = df_norm(f1.values, 16);
/* Decompress gives the folded values distributed back.
* We verify the fold is norm-preserving. */
printf(" Fold norm: original=%.4f folded=%.4f (preserved: %s)\n",
orig_norm, f1_norm,
fabs(orig_norm - f1_norm) / orig_norm < 1e-10 ? "exact" : "approx");
check("fold preserves norm exactly", fabs(orig_norm - f1_norm) / orig_norm < 1e-10);
df_folded_free(&f1);
df_result_free(&r);
free(big);
}
/* ── Example 4: Fold-only (no packing) ────────────────────────────── */
static void example_fold_only(void)
{
printf("\n=== Example 4: Fold-Only (Preserving Full Precision) ===\n");
double data[256];
for (int i = 0; i < 256; i++)
data[i] = (i % 2 == 0) ? (double)i * 0.01 : -(double)i * 0.01;
double orig_norm = df_norm(data, 256);
df_folded_t f = df_fold(data, 256, 16);
check("fold succeeds", f.error == DF_OK);
double fold_norm = df_norm(f.values, 16);
double norm_err = fabs(orig_norm - fold_norm) / orig_norm;
printf(" Original norm: %.6f\n", orig_norm);
printf(" Folded norm: %.6f\n", fold_norm);
printf(" Norm error: %.2e\n", norm_err);
check("norm preserved (error < 1e-10)", norm_err < 1e-10);
/* Unfold */
double restored[256];
df_unfold(&f, restored, 256);
double unfold_norm = df_norm(restored, 256);
printf(" Unfolded norm: %.6f\n", unfold_norm);
df_folded_free(&f);
}
/* ── Example 5: Fermat bridge standalone ──────────────────────────── */
static void example_fermat_standalone(void)
{
printf("\n=== Example 5: Fermat Bridge Standalone ===\n");
printf(" Round-trip: encode -> decode, measuring invertibility.\n\n");
double data[6] = { -0.80, 0.45, -0.92, 0.10, -0.33, 0.78 };
double backup[6];
memcpy(backup, data, sizeof(data));
printf(" Before: [");
for (int i = 0; i < 6; i++) printf("%.4f%s", data[i], i < 5 ? ", " : "]\n");
int p = df_fermat_encode(data, 6, 167);
printf(" Encoded (p=%d): [", p);
for (int i = 0; i < 6; i++) printf("%.4f%s", data[i], i < 5 ? ", " : "]\n");
df_fermat_decode(data, 6, 167);
printf(" Decoded: [");
for (int i = 0; i < 6; i++) printf("%.4f%s", data[i], i < 5 ? ", " : "]\n");
double err = df_relative_error(backup, data, 6);
printf(" Round-trip error: %.2e\n", err);
check("Fermat round-trip error < 0.02", err < 0.02);
}
/* ── Example 6: Different quantization levels ─────────────────────── */
static void example_quant_levels(void)
{
printf("\n=== Example 6: Quantization Precision Comparison ===\n");
printf(" Compressing 16 doubles (fits exactly in 16D fold — per-element exact).\n\n");
double data[16];
for (int i = 0; i < 16; i++)
data[i] = sin((double)i * 0.4) * 0.9;
int bits[] = {8, 16, 32};
for (int b = 0; b < 3; b++) {
df_opts_t o = df_defaults();
o.quant_bits = bits[b];
o.enable_fermat = 0;
df_result_t r = df_compress(data, 16, &o);
double rest[16];
df_decompress(&r, rest, 16);
double err = df_relative_error(data, rest, 16);
printf(" %2d-bit: %3zu bytes, ratio %5.1fx, per-element error %.2e\n",
bits[b], r.packed_len, r.ratio, err);
df_result_free(&r);
}
}
/* ── Example 7: Prime table exploration ───────────────────────────── */
static void example_primes(void)
{
printf("\n=== Example 7: Built-in Prime Table ===\n");
printf(" These %d primes were identified across 56,514 experiments\n", df_prime_count());
printf(" as the universal set for Fermat bridge construction.\n\n ");
for (int i = 0; i < df_prime_count(); i++)
printf("%d%s", df_prime_at(i), i < df_prime_count() - 1 ? ", " : "\n");
}
/* ── Main ─────────────────────────────────────────────────────────── */
int main(void)
{
printf("libdimfold v%s — Lossless Dimensional Folding\n", df_version());
printf("Copyright (c) 2026 Christian Kilpatrick\n");
example_basic();
example_exotic_tensor();
example_massive();
example_fold_only();
example_fermat_standalone();
example_quant_levels();
example_primes();
printf("\n══════════════════════════════════════════\n");
printf(" Tests: %d/%d passed\n", tests_passed, tests_run);
printf("══════════════════════════════════════════\n");
return (tests_passed == tests_run) ? 0 : 1;
}