Class Vector
java.lang.Object
org.la4j.Vector
- Direct Known Subclasses:
DenseVector, SparseVector
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate static final NumberFormatprotected intLength of this vector. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(double value) Adds givenvalue(v) to this vector (X).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.blank()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) copy()Copies this vector.abstract VectorcopyOfLength(int length) Copies this vector into the new vector with specifiedlength.divide(double value) Divides this vector (X) by givenvalue(v).voideach(VectorProcedure procedure) Applies givenprocedureto each element of this vector.protected voidensureLengthIsCorrect(int length) booleanChecks where this vector is equal to the given objecto.booleanReturns true when vector is equal to giventhatvector with givenprecision.doubleCalculates an Euclidean norm of this vector.protected voiddoublefold(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(Collection<? extends Number> list) Creates newBasicVectorfromliststatic VectorParsesVectorfrom the given CSV string.static VectorCreates newSparseVectorfromliststatic VectorParsesVectorfrom the given Matrix Market string.abstract doubleget(int i) Gets the specified element of this vector.hadamardProduct(Vector that) Calculates the Hadamard (element-wise) product of this vector and giventhat.inthashCode()Calculates the hash-code of this vector.doubleCalculates 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.iterator()Returns a vector iterator.intlength()Returns the length of this vector.doubleCalculates 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.mkString(NumberFormat formatter) Converts this vector into the string representation.mkString(NumberFormat formatter, String delimiter) Converts this vector into the string representation.multiply(double value) Multiplies this vector (X) by givenvalue(v).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.outerProduct(Vector that) Calculates the outer product of this vector and giventhat.doubleproduct()Multiplies up all elements of this vector.static 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.shuffle()Shuffles this vector.slice(int from, int until) Retrieves the specified sub-vector of this vector.sliceLeft(int until) Retrieves the specified sub-vector of this vector.sliceRight(int from) Retrieves the specified sub-vector of this vector.subtract(double value) Subtracts givenvalue(v) from this vector (X).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 MatrixConverts this vector to matrix with only one column.toCSV()Converts this vector into the CSV (Comma Separated Value) string.toCSV(NumberFormat formatter) Converts this vector into the CSV (Comma Separated Value) string using the givenformatter.Converts this vector into aDenseVector.abstract MatrixConverts this vector to a diagonal matrix.Converts this vector into the string in Matrix Market format.abstract StringtoMatrixMarket(NumberFormat formatter) Converts this vector into the string in Matrix Market format using the givenformatter;abstract MatrixConverts this vector to matrix with only one row.Converts this vector into aSparseVector.toString()Converts this vector into a string representation.transform(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.Methods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
DEFAULT_DELIMITER
- See Also:
-
DEFAULT_FORMATTER
-
length
protected int lengthLength of this vector.
-
-
Constructor Details
-
Vector
public Vector()Creates a vector of zero length. -
Vector
public Vector(int length) Creates a vector of givenlength.- Parameters:
length- the length of the vector
-
-
Method Details
-
zero
-
constant
-
unit
-
random
-
fromArray
-
fromCSV
-
fromMatrixMarket
-
fromCollection
Creates newBasicVectorfromlist -
fromMap
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
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
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
Converts this vector to matrix with only one row.- Returns:
- the row matrix
-
toColumnMatrix
Converts this vector to matrix with only one column.- Returns:
- the column matrix
-
toDiagonalMatrix
Converts this vector to a diagonal matrix.- Returns:
- a diagonal matrix
-
apply
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
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
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
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
Adds givenvalue(v) to this vector (X).- Parameters:
value- the right hand value for addition- Returns:
- X + v
-
add
-
multiply
Multiplies this vector (X) by givenvalue(v).- Parameters:
value- the right hand value for multiplication- Returns:
- X * v
-
hadamardProduct
-
multiply
-
subtract
Subtracts givenvalue(v) from this vector (X).- Parameters:
value- the right hand value for subtraction- Returns:
- X - v
-
subtract
-
divide
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
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
-
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
Creates a blank (an empty vector with same length) copy of this vector.- Returns:
- blank vector
-
copy
-
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
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
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
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
Returns a new vector with the selected elements.- Parameters:
indices- the array of indices- Returns:
- the new vector with the selected elements
-
each
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
Builds a new vector by applying givenfunctionto each element of this vector.- Parameters:
function- the vector function- Returns:
- the transformed vector
-
update
Updates all elements of this vector by applying givenfunction.- Parameters:
function- the the vector function
-
updateAt
Updates the specified element of this vector by applying givenfunction.- Parameters:
i- element's indexfunction- the vector function
-
fold
Folds all elements of this vector with givenaccumulator.- Parameters:
accumulator- the vector accumulator- Returns:
- the accumulated value
-
is
Checks whether this vector compiles with givenpredicateor not.- Parameters:
predicate- the vector predicate- Returns:
- whether this vector compiles with predicate
-
non
Checks whether this vector compiles with givenpredicateor not.- Parameters:
predicate- the vector predicate- Returns:
- whether this vector compiles with predicate
-
equals
Returns true when vector is equal to giventhatvector with givenprecision.- Parameters:
that- vectorprecision- given precision- Returns:
- equals of this matrix to that
-
mkString
Converts this vector into the string representation.- Parameters:
formatter- the number formatter- Returns:
- the vector converted to a string
-
mkString
Converts this vector into the string representation.- Parameters:
formatter- the number formatterdelimiter- the element's delimiter- Returns:
- the vector converted to a string
-
toString
-
equals
-
hashCode
-
iterator
Returns a vector iterator. -
to
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
-
toSparseVector
Converts this vector into aSparseVector.- Returns:
- a sparse vector
-
toCSV
Converts this vector into the CSV (Comma Separated Value) string.- Returns:
- a CSV string representing this vector
-
toCSV
Converts this vector into the CSV (Comma Separated Value) string using the givenformatter.- Returns:
- a CSV string representing this vector
-
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
-