Class 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 Details

    • size

      public final int size
      The size of this vector. That is, the number of data in this vector.
  • Constructor Details

    • Vector

      public Vector(Shape shape, double[] entries)
      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

      public Vector(Shape shape)
      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

      public Vector(Shape shape, double fillValue)
      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

      public Vector(Vector a)
      Creates a vector from another vector. This essentially copies the vector.
      Parameters:
      a - Vector to make copy of.
  • Method Details

    • makeLikeTensor

      public Vector makeLikeTensor(Shape shape, double[] entries)
      Constructs a tensor of the same type as this tensor with the given the shape and data.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<Vector,Vector,double[],Double>
      Specified by:
      makeLikeTensor in class AbstractTensor<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

      public Vector flatten()
      Flattens tensor to single dimension while preserving order of data.
      Specified by:
      flatten in class AbstractTensor<Vector,double[],Double>
      Returns:
      The flattened tensor.
      See Also:
    • flatten

      public Vector flatten(int axis)
      Flattens a tensor along the specified axis.
      Specified by:
      flatten in class AbstractTensor<Vector,double[],Double>
      Parameters:
      axis - Axis along which to flatten tensor.
      Throws:
      ArrayIndexOutOfBoundsException - If the axis is not positive or larger than this.{@link #getRank()}-1.
      See Also:
    • repeat

      public Matrix repeat(int n, int axis)
      Repeats a vector n times along a certain axis to create a matrix.
      Specified by:
      repeat in interface VectorMixin<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 vertically n times.
      • If axis=1 then the vector will be treated as a column vector and stacked horizontally n times.
      Returns:
      A matrix whose rows/columns are this vector repeated.
    • stack

      public Matrix stack(Vector b)
      Stacks two vectors vertically as if they were row vectors to form a matrix with two rows.
      Specified by:
      stack in interface VectorMixin<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 vector b.
    • stack

      public Matrix stack(Vector b, int axis)

      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 a 2&times;n matrix.

      Stacking two vectors of length n along axis 1 stacks the vectors as if they were column vectors resulting in a n&times;2 matrix.

      Specified by:
      stack in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Parameters:
      b - Vector to stack with this vector.
      axis - Axis along which to stack vectors. If axis=0, then vectors are stacked as if they are row vectors. If axis=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 vector b.
      IllegalArgumentException - If axis is not either 0 or 1.
    • outer

      public Matrix outer(Vector vector)
      Computes the outer product of two vectors.
      Specified by:
      outer in interface VectorMixin<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

      public Matrix toMatrix(boolean columVector)
      Converts a vector to an equivalent matrix representing either a row or column vector.
      Specified by:
      toMatrix in interface VectorMixin<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

      public Vector join(Vector b)
      Joints specified vector with this vector. That is, creates a vector of length this.length() + b.length() containing first the elements of this vector followed by the elements of b.
      Specified by:
      join in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Parameters:
      b - Vector to join with this vector.
      Returns:
      A vector resulting from joining the specified vector with this vector.
    • inner

      public Double inner(Vector b)
      Computes the inner product between two vectors.
      Specified by:
      inner in interface VectorMixin<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 vector b do not have the same number of data.
      See Also:
    • dot

      public Double dot(Vector b)

      Computes the dot product between two vectors.

      Specified by:
      dot in interface VectorMixin<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 vector b 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

      public Vector normalize()
      Computes a unit vector in the same direction as this vector.
      Specified by:
      normalize in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Returns:
      A unit vector with the same direction as this vector. If this vector is zeros, then an equivalently sized zero vector will be returned.
    • mag

      public Double mag()
      Computes the magnitude of this vector.
      Specified by:
      mag in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Returns:
      The magnitude of this vector.
    • get

      public Double get(int idx)
      Gets the element of this vector at the specified index.
      Specified by:
      get in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Parameters:
      idx - Index of the element to get within this vector.
      Returns:
      The element of this vector at index idx.
    • cross

      public Vector cross(Vector b)
      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 or b do not have exactly 3 data.
    • isParallel

      public boolean isParallel(Vector b)
      Checks if a vector is parallel to this vector.
      Parameters:
      b - Vector to compare to this vector.
      Returns:
      true if the vector b is parallel to this vector and the same size; false otherwise.
      See Also:
    • isPerp

      public boolean isPerp(Vector b)
      Checks if a vector is perpendicular to this vector.
      Parameters:
      b - Vector to compare to this vector.
      Returns:
      true if the vector b is perpendicular to this vector and the same size; false otherwise.
      See Also:
    • length

      public int length()
      Gets the length of a vector.
      Specified by:
      length in interface VectorMixin<Vector,Matrix,Matrix,Double>
      Returns:
      The length, i.e. the number of data, in this vector.
    • T

      public Vector T(int axis1, int axis2)
      Computes the transpose of a tensor by exchanging axis1 and axis2.
      Overrides:
      T in class AbstractDenseDoubleTensor<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 either axis1 or axis2 are out of bounds for the rank of this tensor.
      See Also:
    • T

      public Vector T(int... axes)
      Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified by axes.
      Overrides:
      T in class AbstractDenseDoubleTensor<Vector>
      Parameters:
      axes - Permutation of tensor axis. If the tensor has rank N, then this must be an array of length N 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 of axes is out of bounds for the rank of this tensor.
      IllegalArgumentException - If axes is not a permutation of {1, 2, 3, ... N-1}.
      See Also:
    • toCoo

      public CooVector toCoo()
      Converts this dense tensor to an equivalent sparse COO tensor.
      Returns:
      A sparse COO tensor equivalent to this dense tensor.
    • toComplex

      public CVector toComplex()
      Converts this real dense vector to an equivalent complex dense vector.
      Returns:
      A complex dense vector equivalent to this vector.
    • elemMultEq

      public void elemMultEq(Vector b)
      Computes the element-wise multiplication of two tensors and stores the result in this tensor.
      Overrides:
      elemMultEq in class AbstractDenseDoubleTensor<Vector>
      Parameters:
      b - Second tensor in the element-wise product.
      Throws:
      IllegalArgumentException - If this tensor and b do not have the same shape.
    • addEq

      public void addEq(Vector b)
      Computes the element-wise sum between two tensors and stores the result in this tensor.
      Overrides:
      addEq in class AbstractDenseDoubleTensor<Vector>
      Parameters:
      b - Second tensor in the element-wise sum.
      Throws:
      TensorShapeException - If this tensor and b do not have the same shape.
    • subEq

      public void subEq(Vector b)
      Computes the element-wise difference between two tensors and stores the result in this tensor.
      Overrides:
      subEq in class AbstractDenseDoubleTensor<Vector>
      Parameters:
      b - Second tensor in the element-wise difference.
      Throws:
      TensorShapeException - If this tensor and b do not have the same shape.
    • divEq

      public void divEq(Vector b)
      Computes the element-wise division between two tensors and stores the result in this tensor.
      Overrides:
      divEq in class AbstractDenseDoubleTensor<Vector>
      Parameters:
      b - The denominator tensor in the element-wise quotient.
      Throws:
      TensorShapeException - If this tensor and b's shape are not equal.
    • div

      public Vector div(Vector b)
      Computes the element-wise division between two tensors.
      Specified by:
      div in interface TensorOverField<Vector,Vector,double[],Double>
      Overrides:
      div in class AbstractDenseDoubleTensor<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 and b's shape are not equal.
    • equals

      public boolean equals(Object object)
      Checks if an object is equal to this vector object.
      Overrides:
      equals in class Object
      Parameters:
      object - Object to check equality with this vector.
      Returns:
      True if the two vectors have the same shape, are numerically equivalent, and are of type Vector. False otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • add

      public CVector add(CVector b)
      Adds a complex dense vector to this vector.
      Parameters:
      b - Complex dense vector in the sum.
      Returns:
      The sum of this vector and b.
    • add

      public Vector add(CooVector b)
      Adds a real sparse vector to this vector.
      Parameters:
      b - The real sparse vector in the sum.
      Returns:
      The sum of this vector and b.
    • add

      public CVector add(CooCVector b)
      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

      public CVector add(Complex128 b)
      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

      public CVector sub(CVector b)
      Subtracts a complex dense vector from this vector.
      Parameters:
      b - Complex dense vector in the difference.
      Returns:
      The difference of this vector and b.
    • sub

      public Vector sub(CooVector b)
      Subtracts a real sparse vector from this vector.
      Parameters:
      b - The real sparse vector in the difference.
      Returns:
      The difference of this vector and b.
    • sub

      public CVector sub(CooCVector b)
      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

      public CVector sub(Complex128 b)
      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

      public CVector mult(Complex128 b)
      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

      public CVector div(Complex128 b)
      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

      public CVector elemMult(CVector b)
      Computes the element-wise product of this vector and a complex dense vector.
      Parameters:
      b - The complex dense vector in the element-wise product.
      Returns:
      The element-wise product of this vector and b.
    • elemMult

      public CooVector elemMult(CooVector b)
      Computes the element-wise product of this vector and a real sparse vector.
      Parameters:
      b - The real sparse vector in the element-wise product.
      Returns:
      The element-wise product of this vector and b.
    • elemMult

      public CooCVector elemMult(CooCVector b)
      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

      public CVector div(CVector b)
      Computes the element-wise quotient of this vector and a complex dense vector.
      Parameters:
      b - The complex dense vector in the element-wise quotient.
      Returns:
      The element-wise quotient of this vector and b.
    • toTensor

      public Tensor toTensor()
      Converts this vector to an equivalent tensor.
      Returns:
      A tensor equivalent to this vector.
    • toString

      public String toString()
      Converts this vector to a human-readable string format. To specify the maximum number of data to print, use PrintOptions.setMaxColumns(int).
      Overrides:
      toString in class Object
      Returns:
      A human-readable string representation of this vector.