nn/m.test.js

167 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", ()=>
{
let dimensions = 3;
let count = 4;
let 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", ()=>
{
let c = [[1, 2], [3, 4]]
let 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", ()=>
{
let min = [-1, -2, -3];
let max = [1, 2, 3];
let count = 10;
let 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", ()=>
{
let v1 = [1, 2, 3];
let v2 = [4, 5, 6];
let 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", ()=>
{
let v1 = [1, 2, 3];
let v2 = [4, 5];
let 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", ()=>
{
let v1 = [1, 2, 3];
let v2 = [4, 5, 6];
let 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", ()=>
{
let 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", ()=>
{
let 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", ()=>
{
let v = [1, 2];
let m = [[0.1, 0.2], [0.3, 0.4]];
let 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", ()=>
{
let v1 = [1, 2];
let v2 = [3, 4];
let 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", ()=>
{
let v1 = [1, 2];
let v2 = [3, 4];
let 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", ()=>
{
let c = [[1, 2], [3, 4]];
let m = [[0.1, 0.2], [0.3, 0.4]];
let 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", ()=>
{
let c = [[1, 2], [3, 4]];
let s = 0.5;
let t = M.Batch.Scale(c, s);
console.log(t);
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", ()=>
{
let m = [[-1000, 1000]];
let t = M.Batch.Sigmoid(m);
console.log(t);
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", ()=>
{
let m = [[-1000, 0, 1000]];
let 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]);
});