Loading...
Loading...
Design a tensor: a multidimensional array with dimensions shape that holds the entries of values in row-major order. For example, with shape [A, B, C] the elements of values sit at indices [0, 0, 0], [0, 0, 1], and so on up to [A - 1, B - 1, C - 1], in that order.
Implement the class Tensor:
Tensor(values, shape) creates a tensor with dimensions shape holding the entries of values in row-major order.get(indices) returns the entry at the position indices, which holds one index per current dimension.reshape(shape) replaces the dimensions with shape and keeps the entries in row-major order.sumLastDim() replaces the tensor with its sum over the last dimension. The summed dimension disappears and the remaining dimensions keep their relative order.getShape() returns the current dimensions as a list of integers.You can assume every call is valid: indices holds one entry per current dimension and each entry is in range, and reshape receives positive dimensions whose entries multiply to the current number of entries. sumLastDim() is only called when the tensor has at least one dimension. If sumLastDim() is called when the tensor has one dimension, one entry remains, getShape() returns [], and get([]) returns that entry.
1 <= values.length <= 1024-1000 <= values[i] <= 10001 <= shape.length <= 61 <= shape[i] <= 1024shape multiply to values.length.reshape call, 1 <= shape.length <= 6 and every entry of shape is positive.reshape call, the entries of shape multiply to the current number of entries.sumLastDim is only called when the current tensor has at least one dimension.0 <= indices.length <= 6, indices.length equals the current number of dimensions, and 0 <= indices[i] < shape[i] for every i.1 <= number of calls <= 6000 including the constructor.The entries fill a 2 by 2 grid in row-major order. Flattening keeps the same four entries, and summing the last dimension leaves their total 10.
The last index moves fastest, so the entry at [0,1,1] is the fourth value, 4. Summing the last dimension (after the reshape to [2,4]) gives a tensor whose entries are the row sums.
A one-dimensional tensor sums to a single entry, 5. Reshaping that entry to [1,1,1] and summing repeatedly keeps the same value.
A single entry in one dimension. Summing the last dimension leaves a tensor with no dimensions, so getShape() returns [] and get([]) reads the entry. Reshaping it to [1] brings the dimension back.