@digitaldefiance/node-accelerate 1.0.7 → 2.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.
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Trigonometric Functions Examples
3
+ * Demonstrates vectorized trig operations using Apple Accelerate
4
+ */
5
+
6
+ const accelerate = require('..');
7
+
8
+ console.log('=== Trigonometric Functions ===\n');
9
+
10
+ // Create angle array (0 to 2π)
11
+ const n = 1000;
12
+ const angles = new Float64Array(n);
13
+ for (let i = 0; i < n; i++) {
14
+ angles[i] = (i / n) * 2 * Math.PI;
15
+ }
16
+
17
+ // Compute sin, cos, tan
18
+ const sinValues = new Float64Array(n);
19
+ const cosValues = new Float64Array(n);
20
+ const tanValues = new Float64Array(n);
21
+
22
+ console.time('Vectorized trig operations');
23
+ accelerate.vsin(angles, sinValues);
24
+ accelerate.vcos(angles, cosValues);
25
+ accelerate.vtan(angles, tanValues);
26
+ console.timeEnd('Vectorized trig operations');
27
+
28
+ // Verify with known values
29
+ console.log('\n--- Verification at key angles ---');
30
+ const testIndices = [0, n/4, n/2, 3*n/4];
31
+ const testAngles = ['0', 'π/2', 'π', '3π/2'];
32
+
33
+ for (let i = 0; i < testIndices.length; i++) {
34
+ const idx = Math.floor(testIndices[i]);
35
+ console.log(`\nAngle: ${testAngles[i]}`);
36
+ console.log(` sin: ${sinValues[idx].toFixed(4)}`);
37
+ console.log(` cos: ${cosValues[idx].toFixed(4)}`);
38
+ }
39
+
40
+ // Compute sin²(x) + cos²(x) = 1 (identity check)
41
+ const sinSquared = new Float64Array(n);
42
+ const cosSquared = new Float64Array(n);
43
+ const identity = new Float64Array(n);
44
+
45
+ accelerate.vsquare(sinValues, sinSquared);
46
+ accelerate.vsquare(cosValues, cosSquared);
47
+ accelerate.vadd(sinSquared, cosSquared, identity);
48
+
49
+ console.log('\n--- Pythagorean Identity Check ---');
50
+ console.log('sin²(x) + cos²(x) should equal 1.0');
51
+ console.log('Mean value:', accelerate.mean(identity).toFixed(10));
52
+ console.log('Max deviation:', Math.abs(1.0 - accelerate.max(identity)).toFixed(10));