nn/m.test.js

184 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-07-25 18:17:54 -04:00
import { assert, assertEquals } from "https://deno.land/std@0.102.0/testing/asserts.ts";
2021-07-26 11:33:07 -04:00
import M from "./m.ts";
2021-07-25 18:17:54 -04:00
2021-07-26 09:27:38 -04:00
Deno.test("Iterate.Loop", ()=>
2021-07-25 18:17:54 -04:00
{
2021-07-26 09:46:37 -04:00
const dimensions = 3;
const count = 4;
const cloud = M.Iterate.Loop(dimensions, count, (i, j)=>i+j);
2021-07-25 18:17:54 -04:00
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");
});
2021-07-30 23:06:38 -04:00
Deno.test("Iterate.Copy", ()=>
2021-07-26 09:27:38 -04:00
{
2021-07-26 09:46:37 -04:00
const c = [[1, 2], [3, 4]]
2021-07-30 23:06:38 -04:00
const t = M.Iterate.Copy(c, (i)=>i);
2021-07-26 09:27:38 -04:00
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");
});
2021-07-25 18:17:54 -04:00
Deno.test("Create.Box", ()=>
{
2021-07-26 09:46:37 -04:00
const min = [-1, -2, -3];
const max = [1, 2, 3];
const count = 10;
2021-07-25 18:17:54 -04:00
2021-07-26 09:46:37 -04:00
const box = M.Create.Box(min, max, count);
2021-07-25 18:17:54 -04:00
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++)
{
2021-07-26 11:33:07 -04:00
assert(box[i][j] >= min[j]);
assert(box[i][j] <= max[j], "correct range");
2021-07-25 18:17:54 -04:00
}
}
});
Deno.test("Create.Transpose", ()=>
{
2021-07-26 09:46:37 -04:00
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
const tpose = M.Create.Transpose([v1, v2]);
2021-07-25 18:17:54 -04:00
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", ()=>
{
2021-07-26 09:46:37 -04:00
const v1 = [1, 2, 3];
const v2 = [4, 5];
const outer = M.Create.Outer(v1, v2);
2021-07-25 18:17:54 -04:00
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", ()=>
{
2021-07-26 09:46:37 -04:00
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
const clone = M.Create.Clone([v1, v2]);
2021-07-25 18:17:54 -04:00
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", ()=>
{
2021-07-26 09:46:37 -04:00
const matrix = [
2021-07-25 18:17:54 -04:00
[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", ()=>
{
2021-07-26 09:46:37 -04:00
const matrix = [
2021-07-25 18:17:54 -04:00
[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", ()=>
{
2021-07-26 09:46:37 -04:00
const v = [1, 2];
const m = [[0.1, 0.2], [0.3, 0.4]];
const t = M.Single.Affine(v, m);
2021-07-25 18:17:54 -04:00
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], 0.5)
assertEquals(t[1], 1.1, "correct placement");
2021-07-28 20:08:17 -04:00
console.log(t);
2021-07-25 18:17:54 -04:00
});
2021-07-25 21:59:18 -04:00
Deno.test("Single.Subtract", ()=>
{
2021-07-26 09:46:37 -04:00
const v1 = [1, 2];
const v2 = [3, 4];
const t = M.Single.Subtract(v1, v2);
2021-07-25 21:59:18 -04:00
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], -2)
assertEquals(t[1], -2, "correct placement");
});
Deno.test("Single.Multiply", ()=>
{
2021-07-26 09:46:37 -04:00
const v1 = [1, 2];
const v2 = [3, 4];
const t = M.Single.Multiply(v1, v2);
2021-07-25 21:59:18 -04:00
assertEquals(t.length, 2, "correct dimensions");
assertEquals(t[0], 3)
assertEquals(t[1], 8, "correct placement");
});
2021-07-25 18:17:54 -04:00
Deno.test("Batch.Affine", ()=>
{
2021-07-26 09:46:37 -04:00
const c = [[1, 2], [3, 4]];
const m = [[0.1, 0.2], [0.3, 0.4]];
const t = M.Batch.Affine(c, m);
2021-07-25 18:17:54 -04:00
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions")
assertEquals(t[0][1], 1.1, "correct placement");
});
2021-07-25 21:59:18 -04:00
Deno.test("Batch.Scale", ()=>
{
2021-07-26 09:46:37 -04:00
const c = [[1, 2], [3, 4]];
const s = 0.5;
const t = M.Batch.Scale(c, s);
2021-07-25 21:59:18 -04:00
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions");
assertEquals(t[1][0], 1.5, "correct placement");
});
2021-07-28 16:40:58 -04:00
Deno.test("Batch.Subtract", ()=>
{
const c = [[1, 2], [3, 4]];
const s = [[0.5, 0.5], [0.5, 0.5]];
const t = M.Batch.Subtract(c, s);
assertEquals(t.length, 2, "correct count");
assertEquals(t[0].length, 2, "correct dimensions");
assertEquals(t[1][0], 2.5, "correct placement");
});
2021-07-30 23:06:38 -04:00
Deno.test("Batch.Sig", ()=>
2021-07-26 09:27:38 -04:00
{
2021-07-26 09:46:37 -04:00
const m = [[-1000, 1000]];
2021-07-30 23:06:38 -04:00
const t = M.Batch.Sig(m);
2021-07-26 09:27:38 -04:00
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");
2021-07-30 23:06:38 -04:00
2021-07-26 09:27:38 -04:00
});
2021-07-30 23:06:38 -04:00
Deno.test("Batch.SigDeriv", ()=>
2021-07-26 09:27:38 -04:00
{
2021-07-26 09:46:37 -04:00
const m = [[-1000, 0, 1000]];
2021-07-30 23:06:38 -04:00
const t = M.Batch.SigDeriv(M.Batch.Sig(m));
2021-07-26 09:27:38 -04:00
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]);
2021-07-30 23:06:38 -04:00
});
Deno.test("Batch.Rec", ()=>
{
const m = [[-1, 1, 10]];
const t = M.Batch.Rec(m);
assert(t[0][0] == 0);
assert(t[0][1] == 1);
assert(t[0][2] == 10);
});
Deno.test("Batch.RecDeriv", ()=>
{
const m = [[-1, 1, 10]];
const t = M.Batch.RecDeriv(m);
assert(t[0][0] == 0);
assert(t[0][1] == 1);
assert(t[0][2] == 1);
2021-07-26 09:27:38 -04:00
});