Skip to content

Commit c350581

Browse files
committed
v.1.0.0
1 parent 90e19e7 commit c350581

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
View one-dimensional array data, typed array data and/or multi-dimensional array data as multidimensional tensors of various shapes efficiently.
44

5-
version: **1.0.0** (8.9 kB minified)
5+
version: **1.0.0** (9 kB minified)
66

77
`TensorView` is both memory-efficient and speed-efficient since it only creates ways to view array data as multidimensional tensors **without** actually creating new arrays. One can nevertheless explicitly store a TensorView instance as a single-dimensional or multi-dimensional array using `view.toArray()` or `view.toNDArray()` methods.
88

src/TensorView.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ function TensorView(data, o, _)
2626

2727
var self = this,
2828
is_transposed = false, is_value = false,
29-
op = null, refs = null,
30-
stack = null, stack_axis = -1,
29+
op = null, refs = null, stack = null, stack_axis = -1,
3130
global_indices = null, nd_shape = null,
32-
ndim = 0, shape = null, stride = null,
33-
slicing = null, size = null, length = 0, total = 0;
31+
ndim = 0, shape = null, stride = null, size = null,
32+
slicing = null, default_slicing = true, length = 0, total = 0;
3433

3534
function compute_index(indices, ndim, transposed, shape, stride, size, slicing)
3635
{
@@ -259,6 +258,18 @@ function TensorView(data, o, _)
259258
ndim = shape.length;
260259
}
261260

261+
stride = new Array(ndim);
262+
if (is_transposed)
263+
{
264+
stride[0] = 1;
265+
for (var i=1; i<ndim; ++i) stride[i] = stride[i-1]*shape[i-1];
266+
}
267+
else
268+
{
269+
stride[ndim-1] = 1;
270+
for (var i=ndim-2; i>=0; --i) stride[i] = stride[i+1]*shape[i+1];
271+
}
272+
262273
slicing = o.slice;
263274
if (!slicing || !slicing.length)
264275
{
@@ -272,22 +283,11 @@ function TensorView(data, o, _)
272283
for (var i=0; i<ndim; ++i)
273284
{
274285
var s = slicing[i] || {start:0, stop:shape[i]-1, step:1};
275-
slicing[i] = Slice._indices(shape[i], s.start, s.stop, s.step);
286+
slicing[i] = s = Slice._indices(shape[i], s.start, s.stop, s.step);
287+
if (s.start !== 0 || s.stop+1 !== shape[i] || s.step !== 1) default_slicing = false;
276288
}
277289
}
278290

279-
stride = new Array(ndim);
280-
if (is_transposed)
281-
{
282-
stride[0] = 1;
283-
for (var i=1; i<ndim; ++i) stride[i] = stride[i-1]*shape[i-1];
284-
}
285-
else
286-
{
287-
stride[ndim-1] = 1;
288-
for (var i=ndim-2; i>=0; --i) stride[i] = stride[i+1]*shape[i+1];
289-
}
290-
291291
size = new Array(ndim);
292292
for (var i=0; i<ndim; ++i) size[i] = slicing[i].count(shape[i]);
293293
length = ndim ? product(size) : 0;
@@ -416,7 +416,7 @@ function TensorView(data, o, _)
416416
);
417417
};
418418
self.reshape = function(shape) {
419-
return new TensorView(self, {shape: shape});
419+
return default_slicing && !stack && !refs ? new TensorView(data, {ndarray: nd_shape, shape: shape}) : new TensorView(self, {shape: shape});
420420
};
421421
self.concat = function(others, axis) {
422422
axis = axis || 0;

0 commit comments

Comments
 (0)