public static FloatMatrix conv2d(FloatMatrix input, FloatMatrix kernel, Type type) { FloatMatrix xShape = new FloatMatrix(1, 2); xShape.put(0, input.rows); xShape.put(1, input.columns); FloatMatrix yShape = new FloatMatrix(1, 2); yShape.put(0, kernel.rows); yShape.put(1, kernel.columns); FloatMatrix zShape = xShape.add(yShape).sub(1); int retRows = (int) zShape.get(0); int retCols = (int) zShape.get(1); ComplexFloatMatrix fftInput = complexDisceteFourierTransform(input, retRows, retCols); ComplexFloatMatrix fftKernel = complexDisceteFourierTransform(kernel, retRows, retCols); ComplexFloatMatrix mul = fftKernel.mul(fftInput); ComplexFloatMatrix retComplex = complexInverseDisceteFourierTransform(mul); FloatMatrix ret = retComplex.getReal(); if (type == Type.VALID) { FloatMatrix validShape = xShape.subi(yShape).add(1); FloatMatrix start = zShape.sub(validShape).div(2); FloatMatrix end = start.add(validShape); if (start.get(0) < 1 || start.get(1) < 1) throw new IllegalStateException("Illegal row index " + start); if (end.get(0) < 1 || end.get(1) < 1) throw new IllegalStateException("Illegal column index " + end); ret = ret.get( RangeUtils.interval((int) start.get(0), (int) end.get(0)), RangeUtils.interval((int) start.get(1), (int) end.get(1))); } return ret; }