Skip to content

Commit

Permalink
fix(types): optional argument for norm() (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu authored Sep 24, 2023
1 parent bb02eb3 commit 8869e22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion matrix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
19 changes: 7 additions & 12 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,18 +803,13 @@ export class AbstractMatrix {
}

norm(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) {
case 'max':
return this.max();
case 'frobenius':
return Math.sqrt(this.dot(this));
default:
throw new RangeError(`unknown norm type: ${type}`);
}
}

Expand Down

0 comments on commit 8869e22

Please sign in to comment.