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

Add norm of matrix #57

Merged
merged 7 commits into from
Jul 18, 2017
Merged

Add norm of matrix #57

merged 7 commits into from
Jul 18, 2017

Conversation

jajoe
Copy link
Member

@jajoe jajoe commented Jul 18, 2017

Feature which enables to do A.norm() or A.norm("max") to compute the Frobenius norm or the max norm of the Matrix A.

@jajoe jajoe requested a review from targos July 18, 2017 13:04
* @param {string} type - "frob" (default) ou "max" return resp. the Frobenius norm and the max norm.
* @return {number}
*/
norm(type = 'frob') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'frobenius' ?

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ou -> or

var result = 0;
if (type === 'max') {
return this.max();
} else { // by default, norm = "frobenius"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if (type === 'frobenius')

}
return Math.sqrt(result);
}
}
Copy link
Member

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);
Copy link
Member

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

} 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));
Copy link
Member

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.

@targos targos merged commit 221391a into master Jul 18, 2017
@targos targos deleted the norm branch July 18, 2017 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants