Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement eigen decomposition #96

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/hedgehog-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"csv": "^5.5.0",
"esbuild-loader": "^2.9.1",
"gpu.js": "^2.9.5",
"mathjs": "^7.1.0",
"mathjs": "^9.3.0",
"ml-hclust": "^3.0.0",
"ml-matrix": "^6.8.0",
"ml-pca": "^4.0.2",
Expand All @@ -43,7 +43,7 @@
},
"devDependencies": {
"@babel/types": "^7.10.5",
"@types/mathjs": "^6.0.5",
"@types/mathjs": "^9.3.0",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"eslint": "^7.5.0",
Expand Down
15 changes: 14 additions & 1 deletion packages/hedgehog-core/src/lib/algebra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mat } from './matrix';
import { lup, qr } from 'mathjs';
import { lup, qr, eigs } from 'mathjs';

// Cholesky
export class Chol {
Expand Down Expand Up @@ -58,3 +58,16 @@ export class QR {
this.R = new Mat(result.R as any);
}
}

// Eigensystem: A*V = V*D or A = V*D*V.transpose()
export class Eigensystem {
// V: the columns of V are eigenvectors
V: Mat;
// D: diagonal matrix of eigenvalues
D: Mat;
constructor(A: Mat) {
const result = eigs(A.val);
this.V = new Mat(result.vectors as any);
this.D = new Mat().diag(result.values as any);
}
}
6 changes: 5 additions & 1 deletion packages/hedgehog-core/src/runtime/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Tensor } from '../lib/tensor';
import { Table } from '../lib/table';
import _MathLib from '../lib/mathlib';
import { Sym } from '../lib/symbolic';
import { Chol, QR, LU } from '../lib/algebra';
import { Chol, QR, LU, Eigensystem } from '../lib/algebra';
import { OutputItem } from '../output/output-item';
import { rawInputsToTex } from '../utilites/process-raw-inputs';

Expand Down Expand Up @@ -265,6 +265,10 @@ export function lu(A: Mat): LU {
return new LU(A);
}

export function eig(A: Mat): Eigensystem {
return new Eigensystem(A);
}

//tic and toc
let timestamp = 0;
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
Expand Down
63 changes: 32 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.1.2", "@babel/runtime@^7.13.10", "@babel/runtime@^7.15.4":
"@babel/runtime@^7.1.2", "@babel/runtime@^7.13.10", "@babel/runtime@^7.14.6", "@babel/runtime@^7.15.4":
version "7.15.4"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
Expand Down Expand Up @@ -3179,12 +3179,12 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==

"@types/mathjs@^6.0.5":
version "6.0.7"
resolved "https://registry.yarnpkg.com/@types/mathjs/-/mathjs-6.0.7.tgz#89299b8e0dd369c970ee8f477ba2cd2527c56cd0"
integrity sha512-UPpG34wVjlr8uSijJ747q0SmC459t294xm/3Ed8GAnqM/I2K786WgCLQ4BO4lIsM07Gj1UhO7x0n0TSfqO0DNQ==
"@types/mathjs@^9.3.0":
version "9.4.2"
resolved "https://registry.yarnpkg.com/@types/mathjs/-/mathjs-9.4.2.tgz#beec20340d768171fed8331b08fb321d218ec6e1"
integrity sha512-GF5g1vJmvKdWIWsE53XX7EDAyCaZ9p6gaYm1xhlXn5JjrY/NJrOfJN3fBxS3wyZpVh3QqKoMkS2WjFwxWMHOTw==
dependencies:
decimal.js "^10.0.0"
mathjs "*"

"@types/minimatch@*":
version "3.0.3"
Expand Down Expand Up @@ -5656,10 +5656,10 @@ compare-versions@^3.6.0:
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-3.6.0.tgz#1a5689913685e5a87637b8d3ffca75514ec41d62"
integrity sha512-W6Af2Iw1z4CB7q4uU4hv646dW9GQuBM+YpC0UvUCWSD8w90SJjp+ujJuXaEMtAXBtSqGfMPuFOVn4/+FlaqfBA==

complex.js@^2.0.11:
version "2.0.11"
resolved "https://registry.yarnpkg.com/complex.js/-/complex.js-2.0.11.tgz#09a873fbf15ffd8c18c9c2201ccef425c32b8bf1"
integrity sha512-6IArJLApNtdg1P1dFtn3dnyzoZBEF0MwMnrfF1exSBRpZYoy4yieMkpZhQDC0uwctw48vii0CFVyHfpgZ/DfGw==
complex.js@^2.0.15:
version "2.0.15"
resolved "https://registry.yarnpkg.com/complex.js/-/complex.js-2.0.15.tgz#7add6848b4c1d12aa9262f7df925ebe7a51a7406"
integrity sha512-gDBvQU8IG139ZBQTSo2qvDFP+lANMGluM779csXOr6ny1NUtA3wkUnCFjlDNH/moAVfXtvClYt6G0zarFbtz5w==

component-emitter@^1.2.1:
version "1.3.0"
Expand Down Expand Up @@ -6630,10 +6630,10 @@ decamelize@^1.1.0, decamelize@^1.1.2, decamelize@^1.2.0:
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=

decimal.js@^10.0.0, decimal.js@^10.2.1:
version "10.2.1"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.2.1.tgz#238ae7b0f0c793d3e3cea410108b35a2c01426a3"
integrity sha512-KaL7+6Fw6i5A2XSnsbhm/6B+NuEA7TZ4vqxnd5tXz9sbKtrN9Srj8ab4vKVdK8YAqZO9P1kg45Y6YLoduPf+kw==
decimal.js@^10.3.1:
version "10.3.1"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783"
integrity sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==

decode-uri-component@^0.2.0:
version "0.2.0"
Expand Down Expand Up @@ -8263,10 +8263,10 @@ forwarded@~0.1.2:
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
integrity sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=

fraction.js@^4.0.12:
version "4.0.12"
resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-4.0.12.tgz#0526d47c65a5fb4854df78bc77f7bec708d7b8c3"
integrity sha512-8Z1K0VTG4hzYY7kA/1sj4/r1/RWLBD3xwReT/RCrUCbzPszjNQCCsy3ktkU/eaEqX3MYa4pY37a52eiBlPMlhA==
fraction.js@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-4.1.1.tgz#ac4e520473dae67012d618aab91eda09bcb400ff"
integrity sha512-MHOhvvxHTfRFpF1geTK9czMIZ6xclsEor2wkIGYYq+PxcQqT7vStJqjhe6S1TenZrMZzo+wlqOufBDVepUEgPg==

fragment-cache@^0.2.1:
version "0.2.1"
Expand Down Expand Up @@ -11759,17 +11759,18 @@ math-log2@^1.0.1:
resolved "https://registry.yarnpkg.com/math-log2/-/math-log2-1.0.1.tgz#fb8941be5f5ebe8979e718e6273b178e58694565"
integrity sha1-+4lBvl9evol55xjmJzsXjlhpRWU=

mathjs@^7.1.0:
version "7.6.0"
resolved "https://registry.yarnpkg.com/mathjs/-/mathjs-7.6.0.tgz#f0b7579e0756b13422995d0c4f29bd17d65d4dcc"
integrity sha512-abywR28hUpKF4at5jE8Ys+Kigk40eKMT5mcBLD0/dtsqjfOLbtzd3WjlRqIopNo7oQ6FME51qph6lb8h/bhpUg==
mathjs@*, mathjs@^9.3.0:
version "9.4.4"
resolved "https://registry.yarnpkg.com/mathjs/-/mathjs-9.4.4.tgz#29acd67563c1e720910213062824c3faf61bc858"
integrity sha512-5EEJXnWOzLDgMHSFyw623nH+MTBZxquWwXtrzTsingOouJJ6UZG2VNO1lwH31IMt9aMno1axO6TYleIP4YSDaQ==
dependencies:
complex.js "^2.0.11"
decimal.js "^10.2.1"
"@babel/runtime" "^7.14.6"
complex.js "^2.0.15"
decimal.js "^10.3.1"
escape-latex "^1.2.0"
fraction.js "^4.0.12"
fraction.js "^4.1.1"
javascript-natural-sort "^0.7.1"
seed-random "^2.2.0"
seedrandom "^3.0.5"
tiny-emitter "^2.1.0"
typed-function "^2.0.0"

Expand Down Expand Up @@ -15987,16 +15988,16 @@ screenfull@^5.1.0:
resolved "https://registry.yarnpkg.com/screenfull/-/screenfull-5.1.0.tgz#85c13c70f4ead4c1b8a935c70010dfdcd2c0e5c8"
integrity sha512-dYaNuOdzr+kc6J6CFcBrzkLCfyGcMg+gWkJ8us93IQ7y1cevhQAugFsaCdMHb6lw8KV3xPzSxzH7zM1dQap9mA==

seed-random@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/seed-random/-/seed-random-2.2.0.tgz#2a9b19e250a817099231a5b99a4daf80b7fbed54"
integrity sha1-KpsZ4lCoFwmSMaW5mk2vgLf77VQ=

[email protected]:
version "2.4.3"
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-2.4.3.tgz#2438504dad33917314bff18ac4d794f16d6aaecc"
integrity sha1-JDhQTa0zkXMUv/GKxNeU8W1qrsw=

seedrandom@^3.0.5:
version "3.0.5"
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-3.0.5.tgz#54edc85c95222525b0c7a6f6b3543d8e0b3aa0a7"
integrity sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
Expand Down