Java Array Sum sum(double[] vec1, double[] vec2)

Here you can find the source of sum(double[] vec1, double[] vec2)

Description

Method that sums two vectores.

License

Open Source License

Parameter

Parameter Description
vec1 a parameter
vec2 a parameter

Declaration

public static double[] sum(double[] vec1, double[] vec2) 

Method Source Code

//package com.java2s;
/*//from  www . j ava 2 s .  c o  m
* The GPLv3 licence :
* -----------------
* Copyright (c) 2009 Ricardo Dias
*
* This file is part of MuVis.
*
* MuVis 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.
*
* MuVis 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 MuVis.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Method that sums two vectores.
     * Important: vectores should have the same size, otherwise, two situations
     * can occur:
     * 1st: vec1.size is lower than vec2.size and method will sum the vec1.size()
     *      with (vec2.size - vec1.size)
     * 2nd: vec1.size is higher than vec2.size and the method will not work
     * @param vec1
     * @param vec2
     * @return
     */
    public static double[] sum(double[] vec1, double[] vec2) {

        if (vec1.length != vec2.length) {
            return null;
        } else {

            double[] result = new double[vec1.length];
            for (int i = 0; i < vec1.length; i++) {
                result[i] = vec1[i] + vec2[i];
            }
            return result;
        }
    }
}

Related

  1. sum(double[] data)
  2. sum(double[] data, int i_, int n_)
  3. sum(double[] dbs)
  4. sum(double[] output, double[] a, double[] b)
  5. sum(double[] t)
  6. sum(double[] vector)
  7. sum(double[] x)
  8. sum(double[] x, int length)
  9. sum(double[] x, int start, int end)