Skip to content

Commit ba7e214

Browse files
committed
Major refactoring of the API
1 parent 984fd44 commit ba7e214

File tree

5 files changed

+131
-101
lines changed

5 files changed

+131
-101
lines changed

README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55
## Example
66

77
```javascript
8-
var linspace = require('ndarray-linspace')
8+
var ndarray = require('ndarray');
9+
var linspace = require('ndarray-linspace');
910

10-
linspace(2, 3, 5)
11+
linspace(ndarray([], [5]), 2, 3);
1112
// => ndarray([2, 2.25, 2.5, 2.75, 3])
1213

13-
linspace(2, 3, 4, {endpoint: false})
14+
linspace(ndarray([], [5]), 2, 3, {endpoint: false});
1415
// => ndarray([2, 2.25, 2.5, 2.75])
1516

16-
linspace(2, 4, 10, {dtype: 'int8'})
17-
// => ndarray([2, 2, 2, 2, 2, 3, 3, 3, 3, 4])
18-
19-
var y = pool.zeros([5])
20-
linspace(y, 1, 3, 5)
21-
// y => ndarray([1, 1.5, 2, 2.5, 3])
22-
23-
var y = pool.zeros([2, 2])
24-
linspace(y, 0, 1, 2)
17+
linspace(ndarray([], [2, 2]), 0, 1)
2518
// y => [ 0, 0 ]
2619
// [ 1, 1 ]
20+
21+
linspace(ndarray([], [2, 2]), 0, 1, {axis: 1})
22+
// y => [ 0, 1 ]
23+
// [ 0, 1 ]
2724
```
2825

2926
## Installation
@@ -34,17 +31,16 @@ npm install ndarray-linspace
3431

3532
## API
3633

37-
#### `require('ndarray-linspace')([output,] start, end, steps[, options])`
34+
#### `require('ndarray-linspace')(output, start, end[, options])`
3835
An array of equally spaced values.
3936

4037
**Arguments**:
41-
- `output` (optional): if provided, the destination array to be filled with values. If the number of dimensions is greater than one, then the length of the first dimension must match the number of values and the entire ndarray will be filled with the linspace corresponding to the index of the first dimension.
38+
- `output` The destination array to be filled with values.
4239
- `start`: starting value of the interval
4340
- `end`: ending value of the interval
44-
- `steps`: number of divisions
4541
- `options` (optional): A hash of options. Options are:
46-
- `dtype`: dtype of output array (only used if output array not specified)
47-
- `endpoint` (boolean): whether the output contains the endpoint of the interval
42+
- `endpoint` (default: `true`): whether the output contains the endpoint of the interval
43+
- `axis` (default: `0`): the dimension along which to fill the array. Must be an integer less than or equal to the dimension of the input.
4844

4945
**Returns**: A reference to the output
5046

examples/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict'
1+
'use strict';
22

3-
var linspace = require('../')
3+
var linspace = require('../');
44

5-
console.log(linspace(2, 3, 10, {endpoint: false}))
5+
console.log(linspace(2, 3, 10, {endpoint: false}));
66

7-
console.log(linspace(2, 4, 10, {dtype: 'int8'}))
7+
console.log(linspace(2, 4, 10, {dtype: 'int8'}));

index.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
'use strict'
1+
'use strict';
22

3-
var pool = require('ndarray-scratch')
4-
var fill = require('ndarray-fill')
5-
var extend = require('util-extend')
3+
var fill = require('ndarray-fill');
4+
var isndarray = require('isndarray');
5+
var isnonneg = require('validate.io-nonnegative-integer');
6+
var isbool = require('validate.io-boolean');
67

7-
module.exports = linspace
8+
module.exports = linspace;
89

9-
var defaults = {
10-
endpoint: true,
11-
dtype: 'double'
12-
}
10+
function linspace (output, start, end, options) {
11+
var n, endpoint, axis, d;
12+
13+
if (!isndarray(output)) {
14+
throw new Error('ndarray-linspace: First argument must be a ndarray');
15+
}
1316

14-
function linspace () {
15-
var start, end, n, options, output
16-
17-
options = {}
18-
extend(options, defaults)
19-
20-
if (isFinite(arguments[0])) {
21-
start = arguments[0]
22-
end = arguments[1]
23-
n = arguments[2]
24-
extend(options, arguments[3] || {})
25-
output = pool.zeros([n], options.dtype)
26-
} else {
27-
output = arguments[0]
28-
start = arguments[1]
29-
end = arguments[2]
30-
n = arguments[3]
31-
extend(options, arguments[4] || {})
32-
33-
if (output.shape[0] !== n) {
34-
throw new Error('number of values (' + n + ') must match the first dimension of the output (' + output.shape[0] + ')')
35-
}
17+
n = output.shape[0];
18+
19+
options = options || {};
20+
21+
if (options.endpoint !== undefined && !isbool(options.endpoint)) {
22+
throw new Error('ndarray-linspace: Endpoint must be a boolean. Got ' + options.endpoint);
3623
}
24+
endpoint = !!(options.endpoint || true);
25+
26+
if (options.axis !== undefined && !isnonneg(options.axis)) {
27+
throw new Error('ndarray-linspace: Axis must be a nonegative integer. Got ' + options.axis);
28+
}
29+
30+
// Default axis, after we've checked the input
31+
axis = options.axis || 0;
32+
33+
if (axis > output.dimension) {
34+
throw new Error('ndarray-linspace: Axis (' + axis + ') must be <= dimension (' + output.dimension + ')');
35+
}
36+
37+
// Precompute the spacing:
38+
d = (end - start) / Math.max(1, n - (endpoint ? 1 : 0));
3739

38-
var d = (end - start) / (n - (options.endpoint ? 1 : 0))
39-
fill(output, function (i) {
40-
return start + i * d
41-
})
40+
// Fill it!
41+
fill(output, function () {
42+
return start + arguments[axis] * d;
43+
});
4244

43-
return output
45+
return output;
4446
}

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "ndarray-linspace",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Fill an ndarray with equally spaced values",
55
"main": "index.js",
66
"scripts": {
7-
"test": "standard && mocha test/*"
7+
"test": "semistandard && mocha test/*",
8+
"lint": "semistandard",
9+
"lint-fix": "semistandard --fix"
810
},
911
"keywords": [
1012
"ndarray",
@@ -20,12 +22,19 @@
2022
"chai": "^3.4.1",
2123
"mocha": "^2.3.4",
2224
"ndarray": "^1.0.18",
25+
"ndarray-show": "^2.0.0",
2326
"ndarray-tests": "^1.3.0",
24-
"standard": "^5.4.1"
27+
"semistandard": "^9.1.0"
2528
},
2629
"dependencies": {
30+
"is-positive-integer": "^1.1.1",
31+
"isndarray": "^1.0.0",
32+
"ndarray": "^1.0.18",
2733
"ndarray-fill": "^1.0.1",
2834
"ndarray-scratch": "^1.2.0",
29-
"util-extend": "^1.0.1"
35+
"util-extend": "^1.0.1",
36+
"validate.io-boolean": "^1.0.4",
37+
"validate.io-nonnegative-integer": "^1.0.0",
38+
"validate.io-number": "^1.0.3"
3039
}
3140
}

test/index.js

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,73 @@
11
/* global describe, it */
2-
'use strict'
2+
'use strict';
33

4-
var ndarray = require('ndarray')
5-
var ndt = require('ndarray-tests')
6-
var linspace = require('../')
7-
var assert = require('chai').assert
8-
var pool = require('ndarray-scratch')
4+
var ndarray = require('ndarray');
5+
var ndt = require('ndarray-tests');
6+
var linspace = require('../');
7+
var assert = require('chai').assert;
8+
// var show = require('ndarray-show');
99

1010
describe('linspace', function () {
11+
describe('failure modes', function () {
12+
it('fails if the first argument is not a ndarray', function () {
13+
assert.throws(function () {
14+
linspace(0, 10);
15+
}, Error, /must be a ndarray/);
16+
});
17+
18+
it('throws an error if the axis not a number', function () {
19+
assert.throws(function () {
20+
linspace(ndarray([], [2, 2]), 0, 1, {axis: '4'});
21+
}, Error, 'Axis must be a nonegative integer');
22+
});
23+
24+
it('throws an error if the axis is greater than the dimension', function () {
25+
assert.throws(function () {
26+
linspace(ndarray([], [2, 2]), 0, 1, {axis: 4});
27+
}, Error, 'must be <= dimension');
28+
});
29+
30+
it('fails if the endpoint is not a boolean', function () {
31+
assert.throws(function () {
32+
linspace(ndarray([], [1]), 0, 10, {endpoint: 7});
33+
}, Error, /must be a boolean/);
34+
});
35+
});
36+
1137
it('create a new linspace with and endpoint', function () {
12-
var x = linspace(0, 1, 11)
13-
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
14-
assert(ndt.equal(x, y, 1e-8))
15-
})
38+
var x = linspace(ndarray([], [11]), 0, 10);
39+
var y = ndarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
40+
assert(ndt.equal(x, y, 1e-8));
41+
});
1642

1743
it('create a new linspace without and endpoint', function () {
18-
var x = linspace(0, 1, 10, {endpoint: false})
19-
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
20-
assert(ndt.equal(x, y, 1e-8))
21-
})
22-
23-
it('allows a dtype', function () {
24-
var x = linspace(0, 10, 5, {dtype: 'int8'})
25-
var y = ndarray([0, 2, 5, 7, 10])
26-
assert(ndt.equal(x, y, 1e-8))
27-
})
28-
29-
it('writes in place if output array provided', function () {
30-
var x = pool.zeros([10])
31-
linspace(x, 0, 1, 10, {endpoint: false})
32-
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
33-
assert(ndt.equal(x, y, 1e-8))
34-
})
44+
var x = linspace(ndarray([], [10]), 0, 9, {endpoint: false});
45+
var y = ndarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
46+
assert(ndt.equal(x, y, 1e-8));
47+
});
3548

3649
it('fills in more than one dimension', function () {
37-
var x = pool.zeros([2, 2])
38-
linspace(x, 0, 1, 2)
39-
var y = ndarray([0, 0, 1, 1], [2, 2])
40-
assert(ndt.equal(x, y, 1e-8))
41-
})
42-
43-
it("throws an error if the size of the first dimension doesn't match the number of points", function () {
44-
var x = pool.zeros([2, 2])
45-
assert.throws(function () {
46-
linspace(x, 0, 1, 10)
47-
}, Error, 'must match')
48-
})
49-
})
50+
var x = linspace(ndarray([], [2, 2]), 0, 1);
51+
var y = ndarray([0, 0, 1, 1], [2, 2]);
52+
assert(ndt.equal(x, y, 1e-8));
53+
});
54+
55+
it('accepts a different axis', function () {
56+
var x = linspace(ndarray([], [2, 2]), 0, 1, {axis: 1});
57+
var y = ndarray([0, 1, 0, 1], [2, 2]);
58+
assert(ndt.equal(x, y, 1e-8));
59+
});
60+
61+
it('uses the first point if only one point requested and endpoint = true', function () {
62+
var x = linspace(ndarray([], [1]), 1, 2, {endpoint: true});
63+
var y = ndarray([1], [1]);
64+
assert(ndt.equal(x, y));
65+
});
66+
67+
it('uses the first point if only one point requested and endpoint = false', function () {
68+
var x = linspace(ndarray([], [1]), 1, 2, {endpoint: false});
69+
var y = ndarray([1], [1]);
70+
assert(ndt.equal(x, y));
71+
});
72+
});
5073

0 commit comments

Comments
 (0)