@justinelliottcobb/amari-wasm 0.14.0 → 0.15.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/README.md +323 -1
- package/amari_wasm.d.ts +838 -4
- package/amari_wasm.js +1985 -101
- package/amari_wasm_bg.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @justinelliottcobb/amari-wasm v0.
|
|
1
|
+
# @justinelliottcobb/amari-wasm v0.15.1
|
|
2
2
|
|
|
3
3
|
**Unified Mathematical Computing Library with High-Precision WebAssembly Support**
|
|
4
4
|
|
|
@@ -17,6 +17,8 @@ Amari is a comprehensive mathematical computing library that brings advanced alg
|
|
|
17
17
|
- **Automatic Differentiation**: Forward-mode AD with dual numbers for exact derivatives
|
|
18
18
|
- **Measure Theory** *(v0.10.0)*: Lebesgue integration, probability measures, and measure-theoretic foundations
|
|
19
19
|
- **Holographic Memory** *(v0.12.3)*: Vector Symbolic Architecture for associative memory with binding and bundling operations
|
|
20
|
+
- **Functional Analysis** *(v0.15.0)*: Hilbert spaces, linear operators, spectral decomposition, and Sobolev spaces
|
|
21
|
+
- **Optical Field Operations** *(v0.15.1)*: GA-native Lee hologram encoding for DMD displays and VSA-based optical processing
|
|
20
22
|
- **Probability Theory** *(v0.13.0)*: Distributions on multivector spaces, MCMC sampling, and Monte Carlo estimation
|
|
21
23
|
- **Relativistic Physics**: Spacetime algebra (Cl(1,3)) with WebAssembly-compatible precision
|
|
22
24
|
- **Spacecraft Orbital Mechanics**: Full-precision trajectory calculations in browsers
|
|
@@ -334,6 +336,292 @@ async function probabilisticDemo() {
|
|
|
334
336
|
probabilisticDemo();
|
|
335
337
|
```
|
|
336
338
|
|
|
339
|
+
### Functional Analysis *(v0.15.0)*
|
|
340
|
+
|
|
341
|
+
Work with Hilbert spaces, linear operators, and spectral decomposition on multivector spaces:
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
import init, {
|
|
345
|
+
WasmHilbertSpace,
|
|
346
|
+
WasmMatrixOperator,
|
|
347
|
+
WasmSpectralDecomposition,
|
|
348
|
+
WasmSobolevSpace,
|
|
349
|
+
powerMethod,
|
|
350
|
+
computeEigenvalues
|
|
351
|
+
} from '@justinelliottcobb/amari-wasm';
|
|
352
|
+
|
|
353
|
+
async function functionalDemo() {
|
|
354
|
+
await init();
|
|
355
|
+
|
|
356
|
+
// Create a Hilbert space Cl(2,0,0) ≅ ℝ⁴
|
|
357
|
+
const hilbert = new WasmHilbertSpace();
|
|
358
|
+
console.log(`Dimension: ${hilbert.dimension()}`); // 4
|
|
359
|
+
console.log(`Signature: ${hilbert.signature()}`); // [2, 0, 0]
|
|
360
|
+
|
|
361
|
+
// Create multivectors from coefficients [scalar, e1, e2, e12]
|
|
362
|
+
const x = hilbert.fromCoefficients([1.0, 2.0, 3.0, 4.0]);
|
|
363
|
+
const y = hilbert.fromCoefficients([0.5, 1.5, 2.5, 3.5]);
|
|
364
|
+
|
|
365
|
+
// Inner product and norm
|
|
366
|
+
const inner = hilbert.innerProduct(x, y);
|
|
367
|
+
console.log(`⟨x, y⟩ = ${inner}`);
|
|
368
|
+
|
|
369
|
+
const norm = hilbert.norm(x);
|
|
370
|
+
console.log(`‖x‖ = ${norm}`);
|
|
371
|
+
|
|
372
|
+
// Orthogonal projection
|
|
373
|
+
const proj = hilbert.project(x, y);
|
|
374
|
+
console.log(`proj_y(x) = ${proj}`);
|
|
375
|
+
|
|
376
|
+
// Create a matrix operator (4x4 matrix in row-major order)
|
|
377
|
+
const A = new WasmMatrixOperator([
|
|
378
|
+
4, 1, 0, 0,
|
|
379
|
+
1, 3, 1, 0,
|
|
380
|
+
0, 1, 2, 1,
|
|
381
|
+
0, 0, 1, 1
|
|
382
|
+
]);
|
|
383
|
+
|
|
384
|
+
// Apply operator to a vector
|
|
385
|
+
const Ax = A.apply(x);
|
|
386
|
+
console.log(`Ax = ${Ax}`);
|
|
387
|
+
|
|
388
|
+
// Operator properties
|
|
389
|
+
console.log(`‖A‖ = ${A.operatorNorm()}`);
|
|
390
|
+
console.log(`tr(A) = ${A.trace()}`);
|
|
391
|
+
console.log(`Symmetric: ${A.isSymmetric(1e-10)}`);
|
|
392
|
+
|
|
393
|
+
// Spectral decomposition for symmetric matrices
|
|
394
|
+
const decomp = WasmSpectralDecomposition.compute(A, 100, 1e-10);
|
|
395
|
+
const eigenvalues = decomp.eigenvalues();
|
|
396
|
+
console.log(`Eigenvalues: ${eigenvalues}`);
|
|
397
|
+
console.log(`Spectral radius: ${decomp.spectralRadius()}`);
|
|
398
|
+
console.log(`Condition number: ${decomp.conditionNumber()}`);
|
|
399
|
+
|
|
400
|
+
// Functional calculus: apply f(A) = exp(A)
|
|
401
|
+
const expAx = decomp.applyFunction((lambda) => Math.exp(lambda), x);
|
|
402
|
+
console.log(`exp(A)x = ${expAx}`);
|
|
403
|
+
|
|
404
|
+
// Power method for dominant eigenvalue
|
|
405
|
+
const dominant = powerMethod(A, null, 100, 1e-10);
|
|
406
|
+
console.log(`Dominant eigenvalue: ${dominant[0]}`);
|
|
407
|
+
|
|
408
|
+
// Sobolev spaces for PDE analysis
|
|
409
|
+
const h1 = new WasmSobolevSpace(1, 0.0, 1.0); // H^1([0,1])
|
|
410
|
+
console.log(`Poincaré constant: ${h1.poincareConstant()}`);
|
|
411
|
+
|
|
412
|
+
// Compute H^1 norm of sin(πx)
|
|
413
|
+
const f = (x: number) => Math.sin(Math.PI * x);
|
|
414
|
+
const df = (x: number) => Math.PI * Math.cos(Math.PI * x);
|
|
415
|
+
const h1Norm = h1.h1Norm(f, df);
|
|
416
|
+
console.log(`‖sin(πx)‖_{H^1} = ${h1Norm}`);
|
|
417
|
+
|
|
418
|
+
// Clean up WASM memory
|
|
419
|
+
hilbert.free();
|
|
420
|
+
A.free();
|
|
421
|
+
decomp.free();
|
|
422
|
+
h1.free();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
functionalDemo();
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
#### Functional Analysis API
|
|
429
|
+
|
|
430
|
+
**WasmHilbertSpace:**
|
|
431
|
+
- `new()`: Create Hilbert space Cl(2,0,0) ≅ ℝ⁴
|
|
432
|
+
- `dimension()`: Get space dimension
|
|
433
|
+
- `signature()`: Get Clifford algebra signature [p, q, r]
|
|
434
|
+
- `fromCoefficients(coeffs)`: Create multivector from coefficients
|
|
435
|
+
- `innerProduct(x, y)`: Compute ⟨x, y⟩
|
|
436
|
+
- `norm(x)`: Compute ‖x‖
|
|
437
|
+
- `distance(x, y)`: Compute d(x, y) = ‖x - y‖
|
|
438
|
+
- `normalize(x)`: Normalize to unit length
|
|
439
|
+
- `project(x, y)`: Orthogonal projection of x onto y
|
|
440
|
+
- `isOrthogonal(x, y, tol)`: Check orthogonality
|
|
441
|
+
|
|
442
|
+
**WasmMatrixOperator:**
|
|
443
|
+
- `new(entries)`: Create from 16 entries (4×4 row-major)
|
|
444
|
+
- `identity()`: Create identity operator
|
|
445
|
+
- `zero()`: Create zero operator
|
|
446
|
+
- `diagonal(entries)`: Create diagonal matrix
|
|
447
|
+
- `scaling(lambda)`: Create λI
|
|
448
|
+
- `apply(x)`: Apply operator T(x)
|
|
449
|
+
- `operatorNorm()`: Compute ‖T‖
|
|
450
|
+
- `isSymmetric(tol)`: Check symmetry
|
|
451
|
+
- `add(other)`: Add operators
|
|
452
|
+
- `compose(other)`: Compose operators (matrix multiply)
|
|
453
|
+
- `scale(lambda)`: Scale by scalar
|
|
454
|
+
- `transpose()`: Compute transpose
|
|
455
|
+
- `trace()`: Compute trace
|
|
456
|
+
|
|
457
|
+
**WasmSpectralDecomposition:**
|
|
458
|
+
- `compute(matrix, maxIter, tol)`: Compute eigenvalue decomposition
|
|
459
|
+
- `eigenvalues()`: Get eigenvalues
|
|
460
|
+
- `eigenvectors()`: Get eigenvectors (flattened)
|
|
461
|
+
- `isComplete()`: Check if decomposition is complete
|
|
462
|
+
- `spectralRadius()`: Get largest |eigenvalue|
|
|
463
|
+
- `conditionNumber()`: Get condition number
|
|
464
|
+
- `isPositiveDefinite()`: Check positive definiteness
|
|
465
|
+
- `apply(x)`: Apply reconstructed operator
|
|
466
|
+
- `applyFunction(f, x)`: Functional calculus f(T)x
|
|
467
|
+
|
|
468
|
+
**WasmSobolevSpace:**
|
|
469
|
+
- `new(order, lower, upper)`: Create H^k([a,b])
|
|
470
|
+
- `h1UnitInterval()`: Create H^1([0,1])
|
|
471
|
+
- `h2UnitInterval()`: Create H^2([0,1])
|
|
472
|
+
- `poincareConstant()`: Get Poincaré constant
|
|
473
|
+
- `h1Norm(f, df)`: Compute H^1 norm
|
|
474
|
+
- `h1Seminorm(df)`: Compute H^1 seminorm |f|_{H^1}
|
|
475
|
+
- `l2Norm(f)`: Compute L^2 norm
|
|
476
|
+
- `l2InnerProduct(f, g)`: Compute L^2 inner product
|
|
477
|
+
|
|
478
|
+
**Standalone Functions:**
|
|
479
|
+
- `powerMethod(matrix, initial, maxIter, tol)`: Dominant eigenvalue
|
|
480
|
+
- `inverseIteration(matrix, shift, initial, maxIter, tol)`: Eigenvalue near shift
|
|
481
|
+
- `computeEigenvalues(matrix, maxIter, tol)`: All eigenvalues
|
|
482
|
+
|
|
483
|
+
### Optical Field Operations *(v0.15.1)*
|
|
484
|
+
|
|
485
|
+
Encode optical fields as binary holograms for DMD displays using geometric algebra:
|
|
486
|
+
|
|
487
|
+
```typescript
|
|
488
|
+
import init, {
|
|
489
|
+
WasmOpticalRotorField,
|
|
490
|
+
WasmBinaryHologram,
|
|
491
|
+
WasmGeometricLeeEncoder,
|
|
492
|
+
WasmOpticalFieldAlgebra,
|
|
493
|
+
WasmOpticalCodebook,
|
|
494
|
+
WasmTropicalOpticalAlgebra
|
|
495
|
+
} from '@justinelliottcobb/amari-wasm';
|
|
496
|
+
|
|
497
|
+
async function opticalDemo() {
|
|
498
|
+
await init();
|
|
499
|
+
|
|
500
|
+
// Create optical rotor fields (phase + amplitude on a grid)
|
|
501
|
+
const field1 = WasmOpticalRotorField.random(256, 256, 12345n);
|
|
502
|
+
const field2 = WasmOpticalRotorField.random(256, 256, 67890n);
|
|
503
|
+
const uniform = WasmOpticalRotorField.uniform(0.0, 0.5, 256, 256);
|
|
504
|
+
|
|
505
|
+
console.log(`Field dimensions: ${field1.width} x ${field1.height}`);
|
|
506
|
+
console.log(`Total energy: ${field1.totalEnergy()}`);
|
|
507
|
+
|
|
508
|
+
// Lee hologram encoding for DMD display
|
|
509
|
+
const encoder = WasmGeometricLeeEncoder.withFrequency(256, 256, 0.25);
|
|
510
|
+
const hologram = encoder.encode(uniform);
|
|
511
|
+
|
|
512
|
+
console.log(`Hologram fill factor: ${hologram.fillFactor()}`);
|
|
513
|
+
console.log(`Theoretical efficiency: ${encoder.theoreticalEfficiency(uniform)}`);
|
|
514
|
+
|
|
515
|
+
// Get binary data for hardware interface
|
|
516
|
+
const binaryData = hologram.asBytes();
|
|
517
|
+
console.log(`Packed binary size: ${binaryData.length} bytes`);
|
|
518
|
+
|
|
519
|
+
// VSA operations on optical fields
|
|
520
|
+
const algebra = new WasmOpticalFieldAlgebra(256, 256);
|
|
521
|
+
|
|
522
|
+
// Bind two fields (rotor multiplication = phase addition)
|
|
523
|
+
const bound = algebra.bind(field1, field2);
|
|
524
|
+
|
|
525
|
+
// Compute similarity between fields
|
|
526
|
+
const similarity = algebra.similarity(field1, field1); // Self-similarity = 1.0
|
|
527
|
+
console.log(`Self-similarity: ${similarity}`);
|
|
528
|
+
|
|
529
|
+
// Unbind to retrieve original field
|
|
530
|
+
const retrieved = algebra.unbind(field1, bound);
|
|
531
|
+
const retrievalSim = algebra.similarity(retrieved, field2);
|
|
532
|
+
console.log(`Retrieval similarity: ${retrievalSim}`);
|
|
533
|
+
|
|
534
|
+
// Seed-based symbol codebook for VSA
|
|
535
|
+
const codebook = new WasmOpticalCodebook(64, 64, 42n);
|
|
536
|
+
codebook.register("AGENT");
|
|
537
|
+
codebook.register("ACTION");
|
|
538
|
+
codebook.register("TARGET");
|
|
539
|
+
|
|
540
|
+
const agentField = codebook.get("AGENT");
|
|
541
|
+
const actionField = codebook.get("ACTION");
|
|
542
|
+
console.log(`Registered symbols: ${codebook.symbols()}`);
|
|
543
|
+
|
|
544
|
+
// Tropical operations for attractor dynamics
|
|
545
|
+
const tropical = new WasmTropicalOpticalAlgebra(64, 64);
|
|
546
|
+
|
|
547
|
+
// Tropical add: pointwise minimum of phase magnitudes
|
|
548
|
+
const tropicalSum = tropical.tropicalAdd(field1, field2);
|
|
549
|
+
|
|
550
|
+
// Soft tropical add with temperature parameter
|
|
551
|
+
const softSum = tropical.softTropicalAdd(field1, field2, 10.0);
|
|
552
|
+
|
|
553
|
+
// Phase distance between fields
|
|
554
|
+
const distance = tropical.phaseDistance(field1, field2);
|
|
555
|
+
console.log(`Phase distance: ${distance}`);
|
|
556
|
+
|
|
557
|
+
// Clean up WASM memory
|
|
558
|
+
field1.free();
|
|
559
|
+
field2.free();
|
|
560
|
+
uniform.free();
|
|
561
|
+
hologram.free();
|
|
562
|
+
bound.free();
|
|
563
|
+
retrieved.free();
|
|
564
|
+
agentField.free();
|
|
565
|
+
actionField.free();
|
|
566
|
+
tropicalSum.free();
|
|
567
|
+
softSum.free();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
opticalDemo();
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
#### Optical Field API
|
|
574
|
+
|
|
575
|
+
**WasmOpticalRotorField:**
|
|
576
|
+
- `random(width, height, seed)`: Create random phase field
|
|
577
|
+
- `uniform(phase, amplitude, width, height)`: Uniform field
|
|
578
|
+
- `identity(width, height)`: Identity field (phase = 0)
|
|
579
|
+
- `fromPhase(phases, width, height)`: Create from phase array
|
|
580
|
+
- `phaseAt(x, y)`: Get phase at point (radians)
|
|
581
|
+
- `amplitudeAt(x, y)`: Get amplitude at point
|
|
582
|
+
- `totalEnergy()`: Sum of squared amplitudes
|
|
583
|
+
- `normalized()`: Normalized copy (energy = 1)
|
|
584
|
+
|
|
585
|
+
**WasmGeometricLeeEncoder:**
|
|
586
|
+
- `withFrequency(width, height, frequency)`: Create horizontal carrier encoder
|
|
587
|
+
- `new(width, height, frequency, angle)`: Create with angled carrier
|
|
588
|
+
- `encode(field)`: Encode to binary hologram
|
|
589
|
+
- `modulate(field)`: Get modulated field before thresholding
|
|
590
|
+
- `theoreticalEfficiency(field)`: Compute diffraction efficiency
|
|
591
|
+
|
|
592
|
+
**WasmBinaryHologram:**
|
|
593
|
+
- `get(x, y)`: Get pixel value
|
|
594
|
+
- `set(x, y, value)`: Set pixel value
|
|
595
|
+
- `fillFactor()`: Fraction of "on" pixels
|
|
596
|
+
- `hammingDistance(other)`: Compute Hamming distance
|
|
597
|
+
- `asBytes()`: Get packed binary data
|
|
598
|
+
- `inverted()`: Create inverted copy
|
|
599
|
+
|
|
600
|
+
**WasmOpticalFieldAlgebra:**
|
|
601
|
+
- `bind(a, b)`: Rotor multiplication (phase addition)
|
|
602
|
+
- `unbind(key, bound)`: Retrieve associated field
|
|
603
|
+
- `bundle(fields, weights)`: Weighted superposition
|
|
604
|
+
- `bundleUniform(fields)`: Equal-weight bundle
|
|
605
|
+
- `similarity(a, b)`: Normalized inner product
|
|
606
|
+
- `inverse(field)`: Phase negation
|
|
607
|
+
- `scale(field, factor)`: Amplitude scaling
|
|
608
|
+
- `addPhase(field, phase)`: Add constant phase
|
|
609
|
+
|
|
610
|
+
**WasmOpticalCodebook:**
|
|
611
|
+
- `new(width, height, baseSeed)`: Create codebook
|
|
612
|
+
- `register(symbol)`: Register symbol with auto-seed
|
|
613
|
+
- `get(symbol)`: Get or generate field for symbol
|
|
614
|
+
- `contains(symbol)`: Check if symbol is registered
|
|
615
|
+
- `symbols()`: Get all registered symbol names
|
|
616
|
+
|
|
617
|
+
**WasmTropicalOpticalAlgebra:**
|
|
618
|
+
- `tropicalAdd(a, b)`: Pointwise minimum phase magnitude
|
|
619
|
+
- `tropicalMax(a, b)`: Pointwise maximum phase magnitude
|
|
620
|
+
- `tropicalMul(a, b)`: Binding (phase addition)
|
|
621
|
+
- `softTropicalAdd(a, b, beta)`: Soft minimum with temperature
|
|
622
|
+
- `phaseDistance(a, b)`: Sum of absolute phase differences
|
|
623
|
+
- `attractorConverge(initial, attractors, maxIter, tol)`: Attractor dynamics
|
|
624
|
+
|
|
337
625
|
#### Probability API
|
|
338
626
|
|
|
339
627
|
**WasmGaussianMultivector:**
|
|
@@ -391,6 +679,8 @@ probabilisticDemo();
|
|
|
391
679
|
- **Symbolic AI**: Holographic memory for associative reasoning and concept binding
|
|
392
680
|
- **Cognitive Architectures**: Brain-inspired memory systems for AI agents
|
|
393
681
|
- **Embedding Retrieval**: Content-addressable semantic search in vector databases
|
|
682
|
+
- **Holographic Displays**: Lee hologram encoding for DMD and SLM devices
|
|
683
|
+
- **Optical Computing**: Phase-encoded VSA operations for optical neural networks
|
|
394
684
|
|
|
395
685
|
## API Reference
|
|
396
686
|
|
|
@@ -437,6 +727,38 @@ probabilisticDemo();
|
|
|
437
727
|
- `WasmUniformMultivector.sample()`: Draw random sample
|
|
438
728
|
- `WasmMonteCarloEstimator.estimate(fn, dist, n)`: Monte Carlo expectation
|
|
439
729
|
|
|
730
|
+
### Functional Analysis Operations
|
|
731
|
+
|
|
732
|
+
- `WasmHilbertSpace.new()`: Create Hilbert space Cl(2,0,0)
|
|
733
|
+
- `WasmHilbertSpace.innerProduct(x, y)`: Compute inner product
|
|
734
|
+
- `WasmHilbertSpace.norm(x)`: Compute norm
|
|
735
|
+
- `WasmHilbertSpace.project(x, y)`: Orthogonal projection
|
|
736
|
+
- `WasmMatrixOperator.new(entries)`: Create matrix operator
|
|
737
|
+
- `WasmMatrixOperator.apply(x)`: Apply operator to vector
|
|
738
|
+
- `WasmMatrixOperator.operatorNorm()`: Compute operator norm
|
|
739
|
+
- `WasmSpectralDecomposition.compute(matrix, maxIter, tol)`: Eigenvalue decomposition
|
|
740
|
+
- `WasmSpectralDecomposition.eigenvalues()`: Get eigenvalues
|
|
741
|
+
- `WasmSpectralDecomposition.applyFunction(f, x)`: Functional calculus
|
|
742
|
+
- `WasmSobolevSpace.new(order, lower, upper)`: Create Sobolev space
|
|
743
|
+
- `WasmSobolevSpace.h1Norm(f, df)`: Compute H^1 norm
|
|
744
|
+
- `powerMethod(matrix, initial, maxIter, tol)`: Dominant eigenvalue
|
|
745
|
+
- `computeEigenvalues(matrix, maxIter, tol)`: All eigenvalues
|
|
746
|
+
|
|
747
|
+
### Optical Field Operations
|
|
748
|
+
|
|
749
|
+
- `WasmOpticalRotorField.random(width, height, seed)`: Create random phase field
|
|
750
|
+
- `WasmOpticalRotorField.uniform(phase, amplitude, width, height)`: Uniform field
|
|
751
|
+
- `WasmGeometricLeeEncoder.withFrequency(width, height, freq)`: Create Lee encoder
|
|
752
|
+
- `WasmGeometricLeeEncoder.encode(field)`: Encode to binary hologram
|
|
753
|
+
- `WasmBinaryHologram.fillFactor()`: Fraction of "on" pixels
|
|
754
|
+
- `WasmBinaryHologram.asBytes()`: Get packed binary data for hardware
|
|
755
|
+
- `WasmOpticalFieldAlgebra.bind(a, b)`: Rotor product (phase addition)
|
|
756
|
+
- `WasmOpticalFieldAlgebra.unbind(key, bound)`: Retrieve associated field
|
|
757
|
+
- `WasmOpticalFieldAlgebra.similarity(a, b)`: Normalized inner product
|
|
758
|
+
- `WasmOpticalCodebook.register(symbol)`: Register symbol with auto-seed
|
|
759
|
+
- `WasmOpticalCodebook.get(symbol)`: Get field for symbol
|
|
760
|
+
- `WasmTropicalOpticalAlgebra.tropicalAdd(a, b)`: Pointwise minimum phase
|
|
761
|
+
|
|
440
762
|
## Examples
|
|
441
763
|
|
|
442
764
|
Check out the [examples directory](https://github.com/justinelliottcobb/Amari/tree/master/examples) for more detailed usage:
|