### Question:
GenericTensor extends JVMTensor<T, GenericTensor<T>, GenericBuffer.PrimitiveGenericWrapper<T>> implements Tensor<T, GenericTensor<T>> { public static <T> GenericTensor<T> create(T... data) { return create(data, new long[]{data.length}); } private  GenericTensor(GenericBuffer.PrimitiveGenericWrapper<T> buffer, long[] shape, long[] stride); private  GenericTensor(GenericBuffer.PrimitiveGenericWrapper<T> buffer, long[] shape); private  GenericTensor(T[] buffer, long[] shape, long[] stride); private  GenericTensor(T[] buffer, long[] shape); private  GenericTensor(T scalar); static GenericTensor<T> createFilled(T data, long[] shape); static GenericTensor<T> create(T... data); static GenericTensor<T> create(T[] data, long[] shape); static GenericTensor<T> scalar(T data); @Override BooleanTensor elementwiseEquals(GenericTensor<T> that); @Override T[] asFlatArray(); GenericTensor<R> apply(Tensor<T, ?> right,
                                      BiFunction<T, T, R> op);  }### Answer:
@Test public void canGetRandomAccessValue() { GenericTensor<Something> somethingTensor = GenericTensor.create( new Something[]{ Something.A, Something.B, Something.B, Something.C, Something.D, Something.B, Something.D, Something.A, Something.C }, new long[]{3, 3} ); assertEquals(Something.D, somethingTensor.getValue(1, 1)); }
@Test public void canSetRandomAccessValue() { GenericTensor<Something> somethingTensor = GenericTensor.create( new Something[]{ Something.A, Something.B, Something.B, Something.C, Something.D, Something.B, Something.D, Something.A, Something.C }, new long[]{3, 3} ); assertEquals(Something.D, somethingTensor.getValue(1, 1)); somethingTensor.setValue(Something.A, 1, 1); assertEquals(Something.A, somethingTensor.getValue(1, 1)); }
@Test public void canPermute() { GenericTensor<Something> somethingTensor = GenericTensor.create( Something.A, Something.B, Something.B, Something.C, Something.D, Something.B, Something.D, Something.A, Something.C ).reshape(3, 3); GenericTensor<Something> permute = somethingTensor.permute(1, 0); GenericTensor<Something> expected = GenericTensor.create( Something.A, Something.C, Something.D, Something.B, Something.D, Something.A, Something.B, Something.B, Something.C ).reshape(3, 3); assertThat(permute, valuesAndShapesMatch(expected)); }
@Test public void canBroadcastToShape() { GenericTensor<String> a = GenericTensor.create( "a", "b", "c" ).reshape(3); GenericTensor<String> expectedByRow = GenericTensor.create( "a", "b", "c", "a", "b", "c", "a", "b", "c" ).reshape(3, 3); assertThat(a.broadcast(3, 3), valuesAndShapesMatch(expectedByRow)); GenericTensor<String> expectedByColumn = GenericTensor.create( "a", "a", "a", "b", "b", "b", "c", "c", "c" ).reshape(3, 3); assertThat(a.reshape(3, 1).broadcast(3, 3), valuesAndShapesMatch(expectedByColumn)); }
@Test public void canDiagFromVector() { GenericTensor<String> expected = GenericTensor.create(new String[]{"1", null, null, null, "5", null, null, null, "9"}).reshape(3, 3); GenericTensor<String> actual = GenericTensor.create("1", "5", "9").diag(); assertEquals(expected, actual); }
@Test public void canDiagFromMatrix() { GenericTensor<String> actual = GenericTensor.create(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"}).reshape(3, 3).diagPart(); GenericTensor<String> expected = GenericTensor.create("1", "5", "9"); assertEquals(expected, actual); }