Package org.la4j
Class Vector
java.lang.Object
org.la4j.Vector
- Direct Known Subclasses:
DenseVector
,SparseVector
A vector represents an array of elements. It can be re-sized.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private static final NumberFormat
protected int
Length 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> T
apply
(VectorMatrixOperation<T> operation, Matrix that) Pipes this vector to a givenoperation
.abstract <T> T
apply
(VectorOperation<T> operation) Pipes this vector to a givenoperation
.abstract <T> T
apply
(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 Vector
blankOfLength
(int length) Creates a blank (an empty vector) copy of this vector with the givenlength
.static Vector
constant
(int length, double value) copy()
Copies this vector.abstract Vector
copyOfLength
(int length) Copies this vector into the new vector with specifiedlength
.divide
(double value) Divides this vector (X) by givenvalue
(v).void
each
(VectorProcedure procedure) Applies givenprocedure
to each element of this vector.protected void
ensureLengthIsCorrect
(int length) boolean
Checks where this vector is equal to the given objecto
.boolean
Returns true when vector is equal to giventhat
vector with givenprecision
.double
Calculates an Euclidean norm of this vector.protected void
double
fold
(VectorAccumulator accumulator) Folds all elements of this vector with givenaccumulator
.static Vector
fromArray
(double[] array) Creates a newVector
from the givenarray
w/o copying the underlying array.static Vector
fromCollection
(Collection<? extends Number> list) Creates newBasicVector
fromlist
static Vector
ParsesVector
from the given CSV string.static Vector
Creates newSparseVector
fromlist
static Vector
ParsesVector
from the given Matrix Market string.abstract double
get
(int i) Gets the specified element of this vector.hadamardProduct
(Vector that) Calculates the Hadamard (element-wise) product of this vector and giventhat
.int
hashCode()
Calculates the hash-code of this vector.double
Calculates an Infinity norm of this vector.double
innerProduct
(Vector that) Calculates the inner product of this vector and giventhat
.boolean
is
(VectorPredicate predicate) Checks whether this vector compiles with givenpredicate
or not.iterator()
Returns a vector iterator.int
length()
Returns the length of this vector.double
Calculates a Manhattan norm of this vector.double
max()
Searches for the maximum value of the elements of this vector.double
min()
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).boolean
non
(VectorPredicate predicate) Checks whether this vector compiles with givenpredicate
or not.double
norm()
Calculates an Euclidean norm of this vector.outerProduct
(Vector that) Calculates the outer product of this vector and giventhat
.double
product()
Multiplies up all elements of this vector.static Vector
select
(int[] indices) Returns a new vector with the selected elements.abstract void
set
(int i, double value) Sets the specified element of this matrix to givenvalue
.void
setAll
(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).double
sum()
Summarizes all elements of the vectorvoid
swapElements
(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 Matrix
Converts 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 Matrix
Converts this vector to a diagonal matrix.Converts this vector into the string in Matrix Market format.abstract String
toMatrixMarket
(NumberFormat formatter) Converts this vector into the string in Matrix Market format using the givenformatter
;abstract Matrix
Converts 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 givenfunction
to each element of this vector.static Vector
unit
(int length) Creates an unitVector
of the givenlength
.void
update
(VectorFunction function) Updates all elements of this vector by applying givenfunction
.void
updateAt
(int i, VectorFunction function) Updates the specified element of this vector by applying givenfunction
.static Vector
zero
(int length) Creates a zeroVector
of the givenlength
.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.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
Creates a zeroVector
of the givenlength
. -
constant
-
unit
Creates an unitVector
of the givenlength
. -
random
-
fromArray
Creates a newVector
from the givenarray
w/o copying the underlying array. -
fromCSV
ParsesVector
from the given CSV string.- Parameters:
csv
- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
ParsesVector
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
Creates newBasicVector
fromlist
-
fromMap
Creates newSparseVector
fromlist
-
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
that
vector
-
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
that
matrix
-
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
Adds givenvector
(X) to this vector (Y).- Parameters:
that
- the right hand vector for addition- Returns:
- X + Y
-
multiply
Multiplies this vector (X) by givenvalue
(v).- Parameters:
value
- the right hand value for multiplication- Returns:
- X * v
-
hadamardProduct
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
Multiples this vector (X) by giventhat
(A).- Parameters:
that
- the right hand matrix for multiplication- Returns:
- X * A
-
subtract
Subtracts givenvalue
(v) from this vector (X).- Parameters:
value
- the right hand value for subtraction- Returns:
- X - v
-
subtract
Subtracts giventhat
(Y) from this vector (X).- Parameters:
that
- the right hand vector for subtraction- Returns:
- X - Y
-
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
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
Creates a blank (an empty vector with same length) copy of this vector.- Returns:
- blank vector
-
copy
Copies this vector.- Returns:
- the copy of this 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
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 givenprocedure
to 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 givenfunction
to 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 givenpredicate
or not.- Parameters:
predicate
- the vector predicate- Returns:
- whether this vector compiles with predicate
-
non
Checks whether this vector compiles with givenpredicate
or not.- Parameters:
predicate
- the vector predicate- Returns:
- whether this vector compiles with predicate
-
equals
Returns true when vector is equal to giventhat
vector 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
Converts this vector into a string representation. -
equals
Checks where this vector is equal to the given objecto
. -
hashCode
public int hashCode()Calculates the hash-code of this vector. -
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
Converts this vector into aDenseVector
.- Returns:
- a dense vector
-
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
-