Java List Subtract subtract(List vector, int[] sparseVector)

Here you can find the source of subtract(List vector, int[] sparseVector)

Description

subtract

License

Open Source License

Parameter

Parameter Description
vector a parameter
sparseVector a parameter

Declaration

public static List<Integer> subtract(List<Integer> vector, int[] sparseVector) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**//from   w  w  w. j  a va2 s  . co m
     * The first given vector subtract the second one.
     * 
     * @param vector1
     * @param vector2
     * @return
     * @throws IllegalArgumentException
     */
    public static List<Integer> subtract(List<Integer> vector1, List<Integer> vector2) {

        if (vector1.size() != vector2.size()) {
            throw new IllegalArgumentException("The vectors have different dimentions.");
        }

        List<Integer> result = new ArrayList<Integer>();

        Iterator<Integer> iter1 = vector1.iterator();
        Iterator<Integer> iter2 = vector2.iterator();
        while (iter1.hasNext()) {

            result.add(iter1.next() - iter2.next());
        }

        return result;
    }

    /**
     * 
     * @param vector
     * @param sparseVector
     * @return
     */
    public static List<Integer> subtract(List<Integer> vector, int[] sparseVector) {

        List<Integer> result = new ArrayList<Integer>(vector);
        for (int offset : sparseVector) {
            if (offset == -1) {
                continue;
            }
            result.set(offset, result.get(offset) - 1);

        }

        return result;
    }

    /**
     * The addition of two vectors.
     * 
     * @param vector1
     * @param vector2
     * @return
     * @throws IllegalArgumentException
     */
    public static List<Integer> add(List<Integer> vector1, List<Integer> vector2) {

        if (vector1.size() != vector2.size()) {
            throw new IllegalArgumentException("The vectors have different dimentions.");
        }

        List<Integer> result = new ArrayList<Integer>();

        Iterator<Integer> iter1 = vector1.iterator();
        Iterator<Integer> iter2 = vector2.iterator();
        while (iter1.hasNext()) {

            result.add(iter1.next() + iter2.next());
        }

        return result;
    }

    /**
     * The addition of two vectors, one of which is a sparse vector with value
     * 1.
     * 
     * @param vector
     * @param sparseVector
     * @return
     */
    public static List<Integer> add(List<Integer> vector, int[] sparseVector) {

        List<Integer> result = new ArrayList<Integer>(vector);
        for (int offset : sparseVector) {
            if (offset == -1) {
                continue;
            }
            result.set(offset, result.get(offset) + 1);

        }

        return result;
    }
}

Related

  1. subtract(final List list1, final List list2)
  2. subtract(final List list1, final List list2)
  3. subtract(final List result, final Object o1, final Object o2)
  4. subtract(final String[] input, final String[] list)
  5. subtract(List a, List b)
  6. subtract(List a, List b)
  7. subtract(List aList, List bList)
  8. subtract(String[] list1, String[] list2)
  9. subtractAsList(T[] array1, T[] array2)