TensorOps_F32 { public static void insertSpatial(Tensor_F32 src , int []srcCoor, Tensor_F32 dst , int []dstCoor ) { if( srcCoor.length < 3) throw new IllegalArgumentException("dimensions must be >= 3 for src"); if( dstCoor.length < 3) throw new IllegalArgumentException("dimensions must be >= 3 for dst"); if( srcCoor.length != src.getDimension() ) throw new IllegalArgumentException("Coordinate length doesn't match tensor dimension for src"); if( dstCoor.length != dst.getDimension() ) throw new IllegalArgumentException("Coordinate length doesn't match tensor dimension for dst"); int srcAxis = srcCoor.length-3; int dstAxis = dstCoor.length-3; for (int i = 0; i < 3; i++) { srcCoor[srcAxis+i] = 0; } dstCoor[dstAxis] = 0; int numChannels = src.length(-3); int height = src.length(-2); int width = src.length(-1); int heightDst = dst.length(-2); int widthDst = dst.length(-1); if( numChannels != dst.length(dstAxis)) { throw new IllegalArgumentException("Number of channels do not match in src and dst"); } if( height > heightDst ) { throw new IllegalArgumentException("src height is larger than dst"); } if( width > widthDst ) { throw new IllegalArgumentException("src width is larger than dst"); } int pixelSrc = src.idx(srcCoor); int pixelDst = dst.idx(dstCoor); if( width == widthDst && height == heightDst ) System.arraycopy(src.d,pixelSrc,dst.d,pixelDst,numChannels*width*height); else { int channelSrc = pixelSrc; int channelDst = pixelDst; for (int channel = 0; channel < numChannels; channel++) { pixelSrc = channelSrc; pixelDst = channelDst; for (int row = 0; row < height; row++) { System.arraycopy(src.d,pixelSrc,dst.d,pixelDst,width); pixelSrc += width; pixelDst += widthDst; } channelSrc += width*height; channelDst += widthDst*heightDst; } } }  static void elementMult(Tensor_F32 tensor , float value  ); static void elementMult(Tensor_F32 input , float value , Tensor_F32 output ); static void elementMult(Tensor_F32 A , Tensor_F32 B , Tensor_F32 output ); static void elementAdd(Tensor_F32 A , Tensor_F32 B , Tensor_F32 output ); static float elementSum( Tensor_F32 tensor ); static void insertSubChannel( Tensor_F32 src , int srcStartIndex , int srcStride ,
										 Tensor_F32 dst , int dstStartIndex , int dstStride ,
										 int rows , int columns ); static void insertSpatial(Tensor_F32 src , int []srcCoor,
									 Tensor_F32 dst , int []dstCoor ); static void fillSpatialBorder(Tensor_F32 tensor , int[] coor ,
										 int borderY0 , int borderX0 , int borderY1 , int borderX1 ,
										 float value ); static void printSpatial( Tensor_F32 tensor , int batch , int channel ); static void fill( Tensor_F32 tensor , float value );  }
@Test public void insertSpatial() { for( boolean subtensor : new boolean[]{false,true}) { int C = 3; int H = 10; int W = 15; Tensor_F32 src = TensorFactory_F32.random(rand, subtensor, C, H, W); Tensor_F32 dst = TensorFactory_F32.random(rand, subtensor, C, H, W); int[] coorSrc = new int[3]; int[] coorDst = new int[3]; TensorOps_F32.insertSpatial(src, coorSrc, dst, coorDst); checkSpatial(src, coorSrc, dst, coorDst); dst = TensorFactory_F32.random(rand, subtensor, 10, C, H, W); coorDst = new int[]{5, 0, 0, 0}; TensorOps_F32.insertSpatial(src, coorSrc, dst, coorDst); checkSpatial(src, coorSrc, dst, coorDst); src = TensorFactory_F32.random(rand, subtensor, 4, C, H, W); coorSrc = new int[]{1, 0, 0, 0}; TensorOps_F32.insertSpatial(src, coorSrc, dst, coorDst); checkSpatial(src, coorSrc, dst, coorDst); dst = TensorFactory_F32.random(rand, subtensor, 10, C, H+4, W+6); coorDst = new int[]{5, 0, 1, 2}; TensorOps_F32.insertSpatial(src, coorSrc, dst, coorDst); checkSpatial(src, coorSrc, dst, coorDst); src = TensorFactory_F32.random(rand, subtensor, C, W, H); dst = TensorFactory_F32.random(rand, subtensor, 10, C, W+6, H+4); coorSrc = new int[3]; coorDst = new int[]{5, 0, 1, 2}; TensorOps_F32.insertSpatial(src, coorSrc, dst, coorDst); checkSpatial(src, coorSrc, dst, coorDst); } }