-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (33 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
var fill = require('ndarray-fill');
var isndarray = require('isndarray');
var isnonneg = require('validate.io-nonnegative-integer');
var isbool = require('validate.io-boolean');
module.exports = linspace;
function linspace (output, start, end, options) {
var n, endpoint, axis, d;
if (!isndarray(output)) {
throw new Error('ndarray-linspace: First argument must be a ndarray');
}
n = output.shape[0];
options = options || {};
if (options.endpoint !== undefined && !isbool(options.endpoint)) {
throw new Error('ndarray-linspace: Endpoint must be a boolean. Got ' + options.endpoint);
}
endpoint = options.endpoint === undefined ? true : options.endpoint;
if (options.axis !== undefined && !isnonneg(options.axis)) {
throw new Error('ndarray-linspace: Axis must be a nonegative integer. Got ' + options.axis);
}
// Default axis, after we've checked the input
axis = options.axis || 0;
if (axis > output.dimension) {
throw new Error('ndarray-linspace: Axis (' + axis + ') must be <= dimension (' + output.dimension + ')');
}
// Precompute the spacing:
d = (end - start) / Math.max(1, n - (endpoint ? 1 : 0));
// Fill it!
fill(output, function () {
return start + arguments[axis] * d;
});
return output;
}