Here you can find the source of scalarProduct(List
Parameter | Description |
---|---|
a | First vector to multiply |
b | Second vector to multiply |
public static float scalarProduct(List<Float> a, List<Float> b)
//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; } }