From 8f7ff04d075c5a201f54d512aef37fc22c5b206e Mon Sep 17 00:00:00 2001 From: taoqf Date: Wed, 3 May 2017 13:10:52 -0500 Subject: [PATCH] Add type definition for cwise-parser (#16282) * add weixin-app * add wx * add new line at end of file * tslint wx and wx-app * fixed test error * change project name weixin * rename * weixin-app Add param this * change discription of jweixin * add some Event declaration * change tslint config extends dtslint/dt.json * add defination of ccap, qr-image * remove redundant jsdoc * remove doc * allow overloads * inline some types * fix tslint error * ndarray add tslint export like a module style * ndarray make ndarray as a interface * ndarray: export type Data * add defination of cwise-parser * add type definition for cwise-copiler * fix tslint error * add type definition for cwise --- types/cwise-compiler/cwise-compiler-tests.ts | 76 ++++ types/cwise-compiler/index.d.ts | 49 +++ types/cwise-compiler/tsconfig.json | 22 ++ types/cwise-compiler/tslint.json | 3 + types/cwise-parser/cwise-parser-tests.ts | 26 ++ types/cwise-parser/index.d.ts | 22 ++ types/cwise-parser/tsconfig.json | 22 ++ types/cwise-parser/tslint.json | 3 + types/cwise/cwise-tests.ts | 366 +++++++++++++++++++ types/cwise/index.d.ts | 24 ++ types/cwise/tsconfig.json | 22 ++ types/cwise/tslint.json | 3 + 12 files changed, 638 insertions(+) create mode 100644 types/cwise-compiler/cwise-compiler-tests.ts create mode 100644 types/cwise-compiler/index.d.ts create mode 100644 types/cwise-compiler/tsconfig.json create mode 100644 types/cwise-compiler/tslint.json create mode 100644 types/cwise-parser/cwise-parser-tests.ts create mode 100644 types/cwise-parser/index.d.ts create mode 100644 types/cwise-parser/tsconfig.json create mode 100644 types/cwise-parser/tslint.json create mode 100644 types/cwise/cwise-tests.ts create mode 100644 types/cwise/index.d.ts create mode 100644 types/cwise/tsconfig.json create mode 100644 types/cwise/tslint.json diff --git a/types/cwise-compiler/cwise-compiler-tests.ts b/types/cwise-compiler/cwise-compiler-tests.ts new file mode 100644 index 0000000000..38e19de886 --- /dev/null +++ b/types/cwise-compiler/cwise-compiler-tests.ts @@ -0,0 +1,76 @@ +/// + +import * as parse from 'cwise-parser'; +import * as ndarray from 'ndarray'; +import * as compile from 'cwise-compiler'; +import * as tape from 'tape'; + +tape("block tests", (t) => { + const ops = require('ndarray-ops'); + const body2 = parse((a: number, b: number[]) => { + a = b[0] + b[1] + 1; + }); + const body23 = parse((a: number, b: number[][]) => { + a = b[0][0] * b[1][0] + b[0][1] * b[1][1] + b[0][2] * b[1][2]; + }); + + // Test with block index at the front of the indices + const c1 = compile({ + args: ["array", { blockIndices: 1 }], + pre: parse(() => { }), + body: body2, + post: parse(() => { }), + debug: false, + funcName: "cwise", + blockSize: 64 + }); + + const a1 = ndarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [3, 4]); + const b1 = ndarray([57, 17, 95, 78, 16, 96, 85, 93, 38, 42, 16, 66, 23, 77, 17, 36, 30, 52, 16, 18, 23, 69, 67, 27], [2, 3, 4]); + const ref1 = ndarray([81, 95, 113, 115, 47, 149, 102, 112, 62, 112, 84, 94], [3, 4]); + + c1(a1, b1); + + t.ok(ops.equals(a1, ref1), "front block"); + + // Test with block index at the back of the indices + const c2 = compile({ + args: ["array", { blockIndices: -1 }], + pre: parse(() => { }), + body: body2, + post: parse(() => { }), + debug: false, + funcName: "cwise", + blockSize: 64 + }); + + const a2 = ndarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [3, 4]); + const b2 = ndarray([57, 17, 95, 78, 16, 96, 85, 93, 38, 42, 16, 66, 23, 77, 17, 36, 30, 52, 16, 18, 23, 69, 67, 27], [3, 4, 2]); + const ref2 = ndarray([75, 174, 113, 179, 81, 83, 101, 54, 83, 35, 93, 95], [3, 4]); + + c2(a2, b2); + + t.ok(ops.equals(a2, ref2), "back block"); + + // Multiple block indices + const c3 = compile({ + args: ["array", { blockIndices: -2 }], + pre: parse(() => { }), + body: body23, + post: parse(() => { }), + debug: false, + funcName: "cwise", + blockSize: 64 + }); + const a3 = ndarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [3, 4]); + const b3 = ndarray([48, 46, 89, 64, 72, 96, 38, 37, 79, 92, 89, 62, 84, 41, 13, 81, 53, 30, 68, 78, 34, 81, 90, 50, + 82, 97, 46, 18, 11, 79, 15, 68, 88, 58, 71, 84, 76, 35, 74, 82, 27, 47, 59, 25, 78, 61, 10, 43, + 96, 59, 21, 74, 41, 67, 11, 72, 38, 62, 95, 66, 57, 44, 93, 10, 51, 59, 50, 85, 71, 41, 79, 45], [3, 4, 2, 3]); + const ref = ndarray([14928, 11687, 9367, 14228, 6177, 13090, 10655, 7203, 10930, 10030, 8301, 11960], [3, 4]); + + c3(a3, b3); + + t.ok(ops.equals(a3, ref), "block with two indices"); + + t.end(); +}); diff --git a/types/cwise-compiler/index.d.ts b/types/cwise-compiler/index.d.ts new file mode 100644 index 0000000000..e661934a6a --- /dev/null +++ b/types/cwise-compiler/index.d.ts @@ -0,0 +1,49 @@ +// Type definitions for cwise-compiler 1.1 +// Project: https://github.com/scijs/cwise-compiler +// Definitions by: taoqf +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { CompiledRoutine } from 'cwise-parser'; +import * as ndarray from 'ndarray'; + +declare namespace cwise_compiler { + interface BlockIndice { + blockIndices: number; + } + interface OffsetArg { + offset: number[]; + array: number; + } + type ArgType = 'array' | 'offset' | 'shape' | 'scalar' | 'index' | BlockIndice | OffsetArg; + interface UserArgs { + args: ArgType[]; + pre: CompiledRoutine; + body: CompiledRoutine; + post: CompiledRoutine; + debug: boolean; + funcName: string; + blockSize: number; + printCode?: boolean; + } + interface Procedure { + argTypes: ArgType[]; + shimArgs: string[]; + arrayArgs: number[]; + arrayBlockIndices: number[]; + scalarArgs: number[]; + offsetArgs: OffsetArg[]; + offsetArgIndex: number[]; + indexArgs: number[]; + shapeArgs: number[]; + funcName: string; + pre: CompiledRoutine; + body: CompiledRoutine; + post: CompiledRoutine; + debug: boolean; + blockSize?: number; + } +} + +declare function cwise_compiler(user_args: cwise_compiler.UserArgs): (a: ndarray, b: ndarray, ...args: ndarray[]) => ndarray; + +export = cwise_compiler; diff --git a/types/cwise-compiler/tsconfig.json b/types/cwise-compiler/tsconfig.json new file mode 100644 index 0000000000..a78923fa03 --- /dev/null +++ b/types/cwise-compiler/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cwise-compiler-tests.ts" + ] +} diff --git a/types/cwise-compiler/tslint.json b/types/cwise-compiler/tslint.json new file mode 100644 index 0000000000..531fb4ef87 --- /dev/null +++ b/types/cwise-compiler/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file diff --git a/types/cwise-parser/cwise-parser-tests.ts b/types/cwise-parser/cwise-parser-tests.ts new file mode 100644 index 0000000000..c50b9be06a --- /dev/null +++ b/types/cwise-parser/cwise-parser-tests.ts @@ -0,0 +1,26 @@ +import * as parser from "cwise-parser"; +// import parser = require("cwise-parser"); +import * as tape from "tape"; + +tape("basic tests", (t) => { + const parsed = parser((a: number, b: number, c: number) => { + a += b; + c = Math.cos(b); + }); + + t.equals(parsed.args.length, 3); + + t.equals(parsed.args[0].lvalue, true); + t.equals(parsed.args[0].rvalue, true); + t.equals(parsed.args[0].count, 1); + + t.equals(parsed.args[1].lvalue, false); + t.equals(parsed.args[1].rvalue, true); + t.equals(parsed.args[1].count, 2); + + t.equals(parsed.args[2].lvalue, true); + t.equals(parsed.args[2].rvalue, false); + t.equals(parsed.args[2].count, 1); + + t.end(); +}); diff --git a/types/cwise-parser/index.d.ts b/types/cwise-parser/index.d.ts new file mode 100644 index 0000000000..d2a06fd7f1 --- /dev/null +++ b/types/cwise-parser/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for cwise-parser 1.0 +// Project: https://github.com/scijs/cwise-parser#readme +// Definitions by: taoqf +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace cwise_parser { + interface CompiledArgument { + name: string; + lvalue: boolean; + rvalue: boolean; + count: number; + } + interface CompiledRoutine { + body: string; + args: CompiledArgument[]; + thisVars: string[]; + localVars: string[]; + } +} + +declare function cwise_parser(func: (a: number, ...args: T[]) => any): cwise_parser.CompiledRoutine; +export = cwise_parser; diff --git a/types/cwise-parser/tsconfig.json b/types/cwise-parser/tsconfig.json new file mode 100644 index 0000000000..ce6c3c1355 --- /dev/null +++ b/types/cwise-parser/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cwise-parser-tests.ts" + ] +} diff --git a/types/cwise-parser/tslint.json b/types/cwise-parser/tslint.json new file mode 100644 index 0000000000..531fb4ef87 --- /dev/null +++ b/types/cwise-parser/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file diff --git a/types/cwise/cwise-tests.ts b/types/cwise/cwise-tests.ts new file mode 100644 index 0000000000..66843adfe2 --- /dev/null +++ b/types/cwise/cwise-tests.ts @@ -0,0 +1,366 @@ +import * as cwise from 'cwise'; +import * as ndarray from 'ndarray'; +import * as tape from 'tape'; + +// basic +tape("only allow same shape", (t) => { + const op1 = cwise({ + args: ["array"], + body: (a: number) => { a = 1; } + }); + const op2 = cwise({ + args: ["array", "array"], + body: (a: number, b: number) => { a = b; } + }); + const op3 = cwise({ + args: ["array", "array", "array"], + body: (a: number, b: number, c: number) => { a = b + c; } + }); + const op2block_pos = cwise({ + args: ["array", { blockIndices: 1 }], + body: (a: number, b: number[]) => { a = b[1]; } + }); + const op2block_neg = cwise({ + args: ["array", { blockIndices: -1 }], + body: (a: number, b: number[]) => { a = b[1]; } + }); + + t.doesNotThrow(() => { op1(ndarray([1, 2, 3], [3])); }); + t.doesNotThrow(() => { op2(ndarray([1, 2, 3], [3]), ndarray([1, 2, 3], [3])); }); + t.doesNotThrow(() => { op2(ndarray([1, 2, 3, 4, 5, 6], [3, 2]), ndarray([1, 2, 3, 4, 5, 6], [3, 2])); }); + t.doesNotThrow(() => { op3(ndarray([1, 2, 3], [3]), ndarray([1, 2, 3], [3]), ndarray([1, 2, 3], [3])); }); + t.doesNotThrow(() => { op2block_pos(ndarray([1, 2], [2]), ndarray([1, 2, 3, 4, 5, 6], [3, 2])); }); + t.doesNotThrow(() => { op2block_neg(ndarray([1, 2, 3], [3]), ndarray([1, 2, 3, 4, 5, 6], [3, 2])); }); + + t.throws(() => { op2(ndarray([1, 2, 3], [3]), ndarray([1, 2], [2])); }); + t.throws(() => { op2(ndarray([1, 2, 3, 4, 5, 6], [3, 2]), ndarray([1, 2, 3], [3, 1])); }); + t.throws(() => { op2(ndarray([1, 2, 3, 4, 5, 6], [3, 2]), ndarray([1, 2, 3, 4], [2, 2])); }); + t.throws(() => { op3(ndarray([1, 2, 3], [3]), ndarray([1, 2, 3], [3]), ndarray([1, 2], [2])); }); + t.throws(() => { op3(ndarray([1, 2, 3], [3]), ndarray([1, 2], [2]), ndarray([1, 2, 3], [3])); }); + t.throws(() => { op3(ndarray([1, 2], [2]), ndarray([1, 2, 3], [3]), ndarray([1, 2, 3], [3])); }); + t.throws(() => { op2block_pos(ndarray([1, 2, 3], [3]), ndarray([1, 2, 3, 4, 5, 6], [3, 2])); }); + t.throws(() => { op2block_neg(ndarray([1, 2], [2]), ndarray([1, 2, 3, 4, 5, 6], [3, 2])); }); + t.throws(() => { op2block_pos(ndarray([1, 2, 3, 4, 5, 6], [3, 2]), ndarray([1, 2, 3, 4], [2, 2])); }); + t.throws(() => { op2block_neg(ndarray([1, 2, 3, 4, 5, 6], [3, 2]), ndarray([1, 2, 3, 4], [2, 2])); }); + + t.end(); +}); + +// binarry +class DumbStorage { + data: Int32Array; + length: number; + constructor(n: number) { + this.data = new Int32Array(n); + this.length = n; + } + get(i: number) { + return this.data[i]; + } + set(i: number, v: number) { + return this.data[i] = v; + } +} + +tape("binary", (t) => { + const binary = cwise({ + args: ["array", "array", "scalar", "shape", "index"], + body(a: number, b: number, t: tape.Test, s: number[], idx: number) { + if (!(a === 0)) t.fail("idx:" + idx + ", shape:" + s + ",a:" + a); + a = b + 1001; + } + }); + + function testBinary1D(P: ndarray, Q: ndarray, testName: string) { + t.equals(P.shape[0], Q.shape[0], testName + "; shape"); + for (let i = 0; i < P.shape[0]; ++i) { + Q.set(i, i); + P.set(i, 0); + } + binary(P, Q, t); + for (let i = 0; i < P.shape[0]; ++i) { + if (!(P.get(i) === i + 1001)) { + t.fail(testName + "; encountered " + P.get(i) + " instead of " + (i + 1001) + " at " + i); + return; + } + } + t.pass(testName); + } + + const A1 = ndarray(new Int32Array(128)); + const B1 = ndarray(new Int32Array(128)); + + testBinary1D(ndarray(new Int32Array(0)), ndarray(new Int32Array(0)), "length==0"); + testBinary1D(ndarray(new Int32Array(1)), ndarray(new Int32Array(1)), "length==1"); + testBinary1D(A1, B1, "A, B"); + testBinary1D(A1.lo(32), B1.hi(128 - 32), "A.lo(32), B.hi(128-32)"); + testBinary1D(A1.step(-1), B1, "A.step(-1), B"); + testBinary1D(A1, B1.step(-1), "A, B.step(-1)"); + + const A2 = ndarray(new DumbStorage(128) as any); + const B2 = ndarray(new DumbStorage(128) as any); + testBinary1D(ndarray(new DumbStorage(0) as any), ndarray(new DumbStorage(0) as any), "DS; length==0"); + testBinary1D(ndarray(new DumbStorage(1) as any), ndarray(new DumbStorage(1) as any), "DS; length==0"); + testBinary1D(A2, B2, "DS; A, B"); + testBinary1D(A2.lo(32), B2.hi(128 - 32), "DS; A.lo(32), B.hi(128-32)"); + testBinary1D(A2.step(-1), B2, "DS; A.step(-1), B"); + testBinary1D(A2, B2.step(-1), "DS; A, B.step(-1)"); + + const X = ndarray(new Int32Array(64 * 64), [64, 64]); + const Y = ndarray(new Int32Array(64 * 64), [64, 64]); + + function testBinary2D(P: ndarray, Q: ndarray, testName: string) { + for (let i = 0; i < X.shape[0]; ++i) { + for (let j = 0; j < X.shape[1]; ++j) { + X.set(i, j, -10000); + Y.set(i, j, -256); + } + } + t.equals(P.shape[0], Q.shape[0], testName + "; shape[0]"); + t.equals(P.shape[1], Q.shape[1], testName + "; shape[1]"); + for (let i = 0; i < P.shape[0]; ++i) { + for (let j = 0; j < P.shape[1]; ++j) { + Q.set(i, j, i * 1000 + j); + P.set(i, j, 0); + } + } + binary(P, Q, t, P.shape); + for (let i = 0; i < P.shape[0]; ++i) { + for (let j = 0; j < P.shape[1]; ++j) { + if (!(P.get(i, j) === i * 1000 + j + 1001)) { + t.fail(testName + "; encountered " + P.get(i, j) + " instead of " + (i * 1000 + j + 1001) + " at (" + i + "," + j + ")"); + return; + } + } + } + t.pass(testName); + } + + testBinary2D(X, Y, "X, Y"); + testBinary2D(X.transpose(1, 0), Y.transpose(1, 0), "X.T, Y.T"); + testBinary2D(X.transpose(1, 0), Y, "X.T, Y"); + testBinary2D(X, Y.transpose(1, 0), "X, Y.T"); + testBinary2D(X.hi(32, 32), Y.hi(32, 32), "X.hi(32,32), Y.hi(32,32)"); + testBinary2D(X.hi(31, 31), Y.hi(31, 31), "X.hi(31,31), Y.hi(31,31)"); + testBinary2D(X.hi(0, 32), Y.hi(0, 32), "X.hi(0,32), Y.hi(0,32)"); + testBinary2D(X.transpose(1, 0).hi(0, 32), Y.hi(0, 32), "X.T.hi(0,32), Y.hi(0,32)"); + testBinary2D(X.transpose(1, 0).hi(33, 33), Y.hi(33, 33), "X.T.hi(33,33), Y.hi(33,33)"); + testBinary2D(X.transpose(1, 0).hi(31, 31), Y.hi(31, 31), "X.T.hi(31,31), Y.hi(31,31)"); + + t.end(); +}); + +// browserify +import * as browserify from "browserify"; +import * as vm from 'vm'; +import * as path from 'path'; + +const cases = ["unary", "binary", "offset", "fill"]; + +bundleCasesFrom(0); + +function bundleCasesFrom(i: number) { + if (i >= cases.length) return; + const b = browserify(); + b.ignore("tape"); + b.add(__dirname + "/" + cases[i] + ".js"); + b.transform(path.normalize(__dirname + "/../cwise.js")); + tape(cases[i], (t) => { // Without nested tests, the asynchronous nature of bundle causes issues with tape... + b.bundle((err, src) => { + if (err) { + throw new Error("failed to bundle!"); + } + vm.runInNewContext(src.toString(), { + test: t.test.bind(t), + Buffer, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, + Uint8Array, + Uint16Array, + Uint32Array, + Uint8ClampedArray, + console: { log: console.log.bind(console) } + }); + t.end(); + }); + }); + bundleCasesFrom(i + 1); +} + +// fill +tape("fill", (t) => { + const fill = cwise({ + args: ["index", "array", "scalar"], + body(idx, out, f) { + out = f.apply(undefined, idx); + } + }); + + const xlen = 10; + const ylen = 5; + const array = ndarray(new Float32Array(xlen * ylen), [xlen, ylen]); + + fill(array, (row: number, col: number) => { + return 0; + }); + + for (let i = 0; i < xlen; i++) { + for (let j = 0; j < ylen; j++) { + t.equals(array.get(i, j), 0, 'fill (' + i + ',' + j + ')'); + } + } + + fill(array, (row: number, col: number) => { + return 10 * (row + col); + }); + + for (let i = 0; i < xlen; i++) { + for (let j = 0; j < ylen; j++) { + t.equals(array.get(i, j), 10 * (i + j), 'fill (' + i + ',' + j + ')'); + } + } + + t.end(); +}); + +// offset +tape("offset", (t) => { + const binary = cwise({ + args: ["array", "array", { offset: [1], array: 1 }, "scalar", "shape", "index"], + body(a, b, c, t, s, idx) { + if (!(a === 0)) t.fail("idx:" + idx + ", shape:" + s + ",a:" + a); + a = c + b + 1000; + } + }); + + function testBinary1D(P: ndarray, Q: ndarray, testName: string) { + t.equals(P.shape[0], Q.shape[0] - 1, testName + "; shape"); + for (let i = 0; i < P.shape[0]; ++i) { + Q.set(i, i); + P.set(i, 0); + } + Q.set(P.shape[0], P.shape[0]); + binary(P, Q.hi(Q.shape[0] - 1), t); + for (let i = 0; i < P.shape[0]; ++i) { + if (!(P.get(i) === 2 * i + 1001)) { + t.fail(testName + "; encountered " + P.get(i) + " instead of " + (2 * i + 1001) + " at " + i); + return; + } + } + t.pass(testName); + } + + const A1 = ndarray(new Int32Array(128)); + const B1 = ndarray(new Int32Array(129)); + + testBinary1D(ndarray(new Int32Array(0)), ndarray(new Int32Array(1)), "length==0"); + testBinary1D(ndarray(new Int32Array(1)), ndarray(new Int32Array(2)), "length==1"); + testBinary1D(A1, B1, "A, B"); + testBinary1D(A1.lo(32), B1.lo(32), "A.lo(32), B.lo(32)"); + testBinary1D(A1.step(-1), B1, "A.step(-1), B"); + testBinary1D(A1, B1.step(-1), "A, B.step(-1)"); + + const A2 = ndarray(new DumbStorage(128) as any); + const B2 = ndarray(new DumbStorage(129) as any); + testBinary1D(ndarray(new DumbStorage(0) as any), ndarray(new DumbStorage(1) as any), "DS; length==0"); + testBinary1D(ndarray(new DumbStorage(1) as any), ndarray(new DumbStorage(2) as any), "DS; length==1"); + testBinary1D(A2, B2, "DS; A, B"); + testBinary1D(A2.lo(32), B2.lo(32), "DS; A.lo(32), B.lo(32)"); + testBinary1D(A2.step(-1), B2, "DS; A.step(-1), B"); + testBinary1D(A2, B2.step(-1), "DS; A, B.step(-1)"); + + t.end(); +}); + +// unary + +tape("unary", (t) => { + const unary = cwise({ + args: ["array"], + body(a) { + ++a; + } + }); + + function testUnary1D(arr: ndarray, testName: string) { + for (let i = 0; i < arr.shape[0]; ++i) { + arr.set(i, i); + } + unary(arr); + for (let i = 0; i < arr.shape[0]; ++i) { + if (!(arr.get(i) === i + 1)) { + t.fail(testName + "; encountered " + arr.get(i) + " instead of " + (i + 1) + " at " + i); + return; + } + } + t.pass(testName); + } + const simple_zeros = ndarray(new Int32Array(4096)); + + testUnary1D(simple_zeros.hi(0), "simple_zeros.hi(0)"); + testUnary1D(simple_zeros.hi(1), "simple_zeros.hi(1)"); + testUnary1D(simple_zeros.hi(2), "simple_zeros.hi(2)"); + testUnary1D(simple_zeros, "simple_zeros"); + testUnary1D(simple_zeros.hi(31), "simple_zeros.hi(31)"); + testUnary1D(simple_zeros.hi(32), "simple_zeros.hi(32)"); + testUnary1D(simple_zeros.hi(33), "simple_zeros.hi(33)"); + testUnary1D(simple_zeros.step(-1), "simple_zeros.step(-1)"); + testUnary1D(simple_zeros.step(3), "simple_zeros.step(3)"); + testUnary1D(simple_zeros.step(4), "simple_zeros.step(4)"); + testUnary1D(simple_zeros.step(5).lo(10), "simple_zeros.step(5).lo(10)"); + + const custom_zeros = ndarray(new DumbStorage(4096) as any); + + testUnary1D(custom_zeros.hi(0), "custom_zeros.hi(0)"); + testUnary1D(custom_zeros.hi(1), "custom_zeros.hi(1)"); + testUnary1D(custom_zeros.hi(2), "custom_zeros.hi(2)"); + testUnary1D(custom_zeros, "custom_zeros"); + testUnary1D(custom_zeros.hi(31), "custom_zeros.hi(31)"); + testUnary1D(custom_zeros.hi(32), "custom_zeros.hi(32)"); + testUnary1D(custom_zeros.hi(33), "custom_zeros.hi(33)"); + testUnary1D(custom_zeros.step(-1), "custom_zeros.step(-1)"); + testUnary1D(custom_zeros.step(3), "custom_zeros.step(3)"); + testUnary1D(custom_zeros.step(4), "custom_zeros.step(4)"); + testUnary1D(custom_zeros.step(5).lo(10), "custom_zeros.step(5).lo(10)"); + + function testUnary2D(arr: ndarray, testName: string) { + for (let i = 0; i < arr.shape[0]; ++i) { + for (let j = 0; j < arr.shape[1]; ++j) { + arr.set(i, j, i + j * arr.shape[0]); + } + } + unary(arr); + for (let i = 0; i < arr.shape[0]; ++i) { + for (let j = 0; j < arr.shape[1]; ++j) { + if (!(arr.get(i, j) === 1 + i + j * arr.shape[0])) { + t.fail(testName + "; encountered " + arr.get(i, j) + " instead of " + (1 + i + j * arr.shape[0]) + " at (" + i + "," + j + ")"); + return; + } + } + } + t.pass(testName); + } + + const M1 = ndarray(new Int32Array(128 * 128), [128, 128]); + testUnary2D(M1, "M"); + testUnary2D(M1.hi(10, 10), "M.hi(10, 10)"); + testUnary2D(M1.lo(100, 1), "M.lo(100,1)"); + testUnary2D(M1.transpose(1, 0), "M.transpose(1,0)"); + testUnary2D(M1.step(-1, 1), "M.step(-1, 1)"); + testUnary2D(M1.step(-5, -2), "M.step(-5, -2)"); + testUnary2D(M1.step(16, 3), "M.step(16, 3)"); + + const M2 = ndarray(new DumbStorage(128 * 128) as any, [128, 128]); + testUnary2D(M2, "DS; M"); + testUnary2D(M2.hi(10, 10), "DS; M.hi(10, 10)"); + testUnary2D(M2.lo(100, 1), "DS; M.lo(100,1)"); + testUnary2D(M2.transpose(1, 0), "DS; M.transpose(1,0)"); + testUnary2D(M2.step(-1, 1), "DS; M.step(-1, 1)"); + testUnary2D(M2.step(-5, -2), "DS; M.step(-5, -2)"); + testUnary2D(M2.step(16, 3), "DS; M.step(16, 3)"); + + t.end(); +}); diff --git a/types/cwise/index.d.ts b/types/cwise/index.d.ts new file mode 100644 index 0000000000..d99d43ce59 --- /dev/null +++ b/types/cwise/index.d.ts @@ -0,0 +1,24 @@ +// Type definitions for cwise 1.0 +// Project: https://github.com/scijs/cwise#readme +// Definitions by: taoqf +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { ArgType } from "cwise-compiler"; +import * as ndarray from "ndarray"; + +declare function cwise(a: string | cwise.UserArgs): cwise.Return; + +declare namespace cwise { + type Arg = ndarray | ((row: number, col: number) => number) | number[] | any; + type Return = (a: ndarray, ...b: Arg[]) => void; + interface UserArgs { + args: ArgType[]; + pre?(a: number, ...args: any[]): void; + body(a: number, ...args: any[]): void; + post?(a: number, ...args: any[]): void; + funcName?: string; + blockSize?: number; + printCode?: boolean; + } +} +export = cwise; diff --git a/types/cwise/tsconfig.json b/types/cwise/tsconfig.json new file mode 100644 index 0000000000..f4aaf9b1a3 --- /dev/null +++ b/types/cwise/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cwise-tests.ts" + ] +} \ No newline at end of file diff --git a/types/cwise/tslint.json b/types/cwise/tslint.json new file mode 100644 index 0000000000..531fb4ef87 --- /dev/null +++ b/types/cwise/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file