Class Vector
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<Vector,double[],Double>
org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor<Vector>
org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor<Vector>
org.flag4j.arrays.dense.Vector
- All Implemented Interfaces:
Serializable
,TensorOverField<Vector,
,Vector, double[], Double> TensorOverRing<Vector,
,Vector, double[], Double> TensorOverSemiring<Vector,
,Vector, double[], Double> VectorMixin<Vector,
Matrix, Matrix, Double>
public class Vector
extends AbstractDenseDoubleTensor<Vector>
implements VectorMixin<Vector,Matrix,Matrix,Double>
A dense vector backed by a primitive double array.
Vectors are 1D tensors (i.e. rank 1 tensor).
Vectors have mutable data but are fixed in size.
- See Also:
-
Field Summary
FieldsFields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionVector
(double... entries) Creates a vector with specified data.Vector
(int size) Creates a vector of specified size filled with zeros.Vector
(int... entries) Creates a vector with specified data.Vector
(int size, double fillValue) Creates a vector of specified size filled with a specified value.Creates a vector from another vector.Creates a vector of the specified shape filled with zeros.Creates a vector of specified size filled with a specified value.Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionadd
(Complex128 b) Adds a complex-valued scalar value to each entry of this tensor.Adds a complex dense vector to this vector.add
(CooCVector b) Adds a complex sparse vector to this vector.Adds a real sparse vector to this vector.void
Computes the element-wise sum between two tensors and stores the result in this tensor.Computes the vector cross product between two vectors.div
(Complex128 b) Divides this vector by a complex-valued scalar.Computes the element-wise quotient of this vector and a complex dense vector.Computes the element-wise division between two tensors.void
Computes the element-wise division between two tensors and stores the result in this tensor.Computes the dot product between two vectors.Computes the element-wise product of this vector and a complex dense vector.Computes the element-wise product of this vector and a complex sparse vector.Computes the element-wise product of this vector and a real sparse vector.void
elemMultEq
(Vector b) Computes the element-wise multiplication of two tensors and stores the result in this tensor.boolean
Checks if an object is equal to this vector object.flatten()
Flattens tensor to single dimension while preserving order of data.flatten
(int axis) Flattens a tensor along the specified axis.get
(int idx) Gets the element of this vector at the specified index.int
hashCode()
Computes the inner product between two vectors.boolean
isParallel
(Vector b) Checks if a vector is parallel to this vector.boolean
Checks if a vector is perpendicular to this vector.Joints specified vector with this vector.int
length()
Gets the length of a vector.mag()
Computes the magnitude of this vector.makeLikeTensor
(Shape shape, double[] entries) Constructs a tensor of the same type as this tensor with the given the shape and data.mult
(Complex128 b) Multiplies this vector by a complex-valued scalar.double
norm()
Computes the Euclidean norm of this vector.double
norm
(int p) Computes the p-norm of this vector.Computes a unit vector in the same direction as this vector.Computes the outer product of two vectors.repeat
(int n, int axis) Repeats a vectorn
times along a certain axis to create a matrix.Stacks two vectors vertically as if they were row vectors to form a matrix with two rows.Stacks two vectors along specified axis.sub
(Complex128 b) Subtracts a complex-valued scalar from each entry of this vector.Subtracts a complex dense vector from this vector.sub
(CooCVector b) Subtracts a complex sparse vector from this vector.Subtracts a real sparse vector from this vector.void
Computes the element-wise difference between two tensors and stores the result in this tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1
andaxis2
.Converts this real dense vector to an equivalent complex dense vector.toCoo()
Converts this dense tensor to an equivalent sparse COO tensor.toMatrix
(boolean columVector) Converts a vector to an equivalent matrix representing either a row or column vector.toString()
Converts this vector to a human-readable string format.toTensor()
Converts this vector to an equivalent tensor.Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDenseDoubleTensor
add, allClose, allClose, argmax, argmaxAbs, argmin, argminAbs, elemMult, get, reshape, round, round, roundToZero, set, sub, tensorDot, tensorTr
Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor
abs, add, add, addEq, addEq, conj, copy, div, div, divEq, divEq, H, H, isFinite, isInfinite, isNaN, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, prod, recip, roundToZero, sqrt, sub, sub, subEq, subEq, sum
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, T, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
H
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring
getData, getRank, getShape, tensorDot, tensorDot, tensorDot, tensorTr
Methods inherited from interface org.flag4j.arrays.backend.VectorMixin
size, toMatrix
-
Field Details
-
size
public final int sizeThe size of this vector. That is, the number of data in this vector.
-
-
Constructor Details
-
Vector
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- Entries of this tensor. If this tensor is dense, this specifies all data within the tensor. If this tensor is sparse, this specifies only the non-zero data of the tensor.
-
Vector
public Vector(int size) Creates a vector of specified size filled with zeros.- Parameters:
size
- Size of the vector.
-
Vector
public Vector(int size, double fillValue) Creates a vector of specified size filled with a specified value.- Parameters:
size
- Size of the vector.fillValue
- Value to fill vector with.
-
Vector
Creates a vector of the specified shape filled with zeros.- Parameters:
shape
- Shape of this vector.- Throws:
IllegalArgumentException
- If the shapes is not rank 1.
-
Vector
Creates a vector of specified size filled with a specified value.- Parameters:
shape
- Shape of the vector.fillValue
- Value to fill vector with.- Throws:
IllegalArgumentException
- If the shapes is not rank 1.
-
Vector
public Vector(double... entries) Creates a vector with specified data.- Parameters:
entries
- Entries for this column vector.
-
Vector
public Vector(int... entries) Creates a vector with specified data.- Parameters:
entries
- Entries for this column vector.
-
Vector
Creates a vector from another vector. This essentially copies the vector.- Parameters:
a
- Vector to make copy of.
-
-
Method Details
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given the shape and data.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<Vector,
Vector, double[], Double> - Specified by:
makeLikeTensor
in classAbstractTensor<Vector,
double[], Double> - Parameters:
shape
- Shape of the tensor to construct.entries
- Entries of the tensor to construct.- Returns:
- A tensor of the same type as this tensor with the given the shape and data.
-
flatten
Flattens tensor to single dimension while preserving order of data.- Specified by:
flatten
in classAbstractTensor<Vector,
double[], Double> - Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<Vector,
double[], Double> - Parameters:
axis
- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException
- If the axis is not positive or larger thanthis.{@link #getRank()}-1
.- See Also:
-
repeat
Repeats a vectorn
times along a certain axis to create a matrix.- Specified by:
repeat
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
n
- Number of times to repeat vector.axis
- Axis along which to repeat vector:- If
axis=0
, then the vector will be treated as a row vector and stacked verticallyn
times. - If
axis=1
then the vector will be treated as a column vector and stacked horizontallyn
times.
- If
- Returns:
- A matrix whose rows/columns are this vector repeated.
-
stack
Stacks two vectors vertically as if they were row vectors to form a matrix with two rows.- Specified by:
stack
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
b
- Vector to stack below this vector.- Returns:
- The result of stacking this vector and vector
b
. - Throws:
IllegalArgumentException
- If the number of data in this vector is different from the number of data in the vectorb
.
-
stack
Stacks two vectors along specified axis.
Stacking two vectors of length
n
along axis 0 stacks the vectors as if they were row vectors resulting in a2×n
matrix.Stacking two vectors of length
n
along axis 1 stacks the vectors as if they were column vectors resulting in an×2
matrix.- Specified by:
stack
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
b
- Vector to stack with this vector.axis
- Axis along which to stack vectors. Ifaxis=0
, then vectors are stacked as if they are row vectors. Ifaxis=1
, then vectors are stacked as if they are column vectors.- Returns:
- The result of stacking this vector and the vector
b
. - Throws:
IllegalArgumentException
- If the number of data in this vector is different from the number of data in the vectorb
.IllegalArgumentException
- If axis is not either 0 or 1.
-
outer
Computes the outer product of two vectors.- Specified by:
outer
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
vector
- Second vector in the outer product.- Returns:
- The result of the vector outer product between this vector and
b
. - Throws:
IllegalArgumentException
- If the two vectors do not have the same number of data.
-
toMatrix
Converts a vector to an equivalent matrix representing either a row or column vector.- Specified by:
toMatrix
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
columVector
- Flag indicating whether to convert this vector to a matrix representing a row or column vector:If
true
, the vector will be converted to a matrix representing a column vector.If
false
, The vector will be converted to a matrix representing a row vector.- Returns:
- A matrix equivalent to this vector.
-
join
Joints specified vector with this vector. That is, creates a vector of lengththis.length() + b.length()
containing first the elements of this vector followed by the elements ofb
. -
inner
Computes the inner product between two vectors.- Specified by:
inner
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
b
- Second vector in the inner product.- Returns:
- The inner product between this vector and the vector
b
. - Throws:
IllegalArgumentException
- If this vector and vectorb
do not have the same number of data.- See Also:
-
dot
Computes the dot product between two vectors.
- Specified by:
dot
in interfaceVectorMixin<Vector,
Matrix, Matrix, Double> - Parameters:
b
- Second vector in the dot product.- Returns:
- The dot product between this vector and the vector
b
. - Throws:
IllegalArgumentException
- If this vector and vectorb
do not have the same number of data.- See Also:
-
norm
public double norm()Computes the Euclidean norm of this vector.- Returns:
- The Euclidean norm of this vector.
-
norm
public double norm(int p) Computes the p-norm of this vector.- Parameters:
p
-p
value in the p-norm.- Returns:
- The Euclidean norm of this vector.
-
normalize
Computes a unit vector in the same direction as this vector. -
mag
-
get
-
cross
Computes the vector cross product between two vectors.- Parameters:
b
- Second vector in the cross product.- Returns:
- The result of the vector cross product between this vector and
b
. - Throws:
IllegalArgumentException
- If either this vector orb
do not have exactly 3 data.
-
isParallel
Checks if a vector is parallel to this vector.- Parameters:
b
- Vector to compare to this vector.- Returns:
true
if the vectorb
is parallel to this vector and the same size;false
otherwise.- See Also:
-
isPerp
Checks if a vector is perpendicular to this vector.- Parameters:
b
- Vector to compare to this vector.- Returns:
true
if the vectorb
is perpendicular to this vector and the same size;false
otherwise.- See Also:
-
length
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Overrides:
T
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
axis1
- First axis to exchange.axis2
- Second axis to exchange.- Returns:
- The transpose of this tensor according to the specified axes.
- Throws:
IndexOutOfBoundsException
- If eitheraxis1
oraxis2
are out of bounds for the rank of this tensor.- See Also:
-
T
Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified byaxes
.- Overrides:
T
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
axes
- Permutation of tensor axis. If the tensor has rankN
, then this must be an array of lengthN
which is a permutation of{0, 1, 2, ..., N-1}
.- Returns:
- The transpose of this tensor with its axes permuted by the
axes
array. - Throws:
IndexOutOfBoundsException
- If any element ofaxes
is out of bounds for the rank of this tensor.IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.- See Also:
-
toCoo
Converts this dense tensor to an equivalent sparse COO tensor.- Returns:
- A sparse COO tensor equivalent to this dense tensor.
-
toComplex
Converts this real dense vector to an equivalent complex dense vector.- Returns:
- A complex dense vector equivalent to this vector.
-
elemMultEq
Computes the element-wise multiplication of two tensors and stores the result in this tensor.- Overrides:
elemMultEq
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
b
- Second tensor in the element-wise product.- Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
addEq
Computes the element-wise sum between two tensors and stores the result in this tensor.- Overrides:
addEq
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
b
- Second tensor in the element-wise sum.- Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
subEq
Computes the element-wise difference between two tensors and stores the result in this tensor.- Overrides:
subEq
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
b
- Second tensor in the element-wise difference.- Throws:
TensorShapeException
- If this tensor andb
do not have the same shape.
-
divEq
Computes the element-wise division between two tensors and stores the result in this tensor.- Overrides:
divEq
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
b
- The denominator tensor in the element-wise quotient.- Throws:
TensorShapeException
- If this tensor andb
's shape are not equal.
-
div
Computes the element-wise division between two tensors.- Specified by:
div
in interfaceTensorOverField<Vector,
Vector, double[], Double> - Overrides:
div
in classAbstractDenseDoubleTensor<Vector>
- Parameters:
b
- The denominator tensor in the element-wise quotient.- Returns:
- The element-wise quotient of this tensor and
b
. - Throws:
TensorShapeException
- If this tensor andb
's shape are not equal.
-
equals
Checks if an object is equal to this vector object. -
hashCode
-
add
-
add
-
add
Adds a complex sparse vector to this vector.- Parameters:
b
- The complex sparse vector in the sum.- Returns:
- The sum of this vector and
b
.
-
add
Adds a complex-valued scalar value to each entry of this tensor. If the tensor is sparse, the scalar will only be added to the non-zero data of the tensor.- Parameters:
b
- Scalar value in sum.- Returns:
- The sum of this tensor with the scalar
b
.
-
sub
-
sub
-
sub
Subtracts a complex sparse vector from this vector.- Parameters:
b
- The complex sparse vector in the difference.- Returns:
- The difference of this vector and
b
.
-
sub
Subtracts a complex-valued scalar from each entry of this vector.- Parameters:
b
- The scalar value in the difference.- Returns:
- The difference of this vector's data with the scalar value
b
.
-
mult
Multiplies this vector by a complex-valued scalar.- Parameters:
b
- Scalar to multiply this vector by.- Returns:
- The scalar product of this vector with
b
.
-
div
Divides this vector by a complex-valued scalar.- Parameters:
b
- Scalar to divide this vector by.- Returns:
- The scalar quotient of this vector with
b
.
-
elemMult
-
elemMult
-
elemMult
Computes the element-wise product of this vector and a complex sparse vector.- Parameters:
b
- The complex sparse vector in the element-wise product.- Returns:
- The element-wise product of this vector and
b
.
-
div
-
toTensor
Converts this vector to an equivalent tensor.- Returns:
- A tensor equivalent to this vector.
-
toString
Converts this vector to a human-readable string format. To specify the maximum number of data to print, usePrintOptions.setMaxColumns(int)
.
-