Package org.la4j.vector.dense
Class BasicVector
- java.lang.Object
-
- org.la4j.Vector
-
- org.la4j.vector.DenseVector
-
- org.la4j.vector.dense.BasicVector
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
public class BasicVector extends DenseVector
A basic dense vector implementation using an array. A dense data structure stores data in an underlying array. Even zero elements take up memory space. If you want a data structure that will not have zero elements take up memory space, try a sparse structure. However, fetch/store operations on dense data structures only take O(1) time, instead of the O(log n) time on sparse structures.BasicVector
stores the underlying data in a standard array.
-
-
Field Summary
Fields Modifier and Type Field Description private double[]
self
private static byte
VECTOR_TAG
-
Constructor Summary
Constructors Constructor Description BasicVector()
BasicVector(double[] array)
BasicVector(int length)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector
blankOfLength(int length)
Creates a blank (an empty vector) copy of this vector with the givenlength
.static BasicVector
constant(int length, double value)
Vector
copyOfLength(int length)
Copies this vector into the new vector with specifiedlength
.static BasicVector
fromArray(double[] array)
Creates a newBasicVector
from the givenarray
w/o copying the underlying array.static BasicVector
fromBinary(byte[] array)
DecodesBasicVector
from the given bytearray
.static BasicVector
fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newBasicVector
fromstatic BasicVector
fromCSV(java.lang.String csv)
ParsesBasicVector
from the given CSV string.static BasicVector
fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newBasicVector
from index-value mapstatic BasicVector
fromMatrixMarket(java.lang.String mm)
ParsesBasicVector
from the given Matrix Market string.double
get(int i)
Gets the specified element of this vector.static BasicVector
random(int length, java.util.Random random)
void
set(int i, double value)
Sets the specified element of this matrix to givenvalue
.void
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
.double[]
toArray()
Converts this dense vector to a double array.byte[]
toBinary()
Encodes this vector into a byte array.static BasicVector
unit(int length)
Creates an unitBasicVector
of the givenlength
.static BasicVector
zero(int length)
Creates a zeroBasicVector
of the givenlength
.-
Methods inherited from class org.la4j.vector.DenseVector
apply, apply, apply, toColumnMatrix, toDiagonalMatrix, toMatrixMarket, toRowMatrix
-
Methods inherited from class org.la4j.Vector
add, add, blank, copy, divide, each, ensureLengthIsCorrect, equals, equals, euclideanNorm, fail, fold, hadamardProduct, hashCode, infinityNorm, innerProduct, is, iterator, length, manhattanNorm, max, min, mkString, mkString, multiply, multiply, non, norm, outerProduct, product, select, setAll, shuffle, slice, sliceLeft, sliceRight, subtract, subtract, sum, toCSV, toCSV, toDenseVector, toMatrixMarket, toSparseVector, toString, transform, update, updateAt
-
-
-
-
Field Detail
-
VECTOR_TAG
private static final byte VECTOR_TAG
- See Also:
- Constant Field Values
-
self
private double[] self
-
-
Method Detail
-
zero
public static BasicVector zero(int length)
Creates a zeroBasicVector
of the givenlength
.
-
constant
public static BasicVector constant(int length, double value)
-
unit
public static BasicVector unit(int length)
Creates an unitBasicVector
of the givenlength
.
-
random
public static BasicVector random(int length, java.util.Random random)
-
fromArray
public static BasicVector fromArray(double[] array)
Creates a newBasicVector
from the givenarray
w/o copying the underlying array.
-
fromBinary
public static BasicVector fromBinary(byte[] array)
DecodesBasicVector
from the given bytearray
.- Parameters:
array
- the byte array representing a vector- Returns:
- a decoded vector
-
fromCSV
public static BasicVector fromCSV(java.lang.String csv)
ParsesBasicVector
from the given CSV string.- Parameters:
csv
- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
public static BasicVector fromMatrixMarket(java.lang.String mm)
ParsesBasicVector
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
public static BasicVector fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newBasicVector
from- Parameters:
list
- list containing doubles- Returns:
- new vector from given double list
-
fromMap
public static BasicVector fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newBasicVector
from index-value map- Parameters:
map
- index-value maplength
- vector length- Returns:
- created vector
-
get
public double get(int i)
Description copied from class:Vector
Gets the specified element of this vector.
-
set
public void set(int i, double value)
Description copied from class:Vector
Sets the specified element of this matrix to givenvalue
.
-
swapElements
public void swapElements(int i, int j)
Description copied from class:Vector
Swaps the specified elements of this vector.- Overrides:
swapElements
in classVector
- Parameters:
i
- element's indexj
- element's index
-
copyOfLength
public Vector copyOfLength(int length)
Description copied from class:Vector
Copies this vector into the new vector with specifiedlength
.- Specified by:
copyOfLength
in classVector
- Parameters:
length
- the length of new vector- Returns:
- the copy of this vector with new length
-
toArray
public double[] toArray()
Description copied from class:DenseVector
Converts this dense vector to a double array.- Specified by:
toArray
in classDenseVector
- Returns:
- an array representation of this vector
-
to
public <T extends Vector> T to(VectorFactory<T> factory)
Description copied from class:Vector
Converts this vector using the givenfactory
.
-
blankOfLength
public Vector blankOfLength(int length)
Description copied from class:Vector
Creates a blank (an empty vector) copy of this vector with the givenlength
.- Specified by:
blankOfLength
in classVector
- Parameters:
length
- the length of the blank vector- Returns:
- blank vector
-
-