// 8.3.js // 2024-09-07 // $Id: 8.3.js 1.1 2024/11/02 12:53:46 s Exp $ 'use strict' // function const math = require("./math.js"); const sum = (x, y) => x + y; // input const s1 = [11.91, 18.37, 3.64, 24.37, 30.42, -1.45, 20.11, 9.28, 17.63, 15.71]; const s2 = [29.59, 15.25, 3.53, 17.67, 12.74, -2.56, 25.46, 6.92, 9.73, 25.09]; const s3 = [23.27, 19.47, -6.58, 15.08, 16.24, -15.05, 17.80, 18.82, 3.05, 16.94]; const s4 = [27.24, 17.05, 10.20, 20.26, 19.84, 1.51, 12.24, 16.12, 22.93, 3.49]; // calculating const s = [s1, s2, s3, s4]; const n = s.length; const ave = s.map(x => x.reduce(sum) / x.length); const variance = s.map(x => math.variance(x)); let cov = [[], [], [], []]; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ cov[i][j] = s1.map((_, index) => (s[i][index] - ave[i]) * (s[j][index] - ave[j])).reduce(sum) / (s[i].length - 1); } } const ans = math.eigs(cov); // math.eigs() // Compute eigenvalues and optionally eigenvectors of a square matrix. const v = ans.eigenvectors[n - 1].vector; // output console.log("8.3.js"); console.log("s1", ...s1.map(x => x.toFixed(2).padStart(6))); console.log("s2", ...s2.map(x => x.toFixed(2).padStart(6))); console.log("s3", ...s3.map(x => x.toFixed(2).padStart(6))); console.log("s4", ...s4.map(x => x.toFixed(2).padStart(6))); console.log("the average"); ave.forEach((x, i) => console.log(i + 1, x.toFixed(2))); console.log("the variance"); variance.forEach((x, i) => console.log(i + 1, x.toFixed(2).padStart(6))); console.log("the covariance matrix"); cov.forEach((x, i) => console.log(i+1, ...x.map(y=>y.toFixed(2).padStart(6)))); console.log("the eigen value", ans.values[n - 1].toFixed(2)); console.log("the eigen vector", ...v.map(x => (x * 0.154 / 0.295).toFixed(3))); // end