Multiplies all the elements in an array of doubles by the elements of an equally sized array of doubles. - Java java.lang

Java examples for java.lang:Math Array Function

Description

Multiplies all the elements in an array of doubles by the elements of an equally sized array of doubles.

Demo Code

/**/*from  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 {
    /**
     * Multiplies all the elements in an array of <code>double</code>s by the
     * elements of an equally sized array of <code>double</code>s.  The lengths
     * of <code>accumulator</code> and <code>modulator</code> must be equal.
     * @param accumulator The array of <code>double</code>s that is to have its
     *     elements scaled (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 ones before multiplying by the values of
     *     <code>modulator</code>.
     * @param modulator The array of <code>double</code>s by which to multiply
     *     each corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static double[] multiply(double[] accumulator, double[] modulator) {
        if (accumulator == null) {
            return modulator.clone();
        }
        assert (accumulator.length == modulator.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] *= modulator[i];
        }
        return accumulator;
    }

    /**
     * Multiplies all the elements in an array of <code>int</code>s by the
     * elements of an equally sized array of <code>int</code>s.  The lengths
     * of <code>accumulator</code> and <code>modulator</code> must be equal.
     * @param accumulator The array of <code>int</code>s that is to have its
     *     elements scaled (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 ones before multiplying by the values of
     *     <code>modulator</code>.
     * @param modulator The array of <code>int</code>s by which to multiply
     *     each corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static int[] multiply(int[] accumulator, int[] modulator) {
        if (accumulator == null) {
            return modulator.clone();
        }
        assert (accumulator.length == modulator.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] *= modulator[i];
        }
        return accumulator;
    }

    /**
     * Multiplies all the elements in an array of <code>long</code>s by the
     * elements of an equally sized array of <code>long</code>s.  The lengths
     * of <code>accumulator</code> and <code>modulator</code> must be equal.
     * @param accumulator The array of <code>long</code>s that is to have its
     *     elements scaled (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 ones before multiplying by the values of
     *     <code>modulator</code>.
     * @param modulator The array of <code>long</code>s by which to multiply
     *     each corresponding element of <code>accumulator</code>.
     * @return A reference to <code>accumulator</code>.
     */
    public static long[] multiply(long[] accumulator, long[] modulator) {
        if (accumulator == null) {
            return modulator.clone();
        }
        assert (accumulator.length == modulator.length);
        for (int i = 0; i < accumulator.length; i++) {
            accumulator[i] *= modulator[i];
        }
        return accumulator;
    }
}

Related Tutorials