Package org.la4j
Class Vector
- java.lang.Object
-
- org.la4j.Vector
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
- Direct Known Subclasses:
DenseVector,SparseVector
public abstract class Vector extends java.lang.Object implements java.lang.Iterable<java.lang.Double>A vector represents an array of elements. It can be re-sized.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringDEFAULT_DELIMITERprivate static java.text.NumberFormatDEFAULT_FORMATTERprotected intlengthLength of this vector.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Vectoradd(double value)Adds givenvalue(v) to this vector (X).Vectoradd(Vector that)Adds givenvector(X) to this vector (Y).abstract <T> Tapply(VectorMatrixOperation<T> operation, Matrix that)Pipes this vector to a givenoperation.abstract <T> Tapply(VectorOperation<T> operation)Pipes this vector to a givenoperation.abstract <T> Tapply(VectorVectorOperation<T> operation, Vector that)Pipes this vector to a givenoperation.Vectorblank()Creates a blank (an empty vector with same length) copy of this vector.abstract VectorblankOfLength(int length)Creates a blank (an empty vector) copy of this vector with the givenlength.static Vectorconstant(int length, double value)Vectorcopy()Copies this vector.abstract VectorcopyOfLength(int length)Copies this vector into the new vector with specifiedlength.Vectordivide(double value)Divides this vector (X) by givenvalue(v).voideach(VectorProcedure procedure)Applies givenprocedureto each element of this vector.protected voidensureLengthIsCorrect(int length)booleanequals(java.lang.Object o)Checks where this vector is equal to the given objecto.booleanequals(Vector that, double precision)Returns true when vector is equal to giventhatvector with givenprecision.doubleeuclideanNorm()Calculates an Euclidean norm of this vector.protected voidfail(java.lang.String message)doublefold(VectorAccumulator accumulator)Folds all elements of this vector with givenaccumulator.static VectorfromArray(double[] array)Creates a newVectorfrom the givenarrayw/o copying the underlying array.static VectorfromCollection(java.util.Collection<? extends java.lang.Number> list)Creates newBasicVectorfromliststatic VectorfromCSV(java.lang.String csv)ParsesVectorfrom the given CSV string.static VectorfromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)Creates newSparseVectorfromliststatic VectorfromMatrixMarket(java.lang.String mm)ParsesVectorfrom the given Matrix Market string.abstract doubleget(int i)Gets the specified element of this vector.VectorhadamardProduct(Vector that)Calculates the Hadamard (element-wise) product of this vector and giventhat.inthashCode()Calculates the hash-code of this vector.doubleinfinityNorm()Calculates an Infinity norm of this vector.doubleinnerProduct(Vector that)Calculates the inner product of this vector and giventhat.booleanis(VectorPredicate predicate)Checks whether this vector compiles with givenpredicateor not.VectorIteratoriterator()Returns a vector iterator.intlength()Returns the length of this vector.doublemanhattanNorm()Calculates a Manhattan norm of this vector.doublemax()Searches for the maximum value of the elements of this vector.doublemin()Searches for the minimum value of the elements of this vector.java.lang.StringmkString(java.text.NumberFormat formatter)Converts this vector into the string representation.java.lang.StringmkString(java.text.NumberFormat formatter, java.lang.String delimiter)Converts this vector into the string representation.Vectormultiply(double value)Multiplies this vector (X) by givenvalue(v).Vectormultiply(Matrix that)Multiples this vector (X) by giventhat(A).booleannon(VectorPredicate predicate)Checks whether this vector compiles with givenpredicateor not.doublenorm()Calculates an Euclidean norm of this vector.MatrixouterProduct(Vector that)Calculates the outer product of this vector and giventhat.doubleproduct()Multiplies up all elements of this vector.static Vectorrandom(int length, java.util.Random random)Vectorselect(int[] indices)Returns a new vector with the selected elements.abstract voidset(int i, double value)Sets the specified element of this matrix to givenvalue.voidsetAll(double value)Sets all elements of this vector to givenvalue.Vectorshuffle()Shuffles this vector.Vectorslice(int from, int until)Retrieves the specified sub-vector of this vector.VectorsliceLeft(int until)Retrieves the specified sub-vector of this vector.VectorsliceRight(int from)Retrieves the specified sub-vector of this vector.Vectorsubtract(double value)Subtracts givenvalue(v) from this vector (X).Vectorsubtract(Vector that)Subtracts giventhat(Y) from this vector (X).doublesum()Summarizes all elements of the vectorvoidswapElements(int i, int j)Swaps the specified elements of this vector.<T extends Vector>
Tto(VectorFactory<T> factory)Converts this vector using the givenfactory.abstract byte[]toBinary()Encodes this vector into a byte array.abstract MatrixtoColumnMatrix()Converts this vector to matrix with only one column.java.lang.StringtoCSV()Converts this vector into the CSV (Comma Separated Value) string.java.lang.StringtoCSV(java.text.NumberFormat formatter)Converts this vector into the CSV (Comma Separated Value) string using the givenformatter.DenseVectortoDenseVector()Converts this vector into aDenseVector.abstract MatrixtoDiagonalMatrix()Converts this vector to a diagonal matrix.java.lang.StringtoMatrixMarket()Converts this vector into the string in Matrix Market format.abstract java.lang.StringtoMatrixMarket(java.text.NumberFormat formatter)Converts this vector into the string in Matrix Market format using the givenformatter;abstract MatrixtoRowMatrix()Converts this vector to matrix with only one row.SparseVectortoSparseVector()Converts this vector into aSparseVector.java.lang.StringtoString()Converts this vector into a string representation.Vectortransform(VectorFunction function)Builds a new vector by applying givenfunctionto each element of this vector.static Vectorunit(int length)Creates an unitVectorof the givenlength.voidupdate(VectorFunction function)Updates all elements of this vector by applying givenfunction.voidupdateAt(int i, VectorFunction function)Updates the specified element of this vector by applying givenfunction.static Vectorzero(int length)Creates a zeroVectorof the givenlength.
-
-
-
Field Detail
-
DEFAULT_DELIMITER
private static final java.lang.String DEFAULT_DELIMITER
- See Also:
- Constant Field Values
-
DEFAULT_FORMATTER
private static final java.text.NumberFormat DEFAULT_FORMATTER
-
length
protected int length
Length of this vector.
-
-
Method Detail
-
constant
public static Vector constant(int length, double value)
-
random
public static Vector random(int length, java.util.Random random)
-
fromArray
public static Vector fromArray(double[] array)
Creates a newVectorfrom the givenarrayw/o copying the underlying array.
-
fromCSV
public static Vector fromCSV(java.lang.String csv)
ParsesVectorfrom the given CSV string.- Parameters:
csv- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
public static Vector fromMatrixMarket(java.lang.String mm)
ParsesVectorfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
public static Vector fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newBasicVectorfromlist
-
fromMap
public static Vector fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newSparseVectorfromlist
-
get
public abstract double get(int i)
Gets the specified element of this vector.- Parameters:
i- element's index- Returns:
- the element of this vector
-
set
public abstract void set(int i, double value)Sets the specified element of this matrix to givenvalue.- Parameters:
i- element's indexvalue- element's new value
-
blankOfLength
public abstract Vector blankOfLength(int length)
Creates a blank (an empty vector) copy of this vector with the givenlength.- Parameters:
length- the length of the blank vector- Returns:
- blank vector
-
copyOfLength
public abstract Vector copyOfLength(int length)
Copies this vector into the new vector with specifiedlength.- Parameters:
length- the length of new vector- Returns:
- the copy of this vector with new length
-
toRowMatrix
public abstract Matrix toRowMatrix()
Converts this vector to matrix with only one row.- Returns:
- the row matrix
-
toColumnMatrix
public abstract Matrix toColumnMatrix()
Converts this vector to matrix with only one column.- Returns:
- the column matrix
-
toDiagonalMatrix
public abstract Matrix toDiagonalMatrix()
Converts this vector to a diagonal matrix.- Returns:
- a diagonal matrix
-
apply
public abstract <T> T apply(VectorOperation<T> operation)
Pipes this vector to a givenoperation.- Type Parameters:
T- the result type- Parameters:
operation- the vector operation (an operation that take vector and returnsT)- Returns:
- the result of an operation applied to this vector
-
apply
public abstract <T> T apply(VectorVectorOperation<T> operation, Vector that)
Pipes this vector to a givenoperation.- Type Parameters:
T- the result type- Parameters:
operation- the vector-vector operation (an operation that takes two vectors and returnsT)that- the right hand vector for the given operation- Returns:
- the result of an operation applied to this and
thatvector
-
apply
public abstract <T> T apply(VectorMatrixOperation<T> operation, Matrix that)
Pipes this vector to a givenoperation.- Type Parameters:
T- the result type- Parameters:
operation- the vector-matrix operation (an operation that takes vector and matrix and returnsT)that- the right hand matrix for the given operation- Returns:
- the result of an operation applied to this vector and
thatmatrix
-
toBinary
public abstract byte[] toBinary()
Encodes this vector into a byte array.- Returns:
- a byte array representing this vector
-
toMatrixMarket
public abstract java.lang.String toMatrixMarket(java.text.NumberFormat formatter)
Converts this vector into the string in Matrix Market format using the givenformatter;- Parameters:
formatter- the number formater- Returns:
- a Matrix Market string representing this vector
-
setAll
public void setAll(double value)
Sets all elements of this vector to givenvalue.- Parameters:
value- the element's new value
-
length
public int length()
Returns the length of this vector.- Returns:
- length of this vector
-
add
public Vector add(double value)
Adds givenvalue(v) to this vector (X).- Parameters:
value- the right hand value for addition- Returns:
- X + v
-
add
public Vector add(Vector that)
Adds givenvector(X) to this vector (Y).- Parameters:
that- the right hand vector for addition- Returns:
- X + Y
-
multiply
public Vector multiply(double value)
Multiplies this vector (X) by givenvalue(v).- Parameters:
value- the right hand value for multiplication- Returns:
- X * v
-
hadamardProduct
public Vector hadamardProduct(Vector that)
Calculates the Hadamard (element-wise) product of this vector and giventhat.- Parameters:
that- the right hand vector for Hadamard product- Returns:
- the Hadamard product of two vectors
-
multiply
public Vector multiply(Matrix that)
Multiples this vector (X) by giventhat(A).- Parameters:
that- the right hand matrix for multiplication- Returns:
- X * A
-
subtract
public Vector subtract(double value)
Subtracts givenvalue(v) from this vector (X).- Parameters:
value- the right hand value for subtraction- Returns:
- X - v
-
subtract
public Vector subtract(Vector that)
Subtracts giventhat(Y) from this vector (X).- Parameters:
that- the right hand vector for subtraction- Returns:
- X - Y
-
divide
public Vector divide(double value)
Divides this vector (X) by givenvalue(v).- Parameters:
value- the right hand value for division- Returns:
- X / v
-
product
public double product()
Multiplies up all elements of this vector.- Returns:
- product of all elements of this vector
-
sum
public double sum()
Summarizes all elements of the vector- Returns:
- sum of all elements of the vector
-
innerProduct
public double innerProduct(Vector that)
Calculates the inner product of this vector and giventhat.- Parameters:
that- the right hand vector for inner product- Returns:
- the inner product of two vectors
-
outerProduct
public Matrix outerProduct(Vector that)
Calculates the outer product of this vector and giventhat.- Parameters:
that- the the right hand vector for outer product- Returns:
- the outer product of two vectors
-
norm
public double norm()
Calculates an Euclidean norm of this vector.- Returns:
- an Euclidean norm
-
euclideanNorm
public double euclideanNorm()
Calculates an Euclidean norm of this vector.- Returns:
- an Euclidean norm
-
manhattanNorm
public double manhattanNorm()
Calculates a Manhattan norm of this vector.- Returns:
- a Manhattan norm
-
infinityNorm
public double infinityNorm()
Calculates an Infinity norm of this vector.- Returns:
- an Infinity norm
-
swapElements
public void swapElements(int i, int j)Swaps the specified elements of this vector.- Parameters:
i- element's indexj- element's index
-
blank
public Vector blank()
Creates a blank (an empty vector with same length) copy of this vector.- Returns:
- blank vector
-
copy
public Vector copy()
Copies this vector.- Returns:
- the copy of this vector
-
shuffle
public Vector shuffle()
Shuffles this vector.Copies this vector in the new vector that contains the same elements but with the elements shuffled around (which might also result in the same vector (all outcomes are equally probable)).
- Returns:
- the shuffled vector
-
slice
public Vector slice(int from, int until)
Retrieves the specified sub-vector of this vector. The sub-vector is specified by interval of indices.- Parameters:
from- the beginning of indices intervaluntil- the ending of indices interval- Returns:
- the sub-vector of this vector
-
sliceLeft
public Vector sliceLeft(int until)
Retrieves the specified sub-vector of this vector. The sub-vector is specified by interval of indices. The left point of interval is fixed to zero.- Parameters:
until- the ending of indices interval- Returns:
- the sub-vector of this vector
-
sliceRight
public Vector sliceRight(int from)
Retrieves the specified sub-vector of this vector. The sub-vector is specified by interval of indices. The right point of interval is fixed to vector's length.- Parameters:
from- the beginning of indices interval- Returns:
- the sub-vector of this vector
-
select
public Vector select(int[] indices)
Returns a new vector with the selected elements.- Parameters:
indices- the array of indices- Returns:
- the new vector with the selected elements
-
each
public void each(VectorProcedure procedure)
Applies givenprocedureto each element of this vector.- Parameters:
procedure- the vector procedure
-
max
public double max()
Searches for the maximum value of the elements of this vector.- Returns:
- the maximum value of this vector
-
min
public double min()
Searches for the minimum value of the elements of this vector.- Returns:
- the minimum value of this vector
-
transform
public Vector transform(VectorFunction function)
Builds a new vector by applying givenfunctionto each element of this vector.- Parameters:
function- the vector function- Returns:
- the transformed vector
-
update
public void update(VectorFunction function)
Updates all elements of this vector by applying givenfunction.- Parameters:
function- the the vector function
-
updateAt
public void updateAt(int i, VectorFunction function)Updates the specified element of this vector by applying givenfunction.- Parameters:
i- element's indexfunction- the vector function
-
fold
public double fold(VectorAccumulator accumulator)
Folds all elements of this vector with givenaccumulator.- Parameters:
accumulator- the vector accumulator- Returns:
- the accumulated value
-
is
public boolean is(VectorPredicate predicate)
Checks whether this vector compiles with givenpredicateor not.- Parameters:
predicate- the vector predicate- Returns:
- whether this vector compiles with predicate
-
non
public boolean non(VectorPredicate predicate)
Checks whether this vector compiles with givenpredicateor not.- Parameters:
predicate- the vector predicate- Returns:
- whether this vector compiles with predicate
-
equals
public boolean equals(Vector that, double precision)
Returns true when vector is equal to giventhatvector with givenprecision.- Parameters:
that- vectorprecision- given precision- Returns:
- equals of this matrix to that
-
mkString
public java.lang.String mkString(java.text.NumberFormat formatter)
Converts this vector into the string representation.- Parameters:
formatter- the number formatter- Returns:
- the vector converted to a string
-
mkString
public java.lang.String mkString(java.text.NumberFormat formatter, java.lang.String delimiter)Converts this vector into the string representation.- Parameters:
formatter- the number formatterdelimiter- the element's delimiter- Returns:
- the vector converted to a string
-
toString
public java.lang.String toString()
Converts this vector into a string representation.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this vector
-
equals
public boolean equals(java.lang.Object o)
Checks where this vector is equal to the given objecto.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
Calculates the hash-code of this vector.- Overrides:
hashCodein classjava.lang.Object
-
iterator
public VectorIterator iterator()
Returns a vector iterator.- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Double>- Returns:
- a vector iterator.
-
to
public <T extends Vector> T to(VectorFactory<T> factory)
Converts this vector using the givenfactory.- Type Parameters:
T- type of the result vector- Parameters:
factory- the factory that creates an output vector- Returns:
- a converted vector
-
toDenseVector
public DenseVector toDenseVector()
Converts this vector into aDenseVector.- Returns:
- a dense vector
-
toSparseVector
public SparseVector toSparseVector()
Converts this vector into aSparseVector.- Returns:
- a sparse vector
-
toCSV
public java.lang.String toCSV()
Converts this vector into the CSV (Comma Separated Value) string.- Returns:
- a CSV string representing this vector
-
toCSV
public java.lang.String toCSV(java.text.NumberFormat formatter)
Converts this vector into the CSV (Comma Separated Value) string using the givenformatter.- Returns:
- a CSV string representing this vector
-
toMatrixMarket
public java.lang.String toMatrixMarket()
Converts this vector into the string in Matrix Market format.- Returns:
- a Matrix Market string representing this vector
-
ensureLengthIsCorrect
protected void ensureLengthIsCorrect(int length)
-
fail
protected void fail(java.lang.String message)
-
-