mycai 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/hdc.js ADDED
@@ -0,0 +1,312 @@
1
+ /**
2
+ * Hyperdimensional Computing (HDC) & Vector Symbolic Architecture (VSA) Module
3
+ * Supports Binary, Bipolar, Real, Complex/FHRR representations with unified operations.
4
+ */
5
+
6
+ export class Representation {
7
+ constructor(type, values, D) {
8
+ this.type = type; // 'binary' | 'bipolar' | 'real' | 'complex'
9
+ this.values = values; // Float32Array or Uint8Array
10
+ this.D = D;
11
+ }
12
+
13
+ // Calculate memory footprint in bytes
14
+ memorySize() {
15
+ return this.values.byteLength;
16
+ }
17
+ }
18
+
19
+ export class HDCEngine {
20
+ constructor(D = 8192) {
21
+ this.D = D;
22
+ }
23
+
24
+ // ── GENERATION / RANDOM RANDOM VECTORS ─────────────────────
25
+
26
+ generateRandom(type) {
27
+ if (type === 'binary') {
28
+ const vals = new Uint8Array(this.D);
29
+ for (let i = 0; i < this.D; i++) {
30
+ vals[i] = Math.random() < 0.5 ? 0 : 1;
31
+ }
32
+ return new Representation('binary', vals, this.D);
33
+ }
34
+
35
+ if (type === 'bipolar') {
36
+ const vals = new Float32Array(this.D);
37
+ for (let i = 0; i < this.D; i++) {
38
+ vals[i] = Math.random() < 0.5 ? -1.0 : 1.0;
39
+ }
40
+ return new Representation('bipolar', vals, this.D);
41
+ }
42
+
43
+ if (type === 'real') {
44
+ const vals = new Float32Array(this.D);
45
+ // Gaussian distribution approx
46
+ for (let i = 0; i < this.D; i++) {
47
+ let u = 0, v = 0;
48
+ while (u === 0) u = Math.random();
49
+ while (v === 0) v = Math.random();
50
+ vals[i] = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
51
+ }
52
+ return new Representation('real', vals, this.D);
53
+ }
54
+
55
+ // Default to Complex/FHRR (angles in [0, 2*PI])
56
+ const vals = new Float32Array(this.D);
57
+ for (let i = 0; i < this.D; i++) {
58
+ vals[i] = Math.random() * 2 * Math.PI;
59
+ }
60
+ return new Representation('complex', vals, this.D);
61
+ }
62
+
63
+ generateSeeded(type, key) {
64
+ // Generate deterministic seed from string key
65
+ let seed = 0;
66
+ for (let i = 0; i < key.length; i++) {
67
+ seed = (seed * 31 + key.charCodeAt(i)) >>> 0;
68
+ }
69
+
70
+ const nextRand = () => {
71
+ seed = (seed * 1664525 + 1013904223) >>> 0;
72
+ return (seed >>> 8) / 16777216;
73
+ };
74
+
75
+ if (type === 'binary') {
76
+ const vals = new Uint8Array(this.D);
77
+ for (let i = 0; i < this.D; i++) {
78
+ vals[i] = nextRand() < 0.5 ? 0 : 1;
79
+ }
80
+ return new Representation('binary', vals, this.D);
81
+ }
82
+
83
+ if (type === 'bipolar') {
84
+ const vals = new Float32Array(this.D);
85
+ for (let i = 0; i < this.D; i++) {
86
+ vals[i] = nextRand() < 0.5 ? -1.0 : 1.0;
87
+ }
88
+ return new Representation('bipolar', vals, this.D);
89
+ }
90
+
91
+ if (type === 'real') {
92
+ const vals = new Float32Array(this.D);
93
+ for (let i = 0; i < this.D; i++) {
94
+ let u = 0, v = 0;
95
+ while (u === 0) u = nextRand();
96
+ while (v === 0) v = nextRand();
97
+ vals[i] = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
98
+ }
99
+ return new Representation('real', vals, this.D);
100
+ }
101
+
102
+ // Default to Complex/FHRR (angles in [0, 2*PI])
103
+ const vals = new Float32Array(this.D);
104
+ for (let i = 0; i < this.D; i++) {
105
+ vals[i] = nextRand() * 2 * Math.PI;
106
+ }
107
+ return new Representation('complex', vals, this.D);
108
+ }
109
+
110
+ // ── BINDING (Morfem Binding / Variable-Value Association) ──
111
+
112
+ bind(a, b) {
113
+ if (a.type !== b.type || a.D !== b.D) {
114
+ throw new Error('Representation mismatch for bind operation.');
115
+ }
116
+ const D = a.D;
117
+
118
+ if (a.type === 'binary') {
119
+ const o = new Uint8Array(D);
120
+ for (let i = 0; i < D; i++) {
121
+ o[i] = a.values[i] ^ b.values[i];
122
+ }
123
+ return new Representation('binary', o, D);
124
+ }
125
+
126
+ if (a.type === 'bipolar') {
127
+ const o = new Float32Array(D);
128
+ for (let i = 0; i < D; i++) {
129
+ o[i] = a.values[i] * b.values[i];
130
+ }
131
+ return new Representation('bipolar', o, D);
132
+ }
133
+
134
+ if (a.type === 'real') {
135
+ // Circular convolution approximation via element-wise multiplication
136
+ const o = new Float32Array(D);
137
+ for (let i = 0; i < D; i++) {
138
+ o[i] = a.values[i] * b.values[i];
139
+ }
140
+ return new Representation('real', o, D);
141
+ }
142
+
143
+ // Complex/FHRR: adding phases modulo 2*PI
144
+ const o = new Float32Array(D);
145
+ for (let i = 0; i < D; i++) {
146
+ o[i] = (a.values[i] + b.values[i]) % (2 * Math.PI);
147
+ }
148
+ return new Representation('complex', o, D);
149
+ }
150
+
151
+ unbind(a, b) {
152
+ if (a.type !== b.type || a.D !== b.D) {
153
+ throw new Error('Representation mismatch for unbind operation.');
154
+ }
155
+ const D = a.D;
156
+
157
+ if (a.type === 'binary') {
158
+ return this.bind(a, b); // XOR is self-inverse
159
+ }
160
+
161
+ if (a.type === 'bipolar') {
162
+ return this.bind(a, b); // Multiplication of -1/1 is self-inverse
163
+ }
164
+
165
+ if (a.type === 'real') {
166
+ const o = new Float32Array(D);
167
+ for (let i = 0; i < D; i++) {
168
+ // Safe division
169
+ o[i] = Math.abs(b.values[i]) > 1e-6 ? a.values[i] / b.values[i] : a.values[i];
170
+ }
171
+ return new Representation('real', o, D);
172
+ }
173
+
174
+ // Complex/FHRR: subtract phases modulo 2*PI
175
+ const o = new Float32Array(D);
176
+ for (let i = 0; i < D; i++) {
177
+ let v = a.values[i] - b.values[i];
178
+ if (v < 0) v += 2 * Math.PI;
179
+ o[i] = v % (2 * Math.PI);
180
+ }
181
+ return new Representation('complex', o, D);
182
+ }
183
+
184
+ // ── BUNDLING (Superposition / Memory Accumulation) ──────────
185
+
186
+ bundle(reps) {
187
+ if (!reps || reps.length === 0) {
188
+ throw new Error('No representations to bundle.');
189
+ }
190
+ const type = reps[0].type;
191
+ const D = reps[0].D;
192
+
193
+ if (type === 'binary') {
194
+ // Majority vote
195
+ const o = new Uint8Array(D);
196
+ const counts = new Int32Array(D);
197
+ for (const r of reps) {
198
+ for (let i = 0; i < D; i++) {
199
+ counts[i] += r.values[i] === 1 ? 1 : -1;
200
+ }
201
+ }
202
+ for (let i = 0; i < D; i++) {
203
+ o[i] = counts[i] >= 0 ? 1 : 0;
204
+ }
205
+ return new Representation('binary', o, D);
206
+ }
207
+
208
+ if (type === 'bipolar') {
209
+ // Bipolar bundling: sign of the sum
210
+ const o = new Float32Array(D);
211
+ for (const r of reps) {
212
+ for (let i = 0; i < D; i++) {
213
+ o[i] += r.values[i];
214
+ }
215
+ }
216
+ for (let i = 0; i < D; i++) {
217
+ o[i] = o[i] >= 0 ? 1.0 : -1.0;
218
+ }
219
+ return new Representation('bipolar', o, D);
220
+ }
221
+
222
+ if (type === 'real') {
223
+ // Average normalization
224
+ const o = new Float32Array(D);
225
+ for (const r of reps) {
226
+ for (let i = 0; i < D; i++) {
227
+ o[i] += r.values[i];
228
+ }
229
+ }
230
+ const n = reps.length;
231
+ for (let i = 0; i < D; i++) {
232
+ o[i] /= n;
233
+ }
234
+ return new Representation('real', o, D);
235
+ }
236
+
237
+ // Complex/FHRR: Vector summation of complex exponentials
238
+ const re = new Float32Array(D);
239
+ const im = new Float32Array(D);
240
+ for (const r of reps) {
241
+ for (let i = 0; i < D; i++) {
242
+ re[i] += Math.cos(r.values[i]);
243
+ im[i] += Math.sin(r.values[i]);
244
+ }
245
+ }
246
+ const o = new Float32Array(D);
247
+ for (let i = 0; i < D; i++) {
248
+ let v = Math.atan2(im[i], re[i]);
249
+ if (v < 0) v += 2 * Math.PI;
250
+ o[i] = v;
251
+ }
252
+ return new Representation('complex', o, D);
253
+ }
254
+
255
+ // ── PERMUTATION (Shift operation / Order / Position binding) ─
256
+
257
+ permute(a, sh) {
258
+ const D = a.D;
259
+ const shift = ((sh % D) + D) % D;
260
+ const vals = new a.values.constructor(D);
261
+ for (let i = 0; i < D; i++) {
262
+ vals[(i + shift) % D] = a.values[i];
263
+ }
264
+ return new Representation(a.type, vals, D);
265
+ }
266
+
267
+ // ── SIMILARITY (Measured closeness) ──────────────────────────
268
+
269
+ similarity(a, b) {
270
+ if (a.type !== b.type || a.D !== b.D) {
271
+ return 0.0;
272
+ }
273
+ const D = a.D;
274
+
275
+ if (a.type === 'binary') {
276
+ // Normalized Hamming distance similarity (1 - HD/D)
277
+ let hamming = 0;
278
+ for (let i = 0; i < D; i++) {
279
+ if (a.values[i] !== b.values[i]) hamming++;
280
+ }
281
+ return 1.0 - (hamming / D);
282
+ }
283
+
284
+ if (a.type === 'bipolar') {
285
+ // Cosine similarity for -1/1 values (Dot product / D)
286
+ let dot = 0;
287
+ for (let i = 0; i < D; i++) {
288
+ dot += a.values[i] * b.values[i];
289
+ }
290
+ return dot / D;
291
+ }
292
+
293
+ if (a.type === 'real') {
294
+ // Standard cosine similarity
295
+ let dot = 0, normA = 0, normB = 0;
296
+ for (let i = 0; i < D; i++) {
297
+ dot += a.values[i] * b.values[i];
298
+ normA += a.values[i] * a.values[i];
299
+ normB += b.values[i] * b.values[i];
300
+ }
301
+ const denom = Math.sqrt(normA) * Math.sqrt(normB);
302
+ return denom > 0 ? dot / denom : 0.0;
303
+ }
304
+
305
+ // Complex/FHRR: Cosine similarity of phase differences
306
+ let sum = 0;
307
+ for (let i = 0; i < D; i++) {
308
+ sum += Math.cos(a.values[i] - b.values[i]);
309
+ }
310
+ return sum / D;
311
+ }
312
+ }
package/core/memory.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Resonance Memory Module
3
+ * Manages holographic/vector data storage, query pipeline, candidate retrieval, and resonance-based ranking.
4
+ */
5
+
6
+ import { ResonanceEngine } from './resonance.js';
7
+
8
+ export class ResonanceMemory {
9
+ constructor(config = {}) {
10
+ this.entries = [];
11
+ this.resonanceEngine = new ResonanceEngine(config);
12
+ }
13
+
14
+ addEntry(content, hdcVector, metadata = {}) {
15
+ const signature = this.resonanceEngine.generateSignature(content, hdcVector, content);
16
+
17
+ this.entries.push({
18
+ content,
19
+ hdc_vector: hdcVector,
20
+ resonance_signature: signature,
21
+ metadata,
22
+ timestamp: Date.now()
23
+ });
24
+ }
25
+
26
+ search(queryText, queryRepr, k = 10) {
27
+ if (this.entries.length === 0) {
28
+ return [];
29
+ }
30
+
31
+ const results = [];
32
+
33
+ // Score all candidates using the Resonance Engine
34
+ for (const entry of this.entries) {
35
+ const resonanceResult = this.resonanceEngine.computeResonance(
36
+ queryText,
37
+ entry.content,
38
+ queryRepr,
39
+ entry.hdc_vector
40
+ );
41
+
42
+ results.push({
43
+ content: entry.content,
44
+ score: resonanceResult.finalScore,
45
+ breakdown: resonanceResult.breakdown,
46
+ metadata: entry.metadata,
47
+ signature: entry.resonance_signature
48
+ });
49
+ }
50
+
51
+ // Sort descending by resonance score
52
+ results.sort((a, b) => b.score - a.score);
53
+
54
+ return results.slice(0, k);
55
+ }
56
+
57
+ clear() {
58
+ this.entries = [];
59
+ }
60
+ }