Java examples for java.lang:Math Array Function
Subtracts second array from the first
/*/*from w ww .jav a2 s .c om*/ * 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/>. */ //package com.java2s; 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; } }