Java Array Subtract subtract(double[] array, double value)

Here you can find the source of subtract(double[] array, double value)

Description

Subtracts a constant value from all items in an array

License

Open Source License

Parameter

Parameter Description
array a parameter
value a parameter

Return

array - constant value

Declaration

public static double[] subtract(double[] array, double value) throws Exception 

Method Source Code

//package com.java2s;
/*//  w  ww.ja  v a2  s  . com
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Subtracts second array from the first
     * 
     * @param first
     * @param second
     * @return first - second
     */
    public static double[] subtract(double[] first, double[] second) throws Exception {
        if (first.length != second.length) {
            throw new Exception("Lengths of arrays are not equal");
        }
        double[] returnValues = new double[first.length];
        for (int i = 0; i < returnValues.length; i++) {
            returnValues[i] = first[i] - second[i];
        }
        return returnValues;
    }

    /**
     * Subtracts a constant value from all items in an array
     * 
     * @param array
     * @param value
     * @return array - constant value
     */
    public static double[] subtract(double[] array, double value) throws Exception {
        double[] returnValues = new double[array.length];
        for (int i = 0; i < returnValues.length; i++) {
            returnValues[i] = array[i] - value;
        }
        return returnValues;
    }

    /**
     * Subtract one matrix from another
     * 
     * @param input1
     * @param input2
     * @return input1 - input2
     * @throws Exception
     */
    public static int[][] subtract(int[][] input1, int[][] input2) throws Exception {
        int rows = input1.length;
        int columns = input1[0].length;
        if (input2.length != rows) {
            throw new Exception("Row length of arrays are not equal");
        }
        if (input2[0].length != columns) {
            throw new Exception("Column length of arrays are not equal");
        }
        int[][] returnValues = new int[rows][columns];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                returnValues[r][c] = input1[r][c] - input2[r][c];
            }
        }
        return returnValues;
    }

    /**
     * Subtracts a constant value from all items in an array
     * 
     * @param array
     * @param value
     * @return array - constant value
     */
    public static int[] subtract(int[] array, int value) throws Exception {
        int[] returnValues = new int[array.length];
        for (int i = 0; i < returnValues.length; i++) {
            returnValues[i] = array[i] - value;
        }
        return returnValues;
    }
}

Related

  1. subtract(double[] a, double[] b)
  2. subtract(double[] a, double[] b)
  3. subtract(double[] a, double[] b)
  4. subtract(double[] accumulator, double[] values)
  5. subtract(double[] array, double a)
  6. subtract(double[] first, double[] second)
  7. subtract(double[] first, double[] second, double[] out)
  8. subtract(double[] flowFrom, double[] flowTo)
  9. subtract(double[] p1, double[] p2)