From 2ac84829309cff168233fb3f466f74f99e3a0bd6 Mon Sep 17 00:00:00 2001 From: MudroadWhite <53061787+MudroadWhite@users.noreply.github.com> Date: Sun, 8 May 2022 14:00:08 -0500 Subject: [PATCH 1/5] Added identity function for math lib --- identity.hhs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 identity.hhs diff --git a/identity.hhs b/identity.hhs new file mode 100644 index 0000000..3bb77cb --- /dev/null +++ b/identity.hhs @@ -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 + // First, input should be a positive integer + if (!Number.isInteger(input) || input < 0) { + throw new Error('Expecped 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 = []; + for (let i = 0; i < n; 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; + } + + // return as a Mat object + return new Mat(result); +} \ No newline at end of file From 8a245856e743f9a07e60c95f1d1bda458c720ab8 Mon Sep 17 00:00:00 2001 From: MudroadWhite <53061787+MudroadWhite@users.noreply.github.com> Date: Wed, 11 May 2022 18:43:13 -0500 Subject: [PATCH 2/5] Corrected code according to review --- identity.hhs | 23 +++++++++++++---------- identity_test.hhs | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 identity_test.hhs diff --git a/identity.hhs b/identity.hhs index 3bb77cb..923356b 100644 --- a/identity.hhs +++ b/identity.hhs @@ -4,7 +4,6 @@ * @returns - a Mat object representing a 2D identity matrix. */ - function identity(input) { *import math: is_number @@ -16,23 +15,27 @@ function identity(input) { } // Then we return special Mat value when input is 0 or 1 - if (input == 0) { + if (input === 0) { return new Mat([]); } - if (input == 1) { + if (input === 1) { return new Mat([[1]]); } // Now we can start on creating the Id matrix - let result = []; + let result = new Mat().zeros(n, n); for (let i = 0; i < n; 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; - } + result[i][i] = 1; + } // return as a Mat object - return new Mat(result); + 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); } \ No newline at end of file diff --git a/identity_test.hhs b/identity_test.hhs new file mode 100644 index 0000000..8c95102 --- /dev/null +++ b/identity_test.hhs @@ -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)'; + } +} \ No newline at end of file From 1b96667a47a0a5b8755c74df707e42e9ba73d240 Mon Sep 17 00:00:00 2001 From: MudroadWhite <53061787+MudroadWhite@users.noreply.github.com> Date: Wed, 18 May 2022 16:33:54 -0500 Subject: [PATCH 3/5] Corrected code according to code review --- identity.hhs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/identity.hhs b/identity.hhs index 923356b..a2aae0e 100644 --- a/identity.hhs +++ b/identity.hhs @@ -24,18 +24,11 @@ function identity(input) { } // Now we can start on creating the Id matrix - let result = new Mat().zeros(n, n); - for (let i = 0; i < n; i++){ - result[i][i] = 1; + 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; - - // 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); } \ No newline at end of file From 9379023715b90b5c0691dc8f2d197e2d1552f238 Mon Sep 17 00:00:00 2001 From: MudroadWhite <53061787+MudroadWhite@users.noreply.github.com> Date: Wed, 18 May 2022 16:37:28 -0500 Subject: [PATCH 4/5] Corrected code according to code review --- identity.hhs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/identity.hhs b/identity.hhs index a2aae0e..6e76622 100644 --- a/identity.hhs +++ b/identity.hhs @@ -9,9 +9,13 @@ function identity(input) { *import math: is_number // Corner cases for input - // First, input should be a positive integer + // 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('Expecped positive integer input') + throw new Error('Excepted positive integer input') } // Then we return special Mat value when input is 0 or 1 From 151bae3c087e9abe371b25db5ce70fdac6623394 Mon Sep 17 00:00:00 2001 From: MudroadWhite <53061787+MudroadWhite@users.noreply.github.com> Date: Wed, 18 May 2022 16:39:40 -0500 Subject: [PATCH 5/5] Corrected missing `;` --- identity.hhs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity.hhs b/identity.hhs index 6e76622..4ebc73d 100644 --- a/identity.hhs +++ b/identity.hhs @@ -15,7 +15,7 @@ function identity(input) { } // Input should be a positive integer if (!Number.isInteger(input) || input < 0) { - throw new Error('Excepted positive integer input') + throw new Error('Excepted positive integer input'); } // Then we return special Mat value when input is 0 or 1