typescript working

This commit is contained in:
TreetopFlyer 2021-07-26 11:33:07 -04:00
parent 194c66aa36
commit ffb8305617
3 changed files with 58 additions and 54 deletions

51
m.js
View File

@ -1,51 +0,0 @@
const M =
{
Iterate:
{
Loop: (inDimensions, inCount, inFunction)=>
{
let i, j, outputVector;
const outputCloud = [];
for(i=0; row<inCount; i++)
{
outputVector = [];
for(j=0; j<inDimensions; j++)
{
outputVector.push(inFunction(j, i, outputVector));
}
outputCloud.push(outputVector);
}
return outputCloud;
},
Edit: (inCloud, inFunction)=> 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;

View File

@ -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<box[i].length; j++)
{
assert(box[i][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");
}
}
});

55
m.ts Normal file
View File

@ -0,0 +1,55 @@
export type V = Array<number>;
export type M = Array<V>;
export type HandleLoop = (indexComponent:number, indexRow:number, array:Array<number>) => number;
export type HandleEdit = (component:number, index:number, array:Array<number>) => 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<inCount; i++)
{
outputVector = [];
for(j=0; j<inDimensions; j++)
{
outputVector.push(inFunction(j, i, outputVector));
}
outputCloud.push(outputVector);
}
return outputCloud;
},
Edit: (inCloud:M, inFunction:HandleEdit):M=> 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;