diff --git a/m.js b/m.js deleted file mode 100644 index 37a74e5..0000000 --- a/m.js +++ /dev/null @@ -1,51 +0,0 @@ -const M = -{ - Iterate: - { - Loop: (inDimensions, inCount, inFunction)=> - { - let i, j, outputVector; - const outputCloud = []; - for(i=0; row inCloud.map(row=>row.map(inFunction)) - }, - Create: - { - Box: (inV1, inV2, inCount)=> M.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), - Transpose: (inCloud)=> M.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]), - Outer: (inV1, inV2)=> M.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]), - Clone: (inCloud)=> M.Iterate.Edit(inCloud, i=> i) - }, - Mutate: - { - Pad: inCloud=> inCloud.forEach(row=> row.push(1)), - Unpad: inCloud=> inCloud.forEach(row=> row.pop()) - }, - Single: - { - 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: - { - 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.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))), - Derivative: (inCloud)=> M.Iterate.Edit(inCloud, i=>i*(1-i)), - Scale: (inCloud, inScalar)=> M.Iterate.Edit(inCloud, i=>i*inScalar) - } -} - -export default M; \ No newline at end of file diff --git a/m.test.js b/m.test.js index 77381c9..e5e9245 100644 --- a/m.test.js +++ b/m.test.js @@ -1,5 +1,5 @@ import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts"; -import { default as M } from "./m.js"; +import M from "./m.ts"; Deno.test("Iterate.Loop", ()=> { @@ -33,8 +33,8 @@ Deno.test("Create.Box", ()=> assertEquals(box[i].length, min.length, "correct dimensions"); for(let j=0; j= min[j], true); - assert(box[i][j] <= max[j], true, "correct range"); + assert(box[i][j] >= min[j]); + assert(box[i][j] <= max[j], "correct range"); } } }); diff --git a/m.ts b/m.ts new file mode 100644 index 0000000..6ea74dd --- /dev/null +++ b/m.ts @@ -0,0 +1,55 @@ +export type V = Array; +export type M = Array; +export type HandleLoop = (indexComponent:number, indexRow:number, array:Array) => number; +export type HandleEdit = (component:number, index:number, array:Array) => number; + +const Methods = { + Iterate: + { + Loop: (inDimensions:number, inCount:number, inFunction:HandleLoop):M => + { + let i:number, j:number, outputVector:V; + const outputCloud:M = []; + for(i=0; i inCloud.map((row:V):V=>row.map(inFunction)) + }, + Create: + { + Box: (inV1:V, inV2:V, inCount:number):M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()), + Transpose: (inCloud:M):M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]), + Outer: (inV1:V, inV2:V):M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]), + Clone: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=> i) + }, + Mutate: + { + Pad: (inCloud:M):void=> inCloud.forEach(row=> row.push(1)), + Unpad: (inCloud:M):void=> inCloud.forEach(row=> row.pop()) + }, + Single: + { + Subtract: (inV1:V, inV2:V):V=> inV1.map((component, i)=> component-inV2[i]), + Multiply: (inV1:V, inV2:V):V=> inV1.map((component, i)=> component*inV2[i]), + Affine: (inV:V, inMatrix:M):V=> inMatrix.map(row=> row.reduce((sum, current, index)=> sum + current*inV[index])) + }, + Batch: + { + Subtract: (inCloud1:M, inCloud2:M):M=> inCloud1.map((row, rowIndex)=> Methods.Single.Subtract(row, inCloud2[rowIndex])), + Multiply: (inCloud1:M, inCloud2:M):M=> inCloud1.map((row, rowIndex)=> Methods.Single.Multiply(row, inCloud2[rowIndex])), + Affine: (inCloud:M, inMatrix:M):M=> inCloud.map(row=> Methods.Single.Affine(row, inMatrix)), + Sigmoid: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))), + Derivative: (inCloud:M):M=> Methods.Iterate.Edit(inCloud, i=>i*(1-i)), + Scale: (inCloud:M, inScalar:number):M=> Methods.Iterate.Edit(inCloud, i=>i*inScalar) + } +}; + +export default Methods; \ No newline at end of file