@aleph-ai/tinyaleph 1.5.7 → 1.6.1
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/alexander-module.js +1469 -0
- package/core/arithmetic-link-kernel.js +1338 -0
- package/core/emotion.js +565 -0
- package/core/gravity.js +714 -0
- package/core/hilbert.js +506 -3
- package/core/index.js +132 -4
- package/core/nonlocal.js +744 -0
- package/core/oracle.js +662 -0
- package/examples/01-hello-world.js +69 -0
- package/examples/02-basic-hash.js +90 -0
- package/examples/02-observer-stack.js +385 -0
- package/examples/03-quantum-coin.js +136 -0
- package/examples/05-symbolic-resonance.js +146 -0
- package/examples/06-symbol-database.js +150 -0
- package/examples/07-semantic-inference.js +223 -0
- package/examples/08-compound-symbols.js +219 -0
- package/examples/README.md +170 -0
- package/examples/ai/01-embeddings.js +155 -0
- package/examples/ai/02-semantic-memory.js +243 -0
- package/examples/ai/03-reasoning.js +243 -0
- package/examples/ai/04-knowledge-graph.js +279 -0
- package/examples/ai/05-llm-integration.js +333 -0
- package/examples/ai/06-agent.js +294 -0
- package/examples/ai/07-hybrid-ai.js +223 -0
- package/examples/ai/08-entropy-reasoning.js +259 -0
- package/examples/ai/09-concept-learning.js +271 -0
- package/examples/ai/10-prompt-primes.js +312 -0
- package/examples/ai/11-rag.js +332 -0
- package/examples/ai/12-neuro-symbolic.js +321 -0
- package/examples/ai/README.md +80 -0
- package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
- package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
- package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
- package/examples/arithmetic-topology/04-alexander-module.js +117 -0
- package/examples/arithmetic-topology/05-signature-memory.js +118 -0
- package/examples/arithmetic-topology/README.md +291 -0
- package/examples/bioinformatics/01-dna-encoding.js +108 -0
- package/examples/bioinformatics/02-central-dogma.js +162 -0
- package/examples/bioinformatics/03-protein-folding.js +206 -0
- package/examples/bioinformatics/04-dna-computing.js +192 -0
- package/examples/bioinformatics/05-molecular-binding.js +209 -0
- package/examples/book-operators-demo.js +155 -0
- package/examples/chat.js +105 -0
- package/examples/crt-homology/01-residue-encoding.js +87 -0
- package/examples/crt-homology/02-birkhoff-attention.js +100 -0
- package/examples/crt-homology/03-homology-loss.js +132 -0
- package/examples/crt-homology/04-crt-resoformer.js +132 -0
- package/examples/crt-homology/README.md +67 -0
- package/examples/crypto/01-password-hash.js +210 -0
- package/examples/crypto/02-key-derivation.js +210 -0
- package/examples/crypto/03-hmac.js +229 -0
- package/examples/crypto/04-file-integrity.js +263 -0
- package/examples/crypto/05-content-hash.js +263 -0
- package/examples/crypto/README.md +99 -0
- package/examples/demo-modular.js +223 -0
- package/examples/demo-two-layer.js +196 -0
- package/examples/discrete/01-integer-sine-table.js +120 -0
- package/examples/discrete/02-codebook-tunneling.js +118 -0
- package/examples/discrete/03-canonical-fusion.js +135 -0
- package/examples/discrete/04-tick-gate.js +139 -0
- package/examples/discrete/README.md +142 -0
- package/examples/emotion-demo.js +200 -0
- package/examples/formal-semantics/01-typed-terms.js +156 -0
- package/examples/formal-semantics/02-reduction.js +202 -0
- package/examples/formal-semantics/03-lambda-translation.js +206 -0
- package/examples/formal-semantics/04-enochian-language.js +257 -0
- package/examples/formal-semantics/README.md +98 -0
- package/examples/gravity-demo.js +190 -0
- package/examples/math/01-quaternions.js +237 -0
- package/examples/math/02-octonions.js +192 -0
- package/examples/math/03-prime-factorization.js +215 -0
- package/examples/math/04-vector-spaces.js +210 -0
- package/examples/math/05-gaussian-primes.js +234 -0
- package/examples/math/README.md +93 -0
- package/examples/nonlocal-demo.js +237 -0
- package/examples/oracle-demo.js +204 -0
- package/examples/physics/01-oscillator.js +177 -0
- package/examples/physics/02-lyapunov.js +201 -0
- package/examples/physics/03-collapse.js +183 -0
- package/examples/physics/04-kuramoto.js +212 -0
- package/examples/physics/05-entropy.js +226 -0
- package/examples/physics/05-sync-models.js +298 -0
- package/examples/physics/06-primeon-ladder.js +233 -0
- package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
- package/examples/physics/README.md +126 -0
- package/examples/quantum/01-prime-hunter.js +79 -0
- package/examples/quantum/02-entanglement-demo.js +79 -0
- package/examples/quantum/03-wave-analysis.js +63 -0
- package/examples/resonance/01-prime-hilbert-space.js +140 -0
- package/examples/resonance/02-prime-resonance-network.js +221 -0
- package/examples/resonance/03-resoformer.js +349 -0
- package/examples/resonance/04-resoformer-training.js +329 -0
- package/examples/resonance/05-language-model.js +484 -0
- package/examples/resonance/README.md +238 -0
- package/examples/run-examples.js +427 -0
- package/examples/scientific/01-single-qubit.js +185 -0
- package/examples/scientific/02-two-qubit.js +209 -0
- package/examples/scientific/03-quantum-circuits.js +270 -0
- package/examples/scientific/04-measurement.js +229 -0
- package/examples/scientific/05-algorithms.js +245 -0
- package/examples/scientific/06-random.js +225 -0
- package/examples/scientific/07-wavefunction.js +192 -0
- package/examples/scientific/README.md +118 -0
- package/examples/semantic/01-vocabulary.js +186 -0
- package/examples/semantic/02-similarity.js +263 -0
- package/examples/semantic/03-word-algebra.js +295 -0
- package/examples/semantic/04-clustering.js +348 -0
- package/examples/semantic/05-classification.js +386 -0
- package/examples/semantic/06-dna-encoding.js +228 -0
- package/examples/semantic/07-search.js +304 -0
- package/examples/semantic/08-qa-system.js +278 -0
- package/examples/semantic/README.md +116 -0
- package/examples/topology/01-108-invariant.js +81 -0
- package/examples/topology/02-trefoil-constants.js +112 -0
- package/examples/topology/03-gauge-symmetry.js +112 -0
- package/examples/topology/04-free-energy-dynamics.js +124 -0
- package/examples/topology/README.md +129 -0
- package/index.js +32 -0
- package/modular.js +63 -2
- package/package.json +8 -3
- package/physics/alk-kuramoto.js +817 -0
- package/physics/index.js +23 -2
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Neural-Symbolic Bridge
|
|
3
|
+
* @description Map neural embeddings to prime-based representations
|
|
4
|
+
*
|
|
5
|
+
* This example shows how to create a bridge between:
|
|
6
|
+
* - Neural embeddings (from transformers, word2vec, etc.)
|
|
7
|
+
* - Symbolic prime representations (TinyAleph)
|
|
8
|
+
*
|
|
9
|
+
* Use cases:
|
|
10
|
+
* - Interpretability: understand what neural embeddings encode
|
|
11
|
+
* - Grounding: anchor neural representations to symbolic knowledge
|
|
12
|
+
* - Hybrid reasoning: combine neural flexibility with symbolic precision
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { SemanticBackend, Hypercomplex } = require('../../modular');
|
|
16
|
+
|
|
17
|
+
// ===========================================
|
|
18
|
+
// SETUP
|
|
19
|
+
// ===========================================
|
|
20
|
+
|
|
21
|
+
const backend = new SemanticBackend({ dimension: 16 });
|
|
22
|
+
|
|
23
|
+
// Simulate neural embeddings (in production, use actual transformer outputs)
|
|
24
|
+
function simulateNeuralEmbedding(text, dim = 384) {
|
|
25
|
+
// Simulates BERT-like embeddings with higher dimensionality
|
|
26
|
+
const embedding = new Array(dim).fill(0);
|
|
27
|
+
const words = text.toLowerCase().split(/\s+/);
|
|
28
|
+
|
|
29
|
+
for (let w = 0; w < words.length; w++) {
|
|
30
|
+
const word = words[w];
|
|
31
|
+
for (let i = 0; i < word.length; i++) {
|
|
32
|
+
const idx = (word.charCodeAt(i) * (w + 1) * (i + 1)) % dim;
|
|
33
|
+
embedding[idx] += Math.cos(word.charCodeAt(i) * 0.1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Normalize
|
|
38
|
+
const mag = Math.sqrt(embedding.reduce((s, x) => s + x * x, 0));
|
|
39
|
+
return embedding.map(x => x / (mag || 1));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ===========================================
|
|
43
|
+
// NEURAL-SYMBOLIC BRIDGE
|
|
44
|
+
// ===========================================
|
|
45
|
+
|
|
46
|
+
class NeuroSymbolicBridge {
|
|
47
|
+
constructor(backend, options = {}) {
|
|
48
|
+
this.backend = backend;
|
|
49
|
+
this.neuralDim = options.neuralDim || 384;
|
|
50
|
+
this.symbolicDim = options.symbolicDim || 16;
|
|
51
|
+
|
|
52
|
+
// Projection matrices (learned in real systems, random here)
|
|
53
|
+
this.projectionMatrix = this.initProjection();
|
|
54
|
+
|
|
55
|
+
// Symbol grounding table
|
|
56
|
+
this.groundings = new Map();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Initialize projection matrix
|
|
60
|
+
initProjection() {
|
|
61
|
+
// Create a random but fixed projection
|
|
62
|
+
const matrix = [];
|
|
63
|
+
for (let i = 0; i < this.symbolicDim; i++) {
|
|
64
|
+
const row = [];
|
|
65
|
+
for (let j = 0; j < this.neuralDim; j++) {
|
|
66
|
+
// Deterministic pseudo-random based on position
|
|
67
|
+
row.push(Math.sin(i * 7 + j * 13) * Math.cos(i * 11 + j * 17) * 0.1);
|
|
68
|
+
}
|
|
69
|
+
matrix.push(row);
|
|
70
|
+
}
|
|
71
|
+
return matrix;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Project neural embedding to symbolic space
|
|
75
|
+
projectToSymbolic(neuralEmbedding) {
|
|
76
|
+
const symbolic = Hypercomplex.zero(this.symbolicDim);
|
|
77
|
+
|
|
78
|
+
for (let i = 0; i < this.symbolicDim; i++) {
|
|
79
|
+
let sum = 0;
|
|
80
|
+
for (let j = 0; j < this.neuralDim; j++) {
|
|
81
|
+
sum += this.projectionMatrix[i][j] * neuralEmbedding[j];
|
|
82
|
+
}
|
|
83
|
+
symbolic.c[i] = sum;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Normalize
|
|
87
|
+
return symbolic.normalize();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Get symbolic embedding directly from backend
|
|
91
|
+
getSymbolic(text) {
|
|
92
|
+
return this.backend.textToOrderedState(text);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Compute similarity
|
|
96
|
+
similarity(a, b) {
|
|
97
|
+
let dot = 0, magA = 0, magB = 0;
|
|
98
|
+
for (let i = 0; i < a.c.length; i++) {
|
|
99
|
+
dot += a.c[i] * b.c[i];
|
|
100
|
+
magA += a.c[i] * a.c[i];
|
|
101
|
+
magB += b.c[i] * b.c[i];
|
|
102
|
+
}
|
|
103
|
+
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Ground a neural embedding with a symbolic concept
|
|
107
|
+
ground(name, neuralEmbedding) {
|
|
108
|
+
const projected = this.projectToSymbolic(neuralEmbedding);
|
|
109
|
+
const symbolic = this.getSymbolic(name);
|
|
110
|
+
|
|
111
|
+
this.groundings.set(name, {
|
|
112
|
+
neural: neuralEmbedding,
|
|
113
|
+
projected,
|
|
114
|
+
symbolic,
|
|
115
|
+
alignment: this.similarity(projected, symbolic)
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return this.groundings.get(name);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Find the best symbolic grounding for a neural embedding
|
|
122
|
+
findGrounding(neuralEmbedding) {
|
|
123
|
+
const projected = this.projectToSymbolic(neuralEmbedding);
|
|
124
|
+
|
|
125
|
+
let best = null;
|
|
126
|
+
let bestScore = -1;
|
|
127
|
+
|
|
128
|
+
for (const [name, data] of this.groundings) {
|
|
129
|
+
const score = this.similarity(projected, data.symbolic);
|
|
130
|
+
if (score > bestScore) {
|
|
131
|
+
bestScore = score;
|
|
132
|
+
best = { name, score, data };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return best;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Analyze the relationship between neural and symbolic
|
|
140
|
+
analyze(text) {
|
|
141
|
+
const neural = simulateNeuralEmbedding(text, this.neuralDim);
|
|
142
|
+
const projected = this.projectToSymbolic(neural);
|
|
143
|
+
const symbolic = this.getSymbolic(text);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
text,
|
|
147
|
+
projectedFromNeural: projected,
|
|
148
|
+
directSymbolic: symbolic,
|
|
149
|
+
alignment: this.similarity(projected, symbolic),
|
|
150
|
+
neuralMagnitude: Math.sqrt(neural.reduce((s, x) => s + x * x, 0)),
|
|
151
|
+
symbolicMagnitude: symbolic.norm()
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ===========================================
|
|
157
|
+
// EXAMPLE: BRIDGING NEURAL AND SYMBOLIC
|
|
158
|
+
// ===========================================
|
|
159
|
+
|
|
160
|
+
console.log('TinyAleph Neural-Symbolic Bridge Example');
|
|
161
|
+
console.log('========================================\n');
|
|
162
|
+
|
|
163
|
+
const bridge = new NeuroSymbolicBridge(backend);
|
|
164
|
+
|
|
165
|
+
// Ground some concepts
|
|
166
|
+
console.log('Grounding neural embeddings to symbolic concepts:');
|
|
167
|
+
console.log('─'.repeat(50) + '\n');
|
|
168
|
+
|
|
169
|
+
const concepts = ['cat', 'dog', 'animal', 'vehicle', 'computer'];
|
|
170
|
+
|
|
171
|
+
for (const concept of concepts) {
|
|
172
|
+
const neural = simulateNeuralEmbedding(concept);
|
|
173
|
+
const grounding = bridge.ground(concept, neural);
|
|
174
|
+
console.log(`${concept.padEnd(12)} alignment: ${(grounding.alignment * 100).toFixed(1)}%`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ===========================================
|
|
178
|
+
// CONCEPT RECOGNITION
|
|
179
|
+
// ===========================================
|
|
180
|
+
|
|
181
|
+
console.log('\n' + '═'.repeat(50));
|
|
182
|
+
console.log('Recognizing concepts from neural embeddings:');
|
|
183
|
+
console.log('═'.repeat(50) + '\n');
|
|
184
|
+
|
|
185
|
+
const testPhrases = [
|
|
186
|
+
'a furry feline pet',
|
|
187
|
+
'a barking canine friend',
|
|
188
|
+
'living creatures in nature',
|
|
189
|
+
'a car driving on the road',
|
|
190
|
+
'a laptop processing data'
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
for (const phrase of testPhrases) {
|
|
194
|
+
const neural = simulateNeuralEmbedding(phrase);
|
|
195
|
+
const grounding = bridge.findGrounding(neural);
|
|
196
|
+
|
|
197
|
+
console.log(`"${phrase}"`);
|
|
198
|
+
if (grounding) {
|
|
199
|
+
console.log(` → Best match: ${grounding.name} (${(grounding.score * 100).toFixed(1)}%)\n`);
|
|
200
|
+
} else {
|
|
201
|
+
console.log(` → No grounding found\n`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ===========================================
|
|
206
|
+
// ALIGNMENT ANALYSIS
|
|
207
|
+
// ===========================================
|
|
208
|
+
|
|
209
|
+
console.log('═'.repeat(50));
|
|
210
|
+
console.log('Neural-Symbolic Alignment Analysis:');
|
|
211
|
+
console.log('═'.repeat(50) + '\n');
|
|
212
|
+
|
|
213
|
+
const analysisTargets = [
|
|
214
|
+
'The quick brown fox jumps over the lazy dog',
|
|
215
|
+
'Machine learning algorithms process data',
|
|
216
|
+
'Abstract concepts are hard to ground',
|
|
217
|
+
'Simple words are easy'
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
for (const text of analysisTargets) {
|
|
221
|
+
const analysis = bridge.analyze(text);
|
|
222
|
+
console.log(`"${text.substring(0, 40)}..."`);
|
|
223
|
+
console.log(` Alignment: ${(analysis.alignment * 100).toFixed(1)}%`);
|
|
224
|
+
console.log(` Symbolic magnitude: ${analysis.symbolicMagnitude.toFixed(3)}`);
|
|
225
|
+
console.log();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ===========================================
|
|
229
|
+
// HYBRID REASONING EXAMPLE
|
|
230
|
+
// ===========================================
|
|
231
|
+
|
|
232
|
+
console.log('═'.repeat(50));
|
|
233
|
+
console.log('Hybrid Reasoning (Neural + Symbolic):');
|
|
234
|
+
console.log('═'.repeat(50) + '\n');
|
|
235
|
+
|
|
236
|
+
class HybridReasoner {
|
|
237
|
+
constructor(bridge) {
|
|
238
|
+
this.bridge = bridge;
|
|
239
|
+
this.rules = [];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Add a symbolic rule
|
|
243
|
+
addRule(name, condition, conclusion) {
|
|
244
|
+
this.rules.push({
|
|
245
|
+
name,
|
|
246
|
+
conditionEmbed: this.bridge.getSymbolic(condition),
|
|
247
|
+
conclusionEmbed: this.bridge.getSymbolic(conclusion),
|
|
248
|
+
conclusion
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Reason from a neural input
|
|
253
|
+
reason(neuralEmbedding, text = '') {
|
|
254
|
+
// Project to symbolic
|
|
255
|
+
const symbolic = this.bridge.projectToSymbolic(neuralEmbedding);
|
|
256
|
+
|
|
257
|
+
// Find matching rules
|
|
258
|
+
const matches = [];
|
|
259
|
+
for (const rule of this.rules) {
|
|
260
|
+
const score = this.bridge.similarity(symbolic, rule.conditionEmbed);
|
|
261
|
+
if (score > 0.3) {
|
|
262
|
+
matches.push({ rule, score });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
matches.sort((a, b) => b.score - a.score);
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
input: text,
|
|
270
|
+
symbolicProjection: symbolic,
|
|
271
|
+
matchedRules: matches.map(m => ({
|
|
272
|
+
rule: m.rule.name,
|
|
273
|
+
score: m.score,
|
|
274
|
+
conclusion: m.rule.conclusion
|
|
275
|
+
}))
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const reasoner = new HybridReasoner(bridge);
|
|
281
|
+
|
|
282
|
+
// Add symbolic rules
|
|
283
|
+
reasoner.addRule('living-mortal', 'living creature that breathes', 'It will eventually die');
|
|
284
|
+
reasoner.addRule('vehicle-fuel', 'vehicle with engine', 'It needs fuel to operate');
|
|
285
|
+
reasoner.addRule('computer-power', 'electronic computing device', 'It requires electricity');
|
|
286
|
+
|
|
287
|
+
// Test reasoning
|
|
288
|
+
const reasoningTests = [
|
|
289
|
+
'a breathing animal in the wild',
|
|
290
|
+
'a truck with a powerful engine',
|
|
291
|
+
'a smartphone with a processor'
|
|
292
|
+
];
|
|
293
|
+
|
|
294
|
+
for (const test of reasoningTests) {
|
|
295
|
+
const neural = simulateNeuralEmbedding(test);
|
|
296
|
+
const result = reasoner.reason(neural, test);
|
|
297
|
+
|
|
298
|
+
console.log(`Input: "${test}"`);
|
|
299
|
+
if (result.matchedRules.length > 0) {
|
|
300
|
+
for (const match of result.matchedRules) {
|
|
301
|
+
console.log(` Rule: ${match.rule} (${(match.score * 100).toFixed(0)}%)`);
|
|
302
|
+
console.log(` → ${match.conclusion}`);
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
console.log(' No rules matched');
|
|
306
|
+
}
|
|
307
|
+
console.log();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ===========================================
|
|
311
|
+
// KEY TAKEAWAYS
|
|
312
|
+
// ===========================================
|
|
313
|
+
|
|
314
|
+
console.log('═'.repeat(50));
|
|
315
|
+
console.log('KEY TAKEAWAYS:');
|
|
316
|
+
console.log('1. Projection maps neural → symbolic space');
|
|
317
|
+
console.log('2. Grounding anchors neural representations');
|
|
318
|
+
console.log('3. Alignment measures neural-symbolic consistency');
|
|
319
|
+
console.log('4. Hybrid reasoning combines flexibility + precision');
|
|
320
|
+
console.log('5. Replace simulated embeddings with real transformers');
|
|
321
|
+
console.log('6. Learn projection matrices from paired data');
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# AI & Machine Learning Examples
|
|
2
|
+
|
|
3
|
+
This directory contains examples demonstrating how to use TinyAleph for AI and machine learning applications.
|
|
4
|
+
|
|
5
|
+
## Examples Overview
|
|
6
|
+
|
|
7
|
+
| # | File | Description |
|
|
8
|
+
|---|------|-------------|
|
|
9
|
+
| 01 | [`01-embeddings.js`](./01-embeddings.js) | Generate prime-based embeddings for text and concepts |
|
|
10
|
+
| 02 | [`02-semantic-memory.js`](./02-semantic-memory.js) | Build long-term semantic memory for AI agents |
|
|
11
|
+
| 03 | [`03-reasoning.js`](./03-reasoning.js) | Multi-step inference chains with transforms |
|
|
12
|
+
| 04 | [`04-knowledge-graph.js`](./04-knowledge-graph.js) | Build and query prime-based knowledge graphs |
|
|
13
|
+
| 05 | [`05-llm-integration.js`](./05-llm-integration.js) | Integrate TinyAleph with Large Language Models |
|
|
14
|
+
| 06 | [`06-agent.js`](./06-agent.js) | Build a goal-directed reasoning agent |
|
|
15
|
+
| 07 | [`07-hybrid-ai.js`](./07-hybrid-ai.js) | Combine symbolic primes with neural embeddings |
|
|
16
|
+
| 08 | [`08-entropy-reasoning.js`](./08-entropy-reasoning.js) | Use entropy minimization for inference |
|
|
17
|
+
| 09 | [`09-concept-learning.js`](./09-concept-learning.js) | Learn new concepts from examples (few-shot) |
|
|
18
|
+
| 10 | [`10-prompt-primes.js`](./10-prompt-primes.js) | Prime-based prompt engineering |
|
|
19
|
+
| 11 | [`11-rag.js`](./11-rag.js) | Retrieval Augmented Generation pipeline |
|
|
20
|
+
| 12 | [`12-neuro-symbolic.js`](./12-neuro-symbolic.js) | Bridge neural and symbolic representations |
|
|
21
|
+
|
|
22
|
+
## Running Examples
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# Run any example
|
|
26
|
+
node examples/ai/01-embeddings.js
|
|
27
|
+
|
|
28
|
+
# Or from this directory
|
|
29
|
+
cd examples/ai
|
|
30
|
+
node 01-embeddings.js
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Key Concepts
|
|
34
|
+
|
|
35
|
+
### Prime Embeddings vs Neural Embeddings
|
|
36
|
+
|
|
37
|
+
TinyAleph's prime-based embeddings offer unique advantages:
|
|
38
|
+
|
|
39
|
+
| Feature | Prime Embeddings | Neural Embeddings |
|
|
40
|
+
|---------|------------------|-------------------|
|
|
41
|
+
| Deterministic | ✓ Always same output | ✗ Varies with model |
|
|
42
|
+
| Interpretable | ✓ Based on prime structure | ✗ Black box |
|
|
43
|
+
| Order-sensitive | ✓ Non-commutative algebra | △ Context window |
|
|
44
|
+
| Training required | ✗ No training needed | ✓ Requires large data |
|
|
45
|
+
|
|
46
|
+
### When to Use TinyAleph for AI
|
|
47
|
+
|
|
48
|
+
- **Semantic Memory**: Store and retrieve by meaning
|
|
49
|
+
- **Reasoning**: Build inference chains with confidence tracking
|
|
50
|
+
- **Grounding**: Anchor neural outputs to symbolic knowledge
|
|
51
|
+
- **Verification**: Check LLM outputs against known facts
|
|
52
|
+
- **Hybrid Systems**: Combine symbolic precision with neural flexibility
|
|
53
|
+
|
|
54
|
+
## Architecture Patterns
|
|
55
|
+
|
|
56
|
+
### 1. Memory-Augmented Agent
|
|
57
|
+
```javascript
|
|
58
|
+
const memory = new SemanticMemory(aleph);
|
|
59
|
+
const agent = new SemanticAgent(aleph, 'MyAgent');
|
|
60
|
+
// Agent uses memory for context
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. RAG Pipeline
|
|
64
|
+
```javascript
|
|
65
|
+
const store = new DocumentStore(aleph);
|
|
66
|
+
const rag = new RAGPipeline(store);
|
|
67
|
+
const result = rag.query('your question');
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 3. Hybrid Reasoning
|
|
71
|
+
```javascript
|
|
72
|
+
const hybrid = new HybridEmbedding(aleph, { symbolicWeight: 0.6 });
|
|
73
|
+
const embedding = hybrid.getHybrid('concept');
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## See Also
|
|
77
|
+
|
|
78
|
+
- [Quickstart Examples](../) - Get started in 3 minutes
|
|
79
|
+
- [Semantic Examples](../semantic/) - NLP and text processing
|
|
80
|
+
- [Advanced Examples](../advanced/) - Custom backends and transforms
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example 01: Legendre Symbol Coupling
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates the Legendre symbol (a/p), the mod-2 linking number
|
|
5
|
+
* analogue in arithmetic topology. This is the foundational pairwise
|
|
6
|
+
* coupling used in the Arithmetic Link Kernel.
|
|
7
|
+
*
|
|
8
|
+
* Key concepts:
|
|
9
|
+
* - (a/p) = a^((p-1)/2) mod p
|
|
10
|
+
* - Returns +1 (quadratic residue), -1 (non-residue), or 0 (divisible)
|
|
11
|
+
* - Satisfies quadratic reciprocity
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { LegendreSymbol, computeLegendreMatrix } from '../../core/arithmetic-link-kernel.js';
|
|
15
|
+
|
|
16
|
+
console.log('=== Legendre Symbol: Pairwise Prime Coupling ===\n');
|
|
17
|
+
|
|
18
|
+
// 1. Basic Legendre symbol computation
|
|
19
|
+
console.log('1. Computing Legendre symbols:');
|
|
20
|
+
console.log(' (3/7) =', LegendreSymbol.compute(3, 7), ' (3 is a quadratic non-residue mod 7)');
|
|
21
|
+
console.log(' (2/7) =', LegendreSymbol.compute(2, 7), ' (2 is a quadratic residue mod 7)');
|
|
22
|
+
console.log(' (5/11) =', LegendreSymbol.compute(5, 11), ' (5 is a quadratic residue mod 11)');
|
|
23
|
+
console.log(' (7/7) =', LegendreSymbol.compute(7, 7), ' (7 divides 7)\n');
|
|
24
|
+
|
|
25
|
+
// 2. Quadratic reciprocity verification
|
|
26
|
+
console.log('2. Quadratic Reciprocity:');
|
|
27
|
+
console.log(' For odd primes p, q: (p/q)(q/p) = (-1)^((p-1)/2 * (q-1)/2)');
|
|
28
|
+
|
|
29
|
+
const testPairs = [[3, 5], [5, 7], [3, 11], [7, 13]];
|
|
30
|
+
for (const [p, q] of testPairs) {
|
|
31
|
+
const pq = LegendreSymbol.compute(p, q);
|
|
32
|
+
const qp = LegendreSymbol.compute(q, p);
|
|
33
|
+
const expected = Math.pow(-1, ((p - 1) / 2) * ((q - 1) / 2));
|
|
34
|
+
console.log(` (${p}/${q})(${q}/${p}) = ${pq} × ${qp} = ${pq * qp}, expected = ${expected}`);
|
|
35
|
+
}
|
|
36
|
+
console.log();
|
|
37
|
+
|
|
38
|
+
// 3. Coupling encodings
|
|
39
|
+
console.log('3. Coupling Encodings:');
|
|
40
|
+
const symbol = LegendreSymbol.compute(3, 7);
|
|
41
|
+
console.log(` Symbol (3/7) = ${symbol}`);
|
|
42
|
+
console.log(' Bipolar encoding:', LegendreSymbol.toCoupling(symbol, 'bipolar'));
|
|
43
|
+
console.log(' Binary encoding:', LegendreSymbol.toCoupling(symbol, 'binary'));
|
|
44
|
+
console.log(' Phase encoding:', LegendreSymbol.toCoupling(symbol, 'phase'), 'radians\n');
|
|
45
|
+
|
|
46
|
+
// 4. Coupling matrix for prime set
|
|
47
|
+
console.log('4. Coupling Matrix J for primes {5, 7, 11, 13}:');
|
|
48
|
+
const primes = [5, 7, 11, 13];
|
|
49
|
+
const J = computeLegendreMatrix(primes);
|
|
50
|
+
|
|
51
|
+
console.log(' J_ij = (p_i / p_j):\n');
|
|
52
|
+
console.log(' p_j =', primes.map(p => String(p).padStart(3)).join(' '));
|
|
53
|
+
for (let i = 0; i < primes.length; i++) {
|
|
54
|
+
const row = J[i].map(v => String(v).padStart(3)).join(' ');
|
|
55
|
+
console.log(` p_i=${primes[i]}:`, row);
|
|
56
|
+
}
|
|
57
|
+
console.log();
|
|
58
|
+
|
|
59
|
+
// 5. Interpretation for resonance
|
|
60
|
+
console.log('5. Resonance Interpretation:');
|
|
61
|
+
console.log(' J_ij = +1: primes i,j are "in phase" (constructive coupling)');
|
|
62
|
+
console.log(' J_ij = -1: primes i,j are "out of phase" (destructive coupling)');
|
|
63
|
+
console.log(' J_ij = 0: no coupling (one divides the other)\n');
|
|
64
|
+
|
|
65
|
+
// Count coupling types
|
|
66
|
+
let positive = 0, negative = 0;
|
|
67
|
+
for (let i = 0; i < primes.length; i++) {
|
|
68
|
+
for (let j = i + 1; j < primes.length; j++) {
|
|
69
|
+
if (J[i][j] === 1) positive++;
|
|
70
|
+
else if (J[i][j] === -1) negative++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
console.log(` Statistics for {${primes.join(', ')}}:`);
|
|
74
|
+
console.log(` - Constructive pairs: ${positive}`);
|
|
75
|
+
console.log(` - Destructive pairs: ${negative}`);
|
|
76
|
+
console.log(` - Net coupling tendency: ${positive > negative ? 'constructive' : 'destructive'}\n`);
|
|
77
|
+
|
|
78
|
+
console.log('=== End of Example ===');
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example 02: Rédei Symbol and Borromean Primes
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates the Rédei symbol [p₁, p₂, p₃], the arithmetic analogue
|
|
5
|
+
* of the triple linking number (Milnor's μ(123)). This captures
|
|
6
|
+
* Borromean-type coherence: three primes with no pairwise coupling
|
|
7
|
+
* but irreducible triadic coupling.
|
|
8
|
+
*
|
|
9
|
+
* Key concepts:
|
|
10
|
+
* - [p₁, p₂, p₃] ∈ {±1} when all pairwise Legendre symbols are +1
|
|
11
|
+
* - Detects irreducible 3-body prime interactions
|
|
12
|
+
* - Foundation for K³ triadic coupling tensor
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
RedeiSymbol,
|
|
17
|
+
LegendreSymbol,
|
|
18
|
+
ArithmeticLinkKernel,
|
|
19
|
+
quickBorromeanCheck,
|
|
20
|
+
findBorromeanPrimes
|
|
21
|
+
} from '../../core/arithmetic-link-kernel.js';
|
|
22
|
+
|
|
23
|
+
console.log('=== Rédei Symbol: Triadic Prime Coupling ===\n');
|
|
24
|
+
|
|
25
|
+
// 1. Check Rédei computability
|
|
26
|
+
console.log('1. Checking Rédei Symbol Computability:');
|
|
27
|
+
console.log(' Requirements: distinct odd primes with (pᵢ/pⱼ) = 1 for all pairs');
|
|
28
|
+
|
|
29
|
+
const triples = [
|
|
30
|
+
[5, 13, 17],
|
|
31
|
+
[5, 7, 11],
|
|
32
|
+
[3, 5, 7],
|
|
33
|
+
[5, 29, 41]
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
for (const [p1, p2, p3] of triples) {
|
|
37
|
+
const check = RedeiSymbol.isComputable(p1, p2, p3);
|
|
38
|
+
const l12 = LegendreSymbol.compute(p1, p2);
|
|
39
|
+
const l23 = LegendreSymbol.compute(p2, p3);
|
|
40
|
+
const l31 = LegendreSymbol.compute(p3, p1);
|
|
41
|
+
|
|
42
|
+
console.log(` [${p1}, ${p2}, ${p3}]: computable=${check.computable}`);
|
|
43
|
+
console.log(` Legendre: (${p1}/${p2})=${l12}, (${p2}/${p3})=${l23}, (${p3}/${p1})=${l31}`);
|
|
44
|
+
if (!check.computable) {
|
|
45
|
+
console.log(` Reason: ${check.reason}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
console.log();
|
|
49
|
+
|
|
50
|
+
// 2. Compute Rédei symbols for valid triples
|
|
51
|
+
console.log('2. Computing Rédei Symbols:');
|
|
52
|
+
for (const [p1, p2, p3] of triples) {
|
|
53
|
+
const result = RedeiSymbol.compute(p1, p2, p3);
|
|
54
|
+
if (result.computed) {
|
|
55
|
+
console.log(` [${p1}, ${p2}, ${p3}] = ${result.value}`);
|
|
56
|
+
console.log(` Method: ${result.method}`);
|
|
57
|
+
} else {
|
|
58
|
+
console.log(` [${p1}, ${p2}, ${p3}]: not computable (${result.reason})`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
console.log();
|
|
62
|
+
|
|
63
|
+
// 3. Borromean primes concept
|
|
64
|
+
console.log('3. Borromean Primes Concept:');
|
|
65
|
+
console.log(' Like Borromean rings: three linked rings where no two are linked,');
|
|
66
|
+
console.log(' Borromean primes have Jᵢⱼ = +1 (split) for all pairs but [p₁,p₂,p₃] ≠ 0\n');
|
|
67
|
+
|
|
68
|
+
// 4. Quick Borromean check
|
|
69
|
+
console.log('4. Quick Borromean Checks:');
|
|
70
|
+
const candidates = [
|
|
71
|
+
[5, 13, 17],
|
|
72
|
+
[5, 29, 41],
|
|
73
|
+
[17, 41, 73],
|
|
74
|
+
[5, 7, 11]
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
for (const [p1, p2, p3] of candidates) {
|
|
78
|
+
const check = quickBorromeanCheck(p1, p2, p3);
|
|
79
|
+
console.log(` (${p1}, ${p2}, ${p3}): possible=${check.possible}`);
|
|
80
|
+
if (check.isBorromean !== undefined) {
|
|
81
|
+
console.log(` isBorromean: ${check.isBorromean}`);
|
|
82
|
+
}
|
|
83
|
+
if (check.legendreSymbols) {
|
|
84
|
+
const ls = check.legendreSymbols;
|
|
85
|
+
console.log(` Legendre: l12=${ls.l12}, l23=${ls.l23}, l31=${ls.l31}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
console.log();
|
|
89
|
+
|
|
90
|
+
// 5. Triadic coupling tensor K³
|
|
91
|
+
console.log('5. Triadic Coupling Tensor K³:');
|
|
92
|
+
const primes = [5, 7, 11, 13, 17, 19, 23];
|
|
93
|
+
const alk = new ArithmeticLinkKernel(primes);
|
|
94
|
+
const K3 = alk.K3;
|
|
95
|
+
|
|
96
|
+
console.log(` Prime set: {${primes.join(', ')}}`);
|
|
97
|
+
console.log(` Computed triadic entries: ${K3.entries.size}`);
|
|
98
|
+
console.log(` Borromean triples found: ${K3.borromean.length}\n`);
|
|
99
|
+
|
|
100
|
+
if (K3.entries.size > 0) {
|
|
101
|
+
console.log(' Sample K³ entries:');
|
|
102
|
+
let count = 0;
|
|
103
|
+
for (const [key, entry] of K3.entries) {
|
|
104
|
+
if (count >= 5) break;
|
|
105
|
+
const [i, j, k] = entry.indices;
|
|
106
|
+
console.log(` K³[${primes[i]},${primes[j]},${primes[k]}] = ${entry.value}`);
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (K3.borromean.length > 0) {
|
|
112
|
+
console.log('\n Borromean triples:');
|
|
113
|
+
for (const b of K3.borromean.slice(0, 3)) {
|
|
114
|
+
const ps = b.primes;
|
|
115
|
+
console.log(` {${ps.join(', ')}}: value = ${b.value}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
console.log();
|
|
119
|
+
|
|
120
|
+
// 6. Physical interpretation
|
|
121
|
+
console.log('6. Physical Interpretation for Resonance:');
|
|
122
|
+
console.log(' Pairwise coupling (J): oscillator phase locking via sin(θⱼ - θᵢ)');
|
|
123
|
+
console.log(' Triadic coupling (K³): three-body phase locking via sin(θⱼ + θₖ - 2θᵢ)');
|
|
124
|
+
console.log(' Borromean coherence: synchronization without pairwise locking\n');
|
|
125
|
+
|
|
126
|
+
console.log('=== End of Example ===');
|