nn/m.test.js
2021-07-26 10:10:29 -04:00

155 lines
4.7 KiB
JavaScript

import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
import { default as M } from "./m.js";
Deno.test("Iterate.Loop", ()=>
{
const dimensions = 3;
const count = 4;
const cloud = M.Iterate.Loop(dimensions, count, (i, j)=>i+j);
assertEquals(cloud.length, count, "correct count");
assertEquals(cloud[0].length, dimensions, "correct dimensions");
assertEquals(cloud[0][0], 0);
assertEquals(cloud[3][2], 5, "correct output");
});
Deno.test("Iterate.Edit", ()=>
{
const c = [[1, 2], [3, 4]]
const t = M.Iterate.Edit(c, (i)=>i);
assertEquals(t.length, c.length, "correct count");
assertEquals(t[0][0], c[0][0], "correct dimensions");
assertEquals(t[1][1], c[1][1], "correct placement");
});
Deno.test("Create.Box", ()=>
{
const min = [-1, -2, -3];
const max = [1, 2, 3];
const count = 10;
const box = M.Create.Box(min, max, count);
assertEquals(box.length, count, "correct count");
for(let i=0; i<box.length; i++)
{
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");
}
}
});
Deno.test("Create.Transpose", ()=>
{
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
const tpose = M.Create.Transpose([v1, v2]);
assertEquals(tpose.length, 3, "correct count");
assertEquals(tpose[0].length, 2, "correct dimensions");
assertEquals(tpose[0][0], v1[0]);
assertEquals(tpose[0][1], v2[0], "correct placement");
});
Deno.test("Create.Outer", ()=>
{
const v1 = [1, 2, 3];
const v2 = [4, 5];
const outer = M.Create.Outer(v1, v2);
assertEquals(outer.length, v2.length, "correct count");
assertEquals(outer[0].length, v1.length, "correct dimensions");
assertEquals(outer[1][0], v1[0]*v2[1], "correct placement")
});
Deno.test("Create.Clone", ()=>
{
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
const clone = M.Create.Clone([v1, v2]);
assertEquals(clone.length, 2, "correct count");
assertEquals(clone[0].length, v1.length, "correct dimensions");
assertEquals(clone[1][0], v2[0], "correct placement");
});
Deno.test("Mutate.Pad", ()=>
{
const matrix = [
[1, 2, 3],
[4, 5, 6]
];
M.Mutate.Pad(matrix);
assertEquals(matrix.length, 2, "correct count");
assertEquals(matrix[0].length, 4, "correct dimensions");
assertEquals(matrix[0][3], 1, "correct placement");
});
Deno.test("Mutate.Unpad", ()=>
{
const matrix = [
[1, 2, 3, 1],
[4, 5, 6, 1]
];
M.Mutate.Unpad(matrix);
assertEquals(matrix.length, 2, "correct count");
assertEquals(matrix[0].length, 3, "correct dimensions");
assertEquals(matrix[1][0], 4, "correct placement");
});
Deno.test("Single.Affine", ()=>
{
const v = [1, 2];
const m = [[0.1, 0.2], [0.3, 0.4]];
const t = M.Single.Affine(v, m);
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], 0.5)
assertEquals(t[1], 1.1, "correct placement");
});
Deno.test("Single.Subtract", ()=>
{
const v1 = [1, 2];
const v2 = [3, 4];
const 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", ()=>
{
const v1 = [1, 2];
const v2 = [3, 4];
const 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", ()=>
{
const c = [[1, 2], [3, 4]];
const m = [[0.1, 0.2], [0.3, 0.4]];
const t = M.Batch.Affine(c, m);
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions")
assertEquals(t[0][1], 1.1, "correct placement");
});
Deno.test("Batch.Scale", ()=>
{
const c = [[1, 2], [3, 4]];
const s = 0.5;
const t = M.Batch.Scale(c, s);
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions");
assertEquals(t[1][0], 1.5, "correct placement");
});
Deno.test("Batch.Sigmoid", ()=>
{
const m = [[-1000, 1000]];
const t = M.Batch.Sigmoid(m);
assertEquals(t.length, 1, "correct count");
assertEquals(t[0].length, 2, "correct dimensions");
assert(t[0][0]>=0 && t[0][0]<0.5);
assert(t[0][1]<=1 && t[0][1]>0.5, "correct placement");
});
Deno.test("Batch.Derivative", ()=>
{
const m = [[-1000, 0, 1000]];
const t = M.Batch.Derivative(M.Batch.Sigmoid(m));
assertEquals(t.length, 1, "correct count");
assertEquals(t[0].length, 3, "correct dimensions");
assert(t[0][0]<t[0][1] && t[0][1]>t[0][2]);
});