org.la4j

Class Vector

• All Implemented Interfaces:
Iterable<Double>
Direct Known Subclasses:
DenseVector, SparseVector

```public abstract class Vector
extends Object
implements Iterable<Double>```
A vector represents an array of elements. It can be re-sized.
• Field Summary

Fields
Modifier and Type Field and Description
`protected int` `length`
Length of this vector.
• Constructor Summary

Constructors
Constructor and Description
`Vector()`
Creates a vector of zero length.
`Vector(int length)`
Creates a vector of given `length`.
• Method Summary

Methods
Modifier and Type Method and Description
`Vector` `add(double value)`
Adds given `value` (v) to this vector (X).
`Vector` `add(Vector that)`
Adds given `vector` (X) to this vector (Y).
`abstract <T> T` ```apply(VectorMatrixOperation<T> operation, Matrix that)```
Pipes this vector to a given `operation`.
`abstract <T> T` `apply(VectorOperation<T> operation)`
Pipes this vector to a given `operation`.
`abstract <T> T` ```apply(VectorVectorOperation<T> operation, Vector that)```
Pipes this vector to a given `operation`.
`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 given `length`.
`static Vector` ```constant(int length, double value)```
Creates a constant `Vector` of the given `length` with the given `value`.
`Vector` `copy()`
Copies this vector.
`abstract Vector` `copyOfLength(int length)`
Copies this vector into the new vector with specified `length`.
`Vector` `divide(double value)`
Divides this vector (X) by given `value` (v).
`void` `each(VectorProcedure procedure)`
Applies given `procedure` to each element of this vector.
`protected void` `ensureLengthIsCorrect(int length)`
`boolean` `equals(Object o)`
Checks where this vector is equal to the given object `o`.
`boolean` ```equals(Vector that, double precision)```
Returns true when vector is equal to given `that` vector with given `precision`.
`double` `euclideanNorm()`
Calculates an Euclidean norm of this vector.
`protected void` `fail(String message)`
`double` `fold(VectorAccumulator accumulator)`
Folds all elements of this vector with given `accumulator`.
`static Vector` `fromArray(double[] array)`
Creates a new `Vector` from the given `array` w/o copying the underlying array.
`static Vector` `fromCollection(Collection<? extends Number> list)`
Creates new `BasicVector` from `list`
`static Vector` `fromCSV(String csv)`
Parses `Vector` from the given CSV string.
`static Vector` ```fromMap(Map<Integer,? extends Number> map, int length)```
Creates new `SparseVector` from `list`
`static Vector` `fromMatrixMarket(String mm)`
Parses `Vector` 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 given `that`.
`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 given `that`.
`boolean` `is(VectorPredicate predicate)`
Checks whether this vector compiles with given `predicate` 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.
`String` `mkString(NumberFormat formatter)`
Converts this vector into the string representation.
`String` ```mkString(NumberFormat formatter, String delimiter)```
Converts this vector into the string representation.
`Vector` `multiply(double value)`
Multiplies this vector (X) by given `value` (v).
`Vector` `multiply(Matrix that)`
Multiples this vector (X) by given `that` (A).
`boolean` `non(VectorPredicate predicate)`
Checks whether this vector compiles with given `predicate` or not.
`double` `norm()`
Calculates an Euclidean norm of this vector.
`Matrix` `outerProduct(Vector that)`
Calculates the outer product of this vector and given `that`.
`double` `product()`
Multiplies up all elements of this vector.
`static Vector` ```random(int length, Random random)```
Creates a random `Vector` of the given `length` with the given `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 given `value`.
`void` `setAll(double value)`
Sets all elements of this vector to given `value`.
`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 given `value` (v) from this vector (X).
`Vector` `subtract(Vector that)`
Subtracts given `that` (Y) from this vector (X).
`double` `sum()`
Summarizes all elements of the vector
`void` ```swapElements(int i, int j)```
Swaps the specified elements of this vector.
`<T extends Vector> T` `to(VectorFactory<T> factory)`
Converts this vector using the given `factory`.
`abstract byte[]` `toBinary()`
Encodes this vector into a byte array.
`abstract Matrix` `toColumnMatrix()`
Converts this vector to matrix with only one column.
`String` `toCSV()`
Converts this vector into the CSV (Comma Separated Value) string.
`String` `toCSV(NumberFormat formatter)`
Converts this vector into the CSV (Comma Separated Value) string using the given `formatter`.
`DenseVector` `toDenseVector()`
Converts this vector into a `DenseVector`.
`abstract Matrix` `toDiagonalMatrix()`
Converts this vector to a diagonal matrix.
`String` `toMatrixMarket()`
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 given `formatter`;
`abstract Matrix` `toRowMatrix()`
Converts this vector to matrix with only one row.
`SparseVector` `toSparseVector()`
Converts this vector into a `SparseVector`.
`String` `toString()`
Converts this vector into a string representation.
`Vector` `transform(VectorFunction function)`
Builds a new vector by applying given `function` to each element of this vector.
`static Vector` `unit(int length)`
Creates an unit `Vector` of the given `length`.
`void` `update(VectorFunction function)`
Updates all elements of this vector by applying given `function`.
`void` ```updateAt(int i, VectorFunction function)```
Updates the specified element of this vector by applying given `function`.
`static Vector` `zero(int length)`
Creates a zero `Vector` of the given `length`.
• Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
• Field Detail

• length

`protected int length`
Length of this vector.
• Constructor Detail

• Vector

`public Vector()`
Creates a vector of zero length.
• Vector

`public Vector(int length)`
Creates a vector of given `length`.
Parameters:
`length` - the length of the vector
• Method Detail

• zero

`public static Vector zero(int length)`
Creates a zero `Vector` of the given `length`.
• constant

```public static Vector constant(int length,
double value)```
Creates a constant `Vector` of the given `length` with the given `value`.
• unit

`public static Vector unit(int length)`
Creates an unit `Vector` of the given `length`.
• random

```public static Vector random(int length,
Random random)```
Creates a random `Vector` of the given `length` with the given `Random`.
• fromArray

`public static Vector fromArray(double[] array)`
Creates a new `Vector` from the given `array` w/o copying the underlying array.
• fromCSV

`public static Vector fromCSV(String csv)`
Parses `Vector` from the given CSV string.
Parameters:
`csv` - the CSV string representing a vector
Returns:
a parsed vector
• fromMatrixMarket

`public static Vector fromMatrixMarket(String mm)`
Parses `Vector` from the given Matrix Market string.
Parameters:
`mm` - the string in Matrix Market format
Returns:
a parsed vector
• fromCollection

`public static Vector fromCollection(Collection<? extends Number> list)`
Creates new `BasicVector` from `list`
• fromMap

```public static Vector fromMap(Map<Integer,? extends Number> map,
int length)```
Creates new `SparseVector` from `list`
• 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 given `value`.
Parameters:
`i` - element's index
`value` - element's new value
• blankOfLength

`public abstract Vector blankOfLength(int length)`
Creates a blank (an empty vector) copy of this vector with the given `length`.
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 specified `length`.
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 given `operation`.
Type Parameters:
`T` - the result type
Parameters:
`operation` - the vector operation (an operation that take vector and returns `T`)
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 given `operation`.
Type Parameters:
`T` - the result type
Parameters:
`operation` - the vector-vector operation (an operation that takes two vectors and returns `T`)
`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 given `operation`.
Type Parameters:
`T` - the result type
Parameters:
`operation` - the vector-matrix operation (an operation that takes vector and matrix and returns `T`)
`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 String toMatrixMarket(NumberFormat formatter)`
Converts this vector into the string in Matrix Market format using the given `formatter`;
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 given `value`.
Parameters:
`value` - the element's new value
• length

`public int length()`
Returns the length of this vector.
Returns:
length of this vector

`public Vector add(double value)`
Adds given `value` (v) to this vector (X).
Parameters:
`value` - the right hand value for addition
Returns:
X + v

`public Vector add(Vector that)`
Adds given `vector` (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 given `value` (v).
Parameters:
`value` - the right hand value for multiplication
Returns:
X * v

`public Vector hadamardProduct(Vector that)`
Calculates the Hadamard (element-wise) product of this vector and given `that`.
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 given `that` (A).
Parameters:
`that` - the right hand matrix for multiplication
Returns:
X * A
• subtract

`public Vector subtract(double value)`
Subtracts given `value` (v) from this vector (X).
Parameters:
`value` - the right hand value for subtraction
Returns:
X - v
• subtract

`public Vector subtract(Vector that)`
Subtracts given `that` (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 given `value` (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 given `that`.
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 given `that`.
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 index
`j` - 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 interval
`until` - 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 given `procedure` 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 given `function` 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 given `function`.
Parameters:
`function` - the the vector function
• updateAt

```public void updateAt(int i,
VectorFunction function)```
Updates the specified element of this vector by applying given `function`.
Parameters:
`i` - element's index
`function` - the vector function
• fold

`public double fold(VectorAccumulator accumulator)`
Folds all elements of this vector with given `accumulator`.
Parameters:
`accumulator` - the vector accumulator
Returns:
the accumulated value
• is

`public boolean is(VectorPredicate predicate)`
Checks whether this vector compiles with given `predicate` 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 given `predicate` 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 given `that` vector with given `precision`.
Parameters:
`that` - vector
`precision` - given precision
Returns:
equals of this matrix to that
• mkString

`public String mkString(NumberFormat formatter)`
Converts this vector into the string representation.
Parameters:
`formatter` - the number formatter
Returns:
the vector converted to a string
• mkString

```public String mkString(NumberFormat formatter,
String delimiter)```
Converts this vector into the string representation.
Parameters:
`formatter` - the number formatter
`delimiter` - the element's delimiter
Returns:
the vector converted to a string
• toString

`public String toString()`
Converts this vector into a string representation.
Overrides:
`toString` in class `Object`
Returns:
a string representation of this vector
• equals

`public boolean equals(Object o)`
Checks where this vector is equal to the given object `o`.
Overrides:
`equals` in class `Object`
• hashCode

`public int hashCode()`
Calculates the hash-code of this vector.
Overrides:
`hashCode` in class `Object`
• iterator

`public VectorIterator iterator()`
Returns a vector iterator.
Specified by:
`iterator` in interface `Iterable<Double>`
Returns:
a vector iterator.
• to

`public <T extends Vector> T to(VectorFactory<T> factory)`
Converts this vector using the given `factory`.
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 a `DenseVector`.
Returns:
a dense vector
• toSparseVector

`public SparseVector toSparseVector()`
Converts this vector into a `SparseVector`.
Returns:
a sparse vector
• toCSV

`public String toCSV()`
Converts this vector into the CSV (Comma Separated Value) string.
Returns:
a CSV string representing this vector
• toCSV

`public String toCSV(NumberFormat formatter)`
Converts this vector into the CSV (Comma Separated Value) string using the given `formatter`.
Returns:
a CSV string representing this vector
• toMatrixMarket

`public 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(String message)`