67 lines
3.2 KiB
TypeScript
67 lines
3.2 KiB
TypeScript
export namespace Cloud
|
|
{
|
|
export type V = Array<number>
|
|
export type M = Array<Array<number>>
|
|
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:Cloud.HandleLoop):Cloud.M =>
|
|
{
|
|
let i:number, j:number, outputVector:Cloud.V;
|
|
const outputCloud:Cloud.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:Cloud.M, inFunction:Cloud.HandleEdit):Cloud.M=> inCloud.map((row:Cloud.V):Cloud.V=>row.map(inFunction))
|
|
},
|
|
Create:
|
|
{
|
|
Box: (inV1:Cloud.V, inV2:Cloud.V, inCount:number):Cloud.M=> Methods.Iterate.Loop(inV1.length, inCount, i=> inV1[i]+(inV2[i]-inV1[i])*Math.random()),
|
|
Transpose: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Loop(inCloud.length, inCloud[0].length, (i, row)=> inCloud[i][row]),
|
|
Outer: (inV1:Cloud.V, inV2:Cloud.V):Cloud.M=> Methods.Iterate.Loop(inV1.length, inV2.length, (i, row)=> inV1[i]*inV2[row]),
|
|
Clone: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=> i)
|
|
},
|
|
Mutate:
|
|
{
|
|
Pad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.push(1)); return inCloud; },
|
|
Unpad: (inCloud:Cloud.M):Cloud.M=> {inCloud.forEach((row:Cloud.V)=> row.pop()); return inCloud; }
|
|
},
|
|
Test:
|
|
{
|
|
Dot:(v1:Cloud.V, v2:Cloud.V):number=>
|
|
{
|
|
return v1.reduce((sum, current, index)=> sum + current*v2[index]);
|
|
}
|
|
},
|
|
Single:
|
|
{
|
|
Subtract: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component-inV2[i]),
|
|
Multiply: (inV1:Cloud.V, inV2:Cloud.V):Cloud.V=> inV1.map((component, i)=> component*inV2[i]),
|
|
Affine: (inV:Cloud.V, inMatrix:Cloud.M):Cloud.V=> inMatrix.map((row:Cloud.V)=> row.reduce((sum, current, index)=> sum + current*inV[index], 0))
|
|
},
|
|
Batch:
|
|
{
|
|
Subtract: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V, rowIndex:number)=> Methods.Single.Subtract(row, inCloud2[rowIndex])),
|
|
Multiply: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V, rowIndex:number)=> Methods.Single.Multiply(row, inCloud2[rowIndex])),
|
|
Affine: (inCloud1:Cloud.M, inCloud2:Cloud.M):Cloud.M=> inCloud1.map((row:Cloud.V)=> Methods.Single.Affine(row, inCloud2)),
|
|
Sigmoid: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>1/(1+Math.pow(Math.E, -i))),
|
|
Derivative: (inCloud:Cloud.M):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*(1-i)),
|
|
Scale: (inCloud:Cloud.M, inScalar:number):Cloud.M=> Methods.Iterate.Edit(inCloud, i=>i*inScalar)
|
|
}
|
|
};
|
|
|
|
|
|
|
|
export default Methods; |