Java List Product scalarProduct(List a, List b)

Here you can find the source of scalarProduct(List a, List b)

Description

Scalar product of two vectors Be careful: Null entries are treated as 0!

License

Open Source License

Parameter

Parameter Description
a First vector to multiply
b Second vector to multiply

Return

Scalar product

Declaration

public static float scalarProduct(List<Float> a, List<Float> b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of Tmetrics.//  w w  w  .ja  v  a2  s.c  om
 *
 * Tmetrics 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.
 *
 * Tmetrics 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 Tmetrics. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Scalar product of two vectors
     * 
     * Be careful: Null entries are treated as 0! If one vector is longer than
     * the other, higher positions are be ignored
     * 
     * @param a
     *            First vector to multiply
     * @param b
     *            Second vector to multiply
     * @return Scalar product
     */
    public static float scalarProduct(List<Float> a, List<Float> b) {
        float sum = 0;
        for (int i = 0; i < a.size() && i < b.size(); i++) {
            if (a.get(i) != null && b.get(i) != null) {
                sum += a.get(i) * b.get(i);
            }

        }
        return sum;
    }
}

Related

  1. dotProduct(List v1, List v2)
  2. dotProductValue(List oper1, List oper2)
  3. getProductsFromString(List projects)
  4. product(List> lists)
  5. scalerProduct(final List doubleList, final double scalerFactor)