Skip to content

Commit

Permalink
test: adding diagonalization test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yawetse committed Jan 23, 2023
1 parent bf22610 commit 722b972
Show file tree
Hide file tree
Showing 23 changed files with 362 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .dccache

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions build/matrix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export declare class Matrix {
iterations?: number;
rounded?: boolean;
}): Promise<any>;
diagonalize(options?: {
iterations?: number;
rounded?: boolean;
}): Promise<any>;
/**
* @description returns the pivot positions of the matrix
* @returns the pivot positions of the matrix
Expand Down
53 changes: 42 additions & 11 deletions build/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,51 @@ export class Matrix {
return eigenvectors.map(({ eigenvalue, eigenvectors }) => {
const { vector, ...vectors } = eigenvectors;
const evs = Object.values(vectors);
const evectors = evs
.map((ev) => ev.length
? new Vector(ev)
: undefined)
.filter((ev) => ev);
return {
eigenvalue,
eigenvectors: evs.map((ev) => new Vector(ev)),
multiplicity: evs.length
eigenvectors: evectors,
multiplicity: evectors.length
};
});
}
async diagonalize(options = { iterations: 1000, rounded: true }) {
let D = undefined;
let P = undefined;
let P_inverse = undefined;
let diagonalizable = true;
const { rows, columns } = this.properties;
const d = Matrix.zeros(rows, columns).get();
const p_transposed = [];
const eigenvectors = await this.eigenvectors(options);
const numberOfEigenvectors = eigenvectors.reduce((acc, eigenvector) => acc + eigenvector.multiplicity, 0);
console.log({ numberOfEigenvectors, eigenvectors });
if (numberOfEigenvectors < rows)
return { P, D, P_inverse, diagonalizable: false };
let e = 0;
eigenvectors
.sort((a, b) => b.eigenvalue - a.eigenvalue)
.forEach((eigen) => {
const { eigenvalue, eigenvectors, multiplicity } = eigen;
for (let i = 0; i < multiplicity; i++) {
d[e][e] = eigenvalue;
const ev = eigenvectors[i].get();
p_transposed.push(ev);
e++;
}
});
P = new Matrix(p_transposed).transpose();
const p_rref = P.rref();
if (p_rref.pivots.length < columns)
return { P, D, P_inverse, diagonalizable: false };
P_inverse = P.inverse;
D = new Matrix(d);
return { P, D, P_inverse, diagonalizable };
}
/**
* @description returns the pivot positions of the matrix
* @returns the pivot positions of the matrix
Expand Down Expand Up @@ -334,15 +372,8 @@ export class Matrix {
return undefined;
const augmentedMatrix = this.augment(Matrix.identity(rows));
const rref = augmentedMatrix.rref();
const inverseRows = [];
rref.elements.transpose().unstack().forEach((tensor, i) => {
if (i >= columns)
inverseRows.push(tensor);
});
const inverseTensor = (rows === 2) ? tf.stack(inverseRows).transpose() : tf.stack(inverseRows);
return new Matrix(inverseTensor);
// const inverse = rref.column(columns,columns);
// return inverse;
const [I, A_inv] = tf.split(rref.elements, 2, 1);
return new Matrix(A_inv);
}
/**
* @description returns the matrix
Expand Down
42 changes: 28 additions & 14 deletions docs/assets/highlight.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
:root {
--light-hl-0: #0000FF;
--dark-hl-0: #569CD6;
--light-hl-1: #000000;
--dark-hl-1: #D4D4D4;
--light-hl-2: #0070C1;
--dark-hl-2: #4FC1FF;
--light-hl-3: #795E26;
--dark-hl-3: #DCDCAA;
--light-hl-4: #098658;
--dark-hl-4: #B5CEA8;
--light-hl-5: #001080;
--dark-hl-5: #9CDCFE;
--light-hl-6: #008000;
--dark-hl-6: #6A9955;
--light-hl-0: #000000;
--dark-hl-0: #D4D4D4;
--light-hl-1: #A31515;
--dark-hl-1: #CE9178;
--light-hl-2: #AF00DB;
--dark-hl-2: #C586C0;
--light-hl-3: #0000FF;
--dark-hl-3: #569CD6;
--light-hl-4: #001080;
--dark-hl-4: #9CDCFE;
--light-hl-5: #008000;
--dark-hl-5: #6A9955;
--light-hl-6: #795E26;
--dark-hl-6: #DCDCAA;
--light-hl-7: #0070C1;
--dark-hl-7: #4FC1FF;
--light-hl-8: #098658;
--dark-hl-8: #B5CEA8;
--light-code-background: #FFFFFF;
--dark-code-background: #1E1E1E;
}
Expand All @@ -25,6 +29,8 @@
--hl-4: var(--light-hl-4);
--hl-5: var(--light-hl-5);
--hl-6: var(--light-hl-6);
--hl-7: var(--light-hl-7);
--hl-8: var(--light-hl-8);
--code-background: var(--light-code-background);
} }

Expand All @@ -36,6 +42,8 @@
--hl-4: var(--dark-hl-4);
--hl-5: var(--dark-hl-5);
--hl-6: var(--dark-hl-6);
--hl-7: var(--dark-hl-7);
--hl-8: var(--dark-hl-8);
--code-background: var(--dark-code-background);
} }

Expand All @@ -47,6 +55,8 @@
--hl-4: var(--light-hl-4);
--hl-5: var(--light-hl-5);
--hl-6: var(--light-hl-6);
--hl-7: var(--light-hl-7);
--hl-8: var(--light-hl-8);
--code-background: var(--light-code-background);
}

Expand All @@ -58,6 +68,8 @@
--hl-4: var(--dark-hl-4);
--hl-5: var(--dark-hl-5);
--hl-6: var(--dark-hl-6);
--hl-7: var(--dark-hl-7);
--hl-8: var(--dark-hl-8);
--code-background: var(--dark-code-background);
}

Expand All @@ -68,4 +80,6 @@
.hl-4 { color: var(--hl-4); }
.hl-5 { color: var(--hl-5); }
.hl-6 { color: var(--hl-6); }
.hl-7 { color: var(--hl-7); }
.hl-8 { color: var(--hl-8); }
pre, code { background: var(--code-background); }
108 changes: 56 additions & 52 deletions docs/assets/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions docs/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* Light */
--light-color-background: #f2f4f8;
--light-color-background-secondary: #eff0f1;
--light-color-warning-text: #222;
--light-color-background-warning: #e6e600;
--light-color-icon-background: var(--light-color-background);
--light-color-accent: #c5c7c9;
--light-color-text: #222;
Expand All @@ -21,6 +23,8 @@
/* Dark */
--dark-color-background: #2b2e33;
--dark-color-background-secondary: #1e2024;
--dark-color-background-warning: #bebe00;
--dark-color-warning-text: #222;
--dark-color-icon-background: var(--dark-color-background-secondary);
--dark-color-accent: #9096a2;
--dark-color-text: #f5f5f5;
Expand All @@ -42,6 +46,8 @@
:root {
--color-background: var(--light-color-background);
--color-background-secondary: var(--light-color-background-secondary);
--color-background-warning: var(--light-color-background-warning);
--color-warning-text: var(--light-color-warning-text);
--color-icon-background: var(--light-color-icon-background);
--color-accent: var(--light-color-accent);
--color-text: var(--light-color-text);
Expand All @@ -64,6 +70,8 @@
:root {
--color-background: var(--dark-color-background);
--color-background-secondary: var(--dark-color-background-secondary);
--color-background-warning: var(--dark-color-background-warning);
--color-warning-text: var(--dark-color-warning-text);
--color-icon-background: var(--dark-color-icon-background);
--color-accent: var(--dark-color-accent);
--color-text: var(--dark-color-text);
Expand Down Expand Up @@ -93,6 +101,8 @@ body {
:root[data-theme="light"] {
--color-background: var(--light-color-background);
--color-background-secondary: var(--light-color-background-secondary);
--color-background-warning: var(--light-color-background-warning);
--color-warning-text: var(--light-color-warning-text);
--color-icon-background: var(--light-color-icon-background);
--color-accent: var(--light-color-accent);
--color-text: var(--light-color-text);
Expand All @@ -113,6 +123,8 @@ body {
:root[data-theme="dark"] {
--color-background: var(--dark-color-background);
--color-background-secondary: var(--dark-color-background-secondary);
--color-background-warning: var(--dark-color-background-warning);
--color-warning-text: var(--dark-color-warning-text);
--color-icon-background: var(--dark-color-icon-background);
--color-accent: var(--dark-color-accent);
--color-text: var(--dark-color-text);
Expand All @@ -130,6 +142,11 @@ body {
--color-scheme: var(--dark-color-scheme);
}

.always-visible,
.always-visible .tsd-signatures {
display: inherit !important;
}

h1,
h2,
h3,
Expand Down Expand Up @@ -1237,6 +1254,12 @@ img {
text-decoration: line-through;
}

.warning {
padding: 1rem;
color: var(--color-warning-text);
background: var(--color-background-warning);
}

* {
scrollbar-width: thin;
scrollbar-color: var(--color-accent) var(--color-icon-background);
Expand Down

0 comments on commit 722b972

Please sign in to comment.