public static IComplexNDArray exp(IComplexNDArray toExp) { return expi(toExp.dup()); }
private IComplexNDArray fixShape(IComplexNDArray x, int[] shape, int axis, int n) { if (shape[axis] > n) { int[] newShape = ArrayUtil.copy(shape); newShape[axis] = n; x = ComplexNDArrayUtil.truncate(x, n, axis); } else { int[] newShape = ArrayUtil.copy(shape); newShape[axis] = n; x = ComplexNDArrayUtil.padWithZeros(x, newShape); return x; } return x; }
fftedInput = ComplexNDArrayUtil.padWithZeros(fftedInput, fftedKernel.shape()); else fftedKernel = ComplexNDArrayUtil.padWithZeros(fftedKernel, fftedInput.shape()); return convolution.getReal(); case SAME: return ComplexNDArrayUtil.center(convolution, input.shape()).getReal(); case VALID: int[] shape2 = NDArrayUtil.toInts(Transforms.abs(NDArrayUtil.toNDArray(input.shape()) .sub(NDArrayUtil.toNDArray(kernel.shape())).addi(1))); return ComplexNDArrayUtil.center(convolution, shape2).getReal();
@Override public void exec() { if (!x.isVector()) return; if (executed) return; executed = true; //ifft(x) = conj(fft(conj(x)) / length(x) IComplexNDArray ndArray = x instanceof IComplexNDArray ? (IComplexNDArray) x : Nd4j.createComplex(x); IComplexNDArray fft = (IComplexNDArray) Nd4j.getExecutioner() .execAndReturn(new VectorFFT(ndArray.conj(), y, z, x.lengthLong(), fftLength)); IComplexNDArray ret = fft.conj().divi(Nd4j.complexScalar(fftLength)); //completely pass through this.z = originalN > 0 ? ComplexNDArrayUtil.truncate(ret, originalN, 0) : ret; this.x = this.z; } }
protected IComplexNDArray doFFt(IComplexNDArray transform, int[] shape, int[] axes, boolean inverse) { IComplexNDArray result = transform.dup(); if (shape == null) shape = ArrayUtil.copy(result.shape()); boolean noAxes = false; if (axes == null || axes.length < 1) { noAxes = true; axes = ArrayUtil.range(0, shape.length); axes = ArrayUtil.reverseCopy(axes); } if (noAxes) { for (int i : axes) { if (i < 0) i = shape.length + i; transform = fixShape(transform, shape, i, shape[i]); } } if (ArrayUtil.prod(shape) > ArrayUtil.prod(result.shape())) result = ComplexNDArrayUtil.padWithZeros(result, shape); return doInnerFft(result, shape, axes, inverse); }
/** * ND Convolution * * @param input the input to op * @param kernel the kernel to op with * @param type the type of convolution * @param axes the axes to do the convolution along * @return the convolution of the given input and kernel */ @Override public IComplexNDArray convn(IComplexNDArray input, IComplexNDArray kernel, Convolution.Type type, int[] axes) { if (kernel.isScalar() && input.isScalar()) return kernel.mul(input); INDArray shape = NDArrayUtil.toNDArray(Shape.sizeForAxes(axes, input.shape())) .add(NDArrayUtil.toNDArray(Shape.sizeForAxes(axes, kernel.shape()))).subi(1); int[] intShape = NDArrayUtil.toInts(shape); IComplexNDArray ret = FFT.rawifftn(FFT.rawfftn(input, intShape, axes).muli(FFT.rawfftn(kernel, intShape, axes)), intShape, axes); switch (type) { case FULL: return ret; case SAME: return ComplexNDArrayUtil.center(ret, input.shape()); case VALID: return ComplexNDArrayUtil.center(ret, NDArrayUtil.toInts(Transforms.abs(NDArrayUtil .toNDArray(input.shape()).sub(NDArrayUtil.toNDArray(kernel.shape())).addi(1)))); } return ret; }
/** * ND IFFT * * @param transform the ndarray to op * @param dimension the dimension to iterate along * @param numElements the desired number of elements in each fft * @return the transformed array */ @Override public IComplexNDArray ifftn(IComplexNDArray transform, int dimension, int numElements) { if (numElements < 1) throw new IllegalArgumentException("No elements specified"); int[] finalShape = ArrayUtil.replace(transform.shape(), dimension, numElements); int[] axes = ArrayUtil.range(0, finalShape.length); IComplexNDArray result = transform.dup(); int desiredElementsAlongDimension = result.size(dimension); if (numElements > desiredElementsAlongDimension) { result = ComplexNDArrayUtil.padWithZeros(result, finalShape); } else if (numElements < desiredElementsAlongDimension) result = ComplexNDArrayUtil.truncate(result, numElements, dimension); return rawifftn(result, finalShape, axes); }
/** * Performs FFT along the first non singleton dimension of * op. This means * * @param transform the ndarray to op * @param dimension the dimension to iterate along * @param numElements the desired number of elements in each fft * along each dimension from each slice (note: each slice) * @return the transformed array */ @Override public IComplexNDArray fftn(IComplexNDArray transform, int dimension, int numElements) { if (numElements < 1) throw new IllegalArgumentException("No elements specified"); int[] finalShape = ArrayUtil.replace(transform.shape(), dimension, numElements); int[] axes = ArrayUtil.range(0, finalShape.length); IComplexNDArray result = transform.dup(); int desiredElementsAlongDimension = result.size(dimension); if (numElements > desiredElementsAlongDimension) result = ComplexNDArrayUtil.padWithZeros(result, finalShape); else if (numElements < desiredElementsAlongDimension) result = ComplexNDArrayUtil.truncate(result, numElements, dimension); return rawfftn(result, finalShape, axes); }
public static IComplexNDArray exp(IComplexNDArray toExp) { return expi(toExp.dup()); }
protected IComplexNDArray preProcess(IComplexNDArray result, IComplexNDArray transform, int n, int dimension) { if (dimension < 0) dimension = transform.shape().length - 1 - dimension; if (transform.size(dimension) != n) { int[] shape = ArrayUtil.copy(result.shape()); shape[dimension] = n; if (transform.size(dimension) > n) { result = ComplexNDArrayUtil.truncate(result, n, dimension); } else result = ComplexNDArrayUtil.padWithZeros(result, shape); } if (dimension != result.shape().length - 1) result = result.swapAxes(result.shape().length - 1, dimension); return result; }
/** * FFT along a particular dimension * * @param transform the ndarray to op * @param numElements the desired number of elements in each fft * @return the ffted output */ @Override public IComplexNDArray fft(INDArray transform, int numElements, int dimension) { IComplexNDArray inputC = Nd4j.createComplex(transform); if (inputC.isVector()) return (IComplexNDArray) Nd4j.getExecutioner().execAndReturn(getFftOp(inputC, numElements)); else { int[] finalShape = ArrayUtil.replace(transform.shape(), dimension, numElements); IComplexNDArray transform2 = Nd4j.createComplex(transform); IComplexNDArray result = transform2.dup(); int desiredElementsAlongDimension = result.size(dimension); if (numElements > desiredElementsAlongDimension) { result = ComplexNDArrayUtil.padWithZeros(result, finalShape); } else if (numElements < desiredElementsAlongDimension) result = ComplexNDArrayUtil.truncate(result, numElements, dimension); return rawfft(result, numElements, dimension); } }
ret = ComplexNDArrayUtil.padWithZeros(ret, new int[] {fftLength}); } else if (len < desiredElementsAlongDimension) { ret = ComplexNDArrayUtil.truncate(ret, fftLength, 0); IComplexNDArray matrix = reshaped.mmul(M); if (originalN > 0) matrix = ComplexNDArrayUtil.truncate(matrix, originalN, 0);