Adds the elements of one array of doubles to another. - Java java.lang

Java examples for java.lang:Math Array Function

Description

Adds the elements of one array of doubles to another.

Demo Code

/**// w w  w.  j a v a2  s.c  o  m
 * 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 {
    /**
     * Adds the elements of one array of <code>double</code>s to another. The
     * lengths of <code>accumulator</code> and <code>summand</code> must be
     * equal.
     * @param accumulator The array of <code>double</code>s to add the
     *     elements of <code>summand</code> to (the elements of this array
     *     will be modified).  This may be <code>null</code>, in which case a
     *     new array will be created of the same length of
     *     <code>summand</code> and will be initialized to zeros before adding
     *     the values of <code>summand</code>.
     * @param summand The array of <code>double</code>s to add to each
     *     corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static double[] add(double[] accumulator, double[] summand) {
        if (accumulator == null) {
            return summand.clone();
        }
        assert (accumulator.length == summand.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] += summand[i];
        }
        return accumulator;
    }

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

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

    /**
     * Adds a value to 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[] add(double[] x, double value) {
        for (int i = 0; i < x.length; i++) {
            x[i] += value;
        }
        return x;
    }

    /**
     * Adds a value to 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[] add(int[] x, int value) {
        for (int i = 0; i < x.length; i++) {
            x[i] += value;
        }
        return x;
    }

    /**
     * Adds a value to 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[] add(long[] x, long value) {
        for (int i = 0; i < x.length; i++) {
            x[i] += value;
        }
        return x;
    }
}

Related Tutorials