-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add norm of matrix #57
Conversation
src/abstractMatrix.js
Outdated
* @param {string} type - "frob" (default) ou "max" return resp. the Frobenius norm and the max norm. | ||
* @return {number} | ||
*/ | ||
norm(type = 'frob') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'frobenius'
?
src/abstractMatrix.js
Outdated
@@ -924,6 +924,25 @@ export default function AbstractMatrix(superCtor) { | |||
} | |||
|
|||
/** | |||
* Returns the norm of a matrix. | |||
* @param {string} type - "frob" (default) ou "max" return resp. the Frobenius norm and the max norm. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ou -> or
src/abstractMatrix.js
Outdated
var result = 0; | ||
if (type === 'max') { | ||
return this.max(); | ||
} else { // by default, norm = "frobenius" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if (type === 'frobenius')
src/abstractMatrix.js
Outdated
} | ||
return Math.sqrt(result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else {
throw new RangeError(`unknown norm type: ${type}`);
}
@@ -110,6 +110,16 @@ describe('utility methods', () => { | |||
expect(() => m2.det()).toThrow(/square/); | |||
}); | |||
|
|||
it('norm Frobenius', () => { | |||
var m1 = new Matrix([[1, 1, 1], [3, 3, 3], [1, 1, 1]]); | |||
expect(m1.norm()).toBeCloseTo(5.7445626465380286, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a case where "frobenius" is passed explicitly
src/abstractMatrix.js
Outdated
} else { // by default, norm = "frobenius" | ||
for (var i = 0; i < this.rows; i++) { | ||
for (var j = 0; j < this.columns; j++) { | ||
result = result + Math.abs(this.get(i, j)) * Math.abs(this.get(i, j)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to take the absolute value. The square value will always be positive.
Feature which enables to do A.norm() or A.norm("max") to compute the Frobenius norm or the max norm of the Matrix A.