Subtracts a value from all elements of an array in place. - Java java.lang

Java examples for java.lang:Math Array Function

Description

Subtracts a value from all elements of an array in place.

Demo Code

/**//  w w w  . j av a2 s  . c  om
 * Java Modular Image Synthesis Toolkit (JMIST)
 * Copyright (C) 2008-2013 Bradley W. Kimmel
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
//package com.java2s;

public class Main {
    /**
     * Subtracts a value from all elements of an array in place.
     * @param x The array to add to.
     * @param value The value to add to each element in <code>x</code>.
     * @return A reference to <code>x</code>.
     */
    public static double[] subtract(double[] x, double value) {
        for (int i = 0; i < x.length; i++) {
            x[i] -= value;
        }
        return x;
    }

    /**
     * Subtracts a value from all elements of an array in place.
     * @param x The array to add to.
     * @param value The value to add to each element in <code>x</code>.
     * @return A reference to <code>x</code>.
     */
    public static int[] subtract(int[] x, int value) {
        for (int i = 0; i < x.length; i++) {
            x[i] -= value;
        }
        return x;
    }

    /**
     * Subtracts a value from all elements of an array in place.
     * @param x The array to add to.
     * @param value The value to add to each element in <code>x</code>.
     * @return A reference to <code>x</code>.
     */
    public static long[] subtract(long[] x, long value) {
        for (int i = 0; i < x.length; i++) {
            x[i] -= value;
        }
        return x;
    }

    /**
     * Subtracts the elements of one array of <code>double</code>s from
     * another. The lengths of <code>accumulator</code> and <code>values</code>
     * must be equal.
     * @param accumulator The array of <code>double</code>s to subtract the
     *     elements of <code>values</code> from (the elements of this array
     *     will be modified).  This may be <code>null</code>, in which case a
     *     new array will be created and initialized to zeros before
     *     subtracting the values from <code>values</code>.
     * @param values The array of <code>double</code>s to subtract from each
     *     corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static double[] subtract(double[] accumulator, double[] values) {
        if (accumulator == null) {
            accumulator = new double[values.length];
        }
        assert (accumulator.length == values.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] -= values[i];
        }
        return accumulator;
    }

    /**
     * Subtracts the elements of one array of <code>int</code>s from
     * another. The lengths of <code>accumulator</code> and <code>values</code>
     * must be equal.
     * @param accumulator The array of <code>int</code>s to subtract the
     *     elements of <code>values</code> from (the elements of this array
     *     will be modified).  This may be <code>null</code>, in which case a
     *     new array will be created and initialized to zeros before
     *     subtracting the values from <code>values</code>.
     * @param values The array of <code>int</code>s to subtract from each
     *     corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static int[] subtract(int[] accumulator, int[] values) {
        if (accumulator == null) {
            accumulator = new int[values.length];
        }
        assert (accumulator.length == values.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] -= values[i];
        }
        return accumulator;
    }

    /**
     * Subtracts the elements of one array of <code>long</code>s from
     * another. The lengths of <code>accumulator</code> and <code>values</code>
     * must be equal.
     * @param accumulator The array of <code>long</code>s to subtract the
     *     elements of <code>values</code> from (the elements of this array
     *     will be modified).  This may be <code>null</code>, in which case a
     *     new array will be created and initialized to zeros before
     *     subtracting the values from <code>values</code>.
     * @param values The array of <code>long</code>s to subtract from each
     *     corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static long[] subtract(long[] accumulator, long[] values) {
        if (accumulator == null) {
            accumulator = new long[values.length];
        }
        assert (accumulator.length == values.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] -= values[i];
        }
        return accumulator;
    }
}

Related Tutorials