Class VectorFactory
- java.lang.Object
-
- dev.nm.algebra.linear.vector.doubles.operation.VectorFactory
-
public class VectorFactory extends Object
These are the utility functions that create new instances of vectors from existing ones.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static SparseVectorconcat(SparseVector... vectors)Concatenates an array of sparse vectors into one sparse vector.static Vectorconcat(Vector... vectors)Concatenates an array of vectors into one vector.static Vectorconcat(Collection<Vector> vectors)Concatenates an array of vectors into one vector.static Vector[]cumsum(Vector[] arr)Gets the cumulative sums.static Vectordiagonal(Matrix A)Gets the diagonal of a matrix as a vector.static SparseVectordiagonal(SparseMatrix A)Gets the diagonal of a sparse matrix as a sparse vector.static SparseVectorforeach(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.static Vectorforeach(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.static Vectorforeach(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.static VectorforeachColumn(Matrix matrix, RealScalarFunction f)Constructs a vector in which each entry is the result of applying aRealScalarFunctionto each column of an input matrix.static VectorforeachRow(Matrix matrix, RealScalarFunction f)Constructs a vector in which each entry is the result of applying aRealScalarFunctionto each row of an input matrix.static VectorforeachVector(Vector[] vectors, RealScalarFunction f)Applies aRealScalarFunctionon each input vector.static Vector[]foreachVector(Vector[] vectors, RealVectorFunction f)Applies a real vector function on each input vector.static VectorforeachVector(Collection<Vector> vectors, RealScalarFunction f)Applies aRealScalarFunctionon each input vector.static Vector[]foreachVector(Collection<Vector> vectors, RealVectorFunction f)Applies a real vector function on each input vector.static Vector[]get0s(int dimension, int n)Getsn0 vectors.static VectorgetCoordinate(Vector[] vectors, int i)Gets the vector entries from a particular coordinate.static VectorgetCoordinate(Collection<Vector> vectors, int i)Gets the vector entries from a particular coordinate.static List<Vector>getOffsetVectors(Vector v0, Vector dv, int a, int b)Given the reference vectorv0, the deltadv, and the range[a, b], the offset vectors are: v0 + a * dv, v0 + (a + 1) * dv, ..., v0 + b * dv.static VectorreplaceInPlace(Vector original, int from, Vector replacement)Replaces a sub-vector with a given smaller vector.static VectorsubDiagonal(Matrix A)Gets the sub-diagonal of a matrix as a vector.static SparseVectorsubDiagonal(SparseMatrix A)Gets the sub-diagonal of a sparse matrix as a sparse vector.static SparseVectorsubVector(SparseVector vector, int from, int to)Gets a sub-vector from a sparse vector.static VectorsubVector(Vector vector, int[] indices)Gets a sub-vector from a vector according to a given array of ordered indices (repetition allowed).static VectorsubVector(Vector vector, int from, int to)Gets a sub-vector from a vector.static VectorsubVector(Vector vector, List<Integer> indices)Gets a sub-vector from a vector according to a given array of ordered indices (repetition allowed).static VectorsuperDiagonal(Matrix A)Gets the super-diagonal of a matrix as a vector.static SparseVectorsuperDiagonal(SparseMatrix A)Gets the super-diagonal of a sparse matrix as a sparse vector.
-
-
-
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 vectorfrom- the beginning entry index to replacereplacement- 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 vectorvector- 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 vectorvector- 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 vectorvector- 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 vectorfrom- the beginning indexto- 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 vectorfrom- the beginning indexto- 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 vectorindices- 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 vectorindices- 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 aRealScalarFunctionto each row of an input matrix.- Parameters:
matrix- the input matrixf- 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 aRealScalarFunctionto each column of an input matrix.- Parameters:
matrix- the input matrixf- the real scalar function- Returns:
- the vector containing the results
-
foreachVector
public static Vector foreachVector(Vector[] vectors, RealScalarFunction f)
Applies aRealScalarFunctionon each input vector.- Parameters:
vectors- the input vectorsf- the real scalar function- Returns:
- the values after applying
fon the vectors
-
foreachVector
public static Vector foreachVector(Collection<Vector> vectors, RealScalarFunction f)
Applies aRealScalarFunctionon each input vector.- Parameters:
vectors- the input vectorsf- the real scalar function- Returns:
- the values after applying
fon the vectors
-
foreachVector
public static Vector[] foreachVector(Vector[] vectors, RealVectorFunction f)
Applies a real vector function on each input vector.- Parameters:
vectors- the input vectorsf- 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 vectorsf- 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 vectorsi- 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 vectorsi- 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 vectorv0, the deltadv, and the range[a, b], the offset vectors are:v0 + a * dv, v0 + (a + 1) * dv, ..., v0 + b * dv.
- Parameters:
v0- the reference vectorv0dv- the change (base offset) made to the vector at each stepdva- the minimum multiple ofdvthat is added tov0b- the maximum multiple ofdvthat is added tov0- 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)
Getsn0 vectors.- Parameters:
dimension- the dimension of the vectorsn- the number of 0 vectors- Returns:
n0 vectors
-
-