-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (66 loc) · 2.18 KB
/
Copy pathindex.js
File metadata and controls
78 lines (66 loc) · 2.18 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict'
module.exports = concatRows
var extend = require('util-extend')
var ops = require('ndarray-ops')
var pool = require('ndarray-scratch')
var defaults = {
dtype: 'double'
}
function concatRows () {
var output, input, inputs, options, d, i, shape, idx, l, slice
options = extend({}, defaults)
if (arguments.length === 0) {
throw new Error('Array of ndarrays to concatenate must not be empty')
}
if (Array.isArray(arguments[0])) {
// If the first argument is an array, then assume it's the list
// of arrays to concatenate:
inputs = arguments[0]
extend(options, arguments[1] || {})
} else if (arguments.length === 2) {
// Otherwise assume the first argument is the output array:
inputs = arguments[1]
output = arguments[0]
extend(options, arguments[2] || {})
}
if (inputs.length === 0) {
throw new Error('Array of ndarrays to concatenate must not be empty')
}
for (d = 0; d < inputs.length; d++) {
// Verify the other dimensions:
if (!shape) {
// If no shape is set, set it:
shape = inputs[d].shape.slice(0)
} else {
// At the very least, all arrays must share teh same dimensionality:
if (inputs[d].dimension !== shape.length) {
throw new Error('all arrays must have the same dimensionality')
}
// If shape is set, then this shape must match:
for (i = 1; i < inputs[d].shape.length; i++) {
if (inputs[d].shape[i] !== shape[i]) {
throw new Error('last n-1 dimensions of concatenated rows must have the same size')
}
}
// Add to the size of the concatenated dimension:
shape[0] += inputs[d].shape[0]
}
}
if (output) {
if (shape[0] !== output.shape[0]) {
throw new Error('first dimension of output array must match the total number of concatenated rows')
}
} else {
// NB: Nothing after this can fail, otherwise we leak memory. So all
// assertions MUST happen before this.
output = pool.zeros(shape, options.dtype)
}
for (i = 0, idx = 0; i < inputs.length; i++) {
input = inputs[i]
l = input.shape[0]
slice = output.lo(idx).hi(l)
ops.assign(slice, input)
idx += l
}
return output
}