From b0ec2a54f8cd7c2c3cba0f9634535c608e783107 Mon Sep 17 00:00:00 2001 From: TreetopFlyer Date: Sun, 25 Jul 2021 21:59:18 -0400 Subject: [PATCH] add more methods --- m.js | 13 +++++++++---- m.test.js | 33 +++++++++++++++++++++++++++++++++ methods.md | 11 ++++------- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/m.js b/m.js index 341dbd4..e2914fb 100644 --- a/m.js +++ b/m.js @@ -36,13 +36,18 @@ const M = }, Single: { - Affine: (inV, inMatrix)=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index])) + Subtract: (inV1, inV2)=> inV1.map((component, i)=> component-inV2[i]), + Multiply: (inV1, inV2)=> inV1.map((component, i)=> component*inV2[i]), + Affine: (inV, inMatrix)=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index])) }, Batch: { - Affine: (inCloud, inMatrix)=> inCloud.map(row=> M.Single.Affine(row, inMatrix)), - Sigmoid: (inCloud)=> M.Iterate.Old(inCloud, i=>1/(1+Math.Pow(Math.E, i))), - Derivative: (inCloud)=> M.Iterate.Old(inCloud, i=>i*(1-i)) + Subtract: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Subtract(row, inCloud2[rowIndex])), + Multiply: (inCloud1, inCloud2)=> inCloud1.map((row, rowIndex)=> M.Single.Multiply(row, inCloud2[rowIndex])), + Affine: (inCloud, inMatrix)=> inCloud.map(row=> M.Single.Affine(row, inMatrix)), + Sigmoid: (inCloud)=> M.Iterate.Old(inCloud, i=>1/(1+Math.Pow(Math.E, i))), + Derivative: (inCloud)=> M.Iterate.Old(inCloud, i=>i*(1-i)), + Scale: (inCloud, inScalar)=> M.Iterate.Old(inCloud, i=>i*inScalar) } } diff --git a/m.test.js b/m.test.js index dfb4a1d..823298d 100644 --- a/m.test.js +++ b/m.test.js @@ -96,6 +96,28 @@ Deno.test("Single.Affine", ()=> assertEquals(t[1], 1.1, "correct placement"); }); +Deno.test("Single.Subtract", ()=> +{ + let v1 = [1, 2]; + let v2 = [3, 4]; + let t = M.Single.Subtract(v1, v2); + assertEquals(t.length, 2, "correct dimensions"); + assertEquals(t[0], -2) + assertEquals(t[1], -2, "correct placement"); +}); + +Deno.test("Single.Multiply", ()=> +{ + let v1 = [1, 2]; + let v2 = [3, 4]; + let t = M.Single.Multiply(v1, v2); + assertEquals(t.length, 2, "correct dimensions"); + assertEquals(t[0], 3) + assertEquals(t[1], 8, "correct placement"); +}); + + + Deno.test("Batch.Affine", ()=> { let c = [[1, 2], [3, 4]]; @@ -105,3 +127,14 @@ Deno.test("Batch.Affine", ()=> assertEquals(t[0].length, 2, "correct dimensions") assertEquals(t[0][1], 1.1, "correct placement"); }); + +Deno.test("Batch.Scale", ()=> +{ + let c = [[1, 2], [3, 4]]; + let s = 0.5; + let t = M.Batch.Scale(c, s); + assertEquals(t.length, 2, "correct count"); + assertEquals(t[0].length, 2, "correct dimensions"); + console.log(t); + assertEquals(t[1][0], 1.5, "correct placement"); +}); diff --git a/methods.md b/methods.md index 2c4f9bc..b149d13 100644 --- a/methods.md +++ b/methods.md @@ -6,12 +6,9 @@ clone(inCloud) // done pad(inCloud) // done unpad(inCloud) // done -// batch filter transform(inCloud, inMatrix) // done sigmoid(inCloud) // 1/(1+e^x) // -derivative(inCloud) // x*(1-x) -scale(inCloud1, inV) - -// batch of pairs -subtract(inCloud1, inCloud2) -multiply(inCloud1, inCloud2) \ No newline at end of file +derivative(inCloud) // x*(1-x) // +scale(inCloud1, inV) // +subtract(inCloud1, inCloud2) // done +multiply(inCloud1, inCloud2) // done \ No newline at end of file