-
Notifications
You must be signed in to change notification settings - Fork 6
Added Identity Function for Math lib #37
Changes from 2 commits
2ac8482
8a24585
1b96667
9379023
151bae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||||||||
/** | ||||||||||
* @author Mudroad White | ||||||||||
* @param input - a positive integer denoting the size of the expected matrix. | ||||||||||
* @returns - a Mat object representing a 2D identity matrix. | ||||||||||
*/ | ||||||||||
|
||||||||||
function identity(input) { | ||||||||||
|
||||||||||
*import math: is_number | ||||||||||
|
||||||||||
// Corner cases for input | ||||||||||
// First, input should be a positive integer | ||||||||||
if (!Number.isInteger(input) || input < 0) { | ||||||||||
throw new Error('Expecped positive integer input') | ||||||||||
} | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
just a good safety check on the arguments so if the user uses the function incorrectly, they know which function they used incorrectly and why |
||||||||||
// Then we return special Mat value when input is 0 or 1 | ||||||||||
if (input === 0) { | ||||||||||
return new Mat([]); | ||||||||||
} | ||||||||||
|
||||||||||
if (input === 1) { | ||||||||||
return new Mat([[1]]); | ||||||||||
} | ||||||||||
|
||||||||||
// Now we can start on creating the Id matrix | ||||||||||
let result = new Mat().zeros(n, n); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n not defined, i assume you meant input for this and the one below (for loops n -> input) |
||||||||||
for (let i = 0; i < n; i++){ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
result[i][i] = 1; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
because we're modifying a |
||||||||||
} | ||||||||||
|
||||||||||
// return as a Mat object | ||||||||||
return result; | ||||||||||
|
||||||||||
// Note: the implementation of `result` above is for practicing purpose, from which I've learnt a lot. | ||||||||||
// A better implementation suggested by the team is as follows, so feel free to alternate between the above | ||||||||||
// version and the following one. | ||||||||||
// result = mathjs.identity(input); | ||||||||||
// // return the result as a mat object | ||||||||||
// return mat(result); | ||||||||||
} | ||||||||||
Gaoooooo marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @author Mudroad White | ||
* @param | ||
* @returns | ||
* | ||
* Functions for testing the identity function. | ||
*/ | ||
|
||
function identity_test(){ | ||
*import math: identity | ||
|
||
// Corner cases | ||
if (!(identity(0) === new Mat([]))){ | ||
throw 'identity unit test failed on identity(0)'; | ||
} | ||
|
||
if (!(identity(1) === new Mat([[1]]) )){ | ||
throw 'identity unit test failed on identity(1)'; | ||
} | ||
|
||
// Ordinary case | ||
const test1 = new Mat([[1, 0], [0, 1]]); | ||
if (!(identity(2)) === test1){ | ||
throw 'identity unit test failed on identity(2)'; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.