MathArrays { public static double[] convolve(double[] x, double[] h) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(x); MathUtils.checkNotNull(h); final int xLen = x.length; final int hLen = h.length; if (xLen == 0 || hLen == 0) { throw new NoDataException(); } final int totalLength = xLen + hLen - 1; final double[] y = new double[totalLength]; for (int n = 0; n < totalLength; n++) { double yn = 0; int k = FastMath.max(0, n + 1 - xLen); int j = n - k; while (k < hLen && j >= 0) { yn += x[j--] * h[k++]; } y[n] = yn; } return y; } private  MathArrays(); static double[] scale(double val, final double[] arr); static void scaleInPlace(double val, final double[] arr); static double[] ebeAdd(double[] a, double[] b); static double[] ebeSubtract(double[] a, double[] b); static double[] ebeMultiply(double[] a, double[] b); static double[] ebeDivide(double[] a, double[] b); static double distance1(double[] p1, double[] p2); static int distance1(int[] p1, int[] p2); static double distance(double[] p1, double[] p2); static double distance(int[] p1, int[] p2); static double distanceInf(double[] p1, double[] p2); static int distanceInf(int[] p1, int[] p2); static boolean isMonotonic(T[] val,
                                      OrderDirection dir,
                                      boolean strict); static boolean isMonotonic(double[] val, OrderDirection dir, boolean strict); static boolean checkOrder(double[] val, OrderDirection dir,
                                     boolean strict, boolean abort); static void checkOrder(double[] val, OrderDirection dir,
                                  boolean strict); static void checkOrder(double[] val); static void checkRectangular(final long[][] in); static void checkPositive(final double[] in); static void checkNotNaN(final double[] in); static void checkNonNegative(final long[] in); static void checkNonNegative(final long[][] in); static double safeNorm(double[] v); static void sortInPlace(double[] x, double[] ... yList); static void sortInPlace(double[] x,
                                   final OrderDirection dir,
                                   double[] ... yList); static int[] copyOf(int[] source); static double[] copyOf(double[] source); static int[] copyOf(int[] source, int len); static double[] copyOf(double[] source, int len); static double[] copyOfRange(double[] source, int from, int to); static double linearCombination(final double[] a, final double[] b); static double linearCombination(final double a1, final double b1,
                                           final double a2, final double b2); static double linearCombination(final double a1, final double b1,
                                           final double a2, final double b2,
                                           final double a3, final double b3); static double linearCombination(final double a1, final double b1,
                                           final double a2, final double b2,
                                           final double a3, final double b3,
                                           final double a4, final double b4); static boolean equals(float[] x, float[] y); static boolean equalsIncludingNaN(float[] x, float[] y); static boolean equals(double[] x, double[] y); static boolean equalsIncludingNaN(double[] x, double[] y); static double[] normalizeArray(double[] values, double normalizedSum); static T[] buildArray(final Field<T> field, final int length); @SuppressWarnings("unchecked") static T[][] buildArray(final Field<T> field, final int rows, final int columns); static double[] convolve(double[] x, double[] h); static void shuffle(int[] list,
                               int start,
                               Position pos); static void shuffle(int[] list,
                               int start,
                               Position pos,
                               RandomGenerator rng); static void shuffle(int[] list,
                               RandomGenerator rng); static void shuffle(int[] list); static int[] natural(int n); static int[] sequence(int size,
                                 int start,
                                 int stride); static boolean verifyValues(final double[] values, final int begin, final int length); static boolean verifyValues(final double[] values, final int begin,
            final int length, final boolean allowEmpty); static boolean verifyValues(
        final double[] values,
        final double[] weights,
        final int begin,
        final int length); static boolean verifyValues(final double[] values, final double[] weights,
            final int begin, final int length, final boolean allowEmpty);  }
@Test public void testConvolve() { double[] x1 = { 1.2, -1.8, 1.4 }; double[] h1 = { 1, 0.8, 0.5, 0.3 }; double[] y1 = { 1.2, -0.84, 0.56, 0.58, 0.16, 0.42 }; double tolerance = 1e-13; double[] yActual = MathArrays.convolve(x1, h1); Assert.assertArrayEquals(y1, yActual, tolerance); double[] x2 = { 1, 2, 3 }; double[] h2 = { 0, 1, 0.5 }; double[] y2 = { 0, 1, 2.5, 4, 1.5 }; yActual = MathArrays.convolve(x2, h2); Assert.assertArrayEquals(y2, yActual, tolerance); try { MathArrays.convolve(new double[]{1, 2}, null); Assert.fail("an exception should have been thrown"); } catch (NullArgumentException e) { } try { MathArrays.convolve(null, new double[]{1, 2}); Assert.fail("an exception should have been thrown"); } catch (NullArgumentException e) { } try { MathArrays.convolve(new double[]{1, 2}, new double[]{}); Assert.fail("an exception should have been thrown"); } catch (NoDataException e) { } try { MathArrays.convolve(new double[]{}, new double[]{1, 2}); Assert.fail("an exception should have been thrown"); } catch (NoDataException e) { } try { MathArrays.convolve(new double[]{}, new double[]{}); Assert.fail("an exception should have been thrown"); } catch (NoDataException e) { } }