Skip to content

Commit 9429360

Browse files
committed
Add Typescript definitions
1 parent 2a52b8a commit 9429360

File tree

12 files changed

+653
-0
lines changed

12 files changed

+653
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
22+
/**
23+
* Compiles a module string which exports a function for evaluating a polynomial.
24+
*
25+
* @param c - polynomial coefficients sorted in ascending degree
26+
* @returns module string exporting a function for evaluating a polynomial
27+
*
28+
* @example
29+
* var str = compile( [ 3.0, 2.0, 1.0 ] );
30+
* // returns <string>
31+
*/
32+
declare function compile( c: Array<number> ): string;
33+
34+
35+
// EXPORTS //
36+
37+
export = compile;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import compile = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
compile( [ 3.0, 2.0, 1.0 ] ); // $ExpectType string
27+
}
28+
29+
// The function does not compile if provided a first argument which is not an array of numbers...
30+
{
31+
compile( true ); // $ExpectError
32+
compile( false ); // $ExpectError
33+
compile( 'abc' ); // $ExpectError
34+
compile( 123 ); // $ExpectError
35+
compile( {} ); // $ExpectError
36+
compile( ( x: number ): number => x ); // $ExpectError
37+
}
38+
39+
// The compiler throws an error if the function is provided an insufficient number of arguments...
40+
{
41+
compile(); // $ExpectError
42+
}

lib/node_modules/@stdlib/math/base/tools/evalpoly-compile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Evaluates a polynomial.
23+
*
24+
* @param x - value at which to evaluate a polynomial
25+
* @returns evaluated polynomial
26+
*/
27+
type EvaluationFunction = ( x: number ) => number;
28+
29+
/**
30+
* Interface for evaluating polynomials.
31+
*/
32+
interface EvalPoly {
33+
/**
34+
* Evaluates a polynomial.
35+
*
36+
* ## Notes
37+
*
38+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
39+
*
40+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
41+
*
42+
*
43+
* @param c - polynomial coefficients sorted in ascending degree
44+
* @param x - value at which to evaluate the polynomial
45+
* @returns evaluated polynomial
46+
*
47+
* @example
48+
* var v = evalpoly( [3.0,2.0,1.0], 10.0 ); // 3*10^0 + 2*10^1 + 1*10^2
49+
* // returns 123.0
50+
*/
51+
( c: Array<number>, x: number ): number;
52+
53+
/**
54+
* Generates a function for evaluating a polynomial.
55+
*
56+
* ## Notes
57+
*
58+
* - The compiled function uses [Horner's rule][horners-method] for efficient computation.
59+
*
60+
* [horners-method]: http://en.wikipedia.org/wiki/Horner%27s_method
61+
*
62+
*
63+
* @param c - polynomial coefficients sorted in ascending degree
64+
* @returns function for evaluating a polynomial
65+
*
66+
* @example
67+
* var polyval = evalpoly.factory( [3.0,2.0,1.0] );
68+
*
69+
* var v = polyval( 10.0 ); // => 3*10^0 + 2*10^1 + 1*10^2
70+
* // returns 123.0
71+
*
72+
* v = polyval( 5.0 ); // => 3*5^0 + 2*5^1 + 1*5^2
73+
* // returns 38.0
74+
*/
75+
factory( c: Array<number> ): EvaluationFunction;
76+
}
77+
78+
/**
79+
* Evaluates a polynomial.
80+
*
81+
* ## Notes
82+
*
83+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
84+
*
85+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
86+
*
87+
*
88+
* @param c - polynomial coefficients sorted in ascending degree
89+
* @param x - value at which to evaluate the polynomial
90+
* @returns evaluated polynomial
91+
*
92+
* @example
93+
* var v = evalpoly( [3.0,2.0,1.0], 10.0 ); // 3*10^0 + 2*10^1 + 1*10^2
94+
* // returns 123.0
95+
*
96+
* @example
97+
* var polyval = evalpoly.factory( [3.0,2.0,1.0] );
98+
*
99+
* var v = polyval( 10.0 ); // => 3*10^0 + 2*10^1 + 1*10^2
100+
* // returns 123.0
101+
*
102+
* v = polyval( 5.0 ); // => 3*5^0 + 2*5^1 + 1*5^2
103+
* // returns 38.0
104+
*/
105+
declare var evalpoly: EvalPoly;
106+
107+
108+
// EXPORTS //
109+
110+
export = evalpoly;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import evalpoly = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
evalpoly( [ 3.0, 2.0, 1.0 ], 10.0 ); // $ExpectType number
27+
}
28+
29+
// The function does not compile if provided a first argument which is not an array of numbers...
30+
{
31+
evalpoly( true, 10.0 ); // $ExpectError
32+
evalpoly( false, 10.0 ); // $ExpectError
33+
evalpoly( 'abc', 10.0 ); // $ExpectError
34+
evalpoly( 123, 10.0 ); // $ExpectError
35+
evalpoly( {}, 10.0 ); // $ExpectError
36+
evalpoly( ( x: number ): number => x, 10.0 ); // $ExpectError
37+
}
38+
39+
// The function does not compile if provided a second argument which is not a number...
40+
{
41+
const c = [ 3.0, 2.0, 1.0 ];
42+
evalpoly( c, true ); // $ExpectError
43+
evalpoly( c, false ); // $ExpectError
44+
evalpoly( c, 'abc' ); // $ExpectError
45+
evalpoly( c, [] ); // $ExpectError
46+
evalpoly( c, {} ); // $ExpectError
47+
evalpoly( c, ( x: number ): number => x ); // $ExpectError
48+
}
49+
50+
// The compiler throws an error if the function is provided an insufficient number of arguments...
51+
{
52+
const c = [ 3.0, 2.0, 1.0 ];
53+
evalpoly(); // $ExpectError
54+
evalpoly( c ); // $ExpectError
55+
}
56+
57+
// Attached to main export is a `factory` method which returns a function...
58+
{
59+
const c = [ 3.0, 2.0, 1.0 ];
60+
evalpoly.factory( c ); // $ExpectType EvaluationFunction
61+
}
62+
63+
// The compiler throws an error if the `factory` method is provided an unsupported number of arguments...
64+
{
65+
const c = [ 3.0, 2.0, 1.0 ];
66+
evalpoly.factory(); // $ExpectError
67+
evalpoly.factory( c, 2.0 ); // $ExpectError
68+
}
69+
70+
// The `factory` method returns a function which returns a number...
71+
{
72+
const c = [ 3.0, 2.0, 1.0 ];
73+
const polyval = evalpoly.factory( c );
74+
polyval( 1.0 ); // $ExpectType number
75+
}
76+
77+
// The `factory` method returns a function which does not compile if provided a first argument which is not a number...
78+
{
79+
const c = [ 3.0, 2.0, 1.0 ];
80+
const polyval = evalpoly.factory( c );
81+
polyval( true ); // $ExpectError
82+
polyval( false ); // $ExpectError
83+
polyval( 'abc' ); // $ExpectError
84+
polyval( [] ); // $ExpectError
85+
polyval( {} ); // $ExpectError
86+
polyval( ( x: number ): number => x ); // $ExpectError
87+
}
88+
89+
// The compiler throws an error if the `factory` method is provided a first argument which is not an array of numbers...
90+
{
91+
evalpoly.factory( true ); // $ExpectError
92+
evalpoly.factory( false ); // $ExpectError
93+
evalpoly.factory( 'abc' ); // $ExpectError
94+
evalpoly.factory( 123 ); // $ExpectError
95+
evalpoly.factory( {} ); // $ExpectError
96+
evalpoly.factory( ( x: number ): number => x ); // $ExpectError
97+
}

lib/node_modules/@stdlib/math/base/tools/evalpoly/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
22+
/**
23+
* Compiles a module string which exports a function for evaluating a rational function.
24+
*
25+
* @param P - numerator polynomial coefficients sorted in ascending degree
26+
* @param Q - denominator polynomial coefficients sorted in ascending degree
27+
* @returns module string exporting a function for evaluating a rational function
28+
*
29+
* @example
30+
* var P = [ -6.0, -5.0 ];
31+
* var Q = [ 3.0, 0.5 ];
32+
*
33+
* var str = compile( P, Q );
34+
* // returns <string>
35+
*/
36+
declare function compile( P: Array<number>, Q: Array<number> ): string;
37+
38+
39+
// EXPORTS //
40+
41+
export = compile;

0 commit comments

Comments
 (0)