Java List Compare compareVectors(List vectorA, List vectorB)

Here you can find the source of compareVectors(List vectorA, List vectorB)

Description

compare Vectors

License

Open Source License

Declaration

public static void compareVectors(List<Double> vectorA, List<Double> vectorB) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .j  a  va  2s  .  co  m*/
 * Copyright (C) 2013 Universitat Pompeu Fabra
 *
 * 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/>.
 */

import java.util.List;

public class Main {
    public static void compareVectors(List<Double> vectorA, List<Double> vectorB) {
        final int rowsA = vectorA.size();
        final int rowsB = vectorB.size();

        if (rowsA != rowsB) {
            throw new RuntimeException(
                    String.format("vector A dimension (%d) differs from dimension of vector B (%d)", rowsA, rowsB));
        }
        for (int y = 0; y < vectorA.size(); y++) {
            double valA = vectorA.get(y);
            double valB = vectorB.get(y);
            if (!compareValues(valA, valB)) {
                throw new RuntimeException(
                        String.format("vector A differs from vector B at (%d): %f, %f", y, valA, valB));
            }
        }
    }

    /**
     * Returns true if the supplied values are (quite) equal.
     */
    public static boolean compareValues(double valA, double valB) {

        final double diff = Math.abs(valA - valB);
        final double relativeDiff = Math.abs((valA - valB) / (valA + valB));
        return (diff < 0.00000000001) || Double.isNaN(relativeDiff) || (relativeDiff < 0.015); // TODO get rid of magic values, and possibly make them configurable
    }
}

Related

  1. compareStringLists(List elements1, List elements2)
  2. compareSubstring(List pieces, String s)
  3. compareTermList(final List tl0, final List tl1)
  4. compareTo(List list, int i, int j)
  5. compareTwoListOfStrings(List first, List second)
  6. equalsToAny(Object comparable, Object... compareList)
  7. parseAndCompare(List fileNames, int masterPartitionId)
  8. sortList(List sources, Comparator comparer)