This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from MudroadWhite/MRW-intern
Added Identity Function for Math lib
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @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 | ||
// Argument length should be only one | ||
if ( !(arguments.length === 1) ) { | ||
throw new Error('Identity expects only one argument'); | ||
} | ||
// Input should be a positive integer | ||
if (!Number.isInteger(input) || input < 0) { | ||
throw new Error('Excepted positive integer input'); | ||
} | ||
|
||
// 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(input, input); | ||
for (let i = 0; i < input; i++){ | ||
result.val[i][i] = 1; | ||
} | ||
|
||
// return as a Mat object | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)'; | ||
} | ||
} |