-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
67 lines (53 loc) · 1.98 KB
/
script2.js
File metadata and controls
67 lines (53 loc) · 1.98 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
// Part 1: Procedure
function dotProductProcedure(v1, v2, result) {
result.ps = 0;
const length = Math.min(v1.length, v2.length);
for (let i = 0; i < length; i++) {
result.ps += v1[i] * v2[i];
}
}
// Part 2: Check orthogonality using procedure
function checkOrthogonalityWithProcedure(vectorPairs) {
console.log("\n=== Using PROCEDURE ===\n");
for (let i = 0; i < vectorPairs.length; i++) {
const v1 = vectorPairs[i][0];
const v2 = vectorPairs[i][1];
const result = { ps: 0 };
dotProductProcedure(v1, v2, result);
console.log(`Pair ${i + 1}: [${v1}] · [${v2}] = ${result.ps}`);
console.log(` → ${result.ps === 0 ? 'ORTHOGONAL ✓' : 'NOT ORTHOGONAL ✗'}\n`);
}
}
// Part 3: Function
function dotProductFunction(v1, v2) {
let ps = 0;
const length = Math.min(v1.length, v2.length);
for (let i = 0; i < length; i++) {
ps += v1[i] * v2[i];
}
return ps;
}
// Part 3: Check orthogonality using function
function checkOrthogonalityWithFunction(vectorPairs) {
console.log("\n=== Using FUNCTION ===\n");
for (let i = 0; i < vectorPairs.length; i++) {
const v1 = vectorPairs[i][0];
const v2 = vectorPairs[i][1];
const ps = dotProductFunction(v1, v2);
console.log(`Pair ${i + 1}: [${v1}] · [${v2}] = ${ps}`);
console.log(` → ${ps === 0 ? 'ORTHOGONAL ✓' : 'NOT ORTHOGONAL ✗'}\n`);
}
}
// Test data: n pairs of vectors
const vectorPairs = [
[[1, 2], [2, -1]], // 1*2 + 2*(-1) = 0 → Orthogonal
[[3, 4], [4, -3]], // 3*4 + 4*(-3) = 0 → Orthogonal
[[1, 0, 0], [0, 1, 0]], // 0 → Orthogonal
[[1, 2, 3], [4, 5, 6]], // 32 → Not orthogonal
[[2, 3], [-3, 2]], // -6 + 6 = 0 → Orthogonal
[[5, 0], [0, 5]] // 0 → Orthogonal
];
// Run both versions
checkOrthogonalityWithProcedure(vectorPairs);
checkOrthogonalityWithFunction(vectorPairs);
```