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.String
DEFAULT_DELIMITER
private static java.text.NumberFormat
DEFAULT_FORMATTER
protected int
length
Length of this vector.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Vector
add(double value)
Adds givenvalue
(v) to this vector (X).Vector
add(Vector that)
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
.Vector
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)
Vector
copy()
Copies this vector.abstract Vector
copyOfLength(int length)
Copies this vector into the new vector with specifiedlength
.Vector
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
equals(java.lang.Object o)
Checks where this vector is equal to the given objecto
.boolean
equals(Vector that, double precision)
Returns true when vector is equal to giventhat
vector with givenprecision
.double
euclideanNorm()
Calculates an Euclidean norm of this vector.protected void
fail(java.lang.String message)
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(java.util.Collection<? extends java.lang.Number> list)
Creates newBasicVector
fromlist
static Vector
fromCSV(java.lang.String csv)
ParsesVector
from the given CSV string.static Vector
fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newSparseVector
fromlist
static Vector
fromMatrixMarket(java.lang.String mm)
ParsesVector
from the given Matrix Market string.abstract double
get(int i)
Gets the specified element of this vector.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
infinityNorm()
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.VectorIterator
iterator()
Returns a vector iterator.int
length()
Returns the length of this vector.double
manhattanNorm()
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.java.lang.String
mkString(java.text.NumberFormat formatter)
Converts this vector into the string representation.java.lang.String
mkString(java.text.NumberFormat formatter, java.lang.String delimiter)
Converts this vector into the string representation.Vector
multiply(double value)
Multiplies this vector (X) by givenvalue
(v).Vector
multiply(Matrix that)
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.Matrix
outerProduct(Vector that)
Calculates the outer product of this vector and giventhat
.double
product()
Multiplies up all elements of this vector.static Vector
random(int length, java.util.Random random)
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
.Vector
shuffle()
Shuffles this vector.Vector
slice(int from, int until)
Retrieves the specified sub-vector of this vector.Vector
sliceLeft(int until)
Retrieves the specified sub-vector of this vector.Vector
sliceRight(int from)
Retrieves the specified sub-vector of this vector.Vector
subtract(double value)
Subtracts givenvalue
(v) from this vector (X).Vector
subtract(Vector that)
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
toColumnMatrix()
Converts this vector to matrix with only one column.java.lang.String
toCSV()
Converts this vector into the CSV (Comma Separated Value) string.java.lang.String
toCSV(java.text.NumberFormat formatter)
Converts this vector into the CSV (Comma Separated Value) string using the givenformatter
.DenseVector
toDenseVector()
Converts this vector into aDenseVector
.abstract Matrix
toDiagonalMatrix()
Converts this vector to a diagonal matrix.java.lang.String
toMatrixMarket()
Converts this vector into the string in Matrix Market format.abstract java.lang.String
toMatrixMarket(java.text.NumberFormat formatter)
Converts this vector into the string in Matrix Market format using the givenformatter
;abstract Matrix
toRowMatrix()
Converts this vector to matrix with only one row.SparseVector
toSparseVector()
Converts this vector into aSparseVector
.java.lang.String
toString()
Converts this vector into a string representation.Vector
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
.
-
-
-
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 newVector
from the givenarray
w/o copying the underlying array.
-
fromCSV
public static Vector fromCSV(java.lang.String csv)
ParsesVector
from 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)
ParsesVector
from 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 newBasicVector
fromlist
-
fromMap
public static Vector fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
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
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
that
vector
-
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
that
matrix
-
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 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
public Vector transform(VectorFunction function)
Builds a new vector by applying givenfunction
to 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 givenpredicate
or 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 givenpredicate
or 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 giventhat
vector 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:
toString
in 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:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
Calculates the hash-code of this vector.- Overrides:
hashCode
in classjava.lang.Object
-
iterator
public VectorIterator iterator()
Returns a vector iterator.- Specified by:
iterator
in 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)
-
-