From c0cfbf133820af42938062bbc76c1554a702d772 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 31 Aug 2023 23:42:18 -0500 Subject: [PATCH 1/3] Fix optional argument for `norm()` --- matrix.d.ts | 2 +- src/matrix.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/matrix.d.ts b/matrix.d.ts index cf26eb8..a7c2c8b 100644 --- a/matrix.d.ts +++ b/matrix.d.ts @@ -579,7 +579,7 @@ export abstract class AbstractMatrix { * Returns the norm of a matrix. * @param type - Norm type. Default: `'frobenius'`. */ - norm(type: 'frobenius' | 'max'): number; + norm(type?: 'frobenius' | 'max'): number; /** * Computes the cumulative sum of the matrix elements (in place, row by row). diff --git a/src/matrix.js b/src/matrix.js index b35b281..84dc45a 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -802,7 +802,8 @@ export class AbstractMatrix { return diag; } - norm(type = 'frobenius') { + norm(type) { + if (type === undefined) type = 'frobenius'; let result = 0; if (type === 'max') { return this.max(); From 62f65751d93dc78fb11dc3522a280ae21babb324 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Wed, 20 Sep 2023 10:54:09 -0500 Subject: [PATCH 2/3] rewrite norm --- src/matrix.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/matrix.js b/src/matrix.js index 84dc45a..16fb40d 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -803,19 +803,13 @@ export class AbstractMatrix { } norm(type) { - if (type === undefined) type = 'frobenius'; - let result = 0; - if (type === 'max') { - return this.max(); - } else if (type === 'frobenius') { - for (let i = 0; i < this.rows; i++) { - for (let j = 0; j < this.columns; j++) { - result = result + this.get(i, j) * this.get(i, j); - } - } - return Math.sqrt(result); - } else { - throw new RangeError(`unknown norm type: ${type}`); + switch (type ?? 'frobenius') { + case 'max': + return this.max(); + case 'frobenius': + return Math.sqrt(this.dot(this)); + default: + throw new RangeError(`unknown norm type: ${type}`); } } From 5363a1af6c03ae4e271c3702b8ff229096b3d998 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Fri, 22 Sep 2023 09:06:11 -0500 Subject: [PATCH 3/3] Update matrix.js --- src/matrix.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/matrix.js b/src/matrix.js index 16fb40d..c98253b 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -802,8 +802,8 @@ export class AbstractMatrix { return diag; } - norm(type) { - switch (type ?? 'frobenius') { + norm(type = 'frobenius') { + switch (type) { case 'max': return this.max(); case 'frobenius':