Class VectorFactory


  • public class VectorFactory
    extends Object
    These are the utility functions that create new instances of vectors from existing ones.
    • Method Detail

      • concat

        public static Vector concat​(Vector... vectors)
        Concatenates an array of vectors into one vector.
        Parameters:
        vectors - an array of vectors, e.g., \((v_1, v_2, v_3, ...)\).
        Returns:
        \(c(v_1, v_2, v_3, ...)\)
      • concat

        public static Vector concat​(Collection<Vector> vectors)
        Concatenates an array of vectors into one vector.
        Parameters:
        vectors - an array of vectors, e.g., \((v_1, v_2, v_3, ...)\).
        Returns:
        \(c(v_1, v_2, v_3, ...)\)
      • concat

        public static SparseVector concat​(SparseVector... vectors)
        Concatenates an array of sparse vectors into one sparse vector.
        Parameters:
        vectors - an array of sparse vectors, e.g., \((v_1, v_2, v_3, ...)\).
        Returns:
        \(c(v_1, v_2, v_3, ...)\)
      • replaceInPlace

        public static Vector replaceInPlace​(Vector original,
                                            int from,
                                            Vector replacement)
        Replaces a sub-vector with a given smaller vector.
        Parameters:
        original - the original vector
        from - the beginning entry index to replace
        replacement - the vector to be inserted to the original vector
        Returns:
        the modified vector
      • foreach

        public static Vector foreach​(Vector vector,
                                     DoubleUnaryOperator f)
        Constructs a new vector in which each entry is the result of applying a function to the corresponding entry of a vector. For example,
        
         Vector v1 = foreach(v0, x -> 2 * x); // double each entry in v0
         
        Parameters:
        f - the function to be applied to each entry of a vector
        vector - a vector
        Returns:
        [f(vi)] for all is
      • foreach

        public static Vector foreach​(Vector vector,
                                     UnivariateRealFunction f)
        Constructs a new vector in which each entry is the result of applying a function to the corresponding entry of a vector.
        Parameters:
        f - the function to be applied to each entry of a vector
        vector - a vector
        Returns:
        [f(vi)] for all is
      • foreach

        public static SparseVector foreach​(SparseVector vector,
                                           UnivariateRealFunction f)
        Constructs a new vector in which each entry is the result of applying a function to the corresponding entry of a sparse vector. Note that this operation may not preserve sparsity.
        Parameters:
        f - the function to be applied to each entry of a vector
        vector - a sparse vector
        Returns:
        [f(vi)] for all is
      • subVector

        public static Vector subVector​(Vector vector,
                                       int from,
                                       int to)
        Gets a sub-vector from a vector.
        Parameters:
        vector - a vector
        from - the beginning index
        to - the ending index
        Returns:
        v[from : to]
      • subVector

        public static SparseVector subVector​(SparseVector vector,
                                             int from,
                                             int to)
        Gets a sub-vector from a sparse vector.
        Parameters:
        vector - a sparse vector
        from - the beginning index
        to - the ending index
        Returns:
        v[from : to]
      • subVector

        public static Vector subVector​(Vector vector,
                                       List<Integer> indices)
        Gets a sub-vector from a vector according to a given array of ordered indices (repetition allowed).
        Parameters:
        vector - a vector
        indices - the list of ordered indices (repetition allowed)
        Returns:
        a sub-vector
      • subVector

        public static Vector subVector​(Vector vector,
                                       int[] indices)
        Gets a sub-vector from a vector according to a given array of ordered indices (repetition allowed).
        Parameters:
        vector - a vector
        indices - the array of ordered indices (repetition allowed)
        Returns:
        a sub-vector
      • diagonal

        public static Vector diagonal​(Matrix A)
        Gets the diagonal of a matrix as a vector.
        Parameters:
        A - a matrix
        Returns:
        a vector whose entries are Ai
      • diagonal

        public static SparseVector diagonal​(SparseMatrix A)
        Gets the diagonal of a sparse matrix as a sparse vector.
        Parameters:
        A - a sparse matrix
        Returns:
        a sparse vector whose entries are Ai
      • superDiagonal

        public static Vector superDiagonal​(Matrix A)
        Gets the super-diagonal of a matrix as a vector.
        Parameters:
        A - a matrix
        Returns:
        a vector whose entries are Ai,i+1
      • superDiagonal

        public static SparseVector superDiagonal​(SparseMatrix A)
        Gets the super-diagonal of a sparse matrix as a sparse vector.
        Parameters:
        A - a sparse matrix
        Returns:
        a sparse vector whose entries are Ai,i+1
      • subDiagonal

        public static Vector subDiagonal​(Matrix A)
        Gets the sub-diagonal of a matrix as a vector.
        Parameters:
        A - a matrix
        Returns:
        a vector whose entries are Ai+1,i
      • subDiagonal

        public static SparseVector subDiagonal​(SparseMatrix A)
        Gets the sub-diagonal of a sparse matrix as a sparse vector.
        Parameters:
        A - a sparse matrix
        Returns:
        a sparse vector whose entries are Ai+1,i
      • foreachRow

        public static Vector foreachRow​(Matrix matrix,
                                        RealScalarFunction f)
        Constructs a vector in which each entry is the result of applying a RealScalarFunction to each row of an input matrix.
        Parameters:
        matrix - the input matrix
        f - the real scalar function
        Returns:
        the vector containing the results
      • foreachColumn

        public static Vector foreachColumn​(Matrix matrix,
                                           RealScalarFunction f)
        Constructs a vector in which each entry is the result of applying a RealScalarFunction to each column of an input matrix.
        Parameters:
        matrix - the input matrix
        f - the real scalar function
        Returns:
        the vector containing the results
      • foreachVector

        public static Vector foreachVector​(Vector[] vectors,
                                           RealScalarFunction f)
        Applies a RealScalarFunction on each input vector.
        Parameters:
        vectors - the input vectors
        f - the real scalar function
        Returns:
        the values after applying f on the vectors
      • foreachVector

        public static Vector[] foreachVector​(Vector[] vectors,
                                             RealVectorFunction f)
        Applies a real vector function on each input vector.
        Parameters:
        vectors - the input vectors
        f - the real vector function
        Returns:
        the output vectors after applying the function
      • foreachVector

        public static Vector[] foreachVector​(Collection<Vector> vectors,
                                             RealVectorFunction f)
        Applies a real vector function on each input vector.
        Parameters:
        vectors - the input vectors
        f - the real vector function
        Returns:
        the output vectors after applying the function
      • getCoordinate

        public static Vector getCoordinate​(Vector[] vectors,
                                           int i)
        Gets the vector entries from a particular coordinate. If all input vectors are sparse, then returns a sparse vector.
        Parameters:
        vectors - the input vectors
        i - the index to a vector entry; the coordinate index
        Returns:
        a vector composed of the entries from a particular coordinate
      • getCoordinate

        public static Vector getCoordinate​(Collection<Vector> vectors,
                                           int i)
        Gets the vector entries from a particular coordinate.
        Parameters:
        vectors - the input vectors
        i - the index to a vector entry; the coordinate index
        Returns:
        a vector composed of the entries from a particular coordinate
      • getOffsetVectors

        public static List<Vector> getOffsetVectors​(Vector v0,
                                                    Vector dv,
                                                    int a,
                                                    int b)
        Given the reference vector v0, the delta dv, and the range [a, b], the offset vectors are:
        v0 + a * dv, v0 + (a + 1) * dv, ..., v0 + b * dv.
        Parameters:
        v0 - the reference vector v0
        dv - the change (base offset) made to the vector at each step dv
        a - the minimum multiple of dv that is added to v0
        b - the maximum multiple of dv that is added to v0
        Returns:
        a list containing the vector
      • cumsum

        public static Vector[] cumsum​(Vector[] arr)
        Gets the cumulative sums.
        Parameters:
        arr - an array of vectors
        Returns:
        the cumulative sum, element-by-element
      • get0s

        public static Vector[] get0s​(int dimension,
                                     int n)
        Gets n 0 vectors.
        Parameters:
        dimension - the dimension of the vectors
        n - the number of 0 vectors
        Returns:
        n 0 vectors