Java Array Sum sum(double[] a, double[] b)

Here you can find the source of sum(double[] a, double[] b)

Description

sum of two vectors

License

Apache License

Parameter

Parameter Description
a vector
b vector

Return

new vector = a + b

Declaration

public static double[] sum(double[] a, double[] b) 

Method Source Code

//package com.java2s;
/*//from   ww w . j a v a 2s .c o m
* Copyright 2014 Vasya Drobushkov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Main {
    /**
     * sum of two vectors
     * 
     * @param a
     *            vector
     * @param b
     *            vector
     * @return new vector = a + b
     */
    public static double[] sum(double[] a, double[] b) {
        double[] result = new double[a.length];

        for (int i = 0; i < a.length; i++) {
            result[i] = a[i] + b[i];
        }

        return result;
    }

    /**
     * sum of two matrices
     * 
     * @param a
     *            matrix
     * @param b
     *            matrix
     * @return new matrix = a + b
     */
    public static double[][] sum(double[][] a, double[][] b) {
        double[][] result = new double[a.length][a[0].length];

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                result[i][j] = a[i][j] + b[i][j];
            }
        }

        return result;
    }
}

Related

  1. sum(double... values)
  2. sum(double... values)
  3. sum(double[] a)
  4. sum(double[] a)
  5. sum(double[] a, double[] b)
  6. sum(double[] aArray)
  7. sum(double[] addends)
  8. sum(double[] array)
  9. sum(double[] array)