Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Added Identity Function for Math lib #37

Merged
merged 5 commits into from
May 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions identity.hhs
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.
*/

Gaoooooo marked this conversation as resolved.
Show resolved Hide resolved

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')
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if ( !(arguments.length === 1) ) {
throw new Error('Exception occurred in identity. Only 1 argument allowed, but a different number were entered.');
}

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) {
Gaoooooo marked this conversation as resolved.
Show resolved Hide resolved
return new Mat([]);
}

if (input == 1) {
Gaoooooo marked this conversation as resolved.
Show resolved Hide resolved
return new Mat([[1]]);
}

// Now we can start on creating the Id matrix
let result = [];
for (let i = 0; i < n; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for (let i = 0; i < n; i++){
for (let i = 0; i < input; i++){

// For each row, we create a new Array object and fill the corresponded position with 1
let row = new Array(len).fill(0);
row[i] = 1;
result += row;
Gaoooooo marked this conversation as resolved.
Show resolved Hide resolved
}

// return as a Mat object
return new Mat(result);
}
Gaoooooo marked this conversation as resolved.
Show resolved Hide resolved