From d7601435a4a30ca9d34e2d18df54ad1ce8bcfc1b Mon Sep 17 00:00:00 2001 From: taoqf Date: Wed, 26 Jul 2017 22:09:06 +0800 Subject: [PATCH] add type definition numjs (#18343) * add type definition numjs * move ndtype from ndarry to numjs, remove unnecessary comments * remote jsdoc annotations --- types/cwise-compiler/index.d.ts | 1 + types/cwise/index.d.ts | 1 + types/ndarray/index.d.ts | 38 ++- types/numjs/index.d.ts | 555 ++++++++++++++++++++++++++++++++ types/numjs/numjs-tests.ts | 23 ++ types/numjs/tsconfig.json | 22 ++ types/numjs/tslint.json | 1 + 7 files changed, 624 insertions(+), 17 deletions(-) create mode 100644 types/numjs/index.d.ts create mode 100644 types/numjs/numjs-tests.ts create mode 100644 types/numjs/tsconfig.json create mode 100644 types/numjs/tslint.json diff --git a/types/cwise-compiler/index.d.ts b/types/cwise-compiler/index.d.ts index e661934a6a..8703355ba3 100644 --- a/types/cwise-compiler/index.d.ts +++ b/types/cwise-compiler/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/scijs/cwise-compiler // Definitions by: taoqf // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 import { CompiledRoutine } from 'cwise-parser'; import * as ndarray from 'ndarray'; diff --git a/types/cwise/index.d.ts b/types/cwise/index.d.ts index d99d43ce59..95df45bb03 100644 --- a/types/cwise/index.d.ts +++ b/types/cwise/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/scijs/cwise#readme // Definitions by: taoqf // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 import { ArgType } from "cwise-compiler"; import * as ndarray from "ndarray"; diff --git a/types/ndarray/index.d.ts b/types/ndarray/index.d.ts index f409e7fe74..718c7075c3 100644 --- a/types/ndarray/index.d.ts +++ b/types/ndarray/index.d.ts @@ -1,34 +1,38 @@ // Type definitions for ndarray 1.0 // Project: https://github.com/scijs/ndarray -// Definitions by: Giff Song +// Definitions by: Giff Song , taoqf // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 -declare function ndarray( - data: ndarray.Data, shape?: number[], stride?: number[], offset?: number -): ndarray; +declare function ndarray( + data: ndarray.Data, shape?: number[], stride?: number[], offset?: number +): ndarray; -interface ndarray { - data: ndarray.Data; +interface ndarray { + data: ndarray.Data; shape: number[]; stride: number[]; offset: number; - dtype: 'int8' | 'int16' | 'int32' | 'uint8' | 'uint16' | 'uint32' | - 'float32' | 'float64' | 'array' | 'uint8_clamped' | 'buffer' | 'generic'; + dtype: ndarray.DataType; size: number; order: number[]; dimension: number; - get(...args: number[]): number; - set(...args: number[]): number; - index(...args: number[]): number; - lo(...args: number[]): ndarray; - hi(...args: number[]): ndarray; - step(...args: number[]): ndarray; - transpose(...args: number[]): ndarray; - pick(...args: number[]): ndarray; + get(...args: number[]): T; + set(...args: number[]): T; + index(...args: number[]): T; + lo(...args: number[]): ndarray; + hi(...args: number[]): ndarray; + step(...args: number[]): ndarray; + transpose(...args: number[]): ndarray; + pick(...args: number[]): ndarray; + reshape(...shapes: number[]): ndarray; + T: ndarray; } declare namespace ndarray { - type Data = number[] | Int8Array | Int16Array | Int32Array | + type DataType = 'int8' | 'int16' | 'int32' | 'uint8' | 'uint16' | 'uint32' | + 'float32' | 'float64' | 'array' | 'uint8_clamped' | 'buffer' | 'generic'; + type Data = T[] | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Float32Array | Float64Array | Uint8ClampedArray; } diff --git a/types/numjs/index.d.ts b/types/numjs/index.d.ts new file mode 100644 index 0000000000..af464b7410 --- /dev/null +++ b/types/numjs/index.d.ts @@ -0,0 +1,555 @@ +// Type definitions for numjs 0.14 +// Project: https://github.com/nicolaspanel/numjs#readme +// Definitions by: taoqf +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +export as namespace nj; +import * as BaseNdArray from 'ndarray'; + +export type NdType = BaseNdArray.DataType | BaseNdArray.Data; + +export interface NdArray extends BaseNdArray { + ndim: number; + T: NdArray; + slice(...args: number[]): NdArray; + + /** + * Return a copy of the array collapsed into one dimension using row-major order (C-style) + * + */ + flatten

(): NdArray

; + + /** + * Permute the dimensions of the array. + * + */ + transpose(args?: number[]): NdArray; + transpose(...args: number[]): NdArray; + + /** + * Dot product of two arrays. + * + */ + dot(x: NjArray): NdArray; + + /** + * Assign `x` to the array, element-wise. + * + * @param {boolean} [copy=true] + */ + assign(x: NjParam, copy?: boolean): NdArray; + + /** + * Add `x` to the array, element-wise. + * + * @param {boolean} [copy=true] + */ + add(x: NjParam, copy?: boolean): NdArray; + + /** + * Subtract `x` to the array, element-wise. + * + * @param {boolean} [copy=true] + */ + subtract(x: NjParam, copy?: boolean): NdArray; + + /** + * Multiply array by `x`, element-wise. + * + * @param {boolean} [copy=true] + */ + multiply(x: NjParam, copy?: boolean): NdArray; + + /** + * Divide array by `x`, element-wise. + * + * @param {boolean} [copy=true] + */ + divide(x: NjParam, copy?: boolean): NdArray; + + /** + * Raise array elements to powers from given array, element-wise. + * + * @param {boolean} [copy=true] - set to false to modify the array rather than create a new one + */ + pow(x: NjParam, copy?: boolean): NdArray; + + /** + * Calculate the exponential of all elements in the array, element-wise. + * + * @param {boolean} [copy=true] - set to false to modify the array rather than create a new one + */ + exp(copy?: boolean): NdArray; + + /** + * Calculate the positive square-root of all elements in the array, element-wise. + * + * @param {boolean} [copy=true] - set to false to modify the array rather than create a new one + */ + sqrt(copy?: boolean): NdArray; + + /** + * Return the maximum value of the array + * + */ + max(): T; + + /** + * Return the minimum value of the array + * + */ + min(): T; + + /** + * Sum of array elements. + * + */ + sum(): T; + + /** + * Returns the standard deviation, a measure of the spread of a distribution, of the array elements. + * + */ + std(): number; + + /** + * Return the arithmetic mean of array elements. + * + */ + mean(): T; + + /** + * Converts {NdArray} to a native JavaScript {Array} + * + */ + tolist(): T[]; + + valueOf(): T[]; + + /** + * Stringify the array to make it readable in the console, by a human. + * + */ + inspect(): string; + + /** + * Stringify object to JSON + */ + toJSON(): any; + + /** + * Create a full copy of the array + * + */ + clone(): NdArray; + + /** + * Return true if two arrays have the same shape and elements, false otherwise. + */ + equal(array: NjArray): boolean; + + /** + * Round array to the to the nearest integer. + * + * @param {boolean} [copy=true] + */ + round(copy?: boolean): NdArray; + + /** + * Return the inverse of the array, element-wise. + * + */ + negative(): NdArray; + + diag(): NdArray; + + iteraxis(axis: number, cb: (x: NdArray, i: number) => any): void; + + /** + * Returns the discrete, linear convolution of the array using the given filter. + * + * @note: Arrays must have the same dimensions and `filter` must be smaller than the array. + * @note: The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. This behaviour is known as the 'valid' mode. + * @note: Use optimized code for 3x3, 3x3x1, 5x5, 5x5x1 filters, FFT otherwise. + * + */ + convolve(filter: NjArray): NdArray; + + fftconvolve(filter: NjArray): NdArray; +} + +export type NdArrayData = BaseNdArray.Data; +export type NjArray = NdArrayData | NdArray; +export type NjParam = NjArray | number; + +/** + * Return absolute value of the input array, element-wise. + * + */ +export function abs(x: NjParam): NdArray; + +/** + * Add arguments, element-wise. + * + */ +export function add(a: NjParam, b: NjParam): NdArray; + +/** + * Return evenly spaced values within a given interval. + * + * @param {number} [start = 0] Start of interval. The interval includes this value. + * @param {number} stop End of interval. The interval does not include this value. + * @param {number} [step = 1] Spacing between values. The default step size is 1. If step is specified, start must also be given. + * @param {(NdArray.ndType)} [dtype = Array] The type of the output array. + * @returns {NdArray} Array of evenly spaced values. + */ +export function arange(start: number, stop?: number, dtype?: NdType): NdArray; +export function arange(stop: number, dtype: NdType): NdArray; +export function arange(start: number, stop: number, step: number, dtype?: NdType): NdArray; + +/** + * Return trigonometric inverse cosine of the input array, element-wise. + * + */ +export function arccos(x: NjParam): NdArray; + +/** + * Return trigonometric inverse sine of the input array, element-wise. + * + */ +export function arcsin(x: NjParam): NdArray; + +/** + * Return trigonometric inverse tangent of the input array, element-wise. + * + */ +export function arctan(x: NjParam): NdArray; + +/** + * Clip (limit) the values in an array between min and max, element-wise. + * + * @param {number} [min = 0] + * @param {number} [max = 1] + */ +export function clip(x: NjParam, min?: number, max?: number): NdArray; +/** + * Join given arrays along the last axis. + * + */ +export function concatenate(...arrays: Array>): NdArray; + +/** + * Convolve 2 N-dimensionnal arrays + * + */ +export function convolve(a: NjArray, b: NjArray): NdArray; + +/** + * Return trigonometric cosine of the input array, element-wise. + * + */ +export function cos(x: NjParam): NdArray; + +/** + * Divide `a` by `b`, element-wise. + * + */ +export function divide(a: NjArray, b: NjParam): NdArray; + +/** + * Dot product of two arrays. WARNING: supported products are: - matrix dot matrix - vector dot vector - matrix dot vector - vector dot matrix + * + */ +export function dot(a: NjArray, b: NjArray): NdArray; + +/** + * Return a new array of given shape and type, filled with `undefined` values. + * + * @param {(ndArray | number)} shape Shape of the new array, e.g., [2, 3] or 2. + * @param {NdArray.ndType} [dtype] The type of the output array. + * @returns {NdArray} Array of `undefined` values with the given shape and dtype + */ +export function empty(shape: NdArrayData | number, dtype?: NdType): NdArray; + +/** + * Return true if two arrays have the same shape and elements, false otherwise. + * + */ +export function equal(a: NjArray, b: NjArray): boolean; + +/** + * Calculate the exponential of all elements in the input array, element-wise. + * + */ +export function exp(x: NjParam): NdArray; + +/** + * Convolve 2 N-dimensionnal arrays using Fast Fourier Transform (FFT) + * + */ +export function fftconvolve(a: NjArray, b: NjArray): NdArray; + +/** + * Return a copy of the array collapsed into one dimension using row-major order (C-style) + * + */ +export function flatten(array: NjArray): NdArray; + +export function getRawData(array: NdArrayData): Uint8Array; +export function setRawData(array: NdArrayData, data: NdArrayData): Uint8Array; + +/** + * Return the maximum value of the array + * + */ +export function max(x: NjParam): T; + +/** + * Return the arithmetic mean of input array elements. + * + */ +export function mean(x: NjParam): T; + +/** + * Return the minimum value of the array + * + */ +export function min(x: NjParam): T; + +/** + * Multiply arguments, element-wise. + * + */ +export function multiply(a: NjArray, b: NjParam): NdArray; + +/** + * Return the inverse of the input array, element-wise. + * + */ +export function negative(x: NjParam): NdArray; + +/** + * Return a new array of given shape and type, filled with ones. + * + * @param {(ndArray | number)} shape Shape of the new array, e.g., [2, 3] or 2. + * @param {NdArray.dtType} [dtype] The type of the output array. + * @returns {NdArray} Array of ones with the given shape and dtype + */ +export function ones(shape: NdArrayData | number, dtype?: BaseNdArray.DataType): NdArray; + +/** + * Raise first array elements to powers from second array, element-wise. + * + */ +export function power(x1: NjParam, x2: NjParam): NdArray; + +/** + * Create an array of the given shape and propagate it with random samples from a uniform distribution over [0, 1]. + * + * @param {(ndArray | number)} [shape] he dimensions of the returned array, should all be positive integers + */ +export function random(shape?: NdArrayData | number): NdArray; + +/** + * Gives a new shape to an array without changing its data. + * + * @param {ndArray} shape The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length + */ +export function reshape(array: NjArray, shape: NdArray): NdArray; + +/** + * Round an array to the to the nearest integer. + * + */ +export function round(x: NjArray): NdArray; + +/** + * Return the sigmoid of the input array, element-wise. + * + * @param {number} [t = 1] stifness parameter + */ +export function sigmoid(x: NjParam, t?: number): NdArray; + +/** + * Return trigonometric sine of the input array, element-wise. + * + */ +export function sin(x: NjParam): NdArray; + +/** + * Return the softmax, or normalized exponential, of the input array, element-wise. + * + */ +export function softmax(x: NjParam): NdArray; + +/** + * Calculate the positive square-root of all elements in the input array, element-wise. + * + */ +export function sqrt(x: NjParam): NdArray; + +/** + * Returns the standard deviation, a measure of the spread of a distribution, of the input array elements. + * + */ +export function std(x: NjParam): T; + +/** + * Subtract second argument from the first, element-wise. + * + */ +export function subtract(a: NjParam, b: NjParam): T; + +/** + * Return the sum of input array elements. + * + */ +export function sum(x: NjParam): T; + +/** + * Return trigonometric tangent of the input array, element-wise. + * + */ +export function tan(x: NjParam): NdArray; + +/** + * Return hyperbolic tangent of the input array, element-wise. + * + */ +export function tanh(x: NjParam): NdArray; + +/** + * Permute the dimensions of the input array according to the given axes. + * + * @example + * + * arr = nj.arange(6).reshape(1,2,3) + * // array([[[ 0, 1, 2], + * // [ 3, 4, 5]]]) + * arr.T + * // array([[[ 0], + * // [ 3]], + * // [[ 1], + * // [ 4]], + * // [[ 2], + * // [ 5]]]) + * arr.transpose(1,0,2) + * // array([[[ 0, 1, 2]], + * // [[ 3, 4, 5]]]) + */ +export function transpose(x: NjParam, axes?: number): NdArray; + +/** + * Return a new array of given shape and type, filled with zeros. + * + * @param {ndArray} shape Shape of the new array, e.g., [2, 3] or 2. + * @param {NdArray.dtType} [dtype = Array] The type of the output array. + * @returns {numjs} Array of zeros with the given shape and dtype + */ +export function zeros(shape: NdArrayData | number, dtype?: BaseNdArray.DataType): NdArray; + +export namespace errors { + function ValueError(message?: string): Error; + function ConfigError(message?: string): Error; + function NotImplementedError(message?: string): Error; +} + +export function broadcast(shape1: T[], shape2: U[]): Array; + +export function fft(x: NjArray): NdArray; + +export function ifft(x: NjArray): NdArray; + +/** + * Extract a diagonal or construct a diagonal array. + * + * @returns {NdArray} a view a of the original array when possible, a new array otherwise + */ +export function diag(x: NjArray): NdArray; + +/** + * The identity array is a square array with ones on the main diagonal. + * @param {number} Number of rows (and columns) in n x n output. + * @param {(String|Object)} [dtype=Array] The type of the output array. + * @return {Array} n x n array with its main diagonal set to one, and all other elements 0 + */ +export function identity(n: T, dtype?: BaseNdArray.DataType): NdArray; + +/** + * Join a sequence of arrays along a new axis. + * The axis parameter specifies the index of the new axis in the dimensions of the result. + * For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension. + * @param {Array} sequence of array_like + * @param {number} [axis=0] The axis in the result array along which the input arrays are stacked. + * @return {Array} The stacked array has one more dimension than the input arrays. + */ +export function stack(arrays: Array>, axis?: number): NdArray; + +export namespace images { + namespace data { + /** + * @property {NdArray} digit - 28x28 grayscale image with an handwritten digit extracted from MNIST database + */ + const digit: NdArray; + /** + * @property {NdArray} five - 28x28 grayscale image with an handwritten digit extracted from MNIST database + */ + const five: NdArray; + /** + * @property {NdArray} node - 300x600 COLOR image representing Node.js's logo + */ + const node: NdArray; + /** + * @property {NdArray} lena - The standard, yet sometimes controversial + * Lena test image was scanned from the November 1972 edition of + * Playboy magazine. From an image processing perspective, this image + * is useful because it contains smooth, textured, shaded as well as + * detail areas. + */ + const lena: NdArray; + /** + * @property {NdArray} lenna - The standard, yet sometimes + * controversial Lena test image was scanned from the November 1972 + * edition of Playboy magazine. From an image processing perspective, + * this image is useful because it contains smooth, textured, shaded as + * well as detail areas. + */ + const lenna: NdArray; + /** + * @property {NdArray} moon - This low-contrast image of the surface of + * the moon is useful for illustrating histogram equalization and + * contrast stretching. + */ + const moon: NdArray; + } + function read(input: string): NdArray; + function save(img: NdArray, dest: string): void; + function resize(img: NdArray, height: number, width: number): NdArray; + function sat(img: NdArray): NdArray; + function ssat(img: NdArray): NdArray; + function sobel(img: NdArray): NdArray; + function scharr(img: NdArray): NdArray; + function areaSum(h0: number, w0: number, H: number, W: number, SAT: NdArray): number; + function areaValue(img: NdArray): number; + function rgb2gray(img: NdArray): NdArray; + function flip(img: NdArray): NdArray; +} + +export function array(arr: NjArray, dtype?: BaseNdArray.DataType): NdArray; + +export function int8(arr: NjArray): NjArray; + +export function uint8(arr: NjArray): NjArray; + +export function int16(arr: NjArray): NjArray; + +export function uint16(arr: NjArray): NjArray; + +export function int32(arr: NjArray): NjArray; + +export function uint32(arr: NjArray): NjArray; + +export function float32(arr: NjArray): NjArray; + +export function float64(arr: NjArray): NjArray; diff --git a/types/numjs/numjs-tests.ts b/types/numjs/numjs-tests.ts new file mode 100644 index 0000000000..b13cf60fb0 --- /dev/null +++ b/types/numjs/numjs-tests.ts @@ -0,0 +1,23 @@ +import { abs } from 'numjs'; +import * as nj from 'numjs'; + +const a = abs(2); + +const arr = nj.arange(6); +arr.reshape(1, 2, 3); +// array([[[ 0, 1, 2], +// [ 3, 4, 5]]]) +arr.T; +// array([[[ 0], +// [ 3]], +// [[ 1], +// [ 4]], +// [[ 2], +// [ 5]]]) +arr.transpose(1, 0, 2); +// array([[[ 0, 1, 2]], +// [[ 3, 4, 5]]]) + +const b = nj.array([2, 3, 4]); + +const c = nj.uint8([1, 2, 3]); diff --git a/types/numjs/tsconfig.json b/types/numjs/tsconfig.json new file mode 100644 index 0000000000..1c1fa8fbd3 --- /dev/null +++ b/types/numjs/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", + "numjs-tests.ts" + ] +} diff --git a/types/numjs/tslint.json b/types/numjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/numjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }