55 lines
2.6 KiB
TypeScript
55 lines
2.6 KiB
TypeScript
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; |