Class Vector
java.lang.Object
smile.tensor.DenseMatrix
smile.tensor.Vector
- All Implemented Interfaces:
Serializable, Matrix, Tensor
Mathematical vector interface. Vectors are a specialized case of matrices.
Specifically, a vector is a matrix with either one row (a row-vector) or
one column (a column-vector). Column-vectors are the more common
representation and are often simply referred to as vectors.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionabstract voidadd(int i, double x) SetsA[i] += x.doubleapply(int i) ReturnsA[i]for Scala users.doubleasum()Sums the absolute values of the elements of the vector.voidComputes a constant alpha times a vector x plus this vector y.static Vectorcolumn(double[] array) Creates a column vector.static Vectorcolumn(float[] array) Creates a column vector.column(int j) Returns the j-th column.copy()Returns a deep copy of matrix.abstract Vectorcopy(int from, int to) Copies the specified range of the vector into a new vector.static voidCopies the specified range of the vector into another vector.diagflat()Returns the matrix with the elements of this vector as the diagonal.abstract voiddiv(int i, double x) SetsA[i] /= x.doubleComputes the dot product of two vectors.abstract voidfill(int from, int to, double value) Assigns the specified value to each element of the specified range of the specified vector.abstract doubleget(int i) ReturnsA[i].intiamax()Searches a vector for the first occurrence of the maximum absolute value.doublemax()Returns the maximal elements of the vector.doublemean()Returns the mean of the elements of the vector.doublemin()Returns the minimal elements of the vector.abstract voidmul(int i, double x) SetsA[i] *= x.doublenorm1()Computes the L1 norm of a vector.doublenorm2()Computes the Euclidean (L2) norm of a vector.doublenormInf()Computes the L-Infinity norm of a vector.ones(int length) Returns a one column vector of the same scalar type as this vector.static Vectorones(ScalarType scalarType, int length) Returns a one column vector.static Vectorrow(double[] array) Creates a row vector.static Vectorrow(float[] array) Creates a row vector.scale(double alpha) A *= alphaabstract voidset(int i, double x) SetsA[i] = x.abstract intsize()Returns the number of elements.abstract Vectorslice(int from, int to) Returns a slice of vector, which shares the data storage.abstract intsoftmax()The softmax function without overflow.abstract voidsub(int i, double x) SetsA[i] -= x.doublesum()Sums the elements of the vector.voidSwaps two vectors.abstract double[]toArray(double[] a) Returns an array containing all the elements in this vector.abstract float[]toArray(float[] a) Returns an array containing all the elements in this vector.voidupdate(int i, double x) SetsA[i] = xfor Scala users.zeros(int length) Returns a zero column vector of the same scalar type as this vector.static Vectorzeros(ScalarType scalarType, int length) Returns a zero column vector.Methods inherited from class DenseMatrix
aat, add, add, ata, axpy, cholesky, colMeans, colNames, colSds, colSums, columns, columns, diag, diagflat, diagflat, eigen, eigen, equals, eye, eye, eye, eye, fill, ger, inverse, isSymmetric, ld, lu, memory, mm, mm, mt, mv, ncol, nrow, of, of, order, qr, rand, rand, rand, randn, row, rowMeans, rowNames, rows, rows, rowSums, standardize, standardize, sub, submatrix, svd, svd, tm, toeplitz, toeplitz, toeplitz, toeplitz, toString, transpose, uplo, withColNames, withDiag, withRowNames, withUplo, zeros, zerosMethods inherited from class Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Matrix
add, apply, diagonal, dim, div, get, get, length, mul, mv, mv, mv, mv, mv, reshape, set, set, shape, size, sub, toArray, toArray, toString, toString, trace, tv, tv, tv, tv, tv, update, vector, vector, vector, xAxMethods inherited from interface Tensor
scalarType
-
Method Details
-
size
public abstract int size()Returns the number of elements.- Returns:
- the number of elements.
-
get
public abstract double get(int i) ReturnsA[i].- Parameters:
i- the row index.- Returns:
- the cell value.
-
apply
public double apply(int i) ReturnsA[i]for Scala users.- Parameters:
i- the index.- Returns:
- the cell value.
-
set
public abstract void set(int i, double x) SetsA[i] = x.- Parameters:
i- the index.x- the cell value.
-
update
public void update(int i, double x) SetsA[i] = xfor Scala users.- Parameters:
i- the index.x- the cell value.
-
add
public abstract void add(int i, double x) SetsA[i] += x.- Parameters:
i- the index.x- the operand.
-
sub
public abstract void sub(int i, double x) SetsA[i] -= x.- Parameters:
i- the index.x- the operand.
-
mul
public abstract void mul(int i, double x) SetsA[i] *= x.- Parameters:
i- the index.x- the operand.
-
div
public abstract void div(int i, double x) SetsA[i] /= x.- Parameters:
i- the index.x- the operand.
-
slice
Returns a slice of vector, which shares the data storage.- Parameters:
from- the initial index of the range to be copied, inclusive.to- the final index of the range to be copied, exclusive.- Returns:
- a slice of this vector.
-
column
Description copied from class:DenseMatrixReturns the j-th column. Negative index -j means the j-th row from the end.- Specified by:
columnin classDenseMatrix- Parameters:
j- the column index.- Returns:
- the column.
-
copy
-
copy
Copies the specified range of the vector into a new vector. The final index of the range may be greater than vector length, in which case 0 is placed in all extra elements.- Parameters:
from- the initial index of the range to be copied, inclusive.to- the final index of the range to be copied, exclusive. This index may lie outside the array.- Returns:
- a copy of this vector.
-
copy
Copies the specified range of the vector into another vector.- Parameters:
src- the destination vector.pos- starting position in this vector.dest- the destination vector.destPos- starting position in the destination vector.length- the number of vector elements to be copied.
-
toArray
public abstract double[] toArray(double[] a) Returns an array containing all the elements in this vector.- Parameters:
a- the array into which the elements of the vector are to be stored if it is big enough; otherwise, a new array is allocated.- Returns:
- an array containing the elements of the vector.
-
toArray
public abstract float[] toArray(float[] a) Returns an array containing all the elements in this vector.- Parameters:
a- the array into which the elements of the vector are to be stored, if it is big enough; otherwise, a new array is allocated.- Returns:
- an array containing the elements of the vector.
-
fill
public abstract void fill(int from, int to, double value) Assigns the specified value to each element of the specified range of the specified vector.- Parameters:
from- the index of the first element (inclusive) to be filled with the specified value.to- the index of the last element (exclusive) to be filled with the specified value.value- the value to be stored in specified range of the vector.
-
diagflat
Returns the matrix with the elements of this vector as the diagonal.- Returns:
- the diagonal matrix.
-
column
Creates a column vector.- Parameters:
array- the primitive array backing the vector.- Returns:
- a column vector.
-
column
Creates a column vector.- Parameters:
array- the primitive array backing the vector.- Returns:
- a column vector.
-
row
Creates a row vector.- Parameters:
array- the primitive array backing the vector.- Returns:
- a row vector.
-
row
Creates a row vector.- Parameters:
array- the primitive array backing the vector.- Returns:
- a row vector.
-
zeros
Returns a zero column vector.- Parameters:
scalarType- the scalar type.length- the length of vector.- Returns:
- a zero column vector.
-
zeros
Returns a zero column vector of the same scalar type as this vector.- Parameters:
length- the length of vector.- Returns:
- a zero column vector.
-
ones
Returns a one column vector.- Parameters:
scalarType- the scalar type.length- the length of vector.- Returns:
- a one column vector.
-
ones
Returns a one column vector of the same scalar type as this vector.- Parameters:
length- the length of vector.- Returns:
- a one column vector.
-
min
public double min()Returns the minimal elements of the vector.- Returns:
- the minimal elements of the vector.
-
max
public double max()Returns the maximal elements of the vector.- Returns:
- the maximal elements of the vector.
-
sum
public double sum()Sums the elements of the vector.- Returns:
- Sum of the elements of the vector.
-
mean
public double mean()Returns the mean of the elements of the vector.- Returns:
- the mean of the elements of the vector.
-
softmax
public abstract int softmax()The softmax function without overflow. The function normalizes the vector into a probability distribution proportional to the exponentials of the input numbers. That is, prior to applying softmax, some vector components could be negative, or greater than one; and might not sum to 1; but after applying softmax, each component will be in the interval (0,1), and the components will add up to 1, so that they can be interpreted as probabilities. Furthermore, the larger input components will correspond to larger probabilities.- Returns:
- the index of largest posteriori probability.
-
asum
public double asum()Sums the absolute values of the elements of the vector.- Returns:
- Sum of the absolute values of the elements of the vector.
-
axpy
Computes a constant alpha times a vector x plus this vector y. The result overwrites the initial values of this vector y.- Parameters:
alpha- Ifalpha = 0this routine returns without any computation.x- Input vector.
-
dot
Computes the dot product of two vectors.- Parameters:
x- Input vector contains the second vector operand.- Returns:
- dot product.
-
norm2
public double norm2()Computes the Euclidean (L2) norm of a vector.- Returns:
- Euclidean norm.
-
norm1
public double norm1()Computes the L1 norm of a vector.- Returns:
- L1 norm.
-
normInf
public double normInf()Computes the L-Infinity norm of a vector.- Returns:
- L-Infinity norm.
-
scale
-
swap
-
iamax
public int iamax()Searches a vector for the first occurrence of the maximum absolute value.- Returns:
- The first index of the maximum absolute value of vector x.
-