/** Compute y <- alpha*op(a)*x + beta * y (general matrix vector multiplication) */ public static FloatMatrix gemv( float alpha, FloatMatrix a, FloatMatrix x, float beta, FloatMatrix y) { if (false) { NativeBlas.sgemv( 'N', a.rows, a.columns, alpha, a.data, 0, a.rows, x.data, 0, 1, beta, y.data, 0, 1); } else { if (beta == 0.0f) { for (int i = 0; i < y.length; i++) y.data[i] = 0.0f; for (int j = 0; j < a.columns; j++) { float xj = x.get(j); if (xj != 0.0f) { for (int i = 0; i < a.rows; i++) y.data[i] += a.get(i, j) * xj; } } } else { for (int j = 0; j < a.columns; j++) { float byj = beta * y.data[j]; float xj = x.get(j); for (int i = 0; i < a.rows; i++) y.data[j] = a.get(i, j) * xj + byj; } } } return y; }
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; }